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/Clipboard.h"
41 #include "frontends/Painter.h"
42 #include "frontends/Selection.h"
51 using cap::dirtyTabularStack;
52 using cap::tabularStackDirty;
53 using cap::saveSelection;
55 using graphics::PreviewLoader;
59 using frontend::Painter;
60 using frontend::Clipboard;
62 using boost::shared_ptr;
68 using std::istringstream;
70 using std::ostringstream;
74 namespace Alert = frontend::Alert;
79 int const ADD_TO_HEIGHT = 2;
80 int const ADD_TO_TABULAR_WIDTH = 2;
81 int const default_line_space = 10;
84 boost::scoped_ptr<LyXTabular> paste_tabular;
87 struct TabularFeature {
88 LyXTabular::Feature action;
93 TabularFeature tabularFeature[] =
95 { LyXTabular::APPEND_ROW, "append-row" },
96 { LyXTabular::APPEND_COLUMN, "append-column" },
97 { LyXTabular::DELETE_ROW, "delete-row" },
98 { LyXTabular::DELETE_COLUMN, "delete-column" },
99 { LyXTabular::COPY_ROW, "copy-row" },
100 { LyXTabular::COPY_COLUMN, "copy-column" },
101 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
102 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
103 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
104 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
105 { LyXTabular::ALIGN_LEFT, "align-left" },
106 { LyXTabular::ALIGN_RIGHT, "align-right" },
107 { LyXTabular::ALIGN_CENTER, "align-center" },
108 { LyXTabular::ALIGN_BLOCK, "align-block" },
109 { LyXTabular::VALIGN_TOP, "valign-top" },
110 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
111 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
112 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
113 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
114 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
115 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
116 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
117 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
118 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
119 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
120 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
121 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
122 { LyXTabular::MULTICOLUMN, "multicolumn" },
123 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
124 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
125 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
126 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
127 { LyXTabular::SET_PWIDTH, "set-pwidth" },
128 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
129 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
130 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
131 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
132 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
133 { LyXTabular::SET_USEBOX, "set-usebox" },
134 { LyXTabular::SET_LTHEAD, "set-lthead" },
135 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
136 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
137 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
138 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
139 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
140 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
141 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
142 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
143 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
144 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
145 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
146 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
147 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
148 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
149 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
150 { LyXTabular::LAST_ACTION, "" }
154 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
156 FeatureEqual(LyXTabular::Feature feature)
157 : feature_(feature) {}
158 bool operator()(TabularFeature const & tf) const {
159 return tf.action == feature_;
162 LyXTabular::Feature feature_;
168 string const featureAsString(LyXTabular::Feature feature)
170 TabularFeature * end = tabularFeature +
171 sizeof(tabularFeature) / sizeof(TabularFeature);
172 TabularFeature * it = std::find_if(tabularFeature, end,
173 FeatureEqual(feature));
174 return (it == end) ? string() : it->feature;
178 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
180 : tabular(buf.params(), max(rows, row_type(1)),
181 max(columns, col_type(1))), buffer_(&buf), scx_(0), is_deleted_(false)
185 InsetTabular::InsetTabular(InsetTabular const & tab)
186 : InsetOld(tab), tabular(tab.tabular),
187 buffer_(tab.buffer_), scx_(0), is_deleted_(false)
191 InsetTabular::~InsetTabular()
193 InsetTabularMailer(*this).hideDialog();
198 auto_ptr<InsetBase> InsetTabular::doClone() const
200 return auto_ptr<InsetBase>(new InsetTabular(*this));
204 Buffer const & InsetTabular::buffer() const
210 void InsetTabular::buffer(Buffer const * b)
216 void InsetTabular::write(Buffer const & buf, ostream & os) const
218 os << "Tabular" << endl;
219 tabular.write(buf, os);
223 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
225 bool const old_format = (lex.getString() == "\\LyXTable");
227 tabular.read(buf, lex);
233 string token = lex.getString();
234 while (lex && token != "\\end_inset") {
236 token = lex.getString();
239 lex.printError("Missing \\end_inset at this point. "
245 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
247 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
248 // mi.base.textwidth << "\n";
250 lyxerr << "InsetTabular::metrics: need bv" << endl;
255 for (idx_type cell = 0; i < tabular.rows(); ++i) {
258 for (col_type j = 0; j < tabular.columns(); ++j) {
259 if (tabular.isPartOfMultiColumn(i, j))
260 // Multicolumn cell, but not first one
265 if (tabular.cell_info[i][j].multicolumn ==
266 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
267 p_width = tabular.cellinfo_of_cell(cell).p_width;
269 p_width = tabular.column_info[j].p_width;
271 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
272 tabular.getCellInset(cell)->metrics(m, dim);
274 dim.wid = m.base.textwidth;
275 tabular.setWidthOfCell(cell, dim.wid);
276 if (p_width.zero()) {
277 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
278 // FIXME there must be a way to get rid of
279 // the second metrics call
280 tabular.getCellInset(cell)->metrics(m, dim);
282 maxAsc = max(maxAsc, dim.asc);
283 maxDesc = max(maxDesc, dim.des);
286 int const top_space = tabular.row_info[i].top_space_default ?
288 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
289 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
290 int const bottom_space = tabular.row_info[i].bottom_space_default ?
292 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
293 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
296 dim.asc = tabular.getAscentOfRow(0);
297 dim.des = tabular.getHeightOfTabular() - dim.asc;
298 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
299 bool const changed = dim_ != dim;
305 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
307 setPosCache(pi, x, y);
309 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
310 BufferView * bv = pi.base.bv;
312 resetPos(bv->cursor());
315 x += ADD_TO_TABULAR_WIDTH;
318 first_visible_cell = LyXTabular::npos;
319 for (row_type i = 0; i < tabular.rows(); ++i) {
321 int const a = tabular.getAscentOfRow(i);
322 int const d = tabular.getDescentOfRow(i);
323 idx = tabular.getCellNumber(i, 0);
324 for (col_type j = 0; j < tabular.columns(); ++j) {
325 if (tabular.isPartOfMultiColumn(i, j))
327 if (first_visible_cell == LyXTabular::npos)
328 first_visible_cell = idx;
330 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
331 if (nx + tabular.getWidthOfColumn(idx) < 0
332 || nx > bv->workWidth()
334 || y - a > bv->workHeight()) {
335 pi.pain.setDrawingEnabled(false);
336 cell(idx)->draw(pi, cx, y);
337 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
338 pi.pain.setDrawingEnabled(true);
340 cell(idx)->draw(pi, cx, y);
341 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
343 nx += tabular.getWidthOfColumn(idx);
347 if (i + 1 < tabular.rows())
348 y += d + tabular.getAscentOfRow(i + 1) +
349 tabular.getAdditionalHeight(i + 1);
354 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
356 setPosCache(pi, x, y);
358 LCursor & cur = pi.base.bv->cursor();
360 x += scx_ + ADD_TO_TABULAR_WIDTH;
362 // Paint background of current tabular
363 int const w = tabular.getWidthOfTabular();
364 int const h = tabular.getHeightOfTabular();
365 int yy = y - tabular.getAscentOfRow(0);
366 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
368 if (!cur.selection())
370 if (!ptr_cmp(&cur.inset(), this))
376 if (tablemode(cur)) {
379 getSelection(cur, rs, re, cs, ce);
380 y -= tabular.getAscentOfRow(0);
381 for (row_type j = 0; j < tabular.rows(); ++j) {
382 int const a = tabular.getAscentOfRow(j);
383 int const h = a + tabular.getDescentOfRow(j);
385 y += tabular.getAdditionalHeight(j);
386 for (col_type i = 0; i < tabular.columns(); ++i) {
387 if (tabular.isPartOfMultiColumn(j, i))
389 idx_type const cell =
390 tabular.getCellNumber(j, i);
391 int const w = tabular.getWidthOfColumn(cell);
392 if (i >= cs && i <= ce && j >= rs && j <= re)
393 pi.pain.fillRectangle(xx, y, w, h,
402 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
407 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
408 row_type row, idx_type cell, bool erased) const
410 int x2 = x + tabular.getWidthOfColumn(cell);
412 LColor::color col = LColor::tabularline;
413 LColor::color onoffcol = LColor::tabularonoffline;
416 col = LColor::strikeout;
417 onoffcol = LColor::strikeout;
420 if (!tabular.topAlreadyDrawn(cell)) {
421 on_off = !tabular.topLine(cell);
422 pain.line(x, y - tabular.getAscentOfRow(row),
423 x2, y - tabular.getAscentOfRow(row),
424 on_off ? onoffcol : col,
425 on_off ? Painter::line_onoffdash : Painter::line_solid);
427 on_off = !tabular.bottomLine(cell);
428 pain.line(x, y + tabular.getDescentOfRow(row),
429 x2, y + tabular.getDescentOfRow(row),
430 on_off ? onoffcol : col,
431 on_off ? Painter::line_onoffdash : Painter::line_solid);
432 if (!tabular.leftAlreadyDrawn(cell)) {
433 on_off = !tabular.leftLine(cell);
434 pain.line(x, y - tabular.getAscentOfRow(row),
435 x, y + tabular.getDescentOfRow(row),
436 on_off ? onoffcol : col,
437 on_off ? Painter::line_onoffdash : Painter::line_solid);
439 on_off = !tabular.rightLine(cell);
440 pain.line(x2 - tabular.getAdditionalWidth(cell),
441 y - tabular.getAscentOfRow(row),
442 x2 - tabular.getAdditionalWidth(cell),
443 y + tabular.getDescentOfRow(row),
444 on_off ? onoffcol : col,
445 on_off ? Painter::line_onoffdash : Painter::line_solid);
449 docstring const InsetTabular::editMessage() const
451 return _("Opened table");
455 void InsetTabular::edit(LCursor & cur, bool left)
457 //lyxerr << "InsetTabular::edit: " << this << endl;
459 cur.selection() = false;
462 if (isRightToLeft(cur))
463 cur.idx() = tabular.getLastCellInRow(0);
469 if (isRightToLeft(cur))
470 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
472 cur.idx() = tabular.getNumberOfCells() - 1;
474 cur.pos() = cur.lastpos(); // FIXME crude guess
476 // FIXME: this accesses the position cache before it is initialized
478 //cur.bv().fitCursor();
482 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
484 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
485 << "\n cur:" << cur << endl;
486 CursorSlice sl = cur.top();
487 LCursor & bvcur = cur.bv().cursor();
489 switch (cmd.action) {
491 case LFUN_MOUSE_PRESS:
492 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
494 if (cmd.button() == mouse_button::button1
495 || (cmd.button() == mouse_button::button3
496 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
497 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
499 cur.selection() = false;
500 setCursorFromCoordinates(cur, cmd.x, cmd.y);
501 cur.bv().mouseSetCursor(cur);
505 if (cmd.button() == mouse_button::button2) {
506 if (cap::selection()) {
507 // See comment in LyXText::dispatch why we
509 // FIXME This does not use paste_tabular,
510 // another reason why paste_tabular should go.
511 cap::copySelectionToStack();
512 cmd = FuncRequest(LFUN_PASTE, "0");
514 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
517 doDispatch(cur, cmd);
521 case LFUN_MOUSE_MOTION:
522 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
523 if (cmd.button() == mouse_button::button1) {
524 // only accept motions to places not deeper nested than the real anchor
525 if (bvcur.anchor_.hasPart(cur)) {
526 // only update if selection changes
527 if (bvcur.idx() == cur.idx() &&
528 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
530 setCursorFromCoordinates(cur, cmd.x, cmd.y);
531 bvcur.setCursor(cur);
532 bvcur.selection() = true;
538 case LFUN_MOUSE_RELEASE:
539 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
540 if (cmd.button() == mouse_button::button1) {
541 if (bvcur.selection()) {
542 // Bug3238: disable persistent selection for table cells for now
543 if (tablemode(bvcur))
544 theSelection().haveSelection(true);
546 saveSelection(bvcur);
548 } else if (cmd.button() == mouse_button::button3)
549 InsetTabularMailer(*this).showDialog(&cur.bv());
552 case LFUN_CELL_BACKWARD:
554 cur.selection() = false;
558 case LFUN_CELL_FORWARD:
560 cur.selection() = false;
564 case LFUN_CHAR_FORWARD_SELECT:
565 case LFUN_CHAR_FORWARD:
566 cell(cur.idx())->dispatch(cur, cmd);
567 if (!cur.result().dispatched()) {
568 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
569 if (cmd.action == LFUN_CHAR_FORWARD_SELECT)
572 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
578 case LFUN_CHAR_BACKWARD_SELECT:
579 case LFUN_CHAR_BACKWARD:
580 cell(cur.idx())->dispatch(cur, cmd);
581 if (!cur.result().dispatched()) {
582 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
583 if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
586 cmd = FuncRequest(LFUN_FINISHED_LEFT);
592 case LFUN_DOWN_SELECT:
594 cell(cur.idx())->dispatch(cur, cmd);
595 cur.dispatched(); // override the cell's decision
597 // if our LyXText didn't do anything to the cursor
598 // then we try to put the cursor into the cell below
599 // setting also the right targetX.
600 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
601 cur.idx() = tabular.getCellBelow(cur.idx());
603 TextMetrics const & tm =
604 cur.bv().textMetrics(cell(cur.idx())->getText(0));
605 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
606 if (cmd.action == LFUN_DOWN_SELECT) {
607 // Bug3238: disable persistent selection for table cells for now
609 theSelection().haveSelection(true);
614 if (sl == cur.top()) {
615 // we trick it to go to the RIGHT after leaving the
617 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
624 cell(cur.idx())->dispatch(cur, cmd);
625 cur.dispatched(); // override the cell's decision
627 // if our LyXText didn't do anything to the cursor
628 // then we try to put the cursor into the cell above
629 // setting also the right targetX.
630 if (tabular.row_of_cell(cur.idx()) != 0) {
631 cur.idx() = tabular.getCellAbove(cur.idx());
632 cur.pit() = cur.lastpit();
633 LyXText const * text = cell(cur.idx())->getText(0);
634 TextMetrics const & tm = cur.bv().textMetrics(text);
635 ParagraphMetrics const & pm =
636 tm.parMetrics(cur.lastpit());
637 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
638 if (cmd.action == LFUN_UP_SELECT) {
639 // Bug3238: disable persistent selection for table cells for now
641 theSelection().haveSelection(true);
646 if (sl == cur.top()) {
647 cmd = FuncRequest(LFUN_FINISHED_UP);
652 // case LFUN_SCREEN_DOWN: {
653 // //if (hasSelection())
654 // // cur.selection() = false;
655 // col_type const col = tabular.column_of_cell(cur.idx());
656 // int const t = cur.bv().top_y() + cur.bv().height();
657 // if (t < yo() + tabular.getHeightOfTabular()) {
658 // cur.bv().scrollDocView(t);
659 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
661 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
668 // case LFUN_SCREEN_UP: {
669 // //if (hasSelection())
670 // // cur.selection() = false;
671 // col_type const col = tabular.column_of_cell(cur.idx());
672 // int const t = cur.bv().top_y() + cur.bv().height();
674 // cur.bv().scrollDocView(t);
678 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
682 // cur.par() = cur.lastpar();
683 // cur.pos() = cur.lastpos();
687 case LFUN_LAYOUT_TABULAR:
688 InsetTabularMailer(*this).showDialog(&cur.bv());
691 case LFUN_INSET_DIALOG_UPDATE:
692 InsetTabularMailer(*this).updateDialog(&cur.bv());
695 case LFUN_TABULAR_FEATURE:
696 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
700 // insert file functions
701 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
702 case LFUN_FILE_INSERT_PLAINTEXT: {
704 docstring const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
705 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
711 if (tablemode(cur)) {
712 if (copySelection(cur)) {
713 recordUndoInset(cur, Undo::DELETE);
718 cell(cur.idx())->dispatch(cur, cmd);
721 case LFUN_CHAR_DELETE_BACKWARD:
722 case LFUN_CHAR_DELETE_FORWARD:
723 if (tablemode(cur)) {
724 recordUndoInset(cur, Undo::DELETE);
728 cell(cur.idx())->dispatch(cur, cmd);
732 if (!cur.selection())
734 if (tablemode(cur)) {
738 cell(cur.idx())->dispatch(cur, cmd);
741 case LFUN_CLIPBOARD_PASTE:
742 case LFUN_PRIMARY_SELECTION_PASTE: {
743 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
744 theClipboard().getAsText() :
745 theSelection().get();
748 // pass to InsertPlaintextString, but
749 // only if we have multi-cell content
750 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
751 if (insertPlaintextString(cur.bv(), clip, false)) {
752 // content has been replaced,
753 // so cursor might be invalid
754 cur.pos() = cur.lastpos();
755 bvcur.setCursor(cur);
759 // Let the cell handle normal text
760 cell(cur.idx())->dispatch(cur, cmd);
765 if (tabularStackDirty() && theClipboard().isInternal()) {
766 recordUndoInset(cur, Undo::INSERT);
770 cell(cur.idx())->dispatch(cur, cmd);
775 case LFUN_FONT_ROMAN:
781 case LFUN_FONT_FREE_APPLY:
782 case LFUN_FONT_FREE_UPDATE:
784 case LFUN_FONT_UNDERLINE:
786 case LFUN_WORD_CAPITALIZE:
787 case LFUN_WORD_UPCASE:
788 case LFUN_WORD_LOWCASE:
789 case LFUN_CHARS_TRANSPOSE:
790 if (tablemode(cur)) {
793 getSelection(cur, rs, re, cs, ce);
794 LCursor tmpcur = cur;
795 for (row_type i = rs; i <= re; ++i) {
796 for (col_type j = cs; j <= ce; ++j) {
797 // cursor follows cell:
798 tmpcur.idx() = tabular.getCellNumber(i, j);
799 // select this cell only:
802 tmpcur.resetAnchor();
803 tmpcur.pit() = tmpcur.lastpit();
804 tmpcur.pos() = tmpcur.top().lastpos();
805 tmpcur.setCursor(tmpcur);
806 tmpcur.setSelection();
807 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
812 cell(cur.idx())->dispatch(cur, cmd);
816 // we try to handle this event in the insets dispatch function.
817 cell(cur.idx())->dispatch(cur, cmd);
821 // FIXME: this accesses the position cache before it is initialized
824 InsetTabularMailer(*this).updateDialog(&cur.bv());
828 // function sets an object as defined in func_status.h:
829 // states OK, Unknown, Disabled, On, Off.
830 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
831 FuncStatus & status) const
833 switch (cmd.action) {
834 case LFUN_TABULAR_FEATURE: {
835 int action = LyXTabular::LAST_ACTION;
837 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
838 string const tmp = tabularFeature[i].feature;
839 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
840 action = tabularFeature[i].action;
844 if (action == LyXTabular::LAST_ACTION) {
846 status.unknown(true);
850 string const argument
851 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
853 row_type sel_row_start = 0;
854 row_type sel_row_end = 0;
856 LyXTabular::ltType dummyltt;
859 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
862 case LyXTabular::SET_PWIDTH:
863 case LyXTabular::SET_MPWIDTH:
864 case LyXTabular::SET_SPECIAL_COLUMN:
865 case LyXTabular::SET_SPECIAL_MULTI:
866 case LyXTabular::APPEND_ROW:
867 case LyXTabular::APPEND_COLUMN:
868 case LyXTabular::DELETE_ROW:
869 case LyXTabular::DELETE_COLUMN:
870 case LyXTabular::COPY_ROW:
871 case LyXTabular::COPY_COLUMN:
872 case LyXTabular::SET_ALL_LINES:
873 case LyXTabular::UNSET_ALL_LINES:
874 case LyXTabular::SET_TOP_SPACE:
875 case LyXTabular::SET_BOTTOM_SPACE:
876 case LyXTabular::SET_INTERLINE_SPACE:
880 case LyXTabular::MULTICOLUMN:
881 status.setOnOff(tabular.isMultiColumn(cur.idx()));
884 case LyXTabular::M_TOGGLE_LINE_TOP:
886 case LyXTabular::TOGGLE_LINE_TOP:
887 status.setOnOff(tabular.topLine(cur.idx(), flag));
890 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
892 case LyXTabular::TOGGLE_LINE_BOTTOM:
893 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
896 case LyXTabular::M_TOGGLE_LINE_LEFT:
898 case LyXTabular::TOGGLE_LINE_LEFT:
899 status.setOnOff(tabular.leftLine(cur.idx(), flag));
902 case LyXTabular::M_TOGGLE_LINE_RIGHT:
904 case LyXTabular::TOGGLE_LINE_RIGHT:
905 status.setOnOff(tabular.rightLine(cur.idx(), flag));
908 case LyXTabular::M_ALIGN_LEFT:
910 case LyXTabular::ALIGN_LEFT:
911 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
914 case LyXTabular::M_ALIGN_RIGHT:
916 case LyXTabular::ALIGN_RIGHT:
917 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
920 case LyXTabular::M_ALIGN_CENTER:
922 case LyXTabular::ALIGN_CENTER:
923 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
926 case LyXTabular::ALIGN_BLOCK:
927 status.enabled(!tabular.getPWidth(cur.idx()).zero());
928 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
931 case LyXTabular::M_VALIGN_TOP:
933 case LyXTabular::VALIGN_TOP:
935 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
938 case LyXTabular::M_VALIGN_BOTTOM:
940 case LyXTabular::VALIGN_BOTTOM:
942 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
945 case LyXTabular::M_VALIGN_MIDDLE:
947 case LyXTabular::VALIGN_MIDDLE:
949 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
952 case LyXTabular::SET_LONGTABULAR:
953 status.setOnOff(tabular.isLongTabular());
956 case LyXTabular::UNSET_LONGTABULAR:
957 status.setOnOff(!tabular.isLongTabular());
960 case LyXTabular::SET_ROTATE_TABULAR:
961 status.setOnOff(tabular.getRotateTabular());
964 case LyXTabular::UNSET_ROTATE_TABULAR:
965 status.setOnOff(!tabular.getRotateTabular());
968 case LyXTabular::SET_ROTATE_CELL:
969 status.setOnOff(tabular.getRotateCell(cur.idx()));
972 case LyXTabular::UNSET_ROTATE_CELL:
973 status.setOnOff(!tabular.getRotateCell(cur.idx()));
976 case LyXTabular::SET_USEBOX:
977 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
980 case LyXTabular::SET_LTFIRSTHEAD:
981 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
984 case LyXTabular::UNSET_LTFIRSTHEAD:
985 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
988 case LyXTabular::SET_LTHEAD:
989 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
992 case LyXTabular::UNSET_LTHEAD:
993 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
996 case LyXTabular::SET_LTFOOT:
997 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1000 case LyXTabular::UNSET_LTFOOT:
1001 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1004 case LyXTabular::SET_LTLASTFOOT:
1005 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1008 case LyXTabular::UNSET_LTLASTFOOT:
1009 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1012 case LyXTabular::SET_LTNEWPAGE:
1013 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1016 case LyXTabular::SET_BOOKTABS:
1017 status.setOnOff(tabular.useBookTabs());
1020 case LyXTabular::UNSET_BOOKTABS:
1021 status.setOnOff(!tabular.useBookTabs());
1026 status.enabled(false);
1032 // These are only enabled inside tabular
1033 case LFUN_CELL_BACKWARD:
1034 case LFUN_CELL_FORWARD:
1035 status.enabled(true);
1038 // disable these with multiple cells selected
1039 case LFUN_INSET_INSERT:
1040 case LFUN_TABULAR_INSERT:
1041 case LFUN_CHARSTYLE_INSERT:
1042 case LFUN_FLOAT_INSERT:
1043 case LFUN_FLOAT_WIDE_INSERT:
1044 case LFUN_FOOTNOTE_INSERT:
1045 case LFUN_MARGINALNOTE_INSERT:
1046 case LFUN_MATH_INSERT:
1047 case LFUN_MATH_MODE:
1048 case LFUN_MATH_MUTATE:
1049 case LFUN_MATH_DISPLAY:
1050 case LFUN_NOTE_INSERT:
1051 case LFUN_OPTIONAL_INSERT:
1052 case LFUN_BOX_INSERT:
1053 case LFUN_BRANCH_INSERT:
1054 case LFUN_WRAP_INSERT:
1055 case LFUN_ERT_INSERT: {
1056 if (tablemode(cur)) {
1057 status.enabled(false);
1060 return cell(cur.idx())->getStatus(cur, cmd, status);
1063 // disable in non-fixed-width cells
1064 case LFUN_BREAK_LINE:
1065 case LFUN_BREAK_PARAGRAPH:
1066 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1067 case LFUN_BREAK_PARAGRAPH_SKIP: {
1068 if (tabular.getPWidth(cur.idx()).zero()) {
1069 status.enabled(false);
1072 return cell(cur.idx())->getStatus(cur, cmd, status);
1076 if (tabularStackDirty() && theClipboard().isInternal()) {
1077 status.enabled(true);
1080 return cell(cur.idx())->getStatus(cur, cmd, status);
1082 case LFUN_INSET_MODIFY:
1083 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1084 status.enabled(true);
1090 // we try to handle this event in the insets dispatch function.
1091 return cell(cur.idx())->getStatus(cur, cmd, status);
1096 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1097 OutputParams const & runparams) const
1099 return tabular.latex(buf, os, runparams);
1103 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1104 OutputParams const & runparams) const
1106 os << '\n'; // output table on a new line
1107 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
1108 tabular.plaintext(buf, os, runparams, dp, false, 0);
1109 return PLAINTEXT_NEWLINE;
1113 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1114 OutputParams const & runparams) const
1117 InsetBase * master = 0;
1119 #ifdef WITH_WARNINGS
1120 #warning Why not pass a proper DocIterator here?
1123 // if the table is inside a float it doesn't need the informaltable
1124 // wrapper. Search for it.
1125 for (master = owner(); master; master = master->owner())
1126 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1131 os << "<informaltable>";
1134 ret += tabular.docbook(buf, os, runparams);
1136 os << "</informaltable>";
1143 void InsetTabular::validate(LaTeXFeatures & features) const
1145 tabular.validate(features);
1149 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1151 return tabular.getCellInset(idx);
1155 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1157 return tabular.getCellInset(idx);
1161 void InsetTabular::cursorPos(BufferView const & bv,
1162 CursorSlice const & sl, bool boundary, int & x, int & y) const
1164 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1166 // y offset correction
1167 int const row = tabular.row_of_cell(sl.idx());
1168 for (int i = 0; i <= row; ++i) {
1170 y += tabular.getAscentOfRow(i);
1171 y += tabular.getAdditionalHeight(i);
1174 y += tabular.getDescentOfRow(i);
1177 // x offset correction
1178 int const col = tabular.column_of_cell(sl.idx());
1179 int idx = tabular.getCellNumber(row, 0);
1180 for (int j = 0; j < col; ++j) {
1181 if (tabular.isPartOfMultiColumn(row, j))
1183 x += tabular.getWidthOfColumn(idx);
1186 x += tabular.getBeginningOfTextInCell(idx);
1187 x += ADD_TO_TABULAR_WIDTH;
1192 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1196 InsetBase const & inset = *tabular.getCellInset(cell);
1197 Point o = bv.coordCache().getInsets().xy(&inset);
1198 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1199 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1200 row_type const row = tabular.row_of_cell(cell);
1201 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1202 tabular.getAdditionalHeight(row);
1203 int const yend = o.y_ + tabular.getDescentOfRow(row);
1215 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1216 // << " ybeg=" << ybeg << " yend=" << yend
1217 // << " xx=" << xx << " yy=" << yy
1218 // << " dist=" << xx + yy << endl;
1223 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1225 //lyxerr << "InsetTabular::editXY: " << this << endl;
1226 cur.selection() = false;
1228 cur.idx() = getNearestCell(cur.bv(), x, y);
1230 return cell(cur.idx())->text_.editXY(cur, x, y);
1234 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1236 cur.idx() = getNearestCell(cur.bv(), x, y);
1237 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1241 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1243 idx_type idx_min = 0;
1244 int dist_min = std::numeric_limits<int>::max();
1245 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1246 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1247 int const d = dist(bv, i, x, y);
1258 int InsetTabular::getCellXPos(idx_type const cell) const
1262 for (; !tabular.isFirstCellInRow(c); --c)
1264 int lx = tabular.getWidthOfColumn(cell);
1265 for (; c < cell; ++c)
1266 lx += tabular.getWidthOfColumn(c);
1268 return lx - tabular.getWidthOfColumn(cell);
1272 void InsetTabular::resetPos(LCursor & cur) const
1274 BufferView & bv = cur.bv();
1275 int const maxwidth = bv.workWidth();
1277 if (&cur.inset() != this) {
1281 int const X2 = maxwidth;
1282 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1283 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1284 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1287 scx_ = X1 + 20 - x1;
1289 scx_ = X2 - 20 - x2;
1294 cur.updateFlags(Update::Force | Update::FitCursor);
1298 void InsetTabular::moveNextCell(LCursor & cur)
1300 if (isRightToLeft(cur)) {
1301 if (tabular.isFirstCellInRow(cur.idx())) {
1302 row_type const row = tabular.row_of_cell(cur.idx());
1303 if (row == tabular.rows() - 1)
1305 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1312 if (tabular.isLastCell(cur.idx()))
1322 void InsetTabular::movePrevCell(LCursor & cur)
1324 if (isRightToLeft(cur)) {
1325 if (tabular.isLastCellInRow(cur.idx())) {
1326 row_type const row = tabular.row_of_cell(cur.idx());
1329 cur.idx() = tabular.getFirstCellInRow(row);
1330 cur.idx() = tabular.getCellAbove(cur.idx());
1332 if (tabular.isLastCell(cur.idx()))
1337 if (cur.idx() == 0) // first cell
1341 cur.pit() = cur.lastpit();
1342 cur.pos() = cur.lastpos();
1344 // FIXME: this accesses the position cache before it is initialized
1349 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1351 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1354 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1355 string const tmp = tabularFeature[i].feature;
1357 if (tmp == what.substr(0, tmp.length())) {
1358 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1359 //tabularFeatures[i].feature.length())) {
1360 action = tabularFeature[i].action;
1364 if (action == LyXTabular::LAST_ACTION)
1368 ltrim(what.substr(tabularFeature[i].feature.length()));
1369 tabularFeatures(cur, action, val);
1376 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1377 string const & special, bool & flag)
1379 if (special == "dl_above") {
1382 } else if (special == "dl_below") {
1383 ltt.bottomDL = flag;
1385 } else if (special == "empty") {
1397 void InsetTabular::tabularFeatures(LCursor & cur,
1398 LyXTabular::Feature feature, string const & value)
1400 BufferView & bv = cur.bv();
1401 col_type sel_col_start;
1402 col_type sel_col_end;
1403 row_type sel_row_start;
1404 row_type sel_row_end;
1405 bool setLines = false;
1406 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1407 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1411 case LyXTabular::M_ALIGN_LEFT:
1412 case LyXTabular::ALIGN_LEFT:
1413 setAlign = LYX_ALIGN_LEFT;
1416 case LyXTabular::M_ALIGN_RIGHT:
1417 case LyXTabular::ALIGN_RIGHT:
1418 setAlign = LYX_ALIGN_RIGHT;
1421 case LyXTabular::M_ALIGN_CENTER:
1422 case LyXTabular::ALIGN_CENTER:
1423 setAlign = LYX_ALIGN_CENTER;
1426 case LyXTabular::ALIGN_BLOCK:
1427 setAlign = LYX_ALIGN_BLOCK;
1430 case LyXTabular::M_VALIGN_TOP:
1431 case LyXTabular::VALIGN_TOP:
1432 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1435 case LyXTabular::M_VALIGN_BOTTOM:
1436 case LyXTabular::VALIGN_BOTTOM:
1437 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1440 case LyXTabular::M_VALIGN_MIDDLE:
1441 case LyXTabular::VALIGN_MIDDLE:
1442 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1449 recordUndoInset(cur, Undo::ATOMIC);
1451 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1452 row_type const row = tabular.row_of_cell(cur.idx());
1453 col_type const column = tabular.column_of_cell(cur.idx());
1455 LyXTabular::ltType ltt;
1459 case LyXTabular::SET_PWIDTH: {
1460 LyXLength const len(value);
1461 tabular.setColumnPWidth(cur, cur.idx(), len);
1463 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1464 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1468 case LyXTabular::SET_MPWIDTH:
1469 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1472 case LyXTabular::SET_SPECIAL_COLUMN:
1473 case LyXTabular::SET_SPECIAL_MULTI:
1474 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
1477 case LyXTabular::APPEND_ROW:
1478 // append the row into the tabular
1479 tabular.appendRow(bv.buffer()->params(), cur.idx());
1482 case LyXTabular::APPEND_COLUMN:
1483 // append the column into the tabular
1484 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1485 cur.idx() = tabular.getCellNumber(row, column);
1488 case LyXTabular::DELETE_ROW:
1489 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1490 tabular.deleteRow(sel_row_start);
1491 if (sel_row_start >= tabular.rows())
1493 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1496 cur.selection() = false;
1499 case LyXTabular::DELETE_COLUMN:
1500 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1501 tabular.deleteColumn(sel_col_start);
1502 if (sel_col_start >= tabular.columns())
1504 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1507 cur.selection() = false;
1510 case LyXTabular::COPY_ROW:
1511 tabular.copyRow(bv.buffer()->params(), row);
1514 case LyXTabular::COPY_COLUMN:
1515 tabular.copyColumn(bv.buffer()->params(), column);
1516 cur.idx() = tabular.getCellNumber(row, column);
1519 case LyXTabular::M_TOGGLE_LINE_TOP:
1521 case LyXTabular::TOGGLE_LINE_TOP: {
1522 bool lineSet = !tabular.topLine(cur.idx(), flag);
1523 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1524 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1526 tabular.getCellNumber(i, j),
1531 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1533 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1534 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1535 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1536 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1537 tabular.setBottomLine(
1538 tabular.getCellNumber(i, j),
1544 case LyXTabular::M_TOGGLE_LINE_LEFT:
1546 case LyXTabular::TOGGLE_LINE_LEFT: {
1547 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1548 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1549 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1550 tabular.setLeftLine(
1551 tabular.getCellNumber(i,j),
1557 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1559 case LyXTabular::TOGGLE_LINE_RIGHT: {
1560 bool lineSet = !tabular.rightLine(cur.idx(), flag);
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.setRightLine(
1564 tabular.getCellNumber(i,j),
1570 case LyXTabular::M_ALIGN_LEFT:
1571 case LyXTabular::M_ALIGN_RIGHT:
1572 case LyXTabular::M_ALIGN_CENTER:
1574 case LyXTabular::ALIGN_LEFT:
1575 case LyXTabular::ALIGN_RIGHT:
1576 case LyXTabular::ALIGN_CENTER:
1577 case LyXTabular::ALIGN_BLOCK:
1578 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1579 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1580 tabular.setAlignment(
1581 tabular.getCellNumber(i, j),
1586 case LyXTabular::M_VALIGN_TOP:
1587 case LyXTabular::M_VALIGN_BOTTOM:
1588 case LyXTabular::M_VALIGN_MIDDLE:
1590 case LyXTabular::VALIGN_TOP:
1591 case LyXTabular::VALIGN_BOTTOM:
1592 case LyXTabular::VALIGN_MIDDLE:
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.setVAlignment(
1596 tabular.getCellNumber(i, j),
1600 case LyXTabular::MULTICOLUMN: {
1601 if (sel_row_start != sel_row_end) {
1602 #ifdef WITH_WARNINGS
1603 #warning Need I say it ? This is horrible.
1606 Alert::error(_("Error setting multicolumn"),
1607 _("You cannot set multicolumn vertically."));
1610 if (!cur.selection()) {
1611 // just multicol for one single cell
1612 // check whether we are completely in a multicol
1613 if (tabular.isMultiColumn(cur.idx()))
1614 tabular.unsetMultiColumn(cur.idx());
1616 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1619 // we have a selection so this means we just add all this
1620 // cells to form a multicolumn cell
1621 idx_type const s_start = cur.selBegin().idx();
1622 idx_type const s_end = cur.selEnd().idx();
1623 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1624 cur.idx() = s_start;
1627 cur.selection() = false;
1631 case LyXTabular::SET_ALL_LINES:
1633 case LyXTabular::UNSET_ALL_LINES:
1634 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1635 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1636 tabular.setAllLines(
1637 tabular.getCellNumber(i,j), setLines);
1640 case LyXTabular::SET_LONGTABULAR:
1641 tabular.setLongTabular(true);
1644 case LyXTabular::UNSET_LONGTABULAR:
1645 tabular.setLongTabular(false);
1648 case LyXTabular::SET_ROTATE_TABULAR:
1649 tabular.setRotateTabular(true);
1652 case LyXTabular::UNSET_ROTATE_TABULAR:
1653 tabular.setRotateTabular(false);
1656 case LyXTabular::SET_ROTATE_CELL:
1657 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1658 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1659 tabular.setRotateCell(
1660 tabular.getCellNumber(i, j), true);
1663 case LyXTabular::UNSET_ROTATE_CELL:
1664 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1665 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1666 tabular.setRotateCell(
1667 tabular.getCellNumber(i, j), false);
1670 case LyXTabular::SET_USEBOX: {
1671 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1672 if (val == tabular.getUsebox(cur.idx()))
1673 val = LyXTabular::BOX_NONE;
1674 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1675 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1676 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1680 case LyXTabular::UNSET_LTFIRSTHEAD:
1682 case LyXTabular::SET_LTFIRSTHEAD:
1683 tabular.getRowOfLTFirstHead(row, ltt);
1684 checkLongtableSpecial(ltt, value, flag);
1685 tabular.setLTHead(row, flag, ltt, true);
1688 case LyXTabular::UNSET_LTHEAD:
1690 case LyXTabular::SET_LTHEAD:
1691 tabular.getRowOfLTHead(row, ltt);
1692 checkLongtableSpecial(ltt, value, flag);
1693 tabular.setLTHead(row, flag, ltt, false);
1696 case LyXTabular::UNSET_LTFOOT:
1698 case LyXTabular::SET_LTFOOT:
1699 tabular.getRowOfLTFoot(row, ltt);
1700 checkLongtableSpecial(ltt, value, flag);
1701 tabular.setLTFoot(row, flag, ltt, false);
1704 case LyXTabular::UNSET_LTLASTFOOT:
1706 case LyXTabular::SET_LTLASTFOOT:
1707 tabular.getRowOfLTLastFoot(row, ltt);
1708 checkLongtableSpecial(ltt, value, flag);
1709 tabular.setLTFoot(row, flag, ltt, true);
1712 case LyXTabular::SET_LTNEWPAGE:
1713 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1716 case LyXTabular::SET_BOOKTABS:
1717 tabular.setBookTabs(true);
1720 case LyXTabular::UNSET_BOOKTABS:
1721 tabular.setBookTabs(false);
1724 case LyXTabular::SET_TOP_SPACE: {
1726 if (value == "default")
1727 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1728 tabular.row_info[i].top_space_default = true;
1729 else if (isValidLength(value, &len))
1730 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1731 tabular.row_info[i].top_space_default = false;
1732 tabular.row_info[i].top_space = len;
1735 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1736 tabular.row_info[i].top_space_default = false;
1737 tabular.row_info[i].top_space = len;
1742 case LyXTabular::SET_BOTTOM_SPACE: {
1744 if (value == "default")
1745 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1746 tabular.row_info[i].bottom_space_default = true;
1747 else if (isValidLength(value, &len))
1748 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1749 tabular.row_info[i].bottom_space_default = false;
1750 tabular.row_info[i].bottom_space = len;
1753 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1754 tabular.row_info[i].bottom_space_default = false;
1755 tabular.row_info[i].bottom_space = len;
1760 case LyXTabular::SET_INTERLINE_SPACE: {
1762 if (value == "default")
1763 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1764 tabular.row_info[i].interline_space_default = true;
1765 else if (isValidLength(value, &len))
1766 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1767 tabular.row_info[i].interline_space_default = false;
1768 tabular.row_info[i].interline_space = len;
1771 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1772 tabular.row_info[i].interline_space_default = false;
1773 tabular.row_info[i].interline_space = len;
1778 // dummy stuff just to avoid warnings
1779 case LyXTabular::LAST_ACTION:
1785 bool InsetTabular::showInsetDialog(BufferView * bv) const
1787 InsetTabularMailer(*this).showDialog(bv);
1792 void InsetTabular::openLayoutDialog(BufferView * bv) const
1794 InsetTabularMailer(*this).showDialog(bv);
1798 bool InsetTabular::copySelection(LCursor & cur)
1800 if (!cur.selection())
1805 getSelection(cur, rs, re, cs, ce);
1807 paste_tabular.reset(new LyXTabular(tabular));
1809 for (row_type i = 0; i < rs; ++i)
1810 paste_tabular->deleteRow(0);
1812 row_type const rows = re - rs + 1;
1813 while (paste_tabular->rows() > rows)
1814 paste_tabular->deleteRow(rows);
1816 paste_tabular->setTopLine(0, true, true);
1817 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1820 for (col_type i = 0; i < cs; ++i)
1821 paste_tabular->deleteColumn(0);
1823 col_type const columns = ce - cs + 1;
1824 while (paste_tabular->columns() > columns)
1825 paste_tabular->deleteColumn(columns);
1827 paste_tabular->setLeftLine(0, true, true);
1828 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1831 odocstringstream os;
1832 OutputParams const runparams(0);
1833 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1834 // Needed for the "Edit->Paste recent" menu and the system clipboard.
1835 cap::copySelection(cur, os.str());
1837 // mark tabular stack dirty
1838 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1839 // when we (hopefully) have a one-for-all paste mechanism.
1840 // This must be called after cap::copySelection.
1841 dirtyTabularStack(true);
1847 bool InsetTabular::pasteClipboard(LCursor & cur)
1851 col_type const actcol = tabular.column_of_cell(cur.idx());
1852 row_type const actrow = tabular.row_of_cell(cur.idx());
1853 for (row_type r1 = 0, r2 = actrow;
1854 r1 < paste_tabular->rows() && r2 < tabular.rows();
1856 for (col_type c1 = 0, c2 = actcol;
1857 c1 < paste_tabular->columns() && c2 < tabular.columns();
1859 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1860 tabular.isPartOfMultiColumn(r2, c2))
1862 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1866 if (tabular.isPartOfMultiColumn(r2, c2)) {
1870 shared_ptr<InsetText> inset(
1871 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1872 tabular.setCellInset(r2, c2, inset);
1873 // FIXME: change tracking (MG)
1874 inset->setChange(Change(cur.buffer().params().trackChanges ?
1875 Change::INSERTED : Change::UNCHANGED));
1883 void InsetTabular::cutSelection(LCursor & cur)
1885 if (!cur.selection())
1890 getSelection(cur, rs, re, cs, ce);
1891 for (row_type i = rs; i <= re; ++i) {
1892 for (col_type j = cs; j <= ce; ++j) {
1893 shared_ptr<InsetText> t
1894 = cell(tabular.getCellNumber(i, j));
1895 if (cur.buffer().params().trackChanges)
1896 // FIXME: Change tracking (MG)
1897 t->setChange(Change(Change::DELETED));
1903 // cursor position might be invalid now
1904 if (cur.pit() > cur.lastpit())
1905 cur.pit() = cur.lastpit();
1906 if (cur.pos() > cur.lastpos())
1907 cur.pos() = cur.lastpos();
1908 cur.clearSelection();
1909 theSelection().haveSelection(false);
1913 bool InsetTabular::isRightToLeft(LCursor & cur) const
1915 BOOST_ASSERT(cur.depth() > 1);
1916 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1917 pos_type const parentpos = cur[cur.depth() - 2].pos();
1918 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1919 parentpos).language()->rightToLeft();
1923 void InsetTabular::getSelection(LCursor & cur,
1924 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1926 CursorSlice const & beg = cur.selBegin();
1927 CursorSlice const & end = cur.selEnd();
1928 cs = tabular.column_of_cell(beg.idx());
1929 ce = tabular.column_of_cell(end.idx());
1932 cs = tabular.column_of_cell(end.idx());
1934 ce = tabular.right_column_of_cell(end.idx());
1937 rs = tabular.row_of_cell(beg.idx());
1938 re = tabular.row_of_cell(end.idx());
1944 LyXText * InsetTabular::getText(int idx) const
1946 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1950 void InsetTabular::setChange(Change const & change)
1952 for (idx_type idx = 0; idx < nargs(); ++idx)
1953 cell(idx)->setChange(change);
1957 void InsetTabular::acceptChanges(BufferParams const & bparams)
1959 for (idx_type idx = 0; idx < nargs(); ++idx)
1960 cell(idx)->acceptChanges(bparams);
1964 void InsetTabular::rejectChanges(BufferParams const & bparams)
1966 for (idx_type idx = 0; idx < nargs(); ++idx)
1967 cell(idx)->rejectChanges(bparams);
1971 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1973 return tabular.getPWidth(cell).zero();
1977 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
1980 if (buf.length() <= 0)
1983 Buffer const & buffer = *bv.buffer();
1987 col_type maxCols = 1;
1988 docstring::size_type const len = buf.length();
1989 docstring::size_type p = 0;
1992 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
2000 maxCols = max(cols, maxCols);
2006 maxCols = max(cols, maxCols);
2007 LyXTabular * loctab;
2012 paste_tabular.reset(
2013 new LyXTabular(buffer.params(), rows, maxCols));
2014 loctab = paste_tabular.get();
2016 dirtyTabularStack(true);
2019 cell = bv.cursor().idx();
2020 ocol = tabular.column_of_cell(cell);
2021 row = tabular.row_of_cell(cell);
2024 docstring::size_type op = 0;
2025 idx_type const cells = loctab->getNumberOfCells();
2028 rows = loctab->rows();
2029 col_type const columns = loctab->columns();
2031 while (cell < cells && p < len && row < rows &&
2032 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
2038 // we can only set this if we are not too far right
2039 if (cols < columns) {
2040 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2041 Paragraph & par = inset->text_.getPar(0);
2042 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2043 inset->setText(buf.substr(op, p - op), font,
2044 buffer.params().trackChanges);
2050 // we can only set this if we are not too far right
2051 if (cols < columns) {
2052 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2053 Paragraph & par = inset->text_.getPar(0);
2054 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2055 inset->setText(buf.substr(op, p - op), font,
2056 buffer.params().trackChanges);
2061 cell = loctab->getCellNumber(row, cols);
2067 // check for the last cell if there is no trailing '\n'
2068 if (cell < cells && op < len) {
2069 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2070 Paragraph & par = inset->text_.getPar(0);
2071 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2072 inset->setText(buf.substr(op, len - op), font,
2073 buffer.params().trackChanges);
2079 void InsetTabular::addPreview(PreviewLoader & loader) const
2081 row_type const rows = tabular.rows();
2082 col_type const columns = tabular.columns();
2083 for (row_type i = 0; i < rows; ++i) {
2084 for (col_type j = 0; j < columns; ++j)
2085 tabular.getCellInset(i, j)->addPreview(loader);
2090 bool InsetTabular::tablemode(LCursor & cur) const
2092 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2099 string const InsetTabularMailer::name_("tabular");
2101 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2102 : inset_(const_cast<InsetTabular &>(inset))
2106 string const InsetTabularMailer::inset2string(Buffer const &) const
2108 return params2string(inset_);
2112 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2114 istringstream data(in);
2116 lex.setStream(data);
2123 if (!lex || token != name_)
2124 return print_mailer_error("InsetTabularMailer", in, 1,
2127 // This is part of the inset proper that is usually swallowed
2128 // by Buffer::readInset
2130 if (!lex || token != "Tabular")
2131 return print_mailer_error("InsetTabularMailer", in, 2,
2134 Buffer const & buffer = inset.buffer();
2135 inset.read(buffer, lex);
2139 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2142 data << name_ << ' ';
2143 inset.write(inset.buffer(), data);
2144 data << "\\end_inset\n";