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;
54 using graphics::PreviewLoader;
58 using frontend::Painter;
59 using frontend::Clipboard;
61 using boost::shared_ptr;
67 using std::istringstream;
69 using std::ostringstream;
73 namespace Alert = frontend::Alert;
78 int const ADD_TO_HEIGHT = 2;
79 int const ADD_TO_TABULAR_WIDTH = 2;
80 int const default_line_space = 10;
83 boost::scoped_ptr<LyXTabular> paste_tabular;
86 struct TabularFeature {
87 LyXTabular::Feature action;
92 TabularFeature tabularFeature[] =
94 { LyXTabular::APPEND_ROW, "append-row" },
95 { LyXTabular::APPEND_COLUMN, "append-column" },
96 { LyXTabular::DELETE_ROW, "delete-row" },
97 { LyXTabular::DELETE_COLUMN, "delete-column" },
98 { LyXTabular::COPY_ROW, "copy-row" },
99 { LyXTabular::COPY_COLUMN, "copy-column" },
100 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
101 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
102 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
103 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
104 { LyXTabular::ALIGN_LEFT, "align-left" },
105 { LyXTabular::ALIGN_RIGHT, "align-right" },
106 { LyXTabular::ALIGN_CENTER, "align-center" },
107 { LyXTabular::ALIGN_BLOCK, "align-block" },
108 { LyXTabular::VALIGN_TOP, "valign-top" },
109 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
110 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
111 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
112 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
113 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
114 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
115 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
116 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
117 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
118 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
119 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
120 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
121 { LyXTabular::MULTICOLUMN, "multicolumn" },
122 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
123 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
124 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
125 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
126 { LyXTabular::SET_PWIDTH, "set-pwidth" },
127 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
128 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
129 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
130 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
131 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
132 { LyXTabular::SET_USEBOX, "set-usebox" },
133 { LyXTabular::SET_LTHEAD, "set-lthead" },
134 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
135 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
136 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
137 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
138 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
139 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
140 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
141 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
142 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
143 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
144 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
145 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
146 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
147 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
148 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
149 { LyXTabular::LAST_ACTION, "" }
153 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
155 FeatureEqual(LyXTabular::Feature feature)
156 : feature_(feature) {}
157 bool operator()(TabularFeature const & tf) const {
158 return tf.action == feature_;
161 LyXTabular::Feature feature_;
167 string const featureAsString(LyXTabular::Feature feature)
169 TabularFeature * end = tabularFeature +
170 sizeof(tabularFeature) / sizeof(TabularFeature);
171 TabularFeature * it = std::find_if(tabularFeature, end,
172 FeatureEqual(feature));
173 return (it == end) ? string() : it->feature;
177 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
179 : tabular(buf.params(), max(rows, row_type(1)),
180 max(columns, col_type(1))), buffer_(&buf), scx_(0)
184 InsetTabular::InsetTabular(InsetTabular const & tab)
185 : InsetOld(tab), tabular(tab.tabular),
186 buffer_(tab.buffer_), scx_(0)
190 InsetTabular::~InsetTabular()
192 InsetTabularMailer(*this).hideDialog();
196 auto_ptr<InsetBase> InsetTabular::doClone() const
198 return auto_ptr<InsetBase>(new InsetTabular(*this));
202 Buffer const & InsetTabular::buffer() const
208 void InsetTabular::buffer(Buffer const * b)
214 void InsetTabular::write(Buffer const & buf, ostream & os) const
216 os << "Tabular" << endl;
217 tabular.write(buf, os);
221 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
223 bool const old_format = (lex.getString() == "\\LyXTable");
225 tabular.read(buf, lex);
231 string token = lex.getString();
232 while (lex.isOK() && (token != "\\end_inset")) {
234 token = lex.getString();
236 if (token != "\\end_inset") {
237 lex.printError("Missing \\end_inset at this point. "
243 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
245 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
246 // mi.base.textwidth << "\n";
248 lyxerr << "InsetTabular::metrics: need bv" << endl;
253 for (idx_type cell = 0; i < tabular.rows(); ++i) {
256 for (col_type j = 0; j < tabular.columns(); ++j) {
257 if (tabular.isPartOfMultiColumn(i, j))
258 // Multicolumn cell, but not first one
263 if (tabular.cell_info[i][j].multicolumn ==
264 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
265 p_width = tabular.cellinfo_of_cell(cell).p_width;
267 p_width = tabular.column_info[j].p_width;
269 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
270 tabular.getCellInset(cell)->metrics(m, dim);
272 dim.wid = m.base.textwidth;
273 tabular.setWidthOfCell(cell, dim.wid);
274 if (p_width.zero()) {
275 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
276 // FIXME there must be a way to get rid of
277 // the second metrics call
278 tabular.getCellInset(cell)->metrics(m, dim);
280 maxAsc = max(maxAsc, dim.asc);
281 maxDesc = max(maxDesc, dim.des);
284 int const top_space = tabular.row_info[i].top_space_default ?
286 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
287 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
288 int const bottom_space = tabular.row_info[i].bottom_space_default ?
290 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
291 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
294 dim.asc = tabular.getAscentOfRow(0);
295 dim.des = tabular.getHeightOfTabular() - dim.asc;
296 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
297 bool const changed = dim_ != dim;
303 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
305 setPosCache(pi, x, y);
307 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
308 BufferView * bv = pi.base.bv;
310 resetPos(bv->cursor());
313 x += ADD_TO_TABULAR_WIDTH;
316 first_visible_cell = LyXTabular::npos;
317 for (row_type i = 0; i < tabular.rows(); ++i) {
319 int const a = tabular.getAscentOfRow(i);
320 int const d = tabular.getDescentOfRow(i);
321 idx = tabular.getCellNumber(i, 0);
322 for (col_type j = 0; j < tabular.columns(); ++j) {
323 if (tabular.isPartOfMultiColumn(i, j))
325 if (first_visible_cell == LyXTabular::npos)
326 first_visible_cell = idx;
328 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
329 if (nx + tabular.getWidthOfColumn(idx) < 0
330 || nx > bv->workWidth()
332 || y - a > bv->workHeight()) {
333 pi.pain.setDrawingEnabled(false);
334 cell(idx)->draw(pi, cx, y);
335 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
336 pi.pain.setDrawingEnabled(true);
338 cell(idx)->draw(pi, cx, y);
339 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
341 nx += tabular.getWidthOfColumn(idx);
345 if (i + 1 < tabular.rows())
346 y += d + tabular.getAscentOfRow(i + 1) +
347 tabular.getAdditionalHeight(i + 1);
352 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
354 setPosCache(pi, x, y);
356 LCursor & cur = pi.base.bv->cursor();
358 x += scx_ + ADD_TO_TABULAR_WIDTH;
360 // Paint background of current tabular
361 int const w = tabular.getWidthOfTabular();
362 int const h = tabular.getHeightOfTabular();
363 int yy = y - tabular.getAscentOfRow(0);
364 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
366 if (!cur.selection())
368 if (!ptr_cmp(&cur.inset(), this))
374 if (tablemode(cur)) {
377 getSelection(cur, rs, re, cs, ce);
378 y -= tabular.getAscentOfRow(0);
379 for (row_type j = 0; j < tabular.rows(); ++j) {
380 int const a = tabular.getAscentOfRow(j);
381 int const h = a + tabular.getDescentOfRow(j);
383 y += tabular.getAdditionalHeight(j);
384 for (col_type i = 0; i < tabular.columns(); ++i) {
385 if (tabular.isPartOfMultiColumn(j, i))
387 idx_type const cell =
388 tabular.getCellNumber(j, i);
389 int const w = tabular.getWidthOfColumn(cell);
390 if (i >= cs && i <= ce && j >= rs && j <= re)
391 pi.pain.fillRectangle(xx, y, w, h,
400 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
405 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
406 row_type row, idx_type cell, bool erased) const
408 int x2 = x + tabular.getWidthOfColumn(cell);
410 LColor::color col = LColor::tabularline;
411 LColor::color onoffcol = LColor::tabularonoffline;
414 col = LColor::strikeout;
415 onoffcol = LColor::strikeout;
418 if (!tabular.topAlreadyDrawn(cell)) {
419 on_off = !tabular.topLine(cell);
420 pain.line(x, y - tabular.getAscentOfRow(row),
421 x2, y - tabular.getAscentOfRow(row),
422 on_off ? onoffcol : col,
423 on_off ? Painter::line_onoffdash : Painter::line_solid);
425 on_off = !tabular.bottomLine(cell);
426 pain.line(x, y + tabular.getDescentOfRow(row),
427 x2, y + tabular.getDescentOfRow(row),
428 on_off ? onoffcol : col,
429 on_off ? Painter::line_onoffdash : Painter::line_solid);
430 if (!tabular.leftAlreadyDrawn(cell)) {
431 on_off = !tabular.leftLine(cell);
432 pain.line(x, y - tabular.getAscentOfRow(row),
433 x, y + tabular.getDescentOfRow(row),
434 on_off ? onoffcol : col,
435 on_off ? Painter::line_onoffdash : Painter::line_solid);
437 on_off = !tabular.rightLine(cell);
438 pain.line(x2 - tabular.getAdditionalWidth(cell),
439 y - tabular.getAscentOfRow(row),
440 x2 - tabular.getAdditionalWidth(cell),
441 y + tabular.getDescentOfRow(row),
442 on_off ? onoffcol : col,
443 on_off ? Painter::line_onoffdash : Painter::line_solid);
447 docstring const InsetTabular::editMessage() const
449 return _("Opened table");
453 void InsetTabular::edit(LCursor & cur, bool left)
455 //lyxerr << "InsetTabular::edit: " << this << endl;
457 cur.selection() = false;
460 if (isRightToLeft(cur))
461 cur.idx() = tabular.getLastCellInRow(0);
467 if (isRightToLeft(cur))
468 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
470 cur.idx() = tabular.getNumberOfCells() - 1;
472 cur.pos() = cur.lastpos(); // FIXME crude guess
474 // FIXME: this accesses the position cache before it is initialized
476 //cur.bv().fitCursor();
480 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
482 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
483 << "\n cur:" << cur << endl;
484 CursorSlice sl = cur.top();
485 LCursor & bvcur = cur.bv().cursor();
487 switch (cmd.action) {
489 case LFUN_MOUSE_PRESS:
490 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
492 if (cmd.button() == mouse_button::button1
493 || (cmd.button() == mouse_button::button3
494 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
495 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
497 cur.selection() = false;
498 setCursorFromCoordinates(cur, cmd.x, cmd.y);
499 cur.bv().mouseSetCursor(cur);
503 if (cmd.button() == mouse_button::button2) {
504 if (bvcur.selection()) {
505 // See comment in LyXText::dispatch why we
507 // FIXME This does not use paste_tabular,
508 // another reason why paste_tabular should go.
509 cap::copySelectionToStack(bvcur);
510 cmd = FuncRequest(LFUN_PASTE, "0");
512 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
515 doDispatch(cur, cmd);
519 case LFUN_MOUSE_MOTION:
520 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
521 if (cmd.button() == mouse_button::button1) {
522 // only accept motions to places not deeper nested than the real anchor
523 if (bvcur.anchor_.hasPart(cur)) {
524 // only update if selection changes
525 if (bvcur.idx() == cur.idx() &&
526 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
528 setCursorFromCoordinates(cur, cmd.x, cmd.y);
529 bvcur.setCursor(cur);
530 bvcur.selection() = true;
536 case LFUN_MOUSE_RELEASE:
537 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
538 if (cmd.button() == mouse_button::button1) {
539 if (bvcur.selection())
540 theSelection().haveSelection(true);
541 } else if (cmd.button() == mouse_button::button3)
542 InsetTabularMailer(*this).showDialog(&cur.bv());
545 case LFUN_CELL_BACKWARD:
547 cur.selection() = false;
550 case LFUN_CELL_FORWARD:
552 cur.selection() = false;
555 case LFUN_CHAR_FORWARD_SELECT:
556 case LFUN_CHAR_FORWARD:
557 cell(cur.idx())->dispatch(cur, cmd);
558 if (!cur.result().dispatched()) {
559 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
560 if (cmd.action == LFUN_CHAR_FORWARD_SELECT)
561 theSelection().haveSelection(cur.selection());
563 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
569 case LFUN_CHAR_BACKWARD_SELECT:
570 case LFUN_CHAR_BACKWARD:
571 cell(cur.idx())->dispatch(cur, cmd);
572 if (!cur.result().dispatched()) {
573 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
574 if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
575 theSelection().haveSelection(cur.selection());
577 cmd = FuncRequest(LFUN_FINISHED_LEFT);
583 case LFUN_DOWN_SELECT:
585 cell(cur.idx())->dispatch(cur, cmd);
586 cur.dispatched(); // override the cell's decision
588 // if our LyXText didn't do anything to the cursor
589 // then we try to put the cursor into the cell below
590 // setting also the right targetX.
591 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
592 cur.idx() = tabular.getCellBelow(cur.idx());
594 TextMetrics const & tm =
595 cur.bv().textMetrics(cell(cur.idx())->getText(0));
596 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
597 if (cmd.action == LFUN_DOWN_SELECT)
598 theSelection().haveSelection(cur.selection());
600 if (sl == cur.top()) {
601 // we trick it to go to the RIGHT after leaving the
603 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
610 cell(cur.idx())->dispatch(cur, cmd);
611 cur.dispatched(); // override the cell's decision
613 // if our LyXText didn't do anything to the cursor
614 // then we try to put the cursor into the cell above
615 // setting also the right targetX.
616 if (tabular.row_of_cell(cur.idx()) != 0) {
617 cur.idx() = tabular.getCellAbove(cur.idx());
618 cur.pit() = cur.lastpit();
619 LyXText const * text = cell(cur.idx())->getText(0);
620 TextMetrics const & tm = cur.bv().textMetrics(text);
621 ParagraphMetrics const & pm =
622 tm.parMetrics(cur.lastpit());
623 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
624 if (cmd.action == LFUN_UP_SELECT)
625 theSelection().haveSelection(cur.selection());
627 if (sl == cur.top()) {
628 cmd = FuncRequest(LFUN_FINISHED_UP);
633 // case LFUN_SCREEN_DOWN: {
634 // //if (hasSelection())
635 // // cur.selection() = false;
636 // col_type const col = tabular.column_of_cell(cur.idx());
637 // int const t = cur.bv().top_y() + cur.bv().height();
638 // if (t < yo() + tabular.getHeightOfTabular()) {
639 // cur.bv().scrollDocView(t);
640 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
642 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
649 // case LFUN_SCREEN_UP: {
650 // //if (hasSelection())
651 // // cur.selection() = false;
652 // col_type const col = tabular.column_of_cell(cur.idx());
653 // int const t = cur.bv().top_y() + cur.bv().height();
655 // cur.bv().scrollDocView(t);
659 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
663 // cur.par() = cur.lastpar();
664 // cur.pos() = cur.lastpos();
668 case LFUN_LAYOUT_TABULAR:
669 InsetTabularMailer(*this).showDialog(&cur.bv());
672 case LFUN_INSET_DIALOG_UPDATE:
673 InsetTabularMailer(*this).updateDialog(&cur.bv());
676 case LFUN_TABULAR_FEATURE:
677 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
681 // insert file functions
682 case LFUN_FILE_INSERT_ASCII_PARA:
683 case LFUN_FILE_INSERT_ASCII: {
685 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), to_utf8(cmd.argument()), false);
686 // FIXME: We don't know the encoding of the file
687 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), from_utf8(tmpstr), false))
693 if (tablemode(cur)) {
694 if (copySelection(cur)) {
695 recordUndoInset(cur, Undo::DELETE);
700 cell(cur.idx())->dispatch(cur, cmd);
703 case LFUN_CHAR_DELETE_BACKWARD:
704 case LFUN_CHAR_DELETE_FORWARD:
705 if (tablemode(cur)) {
706 recordUndoInset(cur, Undo::DELETE);
710 cell(cur.idx())->dispatch(cur, cmd);
714 if (!cur.selection())
716 if (tablemode(cur)) {
720 cell(cur.idx())->dispatch(cur, cmd);
723 case LFUN_CLIPBOARD_PASTE:
724 case LFUN_PRIMARY_SELECTION_PASTE: {
725 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
726 theClipboard().getAsText() :
727 theSelection().get();
730 // pass to InsertAsciiString, but
731 // only if we have multi-cell content
732 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
733 if (insertAsciiString(cur.bv(), clip, false)) {
734 // content has been replaced,
735 // so cursor might be invalid
736 cur.pos() = cur.lastpos();
737 bvcur.setCursor(cur);
741 // Let the cell handle normal text
742 cell(cur.idx())->dispatch(cur, cmd);
747 if (tabularStackDirty() && theClipboard().isInternal()) {
748 recordUndoInset(cur, Undo::INSERT);
752 cell(cur.idx())->dispatch(cur, cmd);
757 case LFUN_FONT_ROMAN:
763 case LFUN_FONT_FREE_APPLY:
764 case LFUN_FONT_FREE_UPDATE:
766 case LFUN_FONT_UNDERLINE:
768 case LFUN_WORD_CAPITALIZE:
769 case LFUN_WORD_UPCASE:
770 case LFUN_WORD_LOWCASE:
771 case LFUN_CHARS_TRANSPOSE:
772 if (tablemode(cur)) {
775 getSelection(cur, rs, re, cs, ce);
776 LCursor tmpcur = cur;
777 for (row_type i = rs; i <= re; ++i) {
778 for (col_type j = cs; j <= ce; ++j) {
779 // cursor follows cell:
780 tmpcur.idx() = tabular.getCellNumber(i, j);
781 // select this cell only:
784 tmpcur.resetAnchor();
785 tmpcur.pit() = tmpcur.lastpit();
786 tmpcur.pos() = tmpcur.top().lastpos();
787 tmpcur.setCursor(tmpcur);
788 tmpcur.setSelection();
789 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
794 cell(cur.idx())->dispatch(cur, cmd);
798 // we try to handle this event in the insets dispatch function.
799 cell(cur.idx())->dispatch(cur, cmd);
803 // FIXME: this accesses the position cache before it is initialized
805 InsetTabularMailer(*this).updateDialog(&cur.bv());
809 // function sets an object as defined in func_status.h:
810 // states OK, Unknown, Disabled, On, Off.
811 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
812 FuncStatus & status) const
814 switch (cmd.action) {
815 case LFUN_TABULAR_FEATURE: {
816 int action = LyXTabular::LAST_ACTION;
818 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
819 string const tmp = tabularFeature[i].feature;
820 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
821 action = tabularFeature[i].action;
825 if (action == LyXTabular::LAST_ACTION) {
827 status.unknown(true);
831 string const argument
832 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
834 row_type sel_row_start = 0;
835 row_type sel_row_end = 0;
837 LyXTabular::ltType dummyltt;
840 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
843 case LyXTabular::SET_PWIDTH:
844 case LyXTabular::SET_MPWIDTH:
845 case LyXTabular::SET_SPECIAL_COLUMN:
846 case LyXTabular::SET_SPECIAL_MULTI:
847 case LyXTabular::APPEND_ROW:
848 case LyXTabular::APPEND_COLUMN:
849 case LyXTabular::DELETE_ROW:
850 case LyXTabular::DELETE_COLUMN:
851 case LyXTabular::COPY_ROW:
852 case LyXTabular::COPY_COLUMN:
853 case LyXTabular::SET_ALL_LINES:
854 case LyXTabular::UNSET_ALL_LINES:
855 case LyXTabular::SET_TOP_SPACE:
856 case LyXTabular::SET_BOTTOM_SPACE:
857 case LyXTabular::SET_INTERLINE_SPACE:
861 case LyXTabular::MULTICOLUMN:
862 status.setOnOff(tabular.isMultiColumn(cur.idx()));
865 case LyXTabular::M_TOGGLE_LINE_TOP:
867 case LyXTabular::TOGGLE_LINE_TOP:
868 status.setOnOff(tabular.topLine(cur.idx(), flag));
871 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
873 case LyXTabular::TOGGLE_LINE_BOTTOM:
874 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
877 case LyXTabular::M_TOGGLE_LINE_LEFT:
879 case LyXTabular::TOGGLE_LINE_LEFT:
880 status.setOnOff(tabular.leftLine(cur.idx(), flag));
883 case LyXTabular::M_TOGGLE_LINE_RIGHT:
885 case LyXTabular::TOGGLE_LINE_RIGHT:
886 status.setOnOff(tabular.rightLine(cur.idx(), flag));
889 case LyXTabular::M_ALIGN_LEFT:
891 case LyXTabular::ALIGN_LEFT:
892 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
895 case LyXTabular::M_ALIGN_RIGHT:
897 case LyXTabular::ALIGN_RIGHT:
898 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
901 case LyXTabular::M_ALIGN_CENTER:
903 case LyXTabular::ALIGN_CENTER:
904 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
907 case LyXTabular::ALIGN_BLOCK:
908 status.enabled(!tabular.getPWidth(cur.idx()).zero());
909 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
912 case LyXTabular::M_VALIGN_TOP:
914 case LyXTabular::VALIGN_TOP:
916 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
919 case LyXTabular::M_VALIGN_BOTTOM:
921 case LyXTabular::VALIGN_BOTTOM:
923 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
926 case LyXTabular::M_VALIGN_MIDDLE:
928 case LyXTabular::VALIGN_MIDDLE:
930 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
933 case LyXTabular::SET_LONGTABULAR:
934 status.setOnOff(tabular.isLongTabular());
937 case LyXTabular::UNSET_LONGTABULAR:
938 status.setOnOff(!tabular.isLongTabular());
941 case LyXTabular::SET_ROTATE_TABULAR:
942 status.setOnOff(tabular.getRotateTabular());
945 case LyXTabular::UNSET_ROTATE_TABULAR:
946 status.setOnOff(!tabular.getRotateTabular());
949 case LyXTabular::SET_ROTATE_CELL:
950 status.setOnOff(tabular.getRotateCell(cur.idx()));
953 case LyXTabular::UNSET_ROTATE_CELL:
954 status.setOnOff(!tabular.getRotateCell(cur.idx()));
957 case LyXTabular::SET_USEBOX:
958 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
961 case LyXTabular::SET_LTFIRSTHEAD:
962 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
965 case LyXTabular::UNSET_LTFIRSTHEAD:
966 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
969 case LyXTabular::SET_LTHEAD:
970 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
973 case LyXTabular::UNSET_LTHEAD:
974 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
977 case LyXTabular::SET_LTFOOT:
978 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
981 case LyXTabular::UNSET_LTFOOT:
982 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
985 case LyXTabular::SET_LTLASTFOOT:
986 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
989 case LyXTabular::UNSET_LTLASTFOOT:
990 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
993 case LyXTabular::SET_LTNEWPAGE:
994 status.setOnOff(tabular.getLTNewPage(sel_row_start));
997 case LyXTabular::SET_BOOKTABS:
998 status.setOnOff(tabular.useBookTabs());
1001 case LyXTabular::UNSET_BOOKTABS:
1002 status.setOnOff(!tabular.useBookTabs());
1007 status.enabled(false);
1013 // These are only enabled inside tabular
1014 case LFUN_CELL_BACKWARD:
1015 case LFUN_CELL_FORWARD:
1016 status.enabled(true);
1019 // disable these with multiple cells selected
1020 case LFUN_INSET_INSERT:
1021 case LFUN_TABULAR_INSERT:
1022 case LFUN_CHARSTYLE_INSERT:
1023 case LFUN_FLOAT_INSERT:
1024 case LFUN_FLOAT_WIDE_INSERT:
1025 case LFUN_FOOTNOTE_INSERT:
1026 case LFUN_MARGINALNOTE_INSERT:
1027 case LFUN_MATH_INSERT:
1028 case LFUN_MATH_MODE:
1029 case LFUN_MATH_MUTATE:
1030 case LFUN_MATH_DISPLAY:
1031 case LFUN_NOTE_INSERT:
1032 case LFUN_OPTIONAL_INSERT:
1033 case LFUN_BOX_INSERT:
1034 case LFUN_BRANCH_INSERT:
1035 case LFUN_WRAP_INSERT:
1036 case LFUN_ERT_INSERT: {
1037 if (tablemode(cur)) {
1038 status.enabled(false);
1041 return cell(cur.idx())->getStatus(cur, cmd, status);
1044 // disable in non-fixed-width cells
1045 case LFUN_BREAK_LINE:
1046 case LFUN_BREAK_PARAGRAPH:
1047 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1048 case LFUN_BREAK_PARAGRAPH_SKIP: {
1049 if (tabular.getPWidth(cur.idx()).zero()) {
1050 status.enabled(false);
1053 return cell(cur.idx())->getStatus(cur, cmd, status);
1057 if (tabularStackDirty() && theClipboard().isInternal()) {
1058 status.enabled(true);
1061 return cell(cur.idx())->getStatus(cur, cmd, status);
1063 case LFUN_INSET_MODIFY:
1064 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1065 status.enabled(true);
1071 // we try to handle this event in the insets dispatch function.
1072 return cell(cur.idx())->getStatus(cur, cmd, status);
1077 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1078 OutputParams const & runparams) const
1080 return tabular.latex(buf, os, runparams);
1084 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1085 OutputParams const & runparams) const
1087 int const dp = runparams.linelen ? runparams.depth : 0;
1088 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1092 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1093 OutputParams const & runparams) const
1096 InsetBase * master = 0;
1098 #ifdef WITH_WARNINGS
1099 #warning Why not pass a proper DocIterator here?
1102 // if the table is inside a float it doesn't need the informaltable
1103 // wrapper. Search for it.
1104 for (master = owner(); master; master = master->owner())
1105 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1110 os << "<informaltable>";
1113 ret += tabular.docbook(buf, os, runparams);
1115 os << "</informaltable>";
1122 void InsetTabular::validate(LaTeXFeatures & features) const
1124 tabular.validate(features);
1128 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1130 return tabular.getCellInset(idx);
1134 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1136 return tabular.getCellInset(idx);
1140 void InsetTabular::cursorPos(BufferView const & bv,
1141 CursorSlice const & sl, bool boundary, int & x, int & y) const
1143 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1145 // y offset correction
1146 int const row = tabular.row_of_cell(sl.idx());
1147 for (int i = 0; i <= row; ++i) {
1149 y += tabular.getAscentOfRow(i);
1150 y += tabular.getAdditionalHeight(i);
1153 y += tabular.getDescentOfRow(i);
1156 // x offset correction
1157 int const col = tabular.column_of_cell(sl.idx());
1158 int idx = tabular.getCellNumber(row, 0);
1159 for (int j = 0; j < col; ++j) {
1160 if (tabular.isPartOfMultiColumn(row, j))
1162 x += tabular.getWidthOfColumn(idx);
1165 x += tabular.getBeginningOfTextInCell(idx);
1166 x += ADD_TO_TABULAR_WIDTH;
1171 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1175 InsetBase const & inset = *tabular.getCellInset(cell);
1176 Point o = bv.coordCache().getInsets().xy(&inset);
1177 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1178 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1179 row_type const row = tabular.row_of_cell(cell);
1180 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1181 tabular.getAdditionalHeight(row);
1182 int const yend = o.y_ + tabular.getDescentOfRow(row);
1194 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1195 // << " ybeg=" << ybeg << " yend=" << yend
1196 // << " xx=" << xx << " yy=" << yy
1197 // << " dist=" << xx + yy << endl;
1202 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1204 //lyxerr << "InsetTabular::editXY: " << this << endl;
1205 cur.selection() = false;
1207 cur.idx() = getNearestCell(cur.bv(), x, y);
1209 return cell(cur.idx())->text_.editXY(cur, x, y);
1213 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1215 cur.idx() = getNearestCell(cur.bv(), x, y);
1216 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1220 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1222 idx_type idx_min = 0;
1223 int dist_min = std::numeric_limits<int>::max();
1224 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1225 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1226 int const d = dist(bv, i, x, y);
1237 int InsetTabular::getCellXPos(idx_type const cell) const
1241 for (; !tabular.isFirstCellInRow(c); --c)
1243 int lx = tabular.getWidthOfColumn(cell);
1244 for (; c < cell; ++c)
1245 lx += tabular.getWidthOfColumn(c);
1247 return lx - tabular.getWidthOfColumn(cell);
1251 void InsetTabular::resetPos(LCursor & cur) const
1253 BufferView & bv = cur.bv();
1254 int const maxwidth = bv.workWidth();
1256 if (&cur.inset() != this) {
1260 int const X2 = maxwidth;
1261 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1262 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1263 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1266 scx_ = X1 + 20 - x1;
1268 scx_ = X2 - 20 - x2;
1273 cur.updateFlags(Update::Force | Update::FitCursor);
1275 InsetTabularMailer(*this).updateDialog(&bv);
1279 void InsetTabular::moveNextCell(LCursor & cur)
1281 if (isRightToLeft(cur)) {
1282 if (tabular.isFirstCellInRow(cur.idx())) {
1283 row_type const row = tabular.row_of_cell(cur.idx());
1284 if (row == tabular.rows() - 1)
1286 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1293 if (tabular.isLastCell(cur.idx()))
1303 void InsetTabular::movePrevCell(LCursor & cur)
1305 if (isRightToLeft(cur)) {
1306 if (tabular.isLastCellInRow(cur.idx())) {
1307 row_type const row = tabular.row_of_cell(cur.idx());
1310 cur.idx() = tabular.getFirstCellInRow(row);
1311 cur.idx() = tabular.getCellAbove(cur.idx());
1313 if (tabular.isLastCell(cur.idx()))
1318 if (cur.idx() == 0) // first cell
1322 cur.pit() = cur.lastpit();
1323 cur.pos() = cur.lastpos();
1325 // FIXME: this accesses the position cache before it is initialized
1330 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1332 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1335 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1336 string const tmp = tabularFeature[i].feature;
1338 if (tmp == what.substr(0, tmp.length())) {
1339 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1340 //tabularFeatures[i].feature.length())) {
1341 action = tabularFeature[i].action;
1345 if (action == LyXTabular::LAST_ACTION)
1349 ltrim(what.substr(tabularFeature[i].feature.length()));
1350 tabularFeatures(cur, action, val);
1357 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1358 string const & special, bool & flag)
1360 if (special == "dl_above") {
1363 } else if (special == "dl_below") {
1364 ltt.bottomDL = flag;
1366 } else if (special == "empty") {
1378 void InsetTabular::tabularFeatures(LCursor & cur,
1379 LyXTabular::Feature feature, string const & value)
1381 BufferView & bv = cur.bv();
1382 col_type sel_col_start;
1383 col_type sel_col_end;
1384 row_type sel_row_start;
1385 row_type sel_row_end;
1386 bool setLines = false;
1387 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1388 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1392 case LyXTabular::M_ALIGN_LEFT:
1393 case LyXTabular::ALIGN_LEFT:
1394 setAlign = LYX_ALIGN_LEFT;
1397 case LyXTabular::M_ALIGN_RIGHT:
1398 case LyXTabular::ALIGN_RIGHT:
1399 setAlign = LYX_ALIGN_RIGHT;
1402 case LyXTabular::M_ALIGN_CENTER:
1403 case LyXTabular::ALIGN_CENTER:
1404 setAlign = LYX_ALIGN_CENTER;
1407 case LyXTabular::ALIGN_BLOCK:
1408 setAlign = LYX_ALIGN_BLOCK;
1411 case LyXTabular::M_VALIGN_TOP:
1412 case LyXTabular::VALIGN_TOP:
1413 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1416 case LyXTabular::M_VALIGN_BOTTOM:
1417 case LyXTabular::VALIGN_BOTTOM:
1418 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1421 case LyXTabular::M_VALIGN_MIDDLE:
1422 case LyXTabular::VALIGN_MIDDLE:
1423 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1430 recordUndoInset(cur, Undo::ATOMIC);
1432 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1433 row_type const row = tabular.row_of_cell(cur.idx());
1434 col_type const column = tabular.column_of_cell(cur.idx());
1436 LyXTabular::ltType ltt;
1440 case LyXTabular::SET_PWIDTH: {
1441 LyXLength const len(value);
1442 tabular.setColumnPWidth(cur, cur.idx(), len);
1444 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1445 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1449 case LyXTabular::SET_MPWIDTH:
1450 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1453 case LyXTabular::SET_SPECIAL_COLUMN:
1454 case LyXTabular::SET_SPECIAL_MULTI:
1455 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
1458 case LyXTabular::APPEND_ROW:
1459 // append the row into the tabular
1460 tabular.appendRow(bv.buffer()->params(), cur.idx());
1463 case LyXTabular::APPEND_COLUMN:
1464 // append the column into the tabular
1465 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1466 cur.idx() = tabular.getCellNumber(row, column);
1469 case LyXTabular::DELETE_ROW:
1470 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1471 tabular.deleteRow(sel_row_start);
1472 if (sel_row_start >= tabular.rows())
1474 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1477 cur.selection() = false;
1480 case LyXTabular::DELETE_COLUMN:
1481 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1482 tabular.deleteColumn(sel_col_start);
1483 if (sel_col_start >= tabular.columns())
1485 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1488 cur.selection() = false;
1491 case LyXTabular::COPY_ROW:
1492 tabular.copyRow(bv.buffer()->params(), row);
1495 case LyXTabular::COPY_COLUMN:
1496 tabular.copyColumn(bv.buffer()->params(), column);
1497 cur.idx() = tabular.getCellNumber(row, column);
1500 case LyXTabular::M_TOGGLE_LINE_TOP:
1502 case LyXTabular::TOGGLE_LINE_TOP: {
1503 bool lineSet = !tabular.topLine(cur.idx(), flag);
1504 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1505 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1507 tabular.getCellNumber(i, j),
1512 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1514 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1515 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1516 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1517 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1518 tabular.setBottomLine(
1519 tabular.getCellNumber(i, j),
1525 case LyXTabular::M_TOGGLE_LINE_LEFT:
1527 case LyXTabular::TOGGLE_LINE_LEFT: {
1528 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1529 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1530 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1531 tabular.setLeftLine(
1532 tabular.getCellNumber(i,j),
1538 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1540 case LyXTabular::TOGGLE_LINE_RIGHT: {
1541 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1542 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1543 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1544 tabular.setRightLine(
1545 tabular.getCellNumber(i,j),
1551 case LyXTabular::M_ALIGN_LEFT:
1552 case LyXTabular::M_ALIGN_RIGHT:
1553 case LyXTabular::M_ALIGN_CENTER:
1555 case LyXTabular::ALIGN_LEFT:
1556 case LyXTabular::ALIGN_RIGHT:
1557 case LyXTabular::ALIGN_CENTER:
1558 case LyXTabular::ALIGN_BLOCK:
1559 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1560 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1561 tabular.setAlignment(
1562 tabular.getCellNumber(i, j),
1567 case LyXTabular::M_VALIGN_TOP:
1568 case LyXTabular::M_VALIGN_BOTTOM:
1569 case LyXTabular::M_VALIGN_MIDDLE:
1571 case LyXTabular::VALIGN_TOP:
1572 case LyXTabular::VALIGN_BOTTOM:
1573 case LyXTabular::VALIGN_MIDDLE:
1574 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1575 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1576 tabular.setVAlignment(
1577 tabular.getCellNumber(i, j),
1581 case LyXTabular::MULTICOLUMN: {
1582 if (sel_row_start != sel_row_end) {
1583 #ifdef WITH_WARNINGS
1584 #warning Need I say it ? This is horrible.
1587 Alert::error(_("Error setting multicolumn"),
1588 _("You cannot set multicolumn vertically."));
1591 if (!cur.selection()) {
1592 // just multicol for one single cell
1593 // check whether we are completely in a multicol
1594 if (tabular.isMultiColumn(cur.idx()))
1595 tabular.unsetMultiColumn(cur.idx());
1597 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1600 // we have a selection so this means we just add all this
1601 // cells to form a multicolumn cell
1602 idx_type const s_start = cur.selBegin().idx();
1603 idx_type const s_end = cur.selEnd().idx();
1604 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1605 cur.idx() = s_start;
1608 cur.selection() = false;
1612 case LyXTabular::SET_ALL_LINES:
1614 case LyXTabular::UNSET_ALL_LINES:
1615 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1616 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1617 tabular.setAllLines(
1618 tabular.getCellNumber(i,j), setLines);
1621 case LyXTabular::SET_LONGTABULAR:
1622 tabular.setLongTabular(true);
1625 case LyXTabular::UNSET_LONGTABULAR:
1626 tabular.setLongTabular(false);
1629 case LyXTabular::SET_ROTATE_TABULAR:
1630 tabular.setRotateTabular(true);
1633 case LyXTabular::UNSET_ROTATE_TABULAR:
1634 tabular.setRotateTabular(false);
1637 case LyXTabular::SET_ROTATE_CELL:
1638 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1639 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1640 tabular.setRotateCell(
1641 tabular.getCellNumber(i, j), true);
1644 case LyXTabular::UNSET_ROTATE_CELL:
1645 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1646 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1647 tabular.setRotateCell(
1648 tabular.getCellNumber(i, j), false);
1651 case LyXTabular::SET_USEBOX: {
1652 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1653 if (val == tabular.getUsebox(cur.idx()))
1654 val = LyXTabular::BOX_NONE;
1655 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1656 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1657 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1661 case LyXTabular::UNSET_LTFIRSTHEAD:
1663 case LyXTabular::SET_LTFIRSTHEAD:
1664 tabular.getRowOfLTFirstHead(row, ltt);
1665 checkLongtableSpecial(ltt, value, flag);
1666 tabular.setLTHead(row, flag, ltt, true);
1669 case LyXTabular::UNSET_LTHEAD:
1671 case LyXTabular::SET_LTHEAD:
1672 tabular.getRowOfLTHead(row, ltt);
1673 checkLongtableSpecial(ltt, value, flag);
1674 tabular.setLTHead(row, flag, ltt, false);
1677 case LyXTabular::UNSET_LTFOOT:
1679 case LyXTabular::SET_LTFOOT:
1680 tabular.getRowOfLTFoot(row, ltt);
1681 checkLongtableSpecial(ltt, value, flag);
1682 tabular.setLTFoot(row, flag, ltt, false);
1685 case LyXTabular::UNSET_LTLASTFOOT:
1687 case LyXTabular::SET_LTLASTFOOT:
1688 tabular.getRowOfLTLastFoot(row, ltt);
1689 checkLongtableSpecial(ltt, value, flag);
1690 tabular.setLTFoot(row, flag, ltt, true);
1693 case LyXTabular::SET_LTNEWPAGE:
1694 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1697 case LyXTabular::SET_BOOKTABS:
1698 tabular.setBookTabs(true);
1701 case LyXTabular::UNSET_BOOKTABS:
1702 tabular.setBookTabs(false);
1705 case LyXTabular::SET_TOP_SPACE: {
1707 if (value == "default")
1708 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1709 tabular.row_info[i].top_space_default = true;
1710 else if (isValidLength(value, &len))
1711 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1712 tabular.row_info[i].top_space_default = false;
1713 tabular.row_info[i].top_space = len;
1716 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1717 tabular.row_info[i].top_space_default = false;
1718 tabular.row_info[i].top_space = len;
1723 case LyXTabular::SET_BOTTOM_SPACE: {
1725 if (value == "default")
1726 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1727 tabular.row_info[i].bottom_space_default = true;
1728 else if (isValidLength(value, &len))
1729 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1730 tabular.row_info[i].bottom_space_default = false;
1731 tabular.row_info[i].bottom_space = len;
1734 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1735 tabular.row_info[i].bottom_space_default = false;
1736 tabular.row_info[i].bottom_space = len;
1741 case LyXTabular::SET_INTERLINE_SPACE: {
1743 if (value == "default")
1744 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1745 tabular.row_info[i].interline_space_default = true;
1746 else if (isValidLength(value, &len))
1747 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1748 tabular.row_info[i].interline_space_default = false;
1749 tabular.row_info[i].interline_space = len;
1752 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1753 tabular.row_info[i].interline_space_default = false;
1754 tabular.row_info[i].interline_space = len;
1759 // dummy stuff just to avoid warnings
1760 case LyXTabular::LAST_ACTION:
1764 InsetTabularMailer(*this).updateDialog(&bv);
1768 bool InsetTabular::showInsetDialog(BufferView * bv) const
1770 InsetTabularMailer(*this).showDialog(bv);
1775 void InsetTabular::openLayoutDialog(BufferView * bv) const
1777 InsetTabularMailer(*this).showDialog(bv);
1781 bool InsetTabular::copySelection(LCursor & cur)
1783 if (!cur.selection())
1788 getSelection(cur, rs, re, cs, ce);
1790 paste_tabular.reset(new LyXTabular(tabular));
1792 for (row_type i = 0; i < rs; ++i)
1793 paste_tabular->deleteRow(0);
1795 row_type const rows = re - rs + 1;
1796 while (paste_tabular->rows() > rows)
1797 paste_tabular->deleteRow(rows);
1799 paste_tabular->setTopLine(0, true, true);
1800 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1803 for (col_type i = 0; i < cs; ++i)
1804 paste_tabular->deleteColumn(0);
1806 col_type const columns = ce - cs + 1;
1807 while (paste_tabular->columns() > columns)
1808 paste_tabular->deleteColumn(columns);
1810 paste_tabular->setLeftLine(0, true, true);
1811 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1814 odocstringstream os;
1815 OutputParams const runparams;
1816 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1817 // Needed for the "Edit->Paste recent" menu and the system clipboard.
1818 cap::copySelection(cur, os.str());
1820 // mark tabular stack dirty
1821 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1822 // when we (hopefully) have a one-for-all paste mechanism.
1823 // This must be called after cap::copySelection.
1824 dirtyTabularStack(true);
1830 bool InsetTabular::pasteSelection(LCursor & cur)
1834 col_type const actcol = tabular.column_of_cell(cur.idx());
1835 row_type const actrow = tabular.row_of_cell(cur.idx());
1836 for (row_type r1 = 0, r2 = actrow;
1837 r1 < paste_tabular->rows() && r2 < tabular.rows();
1839 for (col_type c1 = 0, c2 = actcol;
1840 c1 < paste_tabular->columns() && c2 < tabular.columns();
1842 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1843 tabular.isPartOfMultiColumn(r2, c2))
1845 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1849 if (tabular.isPartOfMultiColumn(r2, c2)) {
1853 shared_ptr<InsetText> inset(
1854 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1855 tabular.setCellInset(r2, c2, inset);
1856 // FIXME: change tracking (MG)
1857 inset->setChange(Change(cur.buffer().params().trackChanges ?
1858 Change::INSERTED : Change::UNCHANGED));
1866 void InsetTabular::cutSelection(LCursor & cur)
1868 if (!cur.selection())
1873 getSelection(cur, rs, re, cs, ce);
1874 for (row_type i = rs; i <= re; ++i) {
1875 for (col_type j = cs; j <= ce; ++j) {
1876 shared_ptr<InsetText> t
1877 = cell(tabular.getCellNumber(i, j));
1878 if (cur.buffer().params().trackChanges)
1879 // FIXME: Change tracking (MG)
1880 t->setChange(Change(Change::DELETED));
1886 // cursor position might be invalid now
1887 if (cur.pit() > cur.lastpit())
1888 cur.pit() = cur.lastpit();
1889 if (cur.pos() > cur.lastpos())
1890 cur.pos() = cur.lastpos();
1891 cur.clearSelection();
1892 theSelection().haveSelection(false);
1896 bool InsetTabular::isRightToLeft(LCursor & cur) const
1898 BOOST_ASSERT(cur.depth() > 1);
1899 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1900 pos_type const parentpos = cur[cur.depth() - 2].pos();
1901 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1902 parentpos).language()->rightToLeft();
1906 void InsetTabular::getSelection(LCursor & cur,
1907 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1909 CursorSlice const & beg = cur.selBegin();
1910 CursorSlice const & end = cur.selEnd();
1911 cs = tabular.column_of_cell(beg.idx());
1912 ce = tabular.column_of_cell(end.idx());
1915 cs = tabular.column_of_cell(end.idx());
1917 ce = tabular.right_column_of_cell(end.idx());
1920 rs = tabular.row_of_cell(beg.idx());
1921 re = tabular.row_of_cell(end.idx());
1927 LyXText * InsetTabular::getText(int idx) const
1929 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1933 void InsetTabular::setChange(Change const & change)
1935 for (idx_type idx = 0; idx < nargs(); ++idx)
1936 cell(idx)->setChange(change);
1940 void InsetTabular::acceptChanges()
1942 for (idx_type idx = 0; idx < nargs(); ++idx)
1943 cell(idx)->acceptChanges();
1947 void InsetTabular::rejectChanges()
1949 for (idx_type idx = 0; idx < nargs(); ++idx)
1950 cell(idx)->rejectChanges();
1954 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1956 return tabular.getPWidth(cell).zero();
1960 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1963 if (buf.length() <= 0)
1966 Buffer const & buffer = *bv.buffer();
1970 col_type maxCols = 1;
1971 docstring::size_type const len = buf.length();
1972 docstring::size_type p = 0;
1975 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
1983 maxCols = max(cols, maxCols);
1989 maxCols = max(cols, maxCols);
1990 LyXTabular * loctab;
1995 paste_tabular.reset(
1996 new LyXTabular(buffer.params(), rows, maxCols));
1997 loctab = paste_tabular.get();
1999 dirtyTabularStack(true);
2002 cell = bv.cursor().idx();
2003 ocol = tabular.column_of_cell(cell);
2004 row = tabular.row_of_cell(cell);
2007 docstring::size_type op = 0;
2008 idx_type const cells = loctab->getNumberOfCells();
2011 rows = loctab->rows();
2012 col_type const columns = loctab->columns();
2014 while (cell < cells && p < len && row < rows &&
2015 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
2021 // we can only set this if we are not too far right
2022 if (cols < columns) {
2023 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2024 Paragraph & par = inset->text_.getPar(0);
2025 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2026 inset->setText(buf.substr(op, p - op), font,
2027 buffer.params().trackChanges);
2033 // we can only set this if we are not too far right
2034 if (cols < columns) {
2035 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2036 Paragraph & par = inset->text_.getPar(0);
2037 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2038 inset->setText(buf.substr(op, p - op), font,
2039 buffer.params().trackChanges);
2044 cell = loctab->getCellNumber(row, cols);
2050 // check for the last cell if there is no trailing '\n'
2051 if (cell < cells && op < len) {
2052 shared_ptr<InsetText> inset = loctab->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, len - op), font,
2056 buffer.params().trackChanges);
2062 void InsetTabular::addPreview(PreviewLoader & loader) const
2064 row_type const rows = tabular.rows();
2065 col_type const columns = tabular.columns();
2066 for (row_type i = 0; i < rows; ++i) {
2067 for (col_type j = 0; j < columns; ++j)
2068 tabular.getCellInset(i, j)->addPreview(loader);
2073 bool InsetTabular::tablemode(LCursor & cur) const
2075 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2082 string const InsetTabularMailer::name_("tabular");
2084 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2085 : inset_(const_cast<InsetTabular &>(inset))
2089 string const InsetTabularMailer::inset2string(Buffer const &) const
2091 return params2string(inset_);
2095 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2097 istringstream data(in);
2099 lex.setStream(data);
2106 if (!lex || token != name_)
2107 return print_mailer_error("InsetTabularMailer", in, 1,
2110 // This is part of the inset proper that is usually swallowed
2111 // by Buffer::readInset
2113 if (!lex || token != "Tabular")
2114 return print_mailer_error("InsetTabularMailer", in, 2,
2117 Buffer const & buffer = inset.buffer();
2118 inset.read(buffer, lex);
2122 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2125 data << name_ << ' ';
2126 inset.write(inset.buffer(), data);
2127 data << "\\end_inset\n";