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 our LyXText didn't do anything to the cursor
525 // then we try to put the cursor into the cell below
526 // setting also the right targetX.
527 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
528 cur.idx() = tabular.getCellBelow(cur.idx());
530 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
531 cur.pit(), 0, cur.targetX());
533 if (sl == cur.top()) {
534 // we trick it to go to the RIGHT after leaving the
536 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
543 cell(cur.idx())->dispatch(cur, cmd);
544 cur.dispatched(); // override the cell's decision
546 // if our LyXText didn't do anything to the cursor
547 // then we try to put the cursor into the cell above
548 // setting also the right targetX.
549 if (tabular.row_of_cell(cur.idx()) != 0) {
550 cur.idx() = tabular.getCellAbove(cur.idx());
551 cur.pit() = cur.lastpit();
552 LyXText const * text = cell(cur.idx())->getText(0);
553 cur.pos() = text->x2pos(
555 text->paragraphs().back().rows().size()-1,
558 if (sl == cur.top()) {
559 cmd = FuncRequest(LFUN_FINISHED_UP);
565 // //if (hasSelection())
566 // // cur.selection() = false;
567 // col_type const col = tabular.column_of_cell(cur.idx());
568 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
569 // if (t < yo() + tabular.getHeightOfTabular()) {
570 // cur.bv().scrollDocView(t);
571 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
573 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
580 // case LFUN_PRIOR: {
581 // //if (hasSelection())
582 // // cur.selection() = false;
583 // col_type const col = tabular.column_of_cell(cur.idx());
584 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
586 // cur.bv().scrollDocView(t);
590 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
594 // cur.par() = cur.lastpar();
595 // cur.pos() = cur.lastpos();
599 case LFUN_LAYOUT_TABULAR:
600 InsetTabularMailer(*this).showDialog(&cur.bv());
603 case LFUN_INSET_DIALOG_UPDATE:
604 InsetTabularMailer(*this).updateDialog(&cur.bv());
607 case LFUN_TABULAR_FEATURE:
608 if (!tabularFeatures(cur, cmd.argument))
612 // insert file functions
613 case LFUN_FILE_INSERT_ASCII_PARA:
614 case LFUN_FILE_INSERT_ASCII: {
615 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
616 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
622 if (tablemode(cur)) {
623 if (copySelection(cur)) {
624 recordUndo(cur, Undo::DELETE);
629 cell(cur.idx())->dispatch(cur, cmd);
634 recordUndo(cur, Undo::DELETE);
638 cell(cur.idx())->dispatch(cur, cmd);
642 if (!cur.selection())
644 if (tablemode(cur)) {
648 cell(cur.idx())->dispatch(cur, cmd);
651 case LFUN_PASTESELECTION: {
652 string const clip = cur.bv().getClipboard();
655 if (clip.find('\t') != string::npos) {
658 col_type maxCols = 1;
659 size_t len = clip.length();
660 for (size_t p = 0; p < len; ++p) {
661 p = clip.find_first_of("\t\n", p);
662 if (p == string::npos)
671 maxCols = max(cols, maxCols);
676 maxCols = max(cols, maxCols);
679 new LyXTabular(cur.buffer().params(), rows, maxCols));
681 string::size_type op = 0;
683 idx_type const cells =
684 paste_tabular->getNumberOfCells();
687 for (size_t p = 0; cell < cells && p < len; ++p) {
688 p = clip.find_first_of("\t\n", p);
689 if (p == string::npos || p >= len)
693 paste_tabular->getCellInset(cell)->
694 setText(clip.substr(op, p - op), font);
699 paste_tabular->getCellInset(cell)->
700 setText(clip.substr(op, p - op), font);
701 while (cols++ < maxCols)
708 // check for the last cell if there is no trailing '\n'
709 if (cell < cells && op < len)
710 paste_tabular->getCellInset(cell)->
711 setText(clip.substr(op, len - op), font);
712 } else if (!insertAsciiString(cur.bv(), clip, true)) {
713 // so that the clipboard is used and it goes on
715 // and executes LFUN_PASTESELECTION in insettext!
716 paste_tabular.reset();
722 if (hasPasteBuffer()) {
723 recordUndo(cur, Undo::INSERT);
727 cell(cur.idx())->dispatch(cur, cmd);
738 case LFUN_FREEFONT_APPLY:
739 case LFUN_FREEFONT_UPDATE:
743 case LFUN_CAPITALIZE_WORD:
744 case LFUN_UPCASE_WORD:
745 case LFUN_LOWCASE_WORD:
746 case LFUN_TRANSPOSE_CHARS:
747 if (tablemode(cur)) {
750 getSelection(cur, rs, re, cs, ce);
751 for (row_type i = rs; i <= re; ++i) {
752 for (col_type j = cs; j <= ce; ++j) {
753 // cursor follows cell:
754 cur.idx() = tabular.getCellNumber(i, j);
755 // select this cell only:
758 cur.pos() = cur.top().lastpos();
761 cell(cur.idx())->dispatch(cur, cmd);
764 // Restore original selection
765 cur.idx() = tabular.getCellNumber(rs, cs);
768 cur.idx() = tabular.getCellNumber(re, ce);
769 cur.pos() = cur.top().lastpos();
774 cell(cur.idx())->dispatch(cur, cmd);
778 // we try to handle this event in the insets dispatch function.
779 cell(cur.idx())->dispatch(cur, cmd);
784 InsetTabularMailer(*this).updateDialog(&cur.bv());
788 // function sets an object as defined in func_status.h:
789 // states OK, Unknown, Disabled, On, Off.
790 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
791 FuncStatus & status) const
793 switch (cmd.action) {
794 case LFUN_TABULAR_FEATURE: {
795 int action = LyXTabular::LAST_ACTION;
797 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
798 string const tmp = tabularFeature[i].feature;
799 if (tmp == cmd.argument.substr(0, tmp.length())) {
800 action = tabularFeature[i].action;
804 if (action == LyXTabular::LAST_ACTION) {
806 status.unknown(true);
810 string const argument
811 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
813 row_type sel_row_start = 0;
814 row_type sel_row_end = 0;
816 LyXTabular::ltType dummyltt;
819 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
822 case LyXTabular::SET_PWIDTH:
823 case LyXTabular::SET_MPWIDTH:
824 case LyXTabular::SET_SPECIAL_COLUMN:
825 case LyXTabular::SET_SPECIAL_MULTI:
826 case LyXTabular::APPEND_ROW:
827 case LyXTabular::APPEND_COLUMN:
828 case LyXTabular::DELETE_ROW:
829 case LyXTabular::DELETE_COLUMN:
830 case LyXTabular::SET_ALL_LINES:
831 case LyXTabular::UNSET_ALL_LINES:
835 case LyXTabular::MULTICOLUMN:
836 status.setOnOff(tabular.isMultiColumn(cur.idx()));
839 case LyXTabular::M_TOGGLE_LINE_TOP:
841 case LyXTabular::TOGGLE_LINE_TOP:
842 status.setOnOff(tabular.topLine(cur.idx(), flag));
845 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
847 case LyXTabular::TOGGLE_LINE_BOTTOM:
848 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
851 case LyXTabular::M_TOGGLE_LINE_LEFT:
853 case LyXTabular::TOGGLE_LINE_LEFT:
854 status.setOnOff(tabular.leftLine(cur.idx(), flag));
857 case LyXTabular::M_TOGGLE_LINE_RIGHT:
859 case LyXTabular::TOGGLE_LINE_RIGHT:
860 status.setOnOff(tabular.rightLine(cur.idx(), flag));
863 case LyXTabular::M_ALIGN_LEFT:
865 case LyXTabular::ALIGN_LEFT:
866 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
869 case LyXTabular::M_ALIGN_RIGHT:
871 case LyXTabular::ALIGN_RIGHT:
872 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
875 case LyXTabular::M_ALIGN_CENTER:
877 case LyXTabular::ALIGN_CENTER:
878 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
881 case LyXTabular::ALIGN_BLOCK:
882 status.enabled(!tabular.getPWidth(cur.idx()).zero());
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
886 case LyXTabular::M_VALIGN_TOP:
888 case LyXTabular::VALIGN_TOP:
890 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
893 case LyXTabular::M_VALIGN_BOTTOM:
895 case LyXTabular::VALIGN_BOTTOM:
897 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
900 case LyXTabular::M_VALIGN_MIDDLE:
902 case LyXTabular::VALIGN_MIDDLE:
904 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
907 case LyXTabular::SET_LONGTABULAR:
908 status.setOnOff(tabular.isLongTabular());
911 case LyXTabular::UNSET_LONGTABULAR:
912 status.setOnOff(!tabular.isLongTabular());
915 case LyXTabular::SET_ROTATE_TABULAR:
916 status.setOnOff(tabular.getRotateTabular());
919 case LyXTabular::UNSET_ROTATE_TABULAR:
920 status.setOnOff(!tabular.getRotateTabular());
923 case LyXTabular::SET_ROTATE_CELL:
924 status.setOnOff(tabular.getRotateCell(cur.idx()));
927 case LyXTabular::UNSET_ROTATE_CELL:
928 status.setOnOff(!tabular.getRotateCell(cur.idx()));
931 case LyXTabular::SET_USEBOX:
932 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
935 case LyXTabular::SET_LTFIRSTHEAD:
936 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
939 case LyXTabular::UNSET_LTFIRSTHEAD:
940 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
943 case LyXTabular::SET_LTHEAD:
944 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
947 case LyXTabular::UNSET_LTHEAD:
948 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::SET_LTFOOT:
952 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
955 case LyXTabular::UNSET_LTFOOT:
956 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
959 case LyXTabular::SET_LTLASTFOOT:
960 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
963 case LyXTabular::UNSET_LTLASTFOOT:
964 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
967 case LyXTabular::SET_LTNEWPAGE:
968 status.setOnOff(tabular.getLTNewPage(sel_row_start));
973 status.enabled(false);
979 // These are only enabled inside tabular
980 case LFUN_CELL_BACKWARD:
981 case LFUN_CELL_FORWARD:
982 status.enabled(true);
985 // disable these with multiple cells selected
986 case LFUN_INSET_INSERT:
987 case LFUN_TABULAR_INSERT:
988 case LFUN_INSERT_CHARSTYLE:
989 case LFUN_INSET_FLOAT:
990 case LFUN_INSET_WIDE_FLOAT:
991 case LFUN_INSET_FOOTNOTE:
992 case LFUN_INSET_MARGINAL:
993 case LFUN_INSERT_MATH:
995 case LFUN_MATH_MUTATE:
996 case LFUN_MATH_DISPLAY:
997 case LFUN_INSERT_NOTE:
998 case LFUN_INSET_OPTARG:
999 case LFUN_INSERT_BOX:
1000 case LFUN_INSERT_BRANCH:
1001 case LFUN_INSET_WRAP:
1002 case LFUN_INSET_ERT: {
1003 if (tablemode(cur)) {
1004 status.enabled(false);
1009 case LFUN_INSET_MODIFY:
1010 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1011 status.enabled(true);
1017 // we try to handle this event in the insets dispatch function.
1018 return cell(cur.idx())->getStatus(cur, cmd, status);
1023 int InsetTabular::latex(Buffer const & buf, ostream & os,
1024 OutputParams const & runparams) const
1026 return tabular.latex(buf, os, runparams);
1030 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1031 OutputParams const & runparams) const
1033 int const dp = runparams.linelen ? runparams.depth : 0;
1034 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1038 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1039 OutputParams const & runparams) const
1041 return tabular.linuxdoc(buf,os, runparams);
1045 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1046 OutputParams const & runparams) const
1049 InsetBase * master = 0;
1051 #ifdef WITH_WARNINGS
1052 #warning Why not pass a proper DocIterator here?
1055 // if the table is inside a float it doesn't need the informaltable
1056 // wrapper. Search for it.
1057 for (master = owner(); master; master = master->owner())
1058 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1063 os << "<informaltable>";
1066 ret += tabular.docbook(buf, os, runparams);
1068 os << "</informaltable>";
1075 void InsetTabular::validate(LaTeXFeatures & features) const
1077 tabular.validate(features);
1081 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1083 return tabular.getCellInset(idx);
1087 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1089 return tabular.getCellInset(idx);
1093 void InsetTabular::cursorPos
1094 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1096 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1098 // y offset correction
1099 int const row = tabular.row_of_cell(sl.idx());
1100 for (int i = 0; i <= row; ++i) {
1102 y += tabular.getAscentOfRow(i);
1103 y += tabular.getAdditionalHeight(i);
1106 y += tabular.getDescentOfRow(i);
1109 // x offset correction
1110 int const col = tabular.column_of_cell(sl.idx());
1111 int idx = tabular.getCellNumber(row, 0);
1112 for (int j = 0; j < col; ++j) {
1113 if (tabular.isPartOfMultiColumn(row, j))
1115 x += tabular.getWidthOfColumn(idx);
1118 x += tabular.getBeginningOfTextInCell(idx);
1119 x += ADD_TO_TABULAR_WIDTH;
1124 int InsetTabular::dist(idx_type const cell, int x, int y) const
1128 InsetBase const & inset = *tabular.getCellInset(cell);
1129 Point o = theCoords.getInsets().xy(&inset);
1130 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1131 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1132 int const ybeg = o.y_ - inset.ascent();
1133 row_type const row = tabular.row_of_cell(cell);
1134 int const rowheight = tabular.getAscentOfRow(row)
1135 + tabular.getDescentOfRow(row)
1136 + tabular.getAdditionalHeight(row);
1137 int const yend = ybeg + rowheight;
1149 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1150 // << " ybeg=" << ybeg << " yend=" << yend
1151 // << " xx=" << xx << " yy=" << yy
1152 // << " dist=" << xx + yy << endl;
1157 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1159 //lyxerr << "InsetTabular::editXY: " << this << endl;
1160 cur.selection() = false;
1162 cur.idx() = getNearestCell(x, y);
1164 return cell(cur.idx())->text_.editXY(cur, x, y);
1168 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1170 cur.idx() = getNearestCell(x, y);
1171 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1175 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1177 idx_type idx_min = 0;
1178 int dist_min = std::numeric_limits<int>::max();
1179 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1180 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1181 int const d = dist(i, x, y);
1192 int InsetTabular::getCellXPos(idx_type const cell) const
1196 for (; !tabular.isFirstCellInRow(c); --c)
1198 int lx = tabular.getWidthOfColumn(cell);
1199 for (; c < cell; ++c)
1200 lx += tabular.getWidthOfColumn(c);
1202 return lx - tabular.getWidthOfColumn(cell);
1206 void InsetTabular::resetPos(LCursor & cur) const
1208 BufferView & bv = cur.bv();
1209 int const maxwidth = bv.workWidth();
1211 if (&cur.inset() != this) {
1215 int const X2 = maxwidth;
1216 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1217 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1218 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1221 scx_ = X1 + 20 - x1;
1223 scx_ = X2 - 20 - x2;
1230 InsetTabularMailer(*this).updateDialog(&bv);
1234 void InsetTabular::moveNextCell(LCursor & cur)
1236 if (isRightToLeft(cur)) {
1237 if (tabular.isFirstCellInRow(cur.idx())) {
1238 row_type const row = tabular.row_of_cell(cur.idx());
1239 if (row == tabular.rows() - 1)
1241 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1248 if (tabular.isLastCell(cur.idx()))
1258 void InsetTabular::movePrevCell(LCursor & cur)
1260 if (isRightToLeft(cur)) {
1261 if (tabular.isLastCellInRow(cur.idx())) {
1262 row_type const row = tabular.row_of_cell(cur.idx());
1265 cur.idx() = tabular.getFirstCellInRow(row);
1266 cur.idx() = tabular.getCellAbove(cur.idx());
1268 if (tabular.isLastCell(cur.idx()))
1273 if (cur.idx() == 0) // first cell
1277 cur.pit() = cur.lastpit();
1278 cur.pos() = cur.lastpos();
1283 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1285 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1288 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1289 string const tmp = tabularFeature[i].feature;
1291 if (tmp == what.substr(0, tmp.length())) {
1292 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1293 //tabularFeatures[i].feature.length())) {
1294 action = tabularFeature[i].action;
1298 if (action == LyXTabular::LAST_ACTION)
1302 ltrim(what.substr(tabularFeature[i].feature.length()));
1303 tabularFeatures(cur, action, val);
1310 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1311 string const & special, bool & flag)
1313 if (special == "dl_above") {
1316 } else if (special == "dl_below") {
1317 ltt.bottomDL = flag;
1319 } else if (special == "empty") {
1331 void InsetTabular::tabularFeatures(LCursor & cur,
1332 LyXTabular::Feature feature, string const & value)
1334 BufferView & bv = cur.bv();
1335 col_type sel_col_start;
1336 col_type sel_col_end;
1337 row_type sel_row_start;
1338 row_type sel_row_end;
1339 bool setLines = false;
1340 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1341 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1345 case LyXTabular::M_ALIGN_LEFT:
1346 case LyXTabular::ALIGN_LEFT:
1347 setAlign = LYX_ALIGN_LEFT;
1350 case LyXTabular::M_ALIGN_RIGHT:
1351 case LyXTabular::ALIGN_RIGHT:
1352 setAlign = LYX_ALIGN_RIGHT;
1355 case LyXTabular::M_ALIGN_CENTER:
1356 case LyXTabular::ALIGN_CENTER:
1357 setAlign = LYX_ALIGN_CENTER;
1360 case LyXTabular::ALIGN_BLOCK:
1361 setAlign = LYX_ALIGN_BLOCK;
1364 case LyXTabular::M_VALIGN_TOP:
1365 case LyXTabular::VALIGN_TOP:
1366 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1369 case LyXTabular::M_VALIGN_BOTTOM:
1370 case LyXTabular::VALIGN_BOTTOM:
1371 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1374 case LyXTabular::M_VALIGN_MIDDLE:
1375 case LyXTabular::VALIGN_MIDDLE:
1376 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1383 recordUndo(cur, Undo::ATOMIC);
1385 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1386 row_type const row = tabular.row_of_cell(cur.idx());
1387 col_type const column = tabular.column_of_cell(cur.idx());
1389 LyXTabular::ltType ltt;
1393 case LyXTabular::SET_PWIDTH: {
1394 LyXLength const len(value);
1395 tabular.setColumnPWidth(cur.idx(), len);
1397 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1398 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1399 else if (!len.zero()
1400 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1401 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1405 case LyXTabular::SET_MPWIDTH:
1406 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1409 case LyXTabular::SET_SPECIAL_COLUMN:
1410 case LyXTabular::SET_SPECIAL_MULTI:
1411 tabular.setAlignSpecial(cur.idx(),value,feature);
1414 case LyXTabular::APPEND_ROW:
1415 // append the row into the tabular
1416 tabular.appendRow(bv.buffer()->params(), cur.idx());
1419 case LyXTabular::APPEND_COLUMN:
1420 // append the column into the tabular
1421 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1422 cur.idx() = tabular.getCellNumber(row, column);
1425 case LyXTabular::DELETE_ROW:
1426 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1427 tabular.deleteRow(sel_row_start);
1428 if (sel_row_start >= tabular.rows())
1430 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1433 cur.selection() = false;
1436 case LyXTabular::DELETE_COLUMN:
1437 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1438 tabular.deleteColumn(sel_col_start);
1439 if (sel_col_start >= tabular.columns())
1441 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1444 cur.selection() = false;
1447 case LyXTabular::M_TOGGLE_LINE_TOP:
1449 case LyXTabular::TOGGLE_LINE_TOP: {
1450 bool lineSet = !tabular.topLine(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)
1454 tabular.getCellNumber(i, j),
1459 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1461 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1462 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1463 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1464 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1465 tabular.setBottomLine(
1466 tabular.getCellNumber(i, j),
1472 case LyXTabular::M_TOGGLE_LINE_LEFT:
1474 case LyXTabular::TOGGLE_LINE_LEFT: {
1475 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1476 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1477 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1478 tabular.setLeftLine(
1479 tabular.getCellNumber(i,j),
1485 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1487 case LyXTabular::TOGGLE_LINE_RIGHT: {
1488 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1489 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1490 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1491 tabular.setRightLine(
1492 tabular.getCellNumber(i,j),
1498 case LyXTabular::M_ALIGN_LEFT:
1499 case LyXTabular::M_ALIGN_RIGHT:
1500 case LyXTabular::M_ALIGN_CENTER:
1502 case LyXTabular::ALIGN_LEFT:
1503 case LyXTabular::ALIGN_RIGHT:
1504 case LyXTabular::ALIGN_CENTER:
1505 case LyXTabular::ALIGN_BLOCK:
1506 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1507 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508 tabular.setAlignment(
1509 tabular.getCellNumber(i, j),
1514 case LyXTabular::M_VALIGN_TOP:
1515 case LyXTabular::M_VALIGN_BOTTOM:
1516 case LyXTabular::M_VALIGN_MIDDLE:
1518 case LyXTabular::VALIGN_TOP:
1519 case LyXTabular::VALIGN_BOTTOM:
1520 case LyXTabular::VALIGN_MIDDLE:
1521 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1522 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1523 tabular.setVAlignment(
1524 tabular.getCellNumber(i, j),
1528 case LyXTabular::MULTICOLUMN: {
1529 if (sel_row_start != sel_row_end) {
1530 #ifdef WITH_WARNINGS
1531 #warning Need I say it ? This is horrible.
1533 Alert::error(_("Error setting multicolumn"),
1534 _("You cannot set multicolumn vertically."));
1537 if (!cur.selection()) {
1538 // just multicol for one single cell
1539 // check whether we are completely in a multicol
1540 if (tabular.isMultiColumn(cur.idx()))
1541 tabular.unsetMultiColumn(cur.idx());
1543 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1546 // we have a selection so this means we just add all this
1547 // cells to form a multicolumn cell
1548 idx_type const s_start = cur.selBegin().idx();
1549 idx_type const s_end = cur.selEnd().idx();
1550 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1551 cur.idx() = s_start;
1554 cur.selection() = false;
1558 case LyXTabular::SET_ALL_LINES:
1560 case LyXTabular::UNSET_ALL_LINES:
1561 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1562 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1563 tabular.setAllLines(
1564 tabular.getCellNumber(i,j), setLines);
1567 case LyXTabular::SET_LONGTABULAR:
1568 tabular.setLongTabular(true);
1571 case LyXTabular::UNSET_LONGTABULAR:
1572 tabular.setLongTabular(false);
1575 case LyXTabular::SET_ROTATE_TABULAR:
1576 tabular.setRotateTabular(true);
1579 case LyXTabular::UNSET_ROTATE_TABULAR:
1580 tabular.setRotateTabular(false);
1583 case LyXTabular::SET_ROTATE_CELL:
1584 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1585 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1586 tabular.setRotateCell(
1587 tabular.getCellNumber(i, j), true);
1590 case LyXTabular::UNSET_ROTATE_CELL:
1591 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1592 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1593 tabular.setRotateCell(
1594 tabular.getCellNumber(i, j), false);
1597 case LyXTabular::SET_USEBOX: {
1598 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1599 if (val == tabular.getUsebox(cur.idx()))
1600 val = LyXTabular::BOX_NONE;
1601 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1602 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1603 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1607 case LyXTabular::UNSET_LTFIRSTHEAD:
1609 case LyXTabular::SET_LTFIRSTHEAD:
1610 tabular.getRowOfLTFirstHead(row, ltt);
1611 checkLongtableSpecial(ltt, value, flag);
1612 tabular.setLTHead(row, flag, ltt, true);
1615 case LyXTabular::UNSET_LTHEAD:
1617 case LyXTabular::SET_LTHEAD:
1618 tabular.getRowOfLTHead(row, ltt);
1619 checkLongtableSpecial(ltt, value, flag);
1620 tabular.setLTHead(row, flag, ltt, false);
1623 case LyXTabular::UNSET_LTFOOT:
1625 case LyXTabular::SET_LTFOOT:
1626 tabular.getRowOfLTFoot(row, ltt);
1627 checkLongtableSpecial(ltt, value, flag);
1628 tabular.setLTFoot(row, flag, ltt, false);
1631 case LyXTabular::UNSET_LTLASTFOOT:
1633 case LyXTabular::SET_LTLASTFOOT:
1634 tabular.getRowOfLTLastFoot(row, ltt);
1635 checkLongtableSpecial(ltt, value, flag);
1636 tabular.setLTFoot(row, flag, ltt, true);
1639 case LyXTabular::SET_LTNEWPAGE:
1640 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1643 // dummy stuff just to avoid warnings
1644 case LyXTabular::LAST_ACTION:
1648 InsetTabularMailer(*this).updateDialog(&bv);
1652 bool InsetTabular::showInsetDialog(BufferView * bv) const
1654 InsetTabularMailer(*this).showDialog(bv);
1659 void InsetTabular::openLayoutDialog(BufferView * bv) const
1661 InsetTabularMailer(*this).showDialog(bv);
1665 void InsetTabular::getLabelList(Buffer const & buffer,
1666 vector<string> & list) const
1668 tabular.getLabelList(buffer, list);
1672 bool InsetTabular::copySelection(LCursor & cur)
1674 if (!cur.selection())
1679 getSelection(cur, rs, re, cs, ce);
1681 paste_tabular.reset(new LyXTabular(tabular));
1683 for (row_type i = 0; i < rs; ++i)
1684 paste_tabular->deleteRow(0);
1686 row_type const rows = re - rs + 1;
1687 while (paste_tabular->rows() > rows)
1688 paste_tabular->deleteRow(rows);
1690 paste_tabular->setTopLine(0, true, true);
1691 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1694 for (col_type i = 0; i < cs; ++i)
1695 paste_tabular->deleteColumn(0);
1697 col_type const columns = ce - cs + 1;
1698 while (paste_tabular->columns() > columns)
1699 paste_tabular->deleteColumn(columns);
1701 paste_tabular->setLeftLine(0, true, true);
1702 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1706 OutputParams const runparams;
1707 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1708 cur.bv().stuffClipboard(os.str());
1713 bool InsetTabular::pasteSelection(LCursor & cur)
1717 col_type const actcol = tabular.column_of_cell(cur.idx());
1718 row_type const actrow = tabular.row_of_cell(cur.idx());
1719 for (row_type r1 = 0, r2 = actrow;
1720 r1 < paste_tabular->rows() && r2 < tabular.rows();
1722 for (col_type c1 = 0, c2 = actcol;
1723 c1 < paste_tabular->columns() && c2 < tabular.columns();
1725 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1726 tabular.isPartOfMultiColumn(r2, c2))
1728 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1732 if (tabular.isPartOfMultiColumn(r2, c2)) {
1736 shared_ptr<InsetText> inset(
1737 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1738 tabular.setCellInset(r2, c2, inset);
1747 void InsetTabular::cutSelection(LCursor & cur)
1749 if (!cur.selection())
1752 bool const track = cur.buffer().params().tracking_changes;
1755 getSelection(cur, rs, re, cs, ce);
1756 for (row_type i = rs; i <= re; ++i)
1757 for (col_type j = cs; j <= ce; ++j)
1758 cell(tabular.getCellNumber(i, j))->clear(track);
1760 // cursor position might be invalid now
1761 cur.pos() = cur.lastpos();
1762 cur.clearSelection();
1766 bool InsetTabular::isRightToLeft(LCursor & cur) const
1768 BOOST_ASSERT(cur.depth() > 1);
1769 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1770 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1771 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1772 parentpos).language()->RightToLeft();
1776 void InsetTabular::getSelection(LCursor & cur,
1777 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1779 CursorSlice const & beg = cur.selBegin();
1780 CursorSlice const & end = cur.selEnd();
1781 cs = tabular.column_of_cell(beg.idx());
1782 ce = tabular.column_of_cell(end.idx());
1785 cs = tabular.column_of_cell(end.idx());
1787 ce = tabular.right_column_of_cell(end.idx());
1790 rs = tabular.row_of_cell(beg.idx());
1791 re = tabular.row_of_cell(end.idx());
1797 LyXText * InsetTabular::getText(int idx) const
1799 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1803 void InsetTabular::markErased()
1805 for (idx_type idx = 0; idx < nargs(); ++idx)
1806 cell(idx)->markErased();
1810 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1813 idx_type const cell = tabular.getCellFromInset(in);
1814 // FIXME: getCellFromInset() returns now always a valid cell, so
1815 // the stuff below can be deleted, and instead we have:
1816 return tabular.getPWidth(cell).zero();
1819 return tabular.getPWidth(cell).zero();
1821 // this is a workaround for a crash (New, Insert->Tabular,
1822 // Insert->FootNote)
1826 // well we didn't obviously find it so maybe our owner knows more
1827 BOOST_ASSERT(owner());
1828 return owner()->forceDefaultParagraphs(in);
1834 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1837 if (buf.length() <= 0)
1842 col_type maxCols = 1;
1843 string::size_type const len = buf.length();
1844 string::size_type p = 0;
1846 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1854 maxCols = max(cols, maxCols);
1860 maxCols = max(cols, maxCols);
1861 LyXTabular * loctab;
1866 paste_tabular.reset(
1867 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1868 loctab = paste_tabular.get();
1872 cell = bv.cursor().idx();
1873 ocol = tabular.column_of_cell(cell);
1874 row = tabular.row_of_cell(cell);
1877 string::size_type op = 0;
1878 idx_type const cells = loctab->getNumberOfCells();
1881 rows = loctab->rows();
1882 col_type const columns = loctab->columns();
1884 while (cell < cells && p < len && row < rows &&
1885 (p = buf.find_first_of("\t\n", p)) != string::npos)
1891 // we can only set this if we are not too far right
1892 if (cols < columns) {
1893 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1894 Paragraph & par = inset->text_.getPar(0);
1895 LyXFont const font = inset->text_.getFont(par, 0);
1896 inset->setText(buf.substr(op, p - op), font);
1902 // we can only set this if we are not too far right
1903 if (cols < columns) {
1904 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1905 Paragraph & par = inset->text_.getPar(0);
1906 LyXFont const font = inset->text_.getFont(par, 0);
1907 inset->setText(buf.substr(op, p - op), font);
1912 cell = loctab->getCellNumber(row, cols);
1918 // check for the last cell if there is no trailing '\n'
1919 if (cell < cells && op < len) {
1920 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1921 Paragraph & par = inset->text_.getPar(0);
1922 LyXFont const font = inset->text_.getFont(par, 0);
1923 inset->setText(buf.substr(op, len - op), font);
1929 void InsetTabular::addPreview(PreviewLoader & loader) const
1931 row_type const rows = tabular.rows();
1932 col_type const columns = tabular.columns();
1933 for (row_type i = 0; i < rows; ++i) {
1934 for (col_type j = 0; j < columns; ++j)
1935 tabular.getCellInset(i, j)->addPreview(loader);
1940 bool InsetTabular::tablemode(LCursor & cur) const
1942 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1949 string const InsetTabularMailer::name_("tabular");
1951 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1952 : inset_(const_cast<InsetTabular &>(inset))
1956 string const InsetTabularMailer::inset2string(Buffer const &) const
1958 return params2string(inset_);
1962 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1964 istringstream data(in);
1966 lex.setStream(data);
1973 if (!lex || token != name_)
1974 return print_mailer_error("InsetTabularMailer", in, 1,
1977 // This is part of the inset proper that is usually swallowed
1978 // by Buffer::readInset
1980 if (!lex || token != "Tabular")
1981 return print_mailer_error("InsetTabularMailer", in, 2,
1984 Buffer const & buffer = inset.buffer();
1985 inset.read(buffer, lex);
1989 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1992 data << name_ << ' ';
1993 inset.write(inset.buffer(), data);
1994 data << "\\end_inset\n";