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);
509 cell(cur.idx())->dispatch(cur, cmd);
510 cur.dispatched(); // override the cell's decision
512 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
513 if (sl == cur.top()) {
514 cmd = FuncRequest(LFUN_FINISHED_LEFT);
521 cell(cur.idx())->dispatch(cur, cmd);
522 cur.dispatched(); // override the cell's decision
524 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
525 cur.idx() = tabular.getCellBelow(cur.idx());
529 if (sl == cur.top()) {
530 cmd = FuncRequest(LFUN_FINISHED_DOWN);
537 cell(cur.idx())->dispatch(cur, cmd);
538 cur.dispatched(); // override the cell's decision
540 if (tabular.row_of_cell(cur.idx()) != 0) {
541 cur.idx() = tabular.getCellAbove(cur.idx());
542 cur.pit() = cur.lastpit();
543 cur.pos() = cur.lastpos();
545 if (sl == cur.top()) {
546 cmd = FuncRequest(LFUN_FINISHED_UP);
552 // //if (hasSelection())
553 // // cur.selection() = false;
554 // col_type const col = tabular.column_of_cell(cur.idx());
555 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
556 // if (t < yo() + tabular.getHeightOfTabular()) {
557 // cur.bv().scrollDocView(t);
558 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
560 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
567 // case LFUN_PRIOR: {
568 // //if (hasSelection())
569 // // cur.selection() = false;
570 // col_type const col = tabular.column_of_cell(cur.idx());
571 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
573 // cur.bv().scrollDocView(t);
577 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
581 // cur.par() = cur.lastpar();
582 // cur.pos() = cur.lastpos();
586 case LFUN_LAYOUT_TABULAR:
587 InsetTabularMailer(*this).showDialog(&cur.bv());
590 case LFUN_INSET_DIALOG_UPDATE:
591 InsetTabularMailer(*this).updateDialog(&cur.bv());
594 case LFUN_TABULAR_FEATURE:
595 if (!tabularFeatures(cur, cmd.argument))
599 // insert file functions
600 case LFUN_FILE_INSERT_ASCII_PARA:
601 case LFUN_FILE_INSERT_ASCII: {
602 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
603 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
609 if (tablemode(cur)) {
610 if (copySelection(cur)) {
611 recordUndo(cur, Undo::DELETE);
616 cell(cur.idx())->dispatch(cur, cmd);
621 recordUndo(cur, Undo::DELETE);
625 cell(cur.idx())->dispatch(cur, cmd);
629 if (!cur.selection())
631 if (tablemode(cur)) {
635 cell(cur.idx())->dispatch(cur, cmd);
638 case LFUN_PASTESELECTION: {
639 string const clip = cur.bv().getClipboard();
642 if (clip.find('\t') != string::npos) {
645 col_type maxCols = 1;
646 size_t len = clip.length();
647 for (size_t p = 0; p < len; ++p) {
648 p = clip.find_first_of("\t\n", p);
649 if (p == string::npos)
658 maxCols = max(cols, maxCols);
663 maxCols = max(cols, maxCols);
666 new LyXTabular(cur.buffer().params(), rows, maxCols));
668 string::size_type op = 0;
670 idx_type const cells =
671 paste_tabular->getNumberOfCells();
674 for (size_t p = 0; cell < cells && p < len; ++p) {
675 p = clip.find_first_of("\t\n", p);
676 if (p == string::npos || p >= len)
680 paste_tabular->getCellInset(cell)->
681 setText(clip.substr(op, p - op), font);
686 paste_tabular->getCellInset(cell)->
687 setText(clip.substr(op, p - op), font);
688 while (cols++ < maxCols)
695 // check for the last cell if there is no trailing '\n'
696 if (cell < cells && op < len)
697 paste_tabular->getCellInset(cell)->
698 setText(clip.substr(op, len - op), font);
699 } else if (!insertAsciiString(cur.bv(), clip, true)) {
700 // so that the clipboard is used and it goes on
702 // and executes LFUN_PASTESELECTION in insettext!
703 paste_tabular.reset();
709 if (hasPasteBuffer()) {
710 recordUndo(cur, Undo::INSERT);
714 cell(cur.idx())->dispatch(cur, cmd);
725 case LFUN_FREEFONT_APPLY:
726 case LFUN_FREEFONT_UPDATE:
730 case LFUN_CAPITALIZE_WORD:
731 case LFUN_UPCASE_WORD:
732 case LFUN_LOWCASE_WORD:
733 case LFUN_TRANSPOSE_CHARS:
734 if (tablemode(cur)) {
737 getSelection(cur, rs, re, cs, ce);
738 for (row_type i = rs; i <= re; ++i) {
739 for (col_type j = cs; j <= ce; ++j) {
740 // cursor follows cell:
741 cur.idx() = tabular.getCellNumber(i, j);
742 // select this cell only:
745 cur.pos() = cur.top().lastpos();
748 cell(cur.idx())->dispatch(cur, cmd);
751 // Restore original selection
752 cur.idx() = tabular.getCellNumber(rs, cs);
755 cur.idx() = tabular.getCellNumber(re, ce);
756 cur.pos() = cur.top().lastpos();
761 cell(cur.idx())->dispatch(cur, cmd);
765 // we try to handle this event in the insets dispatch function.
766 cell(cur.idx())->dispatch(cur, cmd);
771 InsetTabularMailer(*this).updateDialog(&cur.bv());
775 // function sets an object as defined in func_status.h:
776 // states OK, Unknown, Disabled, On, Off.
777 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
778 FuncStatus & status) const
780 switch (cmd.action) {
781 case LFUN_TABULAR_FEATURE: {
782 int action = LyXTabular::LAST_ACTION;
784 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
785 string const tmp = tabularFeature[i].feature;
786 if (tmp == cmd.argument.substr(0, tmp.length())) {
787 action = tabularFeature[i].action;
791 if (action == LyXTabular::LAST_ACTION) {
793 status.unknown(true);
797 string const argument
798 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
800 row_type sel_row_start = 0;
801 row_type sel_row_end = 0;
803 LyXTabular::ltType dummyltt;
806 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
809 case LyXTabular::SET_PWIDTH:
810 case LyXTabular::SET_MPWIDTH:
811 case LyXTabular::SET_SPECIAL_COLUMN:
812 case LyXTabular::SET_SPECIAL_MULTI:
813 case LyXTabular::APPEND_ROW:
814 case LyXTabular::APPEND_COLUMN:
815 case LyXTabular::DELETE_ROW:
816 case LyXTabular::DELETE_COLUMN:
817 case LyXTabular::SET_ALL_LINES:
818 case LyXTabular::UNSET_ALL_LINES:
822 case LyXTabular::MULTICOLUMN:
823 status.setOnOff(tabular.isMultiColumn(cur.idx()));
826 case LyXTabular::M_TOGGLE_LINE_TOP:
828 case LyXTabular::TOGGLE_LINE_TOP:
829 status.setOnOff(tabular.topLine(cur.idx(), flag));
832 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
834 case LyXTabular::TOGGLE_LINE_BOTTOM:
835 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
838 case LyXTabular::M_TOGGLE_LINE_LEFT:
840 case LyXTabular::TOGGLE_LINE_LEFT:
841 status.setOnOff(tabular.leftLine(cur.idx(), flag));
844 case LyXTabular::M_TOGGLE_LINE_RIGHT:
846 case LyXTabular::TOGGLE_LINE_RIGHT:
847 status.setOnOff(tabular.rightLine(cur.idx(), flag));
850 case LyXTabular::M_ALIGN_LEFT:
852 case LyXTabular::ALIGN_LEFT:
853 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
856 case LyXTabular::M_ALIGN_RIGHT:
858 case LyXTabular::ALIGN_RIGHT:
859 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
862 case LyXTabular::M_ALIGN_CENTER:
864 case LyXTabular::ALIGN_CENTER:
865 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
868 case LyXTabular::ALIGN_BLOCK:
869 status.enabled(!tabular.getPWidth(cur.idx()).zero());
870 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
873 case LyXTabular::M_VALIGN_TOP:
875 case LyXTabular::VALIGN_TOP:
877 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
880 case LyXTabular::M_VALIGN_BOTTOM:
882 case LyXTabular::VALIGN_BOTTOM:
884 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
887 case LyXTabular::M_VALIGN_MIDDLE:
889 case LyXTabular::VALIGN_MIDDLE:
891 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
894 case LyXTabular::SET_LONGTABULAR:
895 status.setOnOff(tabular.isLongTabular());
898 case LyXTabular::UNSET_LONGTABULAR:
899 status.setOnOff(!tabular.isLongTabular());
902 case LyXTabular::SET_ROTATE_TABULAR:
903 status.setOnOff(tabular.getRotateTabular());
906 case LyXTabular::UNSET_ROTATE_TABULAR:
907 status.setOnOff(!tabular.getRotateTabular());
910 case LyXTabular::SET_ROTATE_CELL:
911 status.setOnOff(tabular.getRotateCell(cur.idx()));
914 case LyXTabular::UNSET_ROTATE_CELL:
915 status.setOnOff(!tabular.getRotateCell(cur.idx()));
918 case LyXTabular::SET_USEBOX:
919 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
922 case LyXTabular::SET_LTFIRSTHEAD:
923 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
926 case LyXTabular::UNSET_LTFIRSTHEAD:
927 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
930 case LyXTabular::SET_LTHEAD:
931 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
934 case LyXTabular::UNSET_LTHEAD:
935 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
938 case LyXTabular::SET_LTFOOT:
939 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
942 case LyXTabular::UNSET_LTFOOT:
943 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
946 case LyXTabular::SET_LTLASTFOOT:
947 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
950 case LyXTabular::UNSET_LTLASTFOOT:
951 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
954 case LyXTabular::SET_LTNEWPAGE:
955 status.setOnOff(tabular.getLTNewPage(sel_row_start));
960 status.enabled(false);
966 // These are only enabled inside tabular
967 case LFUN_CELL_BACKWARD:
968 case LFUN_CELL_FORWARD:
969 status.enabled(true);
972 // disable these with multiple cells selected
973 case LFUN_INSET_INSERT:
974 case LFUN_TABULAR_INSERT:
975 case LFUN_INSERT_CHARSTYLE:
976 case LFUN_INSET_FLOAT:
977 case LFUN_INSET_WIDE_FLOAT:
978 case LFUN_INSET_FOOTNOTE:
979 case LFUN_INSET_MARGINAL:
980 case LFUN_INSERT_MATH:
982 case LFUN_MATH_MUTATE:
983 case LFUN_MATH_DISPLAY:
984 case LFUN_INSERT_NOTE:
985 case LFUN_INSET_OPTARG:
986 case LFUN_INSERT_BOX:
987 case LFUN_INSERT_BRANCH:
988 case LFUN_INSET_WRAP:
989 case LFUN_INSET_ERT: {
990 if (tablemode(cur)) {
991 status.enabled(false);
996 case LFUN_INSET_MODIFY:
997 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
998 status.enabled(true);
1004 // we try to handle this event in the insets dispatch function.
1005 return cell(cur.idx())->getStatus(cur, cmd, status);
1010 int InsetTabular::latex(Buffer const & buf, ostream & os,
1011 OutputParams const & runparams) const
1013 return tabular.latex(buf, os, runparams);
1017 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1018 OutputParams const & runparams) const
1020 int const dp = runparams.linelen ? runparams.depth : 0;
1021 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1025 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1026 OutputParams const & runparams) const
1028 return tabular.linuxdoc(buf,os, runparams);
1032 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1033 OutputParams const & runparams) const
1036 InsetBase * master = 0;
1038 #ifdef WITH_WARNINGS
1039 #warning Why not pass a proper DocIterator here?
1042 // if the table is inside a float it doesn't need the informaltable
1043 // wrapper. Search for it.
1044 for (master = owner(); master; master = master->owner())
1045 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1050 os << "<informaltable>";
1053 ret += tabular.docbook(buf, os, runparams);
1055 os << "</informaltable>";
1062 void InsetTabular::validate(LaTeXFeatures & features) const
1064 tabular.validate(features);
1068 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1070 return tabular.getCellInset(idx);
1074 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1076 return tabular.getCellInset(idx);
1080 void InsetTabular::cursorPos
1081 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1083 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1085 // y offset correction
1086 int const row = tabular.row_of_cell(sl.idx());
1087 for (int i = 0; i <= row; ++i) {
1089 y += tabular.getAscentOfRow(i);
1090 y += tabular.getAdditionalHeight(i);
1093 y += tabular.getDescentOfRow(i);
1096 // x offset correction
1097 int const col = tabular.column_of_cell(sl.idx());
1098 int idx = tabular.getCellNumber(row, 0);
1099 for (int j = 0; j < col; ++j) {
1100 if (tabular.isPartOfMultiColumn(row, j))
1102 x += tabular.getWidthOfColumn(idx);
1105 x += tabular.getBeginningOfTextInCell(idx);
1106 x += ADD_TO_TABULAR_WIDTH;
1111 int InsetTabular::dist(idx_type const cell, int x, int y) const
1115 InsetBase const & inset = *tabular.getCellInset(cell);
1116 Point o = theCoords.getInsets().xy(&inset);
1117 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1118 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1119 int const ybeg = o.y_ - inset.ascent();
1120 row_type const row = tabular.row_of_cell(cell);
1121 int const rowheight = tabular.getAscentOfRow(row)
1122 + tabular.getDescentOfRow(row)
1123 + tabular.getAdditionalHeight(row);
1124 int const yend = ybeg + rowheight;
1136 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1137 // << " ybeg=" << ybeg << " yend=" << yend
1138 // << " xx=" << xx << " yy=" << yy
1139 // << " dist=" << xx + yy << endl;
1144 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1146 //lyxerr << "InsetTabular::editXY: " << this << endl;
1147 cur.selection() = false;
1149 cur.idx() = getNearestCell(x, y);
1151 return cell(cur.idx())->text_.editXY(cur, x, y);
1155 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1157 cur.idx() = getNearestCell(x, y);
1158 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1163 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1165 idx_type idx_min = 0;
1166 int dist_min = std::numeric_limits<int>::max();
1167 for (idx_type i = 0; i < nargs(); ++i) {
1168 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1169 int const d = dist(i, x, y);
1180 int InsetTabular::getCellXPos(idx_type const cell) const
1184 for (; !tabular.isFirstCellInRow(c); --c)
1186 int lx = tabular.getWidthOfColumn(cell);
1187 for (; c < cell; ++c)
1188 lx += tabular.getWidthOfColumn(c);
1190 return lx - tabular.getWidthOfColumn(cell);
1194 void InsetTabular::resetPos(LCursor & cur) const
1196 BufferView & bv = cur.bv();
1197 int const maxwidth = bv.workWidth();
1199 if (&cur.inset() != this) {
1203 int const X2 = maxwidth;
1204 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1205 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1206 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1209 scx_ = X1 + 20 - x1;
1211 scx_ = X2 - 20 - x2;
1218 InsetTabularMailer(*this).updateDialog(&bv);
1222 void InsetTabular::moveNextCell(LCursor & cur)
1224 if (isRightToLeft(cur)) {
1225 if (tabular.isFirstCellInRow(cur.idx())) {
1226 row_type const row = tabular.row_of_cell(cur.idx());
1227 if (row == tabular.rows() - 1)
1229 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1236 if (tabular.isLastCell(cur.idx()))
1246 void InsetTabular::movePrevCell(LCursor & cur)
1248 if (isRightToLeft(cur)) {
1249 if (tabular.isLastCellInRow(cur.idx())) {
1250 row_type const row = tabular.row_of_cell(cur.idx());
1253 cur.idx() = tabular.getFirstCellInRow(row);
1254 cur.idx() = tabular.getCellAbove(cur.idx());
1256 if (tabular.isLastCell(cur.idx()))
1261 if (cur.idx() == 0) // first cell
1265 cur.pit() = cur.lastpit();
1266 cur.pos() = cur.lastpos();
1271 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1273 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1276 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1277 string const tmp = tabularFeature[i].feature;
1279 if (tmp == what.substr(0, tmp.length())) {
1280 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1281 //tabularFeatures[i].feature.length())) {
1282 action = tabularFeature[i].action;
1286 if (action == LyXTabular::LAST_ACTION)
1290 ltrim(what.substr(tabularFeature[i].feature.length()));
1291 tabularFeatures(cur, action, val);
1298 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1299 string const & special, bool & flag)
1301 if (special == "dl_above") {
1304 } else if (special == "dl_below") {
1305 ltt.bottomDL = flag;
1307 } else if (special == "empty") {
1319 void InsetTabular::tabularFeatures(LCursor & cur,
1320 LyXTabular::Feature feature, string const & value)
1322 BufferView & bv = cur.bv();
1323 col_type sel_col_start;
1324 col_type sel_col_end;
1325 row_type sel_row_start;
1326 row_type sel_row_end;
1327 bool setLines = false;
1328 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1329 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1333 case LyXTabular::M_ALIGN_LEFT:
1334 case LyXTabular::ALIGN_LEFT:
1335 setAlign = LYX_ALIGN_LEFT;
1338 case LyXTabular::M_ALIGN_RIGHT:
1339 case LyXTabular::ALIGN_RIGHT:
1340 setAlign = LYX_ALIGN_RIGHT;
1343 case LyXTabular::M_ALIGN_CENTER:
1344 case LyXTabular::ALIGN_CENTER:
1345 setAlign = LYX_ALIGN_CENTER;
1348 case LyXTabular::ALIGN_BLOCK:
1349 setAlign = LYX_ALIGN_BLOCK;
1352 case LyXTabular::M_VALIGN_TOP:
1353 case LyXTabular::VALIGN_TOP:
1354 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1357 case LyXTabular::M_VALIGN_BOTTOM:
1358 case LyXTabular::VALIGN_BOTTOM:
1359 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1362 case LyXTabular::M_VALIGN_MIDDLE:
1363 case LyXTabular::VALIGN_MIDDLE:
1364 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1371 recordUndo(cur, Undo::ATOMIC);
1373 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1374 row_type const row = tabular.row_of_cell(cur.idx());
1375 col_type const column = tabular.column_of_cell(cur.idx());
1377 LyXTabular::ltType ltt;
1381 case LyXTabular::SET_PWIDTH: {
1382 LyXLength const len(value);
1383 tabular.setColumnPWidth(cur.idx(), len);
1385 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1386 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1387 else if (!len.zero()
1388 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1389 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1393 case LyXTabular::SET_MPWIDTH:
1394 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1397 case LyXTabular::SET_SPECIAL_COLUMN:
1398 case LyXTabular::SET_SPECIAL_MULTI:
1399 tabular.setAlignSpecial(cur.idx(),value,feature);
1402 case LyXTabular::APPEND_ROW:
1403 // append the row into the tabular
1404 tabular.appendRow(bv.buffer()->params(), cur.idx());
1407 case LyXTabular::APPEND_COLUMN:
1408 // append the column into the tabular
1409 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1410 cur.idx() = tabular.getCellNumber(row, column);
1413 case LyXTabular::DELETE_ROW:
1414 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1415 tabular.deleteRow(sel_row_start);
1416 if (sel_row_start >= tabular.rows())
1418 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1421 cur.selection() = false;
1424 case LyXTabular::DELETE_COLUMN:
1425 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1426 tabular.deleteColumn(sel_col_start);
1427 if (sel_col_start >= tabular.columns())
1429 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1432 cur.selection() = false;
1435 case LyXTabular::M_TOGGLE_LINE_TOP:
1437 case LyXTabular::TOGGLE_LINE_TOP: {
1438 bool lineSet = !tabular.topLine(cur.idx(), flag);
1439 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1440 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1442 tabular.getCellNumber(i, j),
1447 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1449 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1450 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1451 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1452 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1453 tabular.setBottomLine(
1454 tabular.getCellNumber(i, j),
1460 case LyXTabular::M_TOGGLE_LINE_LEFT:
1462 case LyXTabular::TOGGLE_LINE_LEFT: {
1463 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1464 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1465 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1466 tabular.setLeftLine(
1467 tabular.getCellNumber(i,j),
1473 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1475 case LyXTabular::TOGGLE_LINE_RIGHT: {
1476 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1477 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1478 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1479 tabular.setRightLine(
1480 tabular.getCellNumber(i,j),
1486 case LyXTabular::M_ALIGN_LEFT:
1487 case LyXTabular::M_ALIGN_RIGHT:
1488 case LyXTabular::M_ALIGN_CENTER:
1490 case LyXTabular::ALIGN_LEFT:
1491 case LyXTabular::ALIGN_RIGHT:
1492 case LyXTabular::ALIGN_CENTER:
1493 case LyXTabular::ALIGN_BLOCK:
1494 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1495 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1496 tabular.setAlignment(
1497 tabular.getCellNumber(i, j),
1502 case LyXTabular::M_VALIGN_TOP:
1503 case LyXTabular::M_VALIGN_BOTTOM:
1504 case LyXTabular::M_VALIGN_MIDDLE:
1506 case LyXTabular::VALIGN_TOP:
1507 case LyXTabular::VALIGN_BOTTOM:
1508 case LyXTabular::VALIGN_MIDDLE:
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.setVAlignment(
1512 tabular.getCellNumber(i, j),
1516 case LyXTabular::MULTICOLUMN: {
1517 if (sel_row_start != sel_row_end) {
1518 #ifdef WITH_WARNINGS
1519 #warning Need I say it ? This is horrible.
1521 Alert::error(_("Error setting multicolumn"),
1522 _("You cannot set multicolumn vertically."));
1525 if (!cur.selection()) {
1526 // just multicol for one single cell
1527 // check whether we are completely in a multicol
1528 if (tabular.isMultiColumn(cur.idx()))
1529 tabular.unsetMultiColumn(cur.idx());
1531 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1534 // we have a selection so this means we just add all this
1535 // cells to form a multicolumn cell
1536 idx_type const s_start = cur.selBegin().idx();
1537 idx_type const s_end = cur.selEnd().idx();
1538 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1539 cur.idx() = s_start;
1542 cur.selection() = false;
1546 case LyXTabular::SET_ALL_LINES:
1548 case LyXTabular::UNSET_ALL_LINES:
1549 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1550 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1551 tabular.setAllLines(
1552 tabular.getCellNumber(i,j), setLines);
1555 case LyXTabular::SET_LONGTABULAR:
1556 tabular.setLongTabular(true);
1559 case LyXTabular::UNSET_LONGTABULAR:
1560 tabular.setLongTabular(false);
1563 case LyXTabular::SET_ROTATE_TABULAR:
1564 tabular.setRotateTabular(true);
1567 case LyXTabular::UNSET_ROTATE_TABULAR:
1568 tabular.setRotateTabular(false);
1571 case LyXTabular::SET_ROTATE_CELL:
1572 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1573 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1574 tabular.setRotateCell(
1575 tabular.getCellNumber(i, j), true);
1578 case LyXTabular::UNSET_ROTATE_CELL:
1579 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1580 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1581 tabular.setRotateCell(
1582 tabular.getCellNumber(i, j), false);
1585 case LyXTabular::SET_USEBOX: {
1586 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1587 if (val == tabular.getUsebox(cur.idx()))
1588 val = LyXTabular::BOX_NONE;
1589 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1590 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1591 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1595 case LyXTabular::UNSET_LTFIRSTHEAD:
1597 case LyXTabular::SET_LTFIRSTHEAD:
1598 tabular.getRowOfLTFirstHead(row, ltt);
1599 checkLongtableSpecial(ltt, value, flag);
1600 tabular.setLTHead(row, flag, ltt, true);
1603 case LyXTabular::UNSET_LTHEAD:
1605 case LyXTabular::SET_LTHEAD:
1606 tabular.getRowOfLTHead(row, ltt);
1607 checkLongtableSpecial(ltt, value, flag);
1608 tabular.setLTHead(row, flag, ltt, false);
1611 case LyXTabular::UNSET_LTFOOT:
1613 case LyXTabular::SET_LTFOOT:
1614 tabular.getRowOfLTFoot(row, ltt);
1615 checkLongtableSpecial(ltt, value, flag);
1616 tabular.setLTFoot(row, flag, ltt, false);
1619 case LyXTabular::UNSET_LTLASTFOOT:
1621 case LyXTabular::SET_LTLASTFOOT:
1622 tabular.getRowOfLTLastFoot(row, ltt);
1623 checkLongtableSpecial(ltt, value, flag);
1624 tabular.setLTFoot(row, flag, ltt, true);
1627 case LyXTabular::SET_LTNEWPAGE:
1628 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1631 // dummy stuff just to avoid warnings
1632 case LyXTabular::LAST_ACTION:
1636 InsetTabularMailer(*this).updateDialog(&bv);
1640 bool InsetTabular::showInsetDialog(BufferView * bv) const
1642 InsetTabularMailer(*this).showDialog(bv);
1647 void InsetTabular::openLayoutDialog(BufferView * bv) const
1649 InsetTabularMailer(*this).showDialog(bv);
1653 void InsetTabular::getLabelList(Buffer const & buffer,
1654 vector<string> & list) const
1656 tabular.getLabelList(buffer, list);
1660 bool InsetTabular::copySelection(LCursor & cur)
1662 if (!cur.selection())
1667 getSelection(cur, rs, re, cs, ce);
1669 paste_tabular.reset(new LyXTabular(tabular));
1671 for (row_type i = 0; i < rs; ++i)
1672 paste_tabular->deleteRow(0);
1674 row_type const rows = re - rs + 1;
1675 while (paste_tabular->rows() > rows)
1676 paste_tabular->deleteRow(rows);
1678 paste_tabular->setTopLine(0, true, true);
1679 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1682 for (col_type i = 0; i < cs; ++i)
1683 paste_tabular->deleteColumn(0);
1685 col_type const columns = ce - cs + 1;
1686 while (paste_tabular->columns() > columns)
1687 paste_tabular->deleteColumn(columns);
1689 paste_tabular->setLeftLine(0, true, true);
1690 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1694 OutputParams const runparams;
1695 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1696 cur.bv().stuffClipboard(os.str());
1701 bool InsetTabular::pasteSelection(LCursor & cur)
1705 col_type const actcol = tabular.column_of_cell(cur.idx());
1706 row_type const actrow = tabular.row_of_cell(cur.idx());
1707 for (row_type r1 = 0, r2 = actrow;
1708 r1 < paste_tabular->rows() && r2 < tabular.rows();
1710 for (col_type c1 = 0, c2 = actcol;
1711 c1 < paste_tabular->columns() && c2 < tabular.columns();
1713 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1714 tabular.isPartOfMultiColumn(r2, c2))
1716 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1720 if (tabular.isPartOfMultiColumn(r2, c2)) {
1724 shared_ptr<InsetText> inset(
1725 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1726 tabular.setCellInset(r2, c2, inset);
1735 void InsetTabular::cutSelection(LCursor & cur)
1737 if (!cur.selection())
1740 bool const track = cur.buffer().params().tracking_changes;
1743 getSelection(cur, rs, re, cs, ce);
1744 for (row_type i = rs; i <= re; ++i)
1745 for (col_type j = cs; j <= ce; ++j)
1746 cell(tabular.getCellNumber(i, j))->clear(track);
1748 // cursor position might be invalid now
1749 cur.pos() = cur.lastpos();
1750 cur.clearSelection();
1754 bool InsetTabular::isRightToLeft(LCursor & cur) const
1756 BOOST_ASSERT(cur.depth() > 1);
1757 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1758 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1759 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1760 parentpos).language()->RightToLeft();
1764 void InsetTabular::getSelection(LCursor & cur,
1765 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1767 CursorSlice const & beg = cur.selBegin();
1768 CursorSlice const & end = cur.selEnd();
1769 cs = tabular.column_of_cell(beg.idx());
1770 ce = tabular.column_of_cell(end.idx());
1773 cs = tabular.column_of_cell(end.idx());
1775 ce = tabular.right_column_of_cell(end.idx());
1778 rs = tabular.row_of_cell(beg.idx());
1779 re = tabular.row_of_cell(end.idx());
1785 size_t InsetTabular::nargs() const
1787 return tabular.getNumberOfCells();
1791 LyXText * InsetTabular::getText(int idx) const
1793 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1797 void InsetTabular::markErased()
1799 for (idx_type idx = 0; idx < nargs(); ++idx)
1800 cell(idx)->markErased();
1804 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1807 idx_type const cell = tabular.getCellFromInset(in);
1808 // FIXME: getCellFromInset() returns now always a valid cell, so
1809 // the stuff below can be deleted, and instead we have:
1810 return tabular.getPWidth(cell).zero();
1813 return tabular.getPWidth(cell).zero();
1815 // this is a workaround for a crash (New, Insert->Tabular,
1816 // Insert->FootNote)
1820 // well we didn't obviously find it so maybe our owner knows more
1821 BOOST_ASSERT(owner());
1822 return owner()->forceDefaultParagraphs(in);
1828 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1831 if (buf.length() <= 0)
1836 col_type maxCols = 1;
1837 string::size_type const len = buf.length();
1838 string::size_type p = 0;
1840 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1848 maxCols = max(cols, maxCols);
1854 maxCols = max(cols, maxCols);
1855 LyXTabular * loctab;
1860 paste_tabular.reset(
1861 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1862 loctab = paste_tabular.get();
1866 cell = bv.cursor().idx();
1867 ocol = tabular.column_of_cell(cell);
1868 row = tabular.row_of_cell(cell);
1871 string::size_type op = 0;
1872 idx_type const cells = loctab->getNumberOfCells();
1875 rows = loctab->rows();
1876 col_type const columns = loctab->columns();
1878 while (cell < cells && p < len && row < rows &&
1879 (p = buf.find_first_of("\t\n", p)) != string::npos)
1885 // we can only set this if we are not too far right
1886 if (cols < columns) {
1887 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1888 Paragraph & par = inset->text_.getPar(0);
1889 LyXFont const font = inset->text_.getFont(par, 0);
1890 inset->setText(buf.substr(op, p - op), font);
1896 // we can only set this if we are not too far right
1897 if (cols < columns) {
1898 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1899 Paragraph & par = inset->text_.getPar(0);
1900 LyXFont const font = inset->text_.getFont(par, 0);
1901 inset->setText(buf.substr(op, p - op), font);
1906 cell = loctab->getCellNumber(row, cols);
1912 // check for the last cell if there is no trailing '\n'
1913 if (cell < cells && op < len) {
1914 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1915 Paragraph & par = inset->text_.getPar(0);
1916 LyXFont const font = inset->text_.getFont(par, 0);
1917 inset->setText(buf.substr(op, len - op), font);
1923 void InsetTabular::addPreview(PreviewLoader & loader) const
1925 row_type const rows = tabular.rows();
1926 col_type const columns = tabular.columns();
1927 for (row_type i = 0; i < rows; ++i) {
1928 for (col_type j = 0; j < columns; ++j)
1929 tabular.getCellInset(i, j)->addPreview(loader);
1934 bool InsetTabular::tablemode(LCursor & cur) const
1936 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1943 string const InsetTabularMailer::name_("tabular");
1945 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1946 : inset_(const_cast<InsetTabular &>(inset))
1950 string const InsetTabularMailer::inset2string(Buffer const &) const
1952 return params2string(inset_);
1956 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1958 istringstream data(in);
1960 lex.setStream(data);
1967 if (!lex || token != name_)
1968 return print_mailer_error("InsetTabularMailer", in, 1,
1971 // This is part of the inset proper that is usually swallowed
1972 // by Buffer::readInset
1974 if (!lex || token != "Tabular")
1975 return print_mailer_error("InsetTabularMailer", in, 2,
1978 Buffer const & buffer = inset.buffer();
1979 inset.read(buffer, lex);
1983 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1986 data << name_ << ' ';
1987 inset.write(inset.buffer(), data);
1988 data << "\\end_inset\n";