3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
49 using lyx::cap::tabularStackDirty;
51 using lyx::graphics::PreviewLoader;
53 using lyx::support::ltrim;
55 using boost::shared_ptr;
61 using std::istringstream;
63 using std::ostringstream;
70 int const ADD_TO_HEIGHT = 2;
71 int const ADD_TO_TABULAR_WIDTH = 2;
74 boost::scoped_ptr<LyXTabular> paste_tabular;
77 struct TabularFeature {
78 LyXTabular::Feature action;
83 TabularFeature tabularFeature[] =
85 { LyXTabular::APPEND_ROW, "append-row" },
86 { LyXTabular::APPEND_COLUMN, "append-column" },
87 { LyXTabular::DELETE_ROW, "delete-row" },
88 { LyXTabular::DELETE_COLUMN, "delete-column" },
89 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
90 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
91 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
92 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
93 { LyXTabular::ALIGN_LEFT, "align-left" },
94 { LyXTabular::ALIGN_RIGHT, "align-right" },
95 { LyXTabular::ALIGN_CENTER, "align-center" },
96 { LyXTabular::ALIGN_BLOCK, "align-block" },
97 { LyXTabular::VALIGN_TOP, "valign-top" },
98 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
99 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
100 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
101 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
102 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
103 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
104 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
105 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
106 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
107 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
108 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
109 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
110 { LyXTabular::MULTICOLUMN, "multicolumn" },
111 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
112 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
113 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
114 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
115 { LyXTabular::SET_PWIDTH, "set-pwidth" },
116 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
117 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
118 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
119 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
120 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
121 { LyXTabular::SET_USEBOX, "set-usebox" },
122 { LyXTabular::SET_LTHEAD, "set-lthead" },
123 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
124 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
125 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
126 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
127 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
128 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
129 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
130 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
131 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
132 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
133 { LyXTabular::LAST_ACTION, "" }
137 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
139 FeatureEqual(LyXTabular::Feature feature)
140 : feature_(feature) {}
141 bool operator()(TabularFeature const & tf) const {
142 return tf.action == feature_;
145 LyXTabular::Feature feature_;
151 string const featureAsString(LyXTabular::Feature feature)
153 TabularFeature * end = tabularFeature +
154 sizeof(tabularFeature) / sizeof(TabularFeature);
155 TabularFeature * it = std::find_if(tabularFeature, end,
156 FeatureEqual(feature));
157 return (it == end) ? string() : it->feature;
161 bool InsetTabular::hasPasteBuffer() const
163 return (paste_tabular.get() != 0);
167 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
169 : tabular(buf.params(), max(rows, row_type(1)),
170 max(columns, col_type(1))), buffer_(&buf), scx_(0)
174 InsetTabular::InsetTabular(InsetTabular const & tab)
175 : InsetOld(tab), tabular(tab.tabular),
176 buffer_(tab.buffer_), scx_(0)
180 InsetTabular::~InsetTabular()
182 InsetTabularMailer(*this).hideDialog();
186 auto_ptr<InsetBase> InsetTabular::doClone() const
188 return auto_ptr<InsetBase>(new InsetTabular(*this));
192 Buffer const & InsetTabular::buffer() const
198 void InsetTabular::buffer(Buffer const * b)
204 void InsetTabular::write(Buffer const & buf, ostream & os) const
206 os << "Tabular" << endl;
207 tabular.write(buf, os);
211 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
213 bool const old_format = (lex.getString() == "\\LyXTable");
215 tabular.read(buf, lex);
221 string token = lex.getString();
222 while (lex.isOK() && (token != "\\end_inset")) {
224 token = lex.getString();
226 if (token != "\\end_inset") {
227 lex.printError("Missing \\end_inset at this point. "
233 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
235 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
236 // mi.base.textwidth << "\n";
238 lyxerr << "InsetTabular::metrics: need bv" << endl;
243 for (idx_type cell = 0; i < tabular.rows(); ++i) {
246 for (col_type j = 0; j < tabular.columns(); ++j) {
247 if (tabular.isPartOfMultiColumn(i, j))
248 // Multicolumn cell, but not first one
253 if (tabular.cell_info[i][j].multicolumn ==
254 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
255 p_width = tabular.cellinfo_of_cell(cell).p_width;
257 p_width = tabular.column_info[j].p_width;
259 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
260 tabular.getCellInset(cell)->metrics(m, dim);
262 dim.wid = m.base.textwidth;
263 maxAsc = max(maxAsc, dim.asc);
264 maxDesc = max(maxDesc, dim.des);
265 tabular.setWidthOfCell(cell, dim.wid);
268 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
269 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
272 dim.asc = tabular.getAscentOfRow(0);
273 dim.des = tabular.getHeightOfTabular() - dim.asc;
274 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
279 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
281 setPosCache(pi, x, y);
283 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
284 BufferView * bv = pi.base.bv;
286 static NullPainter nop;
287 static PainterInfo nullpi(bv, nop);
289 //resetPos(bv->cursor());
292 x += ADD_TO_TABULAR_WIDTH;
295 first_visible_cell = LyXTabular::npos;
296 for (row_type i = 0; i < tabular.rows(); ++i) {
298 int const a = tabular.getAscentOfRow(i);
299 int const d = tabular.getDescentOfRow(i);
300 idx = tabular.getCellNumber(i, 0);
301 for (col_type j = 0; j < tabular.columns(); ++j) {
302 if (tabular.isPartOfMultiColumn(i, j))
304 if (first_visible_cell == LyXTabular::npos)
305 first_visible_cell = idx;
307 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
308 if (nx + tabular.getWidthOfColumn(idx) < 0
309 || nx > bv->workWidth()
311 || y - a > bv->workHeight()) {
312 cell(idx)->draw(nullpi, cx, y);
313 drawCellLines(nop, nx, y, i, idx, pi.erased_);
315 cell(idx)->draw(pi, cx, y);
316 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
318 nx += tabular.getWidthOfColumn(idx);
322 if (i + 1 < tabular.rows())
323 y += d + tabular.getAscentOfRow(i + 1) +
324 tabular.getAdditionalHeight(i + 1);
329 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
331 setPosCache(pi, x, y);
333 LCursor & cur = pi.base.bv->cursor();
334 if (!cur.selection())
336 if (!ptr_cmp(&cur.inset(), this))
341 x += scx_ + ADD_TO_TABULAR_WIDTH;
343 if (tablemode(cur)) {
346 getSelection(cur, rs, re, cs, ce);
347 y -= tabular.getAscentOfRow(0);
348 for (row_type j = 0; j < tabular.rows(); ++j) {
349 int const a = tabular.getAscentOfRow(j);
350 int const h = a + tabular.getDescentOfRow(j);
352 y += tabular.getAdditionalHeight(j);
353 for (col_type i = 0; i < tabular.columns(); ++i) {
354 if (tabular.isPartOfMultiColumn(j, i))
356 idx_type const cell =
357 tabular.getCellNumber(j, i);
358 int const w = tabular.getWidthOfColumn(cell);
359 if (i >= cs && i <= ce && j >= rs && j <= re)
360 pi.pain.fillRectangle(xx, y, w, h,
369 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
374 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
375 row_type row, idx_type cell, bool erased) const
377 int x2 = x + tabular.getWidthOfColumn(cell);
379 LColor::color col = LColor::tabularline;
380 LColor::color onoffcol = LColor::tabularonoffline;
383 col = LColor::strikeout;
384 onoffcol = LColor::strikeout;
387 if (!tabular.topAlreadyDrawn(cell)) {
388 on_off = !tabular.topLine(cell);
389 pain.line(x, y - tabular.getAscentOfRow(row),
390 x2, y - tabular.getAscentOfRow(row),
391 on_off ? onoffcol : col,
392 on_off ? Painter::line_onoffdash : Painter::line_solid);
394 on_off = !tabular.bottomLine(cell);
395 pain.line(x, y + tabular.getDescentOfRow(row),
396 x2, y + tabular.getDescentOfRow(row),
397 on_off ? onoffcol : col,
398 on_off ? Painter::line_onoffdash : Painter::line_solid);
399 if (!tabular.leftAlreadyDrawn(cell)) {
400 on_off = !tabular.leftLine(cell);
401 pain.line(x, y - tabular.getAscentOfRow(row),
402 x, y + tabular.getDescentOfRow(row),
403 on_off ? onoffcol : col,
404 on_off ? Painter::line_onoffdash : Painter::line_solid);
406 on_off = !tabular.rightLine(cell);
407 pain.line(x2 - tabular.getAdditionalWidth(cell),
408 y - tabular.getAscentOfRow(row),
409 x2 - tabular.getAdditionalWidth(cell),
410 y + tabular.getDescentOfRow(row),
411 on_off ? onoffcol : col,
412 on_off ? Painter::line_onoffdash : Painter::line_solid);
416 string const InsetTabular::editMessage() const
418 return _("Opened table");
422 void InsetTabular::edit(LCursor & cur, bool left)
424 //lyxerr << "InsetTabular::edit: " << this << endl;
426 cur.selection() = false;
429 if (isRightToLeft(cur))
430 cur.idx() = tabular.getLastCellInRow(0);
436 if (isRightToLeft(cur))
437 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
439 cur.idx() = tabular.getNumberOfCells() - 1;
441 cur.pos() = cur.lastpos(); // FIXME crude guess
443 // this accesses the position cache before it is initialized
445 //cur.bv().fitCursor();
449 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
451 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
452 << "\n cur:" << cur << endl;
453 CursorSlice sl = cur.top();
454 LCursor & bvcur = cur.bv().cursor();
456 switch (cmd.action) {
458 case LFUN_MOUSE_PRESS:
459 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
461 if (cmd.button() == mouse_button::button1) {
462 cur.selection() = false;
463 setCursorFromCoordinates(cur, cmd.x, cmd.y);
469 if (cmd.button() == mouse_button::button2) {
470 // FIXME: pasting multiple cells (with insettabular's own
471 // LFUN_PASTESELECTION still does not work! (jspitzm)
472 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
473 cell(cur.idx())->dispatch(cur, cmd);
477 // we'll pop up the table dialog on release
478 if (cmd.button() == mouse_button::button3)
482 case LFUN_MOUSE_MOTION:
483 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
484 if (cmd.button() == mouse_button::button1) {
485 // only accept motions to places not deeper nested than the real anchor
486 if (bvcur.anchor_.hasPart(cur)) {
487 setCursorFromCoordinates(cur, cmd.x, cmd.y);
488 bvcur.setCursor(cur);
489 bvcur.selection() = true;
495 case LFUN_MOUSE_RELEASE:
496 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
497 if (cmd.button() == mouse_button::button3)
498 InsetTabularMailer(*this).showDialog(&cur.bv());
501 case LFUN_CELL_BACKWARD:
503 cur.selection() = false;
506 case LFUN_CELL_FORWARD:
508 cur.selection() = false;
513 cell(cur.idx())->dispatch(cur, cmd);
514 if (!cur.result().dispatched()) {
515 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
517 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
525 cell(cur.idx())->dispatch(cur, cmd);
526 if (!cur.result().dispatched()) {
527 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
529 cmd = FuncRequest(LFUN_FINISHED_LEFT);
537 cell(cur.idx())->dispatch(cur, cmd);
538 cur.dispatched(); // override the cell's decision
540 // if our LyXText didn't do anything to the cursor
541 // then we try to put the cursor into the cell below
542 // setting also the right targetX.
543 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
544 cur.idx() = tabular.getCellBelow(cur.idx());
546 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
547 cur.pit(), 0, cur.targetX());
549 if (sl == cur.top()) {
550 // we trick it to go to the RIGHT after leaving the
552 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
559 cell(cur.idx())->dispatch(cur, cmd);
560 cur.dispatched(); // override the cell's decision
562 // if our LyXText didn't do anything to the cursor
563 // then we try to put the cursor into the cell above
564 // setting also the right targetX.
565 if (tabular.row_of_cell(cur.idx()) != 0) {
566 cur.idx() = tabular.getCellAbove(cur.idx());
567 cur.pit() = cur.lastpit();
568 LyXText const * text = cell(cur.idx())->getText(0);
569 cur.pos() = text->x2pos(
571 text->paragraphs().back().rows().size()-1,
574 if (sl == cur.top()) {
575 cmd = FuncRequest(LFUN_FINISHED_UP);
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();
585 // if (t < yo() + tabular.getHeightOfTabular()) {
586 // cur.bv().scrollDocView(t);
587 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
589 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
596 // case LFUN_PRIOR: {
597 // //if (hasSelection())
598 // // cur.selection() = false;
599 // col_type const col = tabular.column_of_cell(cur.idx());
600 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
602 // cur.bv().scrollDocView(t);
606 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
610 // cur.par() = cur.lastpar();
611 // cur.pos() = cur.lastpos();
615 case LFUN_LAYOUT_TABULAR:
616 InsetTabularMailer(*this).showDialog(&cur.bv());
619 case LFUN_INSET_DIALOG_UPDATE:
620 InsetTabularMailer(*this).updateDialog(&cur.bv());
623 case LFUN_TABULAR_FEATURE:
624 if (!tabularFeatures(cur, cmd.argument))
628 // insert file functions
629 case LFUN_FILE_INSERT_ASCII_PARA:
630 case LFUN_FILE_INSERT_ASCII: {
631 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
632 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
638 if (tablemode(cur)) {
639 if (copySelection(cur)) {
640 recordUndoInset(cur, Undo::DELETE);
645 cell(cur.idx())->dispatch(cur, cmd);
650 if (tablemode(cur)) {
651 recordUndoInset(cur, Undo::DELETE);
655 cell(cur.idx())->dispatch(cur, cmd);
659 if (!cur.selection())
661 if (tablemode(cur)) {
665 cell(cur.idx())->dispatch(cur, cmd);
668 case LFUN_PASTESELECTION: {
669 string const clip = cur.bv().getClipboard();
672 if (clip.find('\t') != string::npos) {
675 col_type maxCols = 1;
676 size_t len = clip.length();
677 for (size_t p = 0; p < len; ++p) {
678 p = clip.find_first_of("\t\n", p);
679 if (p == string::npos)
688 maxCols = max(cols, maxCols);
693 maxCols = max(cols, maxCols);
696 new LyXTabular(cur.buffer().params(), rows, maxCols));
698 string::size_type op = 0;
700 idx_type const cells =
701 paste_tabular->getNumberOfCells();
704 for (size_t p = 0; cell < cells && p < len; ++p) {
705 p = clip.find_first_of("\t\n", p);
706 if (p == string::npos || p >= len)
710 paste_tabular->getCellInset(cell)->
711 setText(clip.substr(op, p - op), font);
716 paste_tabular->getCellInset(cell)->
717 setText(clip.substr(op, p - op), font);
718 while (cols++ < maxCols)
725 // check for the last cell if there is no trailing '\n'
726 if (cell < cells && op < len)
727 paste_tabular->getCellInset(cell)->
728 setText(clip.substr(op, len - op), font);
729 } else if (!insertAsciiString(cur.bv(), clip, true)) {
730 // so that the clipboard is used and it goes on
732 // and executes LFUN_PASTESELECTION in insettext!
733 paste_tabular.reset();
739 if (hasPasteBuffer() && tabularStackDirty()) {
740 recordUndoInset(cur, Undo::INSERT);
744 cell(cur.idx())->dispatch(cur, cmd);
755 case LFUN_FREEFONT_APPLY:
756 case LFUN_FREEFONT_UPDATE:
760 case LFUN_CAPITALIZE_WORD:
761 case LFUN_UPCASE_WORD:
762 case LFUN_LOWCASE_WORD:
763 case LFUN_TRANSPOSE_CHARS:
764 if (tablemode(cur)) {
767 getSelection(cur, rs, re, cs, ce);
768 for (row_type i = rs; i <= re; ++i) {
769 for (col_type j = cs; j <= ce; ++j) {
770 // cursor follows cell:
771 cur.idx() = tabular.getCellNumber(i, j);
772 // select this cell only:
775 cur.pos() = cur.top().lastpos();
778 cell(cur.idx())->dispatch(cur, cmd);
781 // Restore original selection
782 cur.idx() = tabular.getCellNumber(rs, cs);
785 cur.idx() = tabular.getCellNumber(re, ce);
786 cur.pos() = cur.top().lastpos();
791 cell(cur.idx())->dispatch(cur, cmd);
795 // we try to handle this event in the insets dispatch function.
796 cell(cur.idx())->dispatch(cur, cmd);
801 InsetTabularMailer(*this).updateDialog(&cur.bv());
805 // function sets an object as defined in func_status.h:
806 // states OK, Unknown, Disabled, On, Off.
807 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
808 FuncStatus & status) const
810 switch (cmd.action) {
811 case LFUN_TABULAR_FEATURE: {
812 int action = LyXTabular::LAST_ACTION;
814 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
815 string const tmp = tabularFeature[i].feature;
816 if (tmp == cmd.argument.substr(0, tmp.length())) {
817 action = tabularFeature[i].action;
821 if (action == LyXTabular::LAST_ACTION) {
823 status.unknown(true);
827 string const argument
828 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
830 row_type sel_row_start = 0;
831 row_type sel_row_end = 0;
833 LyXTabular::ltType dummyltt;
836 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
839 case LyXTabular::SET_PWIDTH:
840 case LyXTabular::SET_MPWIDTH:
841 case LyXTabular::SET_SPECIAL_COLUMN:
842 case LyXTabular::SET_SPECIAL_MULTI:
843 case LyXTabular::APPEND_ROW:
844 case LyXTabular::APPEND_COLUMN:
845 case LyXTabular::DELETE_ROW:
846 case LyXTabular::DELETE_COLUMN:
847 case LyXTabular::SET_ALL_LINES:
848 case LyXTabular::UNSET_ALL_LINES:
852 case LyXTabular::MULTICOLUMN:
853 status.setOnOff(tabular.isMultiColumn(cur.idx()));
856 case LyXTabular::M_TOGGLE_LINE_TOP:
858 case LyXTabular::TOGGLE_LINE_TOP:
859 status.setOnOff(tabular.topLine(cur.idx(), flag));
862 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
864 case LyXTabular::TOGGLE_LINE_BOTTOM:
865 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
868 case LyXTabular::M_TOGGLE_LINE_LEFT:
870 case LyXTabular::TOGGLE_LINE_LEFT:
871 status.setOnOff(tabular.leftLine(cur.idx(), flag));
874 case LyXTabular::M_TOGGLE_LINE_RIGHT:
876 case LyXTabular::TOGGLE_LINE_RIGHT:
877 status.setOnOff(tabular.rightLine(cur.idx(), flag));
880 case LyXTabular::M_ALIGN_LEFT:
882 case LyXTabular::ALIGN_LEFT:
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
886 case LyXTabular::M_ALIGN_RIGHT:
888 case LyXTabular::ALIGN_RIGHT:
889 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
892 case LyXTabular::M_ALIGN_CENTER:
894 case LyXTabular::ALIGN_CENTER:
895 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
898 case LyXTabular::ALIGN_BLOCK:
899 status.enabled(!tabular.getPWidth(cur.idx()).zero());
900 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
903 case LyXTabular::M_VALIGN_TOP:
905 case LyXTabular::VALIGN_TOP:
907 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
910 case LyXTabular::M_VALIGN_BOTTOM:
912 case LyXTabular::VALIGN_BOTTOM:
914 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
917 case LyXTabular::M_VALIGN_MIDDLE:
919 case LyXTabular::VALIGN_MIDDLE:
921 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
924 case LyXTabular::SET_LONGTABULAR:
925 status.setOnOff(tabular.isLongTabular());
928 case LyXTabular::UNSET_LONGTABULAR:
929 status.setOnOff(!tabular.isLongTabular());
932 case LyXTabular::SET_ROTATE_TABULAR:
933 status.setOnOff(tabular.getRotateTabular());
936 case LyXTabular::UNSET_ROTATE_TABULAR:
937 status.setOnOff(!tabular.getRotateTabular());
940 case LyXTabular::SET_ROTATE_CELL:
941 status.setOnOff(tabular.getRotateCell(cur.idx()));
944 case LyXTabular::UNSET_ROTATE_CELL:
945 status.setOnOff(!tabular.getRotateCell(cur.idx()));
948 case LyXTabular::SET_USEBOX:
949 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
952 case LyXTabular::SET_LTFIRSTHEAD:
953 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
956 case LyXTabular::UNSET_LTFIRSTHEAD:
957 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
960 case LyXTabular::SET_LTHEAD:
961 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
964 case LyXTabular::UNSET_LTHEAD:
965 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
968 case LyXTabular::SET_LTFOOT:
969 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
972 case LyXTabular::UNSET_LTFOOT:
973 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
976 case LyXTabular::SET_LTLASTFOOT:
977 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
980 case LyXTabular::UNSET_LTLASTFOOT:
981 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
984 case LyXTabular::SET_LTNEWPAGE:
985 status.setOnOff(tabular.getLTNewPage(sel_row_start));
990 status.enabled(false);
996 // These are only enabled inside tabular
997 case LFUN_CELL_BACKWARD:
998 case LFUN_CELL_FORWARD:
999 status.enabled(true);
1002 // disable these with multiple cells selected
1003 case LFUN_INSET_INSERT:
1004 case LFUN_TABULAR_INSERT:
1005 case LFUN_INSERT_CHARSTYLE:
1006 case LFUN_INSET_FLOAT:
1007 case LFUN_INSET_WIDE_FLOAT:
1008 case LFUN_INSET_FOOTNOTE:
1009 case LFUN_INSET_MARGINAL:
1010 case LFUN_INSERT_MATH:
1011 case LFUN_MATH_MODE:
1012 case LFUN_MATH_MUTATE:
1013 case LFUN_MATH_DISPLAY:
1014 case LFUN_INSERT_NOTE:
1015 case LFUN_INSET_OPTARG:
1016 case LFUN_INSERT_BOX:
1017 case LFUN_INSERT_BRANCH:
1018 case LFUN_INSET_WRAP:
1019 case LFUN_INSET_ERT: {
1020 if (tablemode(cur)) {
1021 status.enabled(false);
1024 return cell(cur.idx())->getStatus(cur, cmd, status);
1027 // disable in non-fixed-width cells
1028 case LFUN_BREAKLINE:
1029 case LFUN_BREAKPARAGRAPH:
1030 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1031 case LFUN_BREAKPARAGRAPH_SKIP: {
1032 if (tabular.getPWidth(cur.idx()).zero()) {
1033 status.enabled(false);
1036 return cell(cur.idx())->getStatus(cur, cmd, status);
1039 case LFUN_INSET_MODIFY:
1040 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1041 status.enabled(true);
1047 // we try to handle this event in the insets dispatch function.
1048 return cell(cur.idx())->getStatus(cur, cmd, status);
1053 int InsetTabular::latex(Buffer const & buf, ostream & os,
1054 OutputParams const & runparams) const
1056 return tabular.latex(buf, os, runparams);
1060 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1061 OutputParams const & runparams) const
1063 int const dp = runparams.linelen ? runparams.depth : 0;
1064 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1068 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1069 OutputParams const & runparams) const
1071 return tabular.linuxdoc(buf,os, runparams);
1075 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1076 OutputParams const & runparams) const
1079 InsetBase * master = 0;
1081 #ifdef WITH_WARNINGS
1082 #warning Why not pass a proper DocIterator here?
1085 // if the table is inside a float it doesn't need the informaltable
1086 // wrapper. Search for it.
1087 for (master = owner(); master; master = master->owner())
1088 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1093 os << "<informaltable>";
1096 ret += tabular.docbook(buf, os, runparams);
1098 os << "</informaltable>";
1105 void InsetTabular::validate(LaTeXFeatures & features) const
1107 tabular.validate(features);
1111 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1113 return tabular.getCellInset(idx);
1117 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1119 return tabular.getCellInset(idx);
1123 void InsetTabular::cursorPos
1124 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1126 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1128 // y offset correction
1129 int const row = tabular.row_of_cell(sl.idx());
1130 for (int i = 0; i <= row; ++i) {
1132 y += tabular.getAscentOfRow(i);
1133 y += tabular.getAdditionalHeight(i);
1136 y += tabular.getDescentOfRow(i);
1139 // x offset correction
1140 int const col = tabular.column_of_cell(sl.idx());
1141 int idx = tabular.getCellNumber(row, 0);
1142 for (int j = 0; j < col; ++j) {
1143 if (tabular.isPartOfMultiColumn(row, j))
1145 x += tabular.getWidthOfColumn(idx);
1148 x += tabular.getBeginningOfTextInCell(idx);
1149 x += ADD_TO_TABULAR_WIDTH;
1154 int InsetTabular::dist(idx_type const cell, int x, int y) const
1158 InsetBase const & inset = *tabular.getCellInset(cell);
1159 Point o = theCoords.getInsets().xy(&inset);
1160 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1161 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1162 int const ybeg = o.y_ - inset.ascent();
1163 row_type const row = tabular.row_of_cell(cell);
1164 int const rowheight = tabular.getAscentOfRow(row)
1165 + tabular.getDescentOfRow(row);
1166 int const yend = ybeg + rowheight;
1178 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1179 // << " ybeg=" << ybeg << " yend=" << yend
1180 // << " xx=" << xx << " yy=" << yy
1181 // << " dist=" << xx + yy << endl;
1186 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1188 //lyxerr << "InsetTabular::editXY: " << this << endl;
1189 cur.selection() = false;
1191 cur.idx() = getNearestCell(x, y);
1193 return cell(cur.idx())->text_.editXY(cur, x, y);
1197 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1199 cur.idx() = getNearestCell(x, y);
1200 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1204 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1206 idx_type idx_min = 0;
1207 int dist_min = std::numeric_limits<int>::max();
1208 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1209 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1210 int const d = dist(i, x, y);
1221 int InsetTabular::getCellXPos(idx_type const cell) const
1225 for (; !tabular.isFirstCellInRow(c); --c)
1227 int lx = tabular.getWidthOfColumn(cell);
1228 for (; c < cell; ++c)
1229 lx += tabular.getWidthOfColumn(c);
1231 return lx - tabular.getWidthOfColumn(cell);
1235 void InsetTabular::resetPos(LCursor & cur) const
1237 BufferView & bv = cur.bv();
1238 int const maxwidth = bv.workWidth();
1240 if (&cur.inset() != this) {
1244 int const X2 = maxwidth;
1245 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1246 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1247 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1250 scx_ = X1 + 20 - x1;
1252 scx_ = X2 - 20 - x2;
1259 InsetTabularMailer(*this).updateDialog(&bv);
1263 void InsetTabular::moveNextCell(LCursor & cur)
1265 if (isRightToLeft(cur)) {
1266 if (tabular.isFirstCellInRow(cur.idx())) {
1267 row_type const row = tabular.row_of_cell(cur.idx());
1268 if (row == tabular.rows() - 1)
1270 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1277 if (tabular.isLastCell(cur.idx()))
1287 void InsetTabular::movePrevCell(LCursor & cur)
1289 if (isRightToLeft(cur)) {
1290 if (tabular.isLastCellInRow(cur.idx())) {
1291 row_type const row = tabular.row_of_cell(cur.idx());
1294 cur.idx() = tabular.getFirstCellInRow(row);
1295 cur.idx() = tabular.getCellAbove(cur.idx());
1297 if (tabular.isLastCell(cur.idx()))
1302 if (cur.idx() == 0) // first cell
1306 cur.pit() = cur.lastpit();
1307 cur.pos() = cur.lastpos();
1312 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1314 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1317 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1318 string const tmp = tabularFeature[i].feature;
1320 if (tmp == what.substr(0, tmp.length())) {
1321 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1322 //tabularFeatures[i].feature.length())) {
1323 action = tabularFeature[i].action;
1327 if (action == LyXTabular::LAST_ACTION)
1331 ltrim(what.substr(tabularFeature[i].feature.length()));
1332 tabularFeatures(cur, action, val);
1339 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1340 string const & special, bool & flag)
1342 if (special == "dl_above") {
1345 } else if (special == "dl_below") {
1346 ltt.bottomDL = flag;
1348 } else if (special == "empty") {
1360 void InsetTabular::tabularFeatures(LCursor & cur,
1361 LyXTabular::Feature feature, string const & value)
1363 BufferView & bv = cur.bv();
1364 col_type sel_col_start;
1365 col_type sel_col_end;
1366 row_type sel_row_start;
1367 row_type sel_row_end;
1368 bool setLines = false;
1369 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1370 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1374 case LyXTabular::M_ALIGN_LEFT:
1375 case LyXTabular::ALIGN_LEFT:
1376 setAlign = LYX_ALIGN_LEFT;
1379 case LyXTabular::M_ALIGN_RIGHT:
1380 case LyXTabular::ALIGN_RIGHT:
1381 setAlign = LYX_ALIGN_RIGHT;
1384 case LyXTabular::M_ALIGN_CENTER:
1385 case LyXTabular::ALIGN_CENTER:
1386 setAlign = LYX_ALIGN_CENTER;
1389 case LyXTabular::ALIGN_BLOCK:
1390 setAlign = LYX_ALIGN_BLOCK;
1393 case LyXTabular::M_VALIGN_TOP:
1394 case LyXTabular::VALIGN_TOP:
1395 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1398 case LyXTabular::M_VALIGN_BOTTOM:
1399 case LyXTabular::VALIGN_BOTTOM:
1400 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1403 case LyXTabular::M_VALIGN_MIDDLE:
1404 case LyXTabular::VALIGN_MIDDLE:
1405 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1412 recordUndoInset(cur, Undo::ATOMIC);
1414 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1415 row_type const row = tabular.row_of_cell(cur.idx());
1416 col_type const column = tabular.column_of_cell(cur.idx());
1418 LyXTabular::ltType ltt;
1422 case LyXTabular::SET_PWIDTH: {
1423 LyXLength const len(value);
1424 tabular.setColumnPWidth(cur, cur.idx(), len);
1426 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1427 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1431 case LyXTabular::SET_MPWIDTH:
1432 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1435 case LyXTabular::SET_SPECIAL_COLUMN:
1436 case LyXTabular::SET_SPECIAL_MULTI:
1437 tabular.setAlignSpecial(cur.idx(),value,feature);
1440 case LyXTabular::APPEND_ROW:
1441 // append the row into the tabular
1442 tabular.appendRow(bv.buffer()->params(), cur.idx());
1445 case LyXTabular::APPEND_COLUMN:
1446 // append the column into the tabular
1447 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1448 cur.idx() = tabular.getCellNumber(row, column);
1451 case LyXTabular::DELETE_ROW:
1452 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1453 tabular.deleteRow(sel_row_start);
1454 if (sel_row_start >= tabular.rows())
1456 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1459 cur.selection() = false;
1462 case LyXTabular::DELETE_COLUMN:
1463 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1464 tabular.deleteColumn(sel_col_start);
1465 if (sel_col_start >= tabular.columns())
1467 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1470 cur.selection() = false;
1473 case LyXTabular::M_TOGGLE_LINE_TOP:
1475 case LyXTabular::TOGGLE_LINE_TOP: {
1476 bool lineSet = !tabular.topLine(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)
1480 tabular.getCellNumber(i, j),
1485 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1487 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1488 bool lineSet = !tabular.bottomLine(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.setBottomLine(
1492 tabular.getCellNumber(i, j),
1498 case LyXTabular::M_TOGGLE_LINE_LEFT:
1500 case LyXTabular::TOGGLE_LINE_LEFT: {
1501 bool lineSet = !tabular.leftLine(cur.idx(), flag);
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.setLeftLine(
1505 tabular.getCellNumber(i,j),
1511 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1513 case LyXTabular::TOGGLE_LINE_RIGHT: {
1514 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1515 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1516 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1517 tabular.setRightLine(
1518 tabular.getCellNumber(i,j),
1524 case LyXTabular::M_ALIGN_LEFT:
1525 case LyXTabular::M_ALIGN_RIGHT:
1526 case LyXTabular::M_ALIGN_CENTER:
1528 case LyXTabular::ALIGN_LEFT:
1529 case LyXTabular::ALIGN_RIGHT:
1530 case LyXTabular::ALIGN_CENTER:
1531 case LyXTabular::ALIGN_BLOCK:
1532 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1533 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1534 tabular.setAlignment(
1535 tabular.getCellNumber(i, j),
1540 case LyXTabular::M_VALIGN_TOP:
1541 case LyXTabular::M_VALIGN_BOTTOM:
1542 case LyXTabular::M_VALIGN_MIDDLE:
1544 case LyXTabular::VALIGN_TOP:
1545 case LyXTabular::VALIGN_BOTTOM:
1546 case LyXTabular::VALIGN_MIDDLE:
1547 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1548 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1549 tabular.setVAlignment(
1550 tabular.getCellNumber(i, j),
1554 case LyXTabular::MULTICOLUMN: {
1555 if (sel_row_start != sel_row_end) {
1556 #ifdef WITH_WARNINGS
1557 #warning Need I say it ? This is horrible.
1559 Alert::error(_("Error setting multicolumn"),
1560 _("You cannot set multicolumn vertically."));
1563 if (!cur.selection()) {
1564 // just multicol for one single cell
1565 // check whether we are completely in a multicol
1566 if (tabular.isMultiColumn(cur.idx()))
1567 tabular.unsetMultiColumn(cur.idx());
1569 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1572 // we have a selection so this means we just add all this
1573 // cells to form a multicolumn cell
1574 idx_type const s_start = cur.selBegin().idx();
1575 idx_type const s_end = cur.selEnd().idx();
1576 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1577 cur.idx() = s_start;
1580 cur.selection() = false;
1584 case LyXTabular::SET_ALL_LINES:
1586 case LyXTabular::UNSET_ALL_LINES:
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.setAllLines(
1590 tabular.getCellNumber(i,j), setLines);
1593 case LyXTabular::SET_LONGTABULAR:
1594 tabular.setLongTabular(true);
1597 case LyXTabular::UNSET_LONGTABULAR:
1598 tabular.setLongTabular(false);
1601 case LyXTabular::SET_ROTATE_TABULAR:
1602 tabular.setRotateTabular(true);
1605 case LyXTabular::UNSET_ROTATE_TABULAR:
1606 tabular.setRotateTabular(false);
1609 case LyXTabular::SET_ROTATE_CELL:
1610 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1611 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1612 tabular.setRotateCell(
1613 tabular.getCellNumber(i, j), true);
1616 case LyXTabular::UNSET_ROTATE_CELL:
1617 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1618 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1619 tabular.setRotateCell(
1620 tabular.getCellNumber(i, j), false);
1623 case LyXTabular::SET_USEBOX: {
1624 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1625 if (val == tabular.getUsebox(cur.idx()))
1626 val = LyXTabular::BOX_NONE;
1627 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1628 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1629 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1633 case LyXTabular::UNSET_LTFIRSTHEAD:
1635 case LyXTabular::SET_LTFIRSTHEAD:
1636 tabular.getRowOfLTFirstHead(row, ltt);
1637 checkLongtableSpecial(ltt, value, flag);
1638 tabular.setLTHead(row, flag, ltt, true);
1641 case LyXTabular::UNSET_LTHEAD:
1643 case LyXTabular::SET_LTHEAD:
1644 tabular.getRowOfLTHead(row, ltt);
1645 checkLongtableSpecial(ltt, value, flag);
1646 tabular.setLTHead(row, flag, ltt, false);
1649 case LyXTabular::UNSET_LTFOOT:
1651 case LyXTabular::SET_LTFOOT:
1652 tabular.getRowOfLTFoot(row, ltt);
1653 checkLongtableSpecial(ltt, value, flag);
1654 tabular.setLTFoot(row, flag, ltt, false);
1657 case LyXTabular::UNSET_LTLASTFOOT:
1659 case LyXTabular::SET_LTLASTFOOT:
1660 tabular.getRowOfLTLastFoot(row, ltt);
1661 checkLongtableSpecial(ltt, value, flag);
1662 tabular.setLTFoot(row, flag, ltt, true);
1665 case LyXTabular::SET_LTNEWPAGE:
1666 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1669 // dummy stuff just to avoid warnings
1670 case LyXTabular::LAST_ACTION:
1674 InsetTabularMailer(*this).updateDialog(&bv);
1678 bool InsetTabular::showInsetDialog(BufferView * bv) const
1680 InsetTabularMailer(*this).showDialog(bv);
1685 void InsetTabular::openLayoutDialog(BufferView * bv) const
1687 InsetTabularMailer(*this).showDialog(bv);
1691 bool InsetTabular::copySelection(LCursor & cur)
1693 if (!cur.selection())
1698 getSelection(cur, rs, re, cs, ce);
1700 paste_tabular.reset(new LyXTabular(tabular));
1702 for (row_type i = 0; i < rs; ++i)
1703 paste_tabular->deleteRow(0);
1705 row_type const rows = re - rs + 1;
1706 while (paste_tabular->rows() > rows)
1707 paste_tabular->deleteRow(rows);
1709 paste_tabular->setTopLine(0, true, true);
1710 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1713 for (col_type i = 0; i < cs; ++i)
1714 paste_tabular->deleteColumn(0);
1716 col_type const columns = ce - cs + 1;
1717 while (paste_tabular->columns() > columns)
1718 paste_tabular->deleteColumn(columns);
1720 paste_tabular->setLeftLine(0, true, true);
1721 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1725 OutputParams const runparams;
1726 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1727 cur.bv().stuffClipboard(os.str());
1728 // mark tabular stack dirty
1729 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1730 // when we (hopefully) have a one-for-all paste mechanism.
1731 lyx::cap::dirtyTabularStack(true);
1737 bool InsetTabular::pasteSelection(LCursor & cur)
1741 col_type const actcol = tabular.column_of_cell(cur.idx());
1742 row_type const actrow = tabular.row_of_cell(cur.idx());
1743 for (row_type r1 = 0, r2 = actrow;
1744 r1 < paste_tabular->rows() && r2 < tabular.rows();
1746 for (col_type c1 = 0, c2 = actcol;
1747 c1 < paste_tabular->columns() && c2 < tabular.columns();
1749 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1750 tabular.isPartOfMultiColumn(r2, c2))
1752 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1756 if (tabular.isPartOfMultiColumn(r2, c2)) {
1760 shared_ptr<InsetText> inset(
1761 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1762 tabular.setCellInset(r2, c2, inset);
1771 void InsetTabular::cutSelection(LCursor & cur)
1773 if (!cur.selection())
1778 getSelection(cur, rs, re, cs, ce);
1779 for (row_type i = rs; i <= re; ++i) {
1780 for (col_type j = cs; j <= ce; ++j) {
1781 shared_ptr<InsetText> t
1782 = cell(tabular.getCellNumber(i, j));
1783 if (cur.buffer().params().tracking_changes)
1784 t->markErased(true);
1790 // cursor position might be invalid now
1791 cur.pos() = cur.lastpos();
1792 cur.clearSelection();
1796 bool InsetTabular::isRightToLeft(LCursor & cur) const
1798 BOOST_ASSERT(cur.depth() > 1);
1799 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1800 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1801 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1802 parentpos).language()->RightToLeft();
1806 void InsetTabular::getSelection(LCursor & cur,
1807 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1809 CursorSlice const & beg = cur.selBegin();
1810 CursorSlice const & end = cur.selEnd();
1811 cs = tabular.column_of_cell(beg.idx());
1812 ce = tabular.column_of_cell(end.idx());
1815 cs = tabular.column_of_cell(end.idx());
1817 ce = tabular.right_column_of_cell(end.idx());
1820 rs = tabular.row_of_cell(beg.idx());
1821 re = tabular.row_of_cell(end.idx());
1827 LyXText * InsetTabular::getText(int idx) const
1829 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1833 void InsetTabular::markErased(bool erased)
1835 for (idx_type idx = 0; idx < nargs(); ++idx)
1836 cell(idx)->markErased(erased);
1840 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1842 return tabular.getPWidth(cell).zero();
1846 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1849 if (buf.length() <= 0)
1854 col_type maxCols = 1;
1855 string::size_type const len = buf.length();
1856 string::size_type p = 0;
1858 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1866 maxCols = max(cols, maxCols);
1872 maxCols = max(cols, maxCols);
1873 LyXTabular * loctab;
1878 paste_tabular.reset(
1879 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1880 loctab = paste_tabular.get();
1884 cell = bv.cursor().idx();
1885 ocol = tabular.column_of_cell(cell);
1886 row = tabular.row_of_cell(cell);
1889 string::size_type op = 0;
1890 idx_type const cells = loctab->getNumberOfCells();
1893 rows = loctab->rows();
1894 col_type const columns = loctab->columns();
1896 while (cell < cells && p < len && row < rows &&
1897 (p = buf.find_first_of("\t\n", p)) != string::npos)
1903 // we can only set this if we are not too far right
1904 if (cols < columns) {
1905 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1906 Paragraph & par = inset->text_.getPar(0);
1907 LyXFont const font = inset->text_.getFont(par, 0);
1908 inset->setText(buf.substr(op, p - op), font);
1914 // we can only set this if we are not too far right
1915 if (cols < columns) {
1916 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1917 Paragraph & par = inset->text_.getPar(0);
1918 LyXFont const font = inset->text_.getFont(par, 0);
1919 inset->setText(buf.substr(op, p - op), font);
1924 cell = loctab->getCellNumber(row, cols);
1930 // check for the last cell if there is no trailing '\n'
1931 if (cell < cells && op < len) {
1932 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1933 Paragraph & par = inset->text_.getPar(0);
1934 LyXFont const font = inset->text_.getFont(par, 0);
1935 inset->setText(buf.substr(op, len - op), font);
1941 void InsetTabular::addPreview(PreviewLoader & loader) const
1943 row_type const rows = tabular.rows();
1944 col_type const columns = tabular.columns();
1945 for (row_type i = 0; i < rows; ++i) {
1946 for (col_type j = 0; j < columns; ++j)
1947 tabular.getCellInset(i, j)->addPreview(loader);
1952 bool InsetTabular::tablemode(LCursor & cur) const
1954 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1961 string const InsetTabularMailer::name_("tabular");
1963 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1964 : inset_(const_cast<InsetTabular &>(inset))
1968 string const InsetTabularMailer::inset2string(Buffer const &) const
1970 return params2string(inset_);
1974 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1976 istringstream data(in);
1978 lex.setStream(data);
1985 if (!lex || token != name_)
1986 return print_mailer_error("InsetTabularMailer", in, 1,
1989 // This is part of the inset proper that is usually swallowed
1990 // by Buffer::readInset
1992 if (!lex || token != "Tabular")
1993 return print_mailer_error("InsetTabularMailer", in, 2,
1996 Buffer const & buffer = inset.buffer();
1997 inset.read(buffer, lex);
2001 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2004 data << name_ << ' ';
2005 inset.write(inset.buffer(), data);
2006 data << "\\end_inset\n";