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)), buf.text().bv()), 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, &cur.bv()));
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);
1040 if (tabularStackDirty()) {
1041 status.enabled(true);
1045 case LFUN_INSET_MODIFY:
1046 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1047 status.enabled(true);
1053 // we try to handle this event in the insets dispatch function.
1054 return cell(cur.idx())->getStatus(cur, cmd, status);
1059 int InsetTabular::latex(Buffer const & buf, ostream & os,
1060 OutputParams const & runparams) const
1062 return tabular.latex(buf, os, runparams);
1066 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1067 OutputParams const & runparams) const
1069 int const dp = runparams.linelen ? runparams.depth : 0;
1070 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1074 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1075 OutputParams const & runparams) const
1077 return tabular.linuxdoc(buf,os, runparams);
1081 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1082 OutputParams const & runparams) const
1085 InsetBase * master = 0;
1087 #ifdef WITH_WARNINGS
1088 #warning Why not pass a proper DocIterator here?
1091 // if the table is inside a float it doesn't need the informaltable
1092 // wrapper. Search for it.
1093 for (master = owner(); master; master = master->owner())
1094 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1099 os << "<informaltable>";
1102 ret += tabular.docbook(buf, os, runparams);
1104 os << "</informaltable>";
1111 void InsetTabular::validate(LaTeXFeatures & features) const
1113 tabular.validate(features);
1117 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1119 return tabular.getCellInset(idx);
1123 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1125 return tabular.getCellInset(idx);
1129 void InsetTabular::cursorPos
1130 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1132 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1134 // y offset correction
1135 int const row = tabular.row_of_cell(sl.idx());
1136 for (int i = 0; i <= row; ++i) {
1138 y += tabular.getAscentOfRow(i);
1139 y += tabular.getAdditionalHeight(i);
1142 y += tabular.getDescentOfRow(i);
1145 // x offset correction
1146 int const col = tabular.column_of_cell(sl.idx());
1147 int idx = tabular.getCellNumber(row, 0);
1148 for (int j = 0; j < col; ++j) {
1149 if (tabular.isPartOfMultiColumn(row, j))
1151 x += tabular.getWidthOfColumn(idx);
1154 x += tabular.getBeginningOfTextInCell(idx);
1155 x += ADD_TO_TABULAR_WIDTH;
1160 int InsetTabular::dist(idx_type const cell, int x, int y) const
1164 InsetBase const & inset = *tabular.getCellInset(cell);
1165 Point o = theCoords.getInsets().xy(&inset);
1166 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1167 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1168 int const ybeg = o.y_ - inset.ascent();
1169 row_type const row = tabular.row_of_cell(cell);
1170 int const rowheight = tabular.getAscentOfRow(row)
1171 + tabular.getDescentOfRow(row);
1172 int const yend = ybeg + rowheight;
1184 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1185 // << " ybeg=" << ybeg << " yend=" << yend
1186 // << " xx=" << xx << " yy=" << yy
1187 // << " dist=" << xx + yy << endl;
1192 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1194 //lyxerr << "InsetTabular::editXY: " << this << endl;
1195 cur.selection() = false;
1197 cur.idx() = getNearestCell(x, y);
1199 return cell(cur.idx())->text_.editXY(cur, x, y);
1203 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1205 cur.idx() = getNearestCell(x, y);
1206 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1210 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1212 idx_type idx_min = 0;
1213 int dist_min = std::numeric_limits<int>::max();
1214 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1215 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1216 int const d = dist(i, x, y);
1227 int InsetTabular::getCellXPos(idx_type const cell) const
1231 for (; !tabular.isFirstCellInRow(c); --c)
1233 int lx = tabular.getWidthOfColumn(cell);
1234 for (; c < cell; ++c)
1235 lx += tabular.getWidthOfColumn(c);
1237 return lx - tabular.getWidthOfColumn(cell);
1241 void InsetTabular::resetPos(LCursor & cur) const
1243 BufferView & bv = cur.bv();
1244 int const maxwidth = bv.workWidth();
1246 if (&cur.inset() != this) {
1250 int const X2 = maxwidth;
1251 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1252 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1253 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1256 scx_ = X1 + 20 - x1;
1258 scx_ = X2 - 20 - x2;
1265 InsetTabularMailer(*this).updateDialog(&bv);
1269 void InsetTabular::moveNextCell(LCursor & cur)
1271 if (isRightToLeft(cur)) {
1272 if (tabular.isFirstCellInRow(cur.idx())) {
1273 row_type const row = tabular.row_of_cell(cur.idx());
1274 if (row == tabular.rows() - 1)
1276 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1283 if (tabular.isLastCell(cur.idx()))
1293 void InsetTabular::movePrevCell(LCursor & cur)
1295 if (isRightToLeft(cur)) {
1296 if (tabular.isLastCellInRow(cur.idx())) {
1297 row_type const row = tabular.row_of_cell(cur.idx());
1300 cur.idx() = tabular.getFirstCellInRow(row);
1301 cur.idx() = tabular.getCellAbove(cur.idx());
1303 if (tabular.isLastCell(cur.idx()))
1308 if (cur.idx() == 0) // first cell
1312 cur.pit() = cur.lastpit();
1313 cur.pos() = cur.lastpos();
1318 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1320 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1323 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1324 string const tmp = tabularFeature[i].feature;
1326 if (tmp == what.substr(0, tmp.length())) {
1327 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1328 //tabularFeatures[i].feature.length())) {
1329 action = tabularFeature[i].action;
1333 if (action == LyXTabular::LAST_ACTION)
1337 ltrim(what.substr(tabularFeature[i].feature.length()));
1338 tabularFeatures(cur, action, val);
1345 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1346 string const & special, bool & flag)
1348 if (special == "dl_above") {
1351 } else if (special == "dl_below") {
1352 ltt.bottomDL = flag;
1354 } else if (special == "empty") {
1366 void InsetTabular::tabularFeatures(LCursor & cur,
1367 LyXTabular::Feature feature, string const & value)
1369 BufferView & bv = cur.bv();
1370 col_type sel_col_start;
1371 col_type sel_col_end;
1372 row_type sel_row_start;
1373 row_type sel_row_end;
1374 bool setLines = false;
1375 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1376 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1380 case LyXTabular::M_ALIGN_LEFT:
1381 case LyXTabular::ALIGN_LEFT:
1382 setAlign = LYX_ALIGN_LEFT;
1385 case LyXTabular::M_ALIGN_RIGHT:
1386 case LyXTabular::ALIGN_RIGHT:
1387 setAlign = LYX_ALIGN_RIGHT;
1390 case LyXTabular::M_ALIGN_CENTER:
1391 case LyXTabular::ALIGN_CENTER:
1392 setAlign = LYX_ALIGN_CENTER;
1395 case LyXTabular::ALIGN_BLOCK:
1396 setAlign = LYX_ALIGN_BLOCK;
1399 case LyXTabular::M_VALIGN_TOP:
1400 case LyXTabular::VALIGN_TOP:
1401 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1404 case LyXTabular::M_VALIGN_BOTTOM:
1405 case LyXTabular::VALIGN_BOTTOM:
1406 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1409 case LyXTabular::M_VALIGN_MIDDLE:
1410 case LyXTabular::VALIGN_MIDDLE:
1411 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1418 recordUndoInset(cur, Undo::ATOMIC);
1420 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1421 row_type const row = tabular.row_of_cell(cur.idx());
1422 col_type const column = tabular.column_of_cell(cur.idx());
1424 LyXTabular::ltType ltt;
1428 case LyXTabular::SET_PWIDTH: {
1429 LyXLength const len(value);
1430 tabular.setColumnPWidth(cur, cur.idx(), len);
1432 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1433 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1437 case LyXTabular::SET_MPWIDTH:
1438 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1441 case LyXTabular::SET_SPECIAL_COLUMN:
1442 case LyXTabular::SET_SPECIAL_MULTI:
1443 tabular.setAlignSpecial(cur.idx(),value,feature);
1446 case LyXTabular::APPEND_ROW:
1447 // append the row into the tabular
1448 tabular.appendRow(bv.buffer()->params(), cur.idx());
1451 case LyXTabular::APPEND_COLUMN:
1452 // append the column into the tabular
1453 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1454 cur.idx() = tabular.getCellNumber(row, column);
1457 case LyXTabular::DELETE_ROW:
1458 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1459 tabular.deleteRow(sel_row_start);
1460 if (sel_row_start >= tabular.rows())
1462 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1465 cur.selection() = false;
1468 case LyXTabular::DELETE_COLUMN:
1469 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1470 tabular.deleteColumn(sel_col_start);
1471 if (sel_col_start >= tabular.columns())
1473 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1476 cur.selection() = false;
1479 case LyXTabular::M_TOGGLE_LINE_TOP:
1481 case LyXTabular::TOGGLE_LINE_TOP: {
1482 bool lineSet = !tabular.topLine(cur.idx(), flag);
1483 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1484 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1486 tabular.getCellNumber(i, j),
1491 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1493 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1494 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1495 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1496 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1497 tabular.setBottomLine(
1498 tabular.getCellNumber(i, j),
1504 case LyXTabular::M_TOGGLE_LINE_LEFT:
1506 case LyXTabular::TOGGLE_LINE_LEFT: {
1507 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1508 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1509 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1510 tabular.setLeftLine(
1511 tabular.getCellNumber(i,j),
1517 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1519 case LyXTabular::TOGGLE_LINE_RIGHT: {
1520 bool lineSet = !tabular.rightLine(cur.idx(), flag);
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.setRightLine(
1524 tabular.getCellNumber(i,j),
1530 case LyXTabular::M_ALIGN_LEFT:
1531 case LyXTabular::M_ALIGN_RIGHT:
1532 case LyXTabular::M_ALIGN_CENTER:
1534 case LyXTabular::ALIGN_LEFT:
1535 case LyXTabular::ALIGN_RIGHT:
1536 case LyXTabular::ALIGN_CENTER:
1537 case LyXTabular::ALIGN_BLOCK:
1538 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1539 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1540 tabular.setAlignment(
1541 tabular.getCellNumber(i, j),
1546 case LyXTabular::M_VALIGN_TOP:
1547 case LyXTabular::M_VALIGN_BOTTOM:
1548 case LyXTabular::M_VALIGN_MIDDLE:
1550 case LyXTabular::VALIGN_TOP:
1551 case LyXTabular::VALIGN_BOTTOM:
1552 case LyXTabular::VALIGN_MIDDLE:
1553 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1554 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1555 tabular.setVAlignment(
1556 tabular.getCellNumber(i, j),
1560 case LyXTabular::MULTICOLUMN: {
1561 if (sel_row_start != sel_row_end) {
1562 #ifdef WITH_WARNINGS
1563 #warning Need I say it ? This is horrible.
1565 Alert::error(_("Error setting multicolumn"),
1566 _("You cannot set multicolumn vertically."));
1569 if (!cur.selection()) {
1570 // just multicol for one single cell
1571 // check whether we are completely in a multicol
1572 if (tabular.isMultiColumn(cur.idx()))
1573 tabular.unsetMultiColumn(cur.idx());
1575 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1578 // we have a selection so this means we just add all this
1579 // cells to form a multicolumn cell
1580 idx_type const s_start = cur.selBegin().idx();
1581 idx_type const s_end = cur.selEnd().idx();
1582 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1583 cur.idx() = s_start;
1586 cur.selection() = false;
1590 case LyXTabular::SET_ALL_LINES:
1592 case LyXTabular::UNSET_ALL_LINES:
1593 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1594 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1595 tabular.setAllLines(
1596 tabular.getCellNumber(i,j), setLines);
1599 case LyXTabular::SET_LONGTABULAR:
1600 tabular.setLongTabular(true);
1603 case LyXTabular::UNSET_LONGTABULAR:
1604 tabular.setLongTabular(false);
1607 case LyXTabular::SET_ROTATE_TABULAR:
1608 tabular.setRotateTabular(true);
1611 case LyXTabular::UNSET_ROTATE_TABULAR:
1612 tabular.setRotateTabular(false);
1615 case LyXTabular::SET_ROTATE_CELL:
1616 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1617 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1618 tabular.setRotateCell(
1619 tabular.getCellNumber(i, j), true);
1622 case LyXTabular::UNSET_ROTATE_CELL:
1623 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1624 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1625 tabular.setRotateCell(
1626 tabular.getCellNumber(i, j), false);
1629 case LyXTabular::SET_USEBOX: {
1630 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1631 if (val == tabular.getUsebox(cur.idx()))
1632 val = LyXTabular::BOX_NONE;
1633 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1634 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1635 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1639 case LyXTabular::UNSET_LTFIRSTHEAD:
1641 case LyXTabular::SET_LTFIRSTHEAD:
1642 tabular.getRowOfLTFirstHead(row, ltt);
1643 checkLongtableSpecial(ltt, value, flag);
1644 tabular.setLTHead(row, flag, ltt, true);
1647 case LyXTabular::UNSET_LTHEAD:
1649 case LyXTabular::SET_LTHEAD:
1650 tabular.getRowOfLTHead(row, ltt);
1651 checkLongtableSpecial(ltt, value, flag);
1652 tabular.setLTHead(row, flag, ltt, false);
1655 case LyXTabular::UNSET_LTFOOT:
1657 case LyXTabular::SET_LTFOOT:
1658 tabular.getRowOfLTFoot(row, ltt);
1659 checkLongtableSpecial(ltt, value, flag);
1660 tabular.setLTFoot(row, flag, ltt, false);
1663 case LyXTabular::UNSET_LTLASTFOOT:
1665 case LyXTabular::SET_LTLASTFOOT:
1666 tabular.getRowOfLTLastFoot(row, ltt);
1667 checkLongtableSpecial(ltt, value, flag);
1668 tabular.setLTFoot(row, flag, ltt, true);
1671 case LyXTabular::SET_LTNEWPAGE:
1672 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1675 // dummy stuff just to avoid warnings
1676 case LyXTabular::LAST_ACTION:
1680 InsetTabularMailer(*this).updateDialog(&bv);
1684 bool InsetTabular::showInsetDialog(BufferView * bv) const
1686 InsetTabularMailer(*this).showDialog(bv);
1691 void InsetTabular::openLayoutDialog(BufferView * bv) const
1693 InsetTabularMailer(*this).showDialog(bv);
1697 bool InsetTabular::copySelection(LCursor & cur)
1699 if (!cur.selection())
1704 getSelection(cur, rs, re, cs, ce);
1706 paste_tabular.reset(new LyXTabular(tabular));
1708 for (row_type i = 0; i < rs; ++i)
1709 paste_tabular->deleteRow(0);
1711 row_type const rows = re - rs + 1;
1712 while (paste_tabular->rows() > rows)
1713 paste_tabular->deleteRow(rows);
1715 paste_tabular->setTopLine(0, true, true);
1716 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1719 for (col_type i = 0; i < cs; ++i)
1720 paste_tabular->deleteColumn(0);
1722 col_type const columns = ce - cs + 1;
1723 while (paste_tabular->columns() > columns)
1724 paste_tabular->deleteColumn(columns);
1726 paste_tabular->setLeftLine(0, true, true);
1727 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1731 OutputParams const runparams;
1732 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1733 cur.bv().stuffClipboard(os.str());
1734 // mark tabular stack dirty
1735 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1736 // when we (hopefully) have a one-for-all paste mechanism.
1737 lyx::cap::dirtyTabularStack(true);
1743 bool InsetTabular::pasteSelection(LCursor & cur)
1747 col_type const actcol = tabular.column_of_cell(cur.idx());
1748 row_type const actrow = tabular.row_of_cell(cur.idx());
1749 for (row_type r1 = 0, r2 = actrow;
1750 r1 < paste_tabular->rows() && r2 < tabular.rows();
1752 for (col_type c1 = 0, c2 = actcol;
1753 c1 < paste_tabular->columns() && c2 < tabular.columns();
1755 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1756 tabular.isPartOfMultiColumn(r2, c2))
1758 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1762 if (tabular.isPartOfMultiColumn(r2, c2)) {
1766 shared_ptr<InsetText> inset(
1767 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1768 tabular.setCellInset(r2, c2, inset);
1777 void InsetTabular::cutSelection(LCursor & cur)
1779 if (!cur.selection())
1784 getSelection(cur, rs, re, cs, ce);
1785 for (row_type i = rs; i <= re; ++i) {
1786 for (col_type j = cs; j <= ce; ++j) {
1787 shared_ptr<InsetText> t
1788 = cell(tabular.getCellNumber(i, j));
1789 if (cur.buffer().params().tracking_changes)
1790 t->markErased(true);
1796 // cursor position might be invalid now
1797 cur.pos() = cur.lastpos();
1798 cur.clearSelection();
1802 bool InsetTabular::isRightToLeft(LCursor & cur) const
1804 BOOST_ASSERT(cur.depth() > 1);
1805 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1806 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1807 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1808 parentpos).language()->RightToLeft();
1812 void InsetTabular::getSelection(LCursor & cur,
1813 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1815 CursorSlice const & beg = cur.selBegin();
1816 CursorSlice const & end = cur.selEnd();
1817 cs = tabular.column_of_cell(beg.idx());
1818 ce = tabular.column_of_cell(end.idx());
1821 cs = tabular.column_of_cell(end.idx());
1823 ce = tabular.right_column_of_cell(end.idx());
1826 rs = tabular.row_of_cell(beg.idx());
1827 re = tabular.row_of_cell(end.idx());
1833 LyXText * InsetTabular::getText(int idx) const
1835 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1839 void InsetTabular::markErased(bool erased)
1841 for (idx_type idx = 0; idx < nargs(); ++idx)
1842 cell(idx)->markErased(erased);
1846 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1848 return tabular.getPWidth(cell).zero();
1852 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1855 if (buf.length() <= 0)
1860 col_type maxCols = 1;
1861 string::size_type const len = buf.length();
1862 string::size_type p = 0;
1864 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1872 maxCols = max(cols, maxCols);
1878 maxCols = max(cols, maxCols);
1879 LyXTabular * loctab;
1884 paste_tabular.reset(
1885 new LyXTabular(bv.buffer()->params(), rows, maxCols, &bv));
1886 loctab = paste_tabular.get();
1890 cell = bv.cursor().idx();
1891 ocol = tabular.column_of_cell(cell);
1892 row = tabular.row_of_cell(cell);
1895 string::size_type op = 0;
1896 idx_type const cells = loctab->getNumberOfCells();
1899 rows = loctab->rows();
1900 col_type const columns = loctab->columns();
1902 while (cell < cells && p < len && row < rows &&
1903 (p = buf.find_first_of("\t\n", p)) != string::npos)
1909 // we can only set this if we are not too far right
1910 if (cols < columns) {
1911 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1912 Paragraph & par = inset->text_.getPar(0);
1913 LyXFont const font = inset->text_.getFont(par, 0);
1914 inset->setText(buf.substr(op, p - op), font);
1920 // we can only set this if we are not too far right
1921 if (cols < columns) {
1922 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1923 Paragraph & par = inset->text_.getPar(0);
1924 LyXFont const font = inset->text_.getFont(par, 0);
1925 inset->setText(buf.substr(op, p - op), font);
1930 cell = loctab->getCellNumber(row, cols);
1936 // check for the last cell if there is no trailing '\n'
1937 if (cell < cells && op < len) {
1938 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1939 Paragraph & par = inset->text_.getPar(0);
1940 LyXFont const font = inset->text_.getFont(par, 0);
1941 inset->setText(buf.substr(op, len - op), font);
1947 void InsetTabular::addPreview(PreviewLoader & loader) const
1949 row_type const rows = tabular.rows();
1950 col_type const columns = tabular.columns();
1951 for (row_type i = 0; i < rows; ++i) {
1952 for (col_type j = 0; j < columns; ++j)
1953 tabular.getCellInset(i, j)->addPreview(loader);
1958 bool InsetTabular::tablemode(LCursor & cur) const
1960 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1967 string const InsetTabularMailer::name_("tabular");
1969 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1970 : inset_(const_cast<InsetTabular &>(inset))
1974 string const InsetTabularMailer::inset2string(Buffer const &) const
1976 return params2string(inset_);
1980 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1982 istringstream data(in);
1984 lex.setStream(data);
1991 if (!lex || token != name_)
1992 return print_mailer_error("InsetTabularMailer", in, 1,
1995 // This is part of the inset proper that is usually swallowed
1996 // by Buffer::readInset
1998 if (!lex || token != "Tabular")
1999 return print_mailer_error("InsetTabularMailer", in, 2,
2002 Buffer const & buffer = inset.buffer();
2003 inset.read(buffer, lex);
2007 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2010 data << name_ << ' ';
2011 inset.write(inset.buffer(), data);
2012 data << "\\end_inset\n";