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)
185 InsetTabular::InsetTabular(InsetTabular const & tab)
186 : InsetOld(tab), tabular(tab.tabular),
187 buffer_(tab.buffer_), scx_(0)
191 InsetTabular::~InsetTabular()
193 InsetTabularMailer(*this).hideDialog();
197 auto_ptr<InsetBase> InsetTabular::doClone() const
199 return auto_ptr<InsetBase>(new InsetTabular(*this));
203 Buffer const & InsetTabular::buffer() const
209 void InsetTabular::buffer(Buffer const * b)
215 void InsetTabular::write(Buffer const & buf, ostream & os) const
217 os << "Tabular" << endl;
218 tabular.write(buf, os);
222 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
224 bool const old_format = (lex.getString() == "\\LyXTable");
226 tabular.read(buf, lex);
232 string token = lex.getString();
233 while (lex.isOK() && (token != "\\end_inset")) {
235 token = lex.getString();
237 if (token != "\\end_inset") {
238 lex.printError("Missing \\end_inset at this point. "
244 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
246 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
247 // mi.base.textwidth << "\n";
249 lyxerr << "InsetTabular::metrics: need bv" << endl;
254 for (idx_type cell = 0; i < tabular.rows(); ++i) {
257 for (col_type j = 0; j < tabular.columns(); ++j) {
258 if (tabular.isPartOfMultiColumn(i, j))
259 // Multicolumn cell, but not first one
264 if (tabular.cell_info[i][j].multicolumn ==
265 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
266 p_width = tabular.cellinfo_of_cell(cell).p_width;
268 p_width = tabular.column_info[j].p_width;
270 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
271 tabular.getCellInset(cell)->metrics(m, dim);
273 dim.wid = m.base.textwidth;
274 tabular.setWidthOfCell(cell, dim.wid);
275 if (p_width.zero()) {
276 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
277 // FIXME there must be a way to get rid of
278 // the second metrics call
279 tabular.getCellInset(cell)->metrics(m, dim);
281 maxAsc = max(maxAsc, dim.asc);
282 maxDesc = max(maxDesc, dim.des);
285 int const top_space = tabular.row_info[i].top_space_default ?
287 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
288 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
289 int const bottom_space = tabular.row_info[i].bottom_space_default ?
291 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
292 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
295 dim.asc = tabular.getAscentOfRow(0);
296 dim.des = tabular.getHeightOfTabular() - dim.asc;
297 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
298 bool const changed = dim_ != dim;
304 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
306 setPosCache(pi, x, y);
308 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
309 BufferView * bv = pi.base.bv;
311 resetPos(bv->cursor());
314 x += ADD_TO_TABULAR_WIDTH;
317 first_visible_cell = LyXTabular::npos;
318 for (row_type i = 0; i < tabular.rows(); ++i) {
320 int const a = tabular.getAscentOfRow(i);
321 int const d = tabular.getDescentOfRow(i);
322 idx = tabular.getCellNumber(i, 0);
323 for (col_type j = 0; j < tabular.columns(); ++j) {
324 if (tabular.isPartOfMultiColumn(i, j))
326 if (first_visible_cell == LyXTabular::npos)
327 first_visible_cell = idx;
329 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
330 if (nx + tabular.getWidthOfColumn(idx) < 0
331 || nx > bv->workWidth()
333 || y - a > bv->workHeight()) {
334 pi.pain.setDrawingEnabled(false);
335 cell(idx)->draw(pi, cx, y);
336 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
337 pi.pain.setDrawingEnabled(true);
339 cell(idx)->draw(pi, cx, y);
340 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
342 nx += tabular.getWidthOfColumn(idx);
346 if (i + 1 < tabular.rows())
347 y += d + tabular.getAscentOfRow(i + 1) +
348 tabular.getAdditionalHeight(i + 1);
353 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
355 setPosCache(pi, x, y);
357 LCursor & cur = pi.base.bv->cursor();
359 x += scx_ + ADD_TO_TABULAR_WIDTH;
361 // Paint background of current tabular
362 int const w = tabular.getWidthOfTabular();
363 int const h = tabular.getHeightOfTabular();
364 int yy = y - tabular.getAscentOfRow(0);
365 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
367 if (!cur.selection())
369 if (!ptr_cmp(&cur.inset(), this))
375 if (tablemode(cur)) {
378 getSelection(cur, rs, re, cs, ce);
379 y -= tabular.getAscentOfRow(0);
380 for (row_type j = 0; j < tabular.rows(); ++j) {
381 int const a = tabular.getAscentOfRow(j);
382 int const h = a + tabular.getDescentOfRow(j);
384 y += tabular.getAdditionalHeight(j);
385 for (col_type i = 0; i < tabular.columns(); ++i) {
386 if (tabular.isPartOfMultiColumn(j, i))
388 idx_type const cell =
389 tabular.getCellNumber(j, i);
390 int const w = tabular.getWidthOfColumn(cell);
391 if (i >= cs && i <= ce && j >= rs && j <= re)
392 pi.pain.fillRectangle(xx, y, w, h,
401 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
406 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
407 row_type row, idx_type cell, bool erased) const
409 int x2 = x + tabular.getWidthOfColumn(cell);
411 LColor::color col = LColor::tabularline;
412 LColor::color onoffcol = LColor::tabularonoffline;
415 col = LColor::strikeout;
416 onoffcol = LColor::strikeout;
419 if (!tabular.topAlreadyDrawn(cell)) {
420 on_off = !tabular.topLine(cell);
421 pain.line(x, y - tabular.getAscentOfRow(row),
422 x2, y - tabular.getAscentOfRow(row),
423 on_off ? onoffcol : col,
424 on_off ? Painter::line_onoffdash : Painter::line_solid);
426 on_off = !tabular.bottomLine(cell);
427 pain.line(x, y + tabular.getDescentOfRow(row),
428 x2, y + tabular.getDescentOfRow(row),
429 on_off ? onoffcol : col,
430 on_off ? Painter::line_onoffdash : Painter::line_solid);
431 if (!tabular.leftAlreadyDrawn(cell)) {
432 on_off = !tabular.leftLine(cell);
433 pain.line(x, y - tabular.getAscentOfRow(row),
434 x, y + tabular.getDescentOfRow(row),
435 on_off ? onoffcol : col,
436 on_off ? Painter::line_onoffdash : Painter::line_solid);
438 on_off = !tabular.rightLine(cell);
439 pain.line(x2 - tabular.getAdditionalWidth(cell),
440 y - tabular.getAscentOfRow(row),
441 x2 - tabular.getAdditionalWidth(cell),
442 y + tabular.getDescentOfRow(row),
443 on_off ? onoffcol : col,
444 on_off ? Painter::line_onoffdash : Painter::line_solid);
448 docstring const InsetTabular::editMessage() const
450 return _("Opened table");
454 void InsetTabular::edit(LCursor & cur, bool left)
456 //lyxerr << "InsetTabular::edit: " << this << endl;
458 cur.selection() = false;
461 if (isRightToLeft(cur))
462 cur.idx() = tabular.getLastCellInRow(0);
468 if (isRightToLeft(cur))
469 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
471 cur.idx() = tabular.getNumberOfCells() - 1;
473 cur.pos() = cur.lastpos(); // FIXME crude guess
475 // FIXME: this accesses the position cache before it is initialized
477 //cur.bv().fitCursor();
481 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
483 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
484 << "\n cur:" << cur << endl;
485 CursorSlice sl = cur.top();
486 LCursor & bvcur = cur.bv().cursor();
488 switch (cmd.action) {
490 case LFUN_MOUSE_PRESS:
491 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
493 if (cmd.button() == mouse_button::button1
494 || (cmd.button() == mouse_button::button3
495 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
496 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
498 cur.selection() = false;
499 setCursorFromCoordinates(cur, cmd.x, cmd.y);
500 cur.bv().mouseSetCursor(cur);
504 if (cmd.button() == mouse_button::button2) {
505 if (cap::selection()) {
506 // See comment in LyXText::dispatch why we
508 // FIXME This does not use paste_tabular,
509 // another reason why paste_tabular should go.
510 cap::copySelectionToStack();
511 cmd = FuncRequest(LFUN_PASTE, "0");
513 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
516 doDispatch(cur, cmd);
520 case LFUN_MOUSE_MOTION:
521 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
522 if (cmd.button() == mouse_button::button1) {
523 // only accept motions to places not deeper nested than the real anchor
524 if (bvcur.anchor_.hasPart(cur)) {
525 // only update if selection changes
526 if (bvcur.idx() == cur.idx() &&
527 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
529 setCursorFromCoordinates(cur, cmd.x, cmd.y);
530 bvcur.setCursor(cur);
531 bvcur.selection() = true;
537 case LFUN_MOUSE_RELEASE:
538 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
539 if (cmd.button() == mouse_button::button1) {
540 if (bvcur.selection())
541 saveSelection(bvcur);// theSelection().haveSelection(true);
542 } else if (cmd.button() == mouse_button::button3)
543 InsetTabularMailer(*this).showDialog(&cur.bv());
546 case LFUN_CELL_BACKWARD:
548 cur.selection() = false;
552 case LFUN_CELL_FORWARD:
554 cur.selection() = false;
558 case LFUN_CHAR_FORWARD_SELECT:
559 case LFUN_CHAR_FORWARD:
560 cell(cur.idx())->dispatch(cur, cmd);
561 if (!cur.result().dispatched()) {
562 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
563 if (cmd.action == LFUN_CHAR_FORWARD_SELECT)
566 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
572 case LFUN_CHAR_BACKWARD_SELECT:
573 case LFUN_CHAR_BACKWARD:
574 cell(cur.idx())->dispatch(cur, cmd);
575 if (!cur.result().dispatched()) {
576 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
577 if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
580 cmd = FuncRequest(LFUN_FINISHED_LEFT);
586 case LFUN_DOWN_SELECT:
588 cell(cur.idx())->dispatch(cur, cmd);
589 cur.dispatched(); // override the cell's decision
591 // if our LyXText didn't do anything to the cursor
592 // then we try to put the cursor into the cell below
593 // setting also the right targetX.
594 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
595 cur.idx() = tabular.getCellBelow(cur.idx());
597 TextMetrics const & tm =
598 cur.bv().textMetrics(cell(cur.idx())->getText(0));
599 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
600 if (cmd.action == LFUN_DOWN_SELECT)
603 if (sl == cur.top()) {
604 // we trick it to go to the RIGHT after leaving the
606 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
613 cell(cur.idx())->dispatch(cur, cmd);
614 cur.dispatched(); // override the cell's decision
616 // if our LyXText didn't do anything to the cursor
617 // then we try to put the cursor into the cell above
618 // setting also the right targetX.
619 if (tabular.row_of_cell(cur.idx()) != 0) {
620 cur.idx() = tabular.getCellAbove(cur.idx());
621 cur.pit() = cur.lastpit();
622 LyXText const * text = cell(cur.idx())->getText(0);
623 TextMetrics const & tm = cur.bv().textMetrics(text);
624 ParagraphMetrics const & pm =
625 tm.parMetrics(cur.lastpit());
626 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
627 if (cmd.action == LFUN_UP_SELECT)
630 if (sl == cur.top()) {
631 cmd = FuncRequest(LFUN_FINISHED_UP);
636 // case LFUN_SCREEN_DOWN: {
637 // //if (hasSelection())
638 // // cur.selection() = false;
639 // col_type const col = tabular.column_of_cell(cur.idx());
640 // int const t = cur.bv().top_y() + cur.bv().height();
641 // if (t < yo() + tabular.getHeightOfTabular()) {
642 // cur.bv().scrollDocView(t);
643 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
645 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
652 // case LFUN_SCREEN_UP: {
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();
658 // cur.bv().scrollDocView(t);
662 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
666 // cur.par() = cur.lastpar();
667 // cur.pos() = cur.lastpos();
671 case LFUN_LAYOUT_TABULAR:
672 InsetTabularMailer(*this).showDialog(&cur.bv());
675 case LFUN_INSET_DIALOG_UPDATE:
676 InsetTabularMailer(*this).updateDialog(&cur.bv());
679 case LFUN_TABULAR_FEATURE:
680 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
684 // insert file functions
685 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
686 case LFUN_FILE_INSERT_PLAINTEXT: {
688 string const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
689 // FIXME: We don't know the encoding of the file
690 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), from_utf8(tmpstr), false))
696 if (tablemode(cur)) {
697 if (copySelection(cur)) {
698 recordUndoInset(cur, Undo::DELETE);
703 cell(cur.idx())->dispatch(cur, cmd);
706 case LFUN_CHAR_DELETE_BACKWARD:
707 case LFUN_CHAR_DELETE_FORWARD:
708 if (tablemode(cur)) {
709 recordUndoInset(cur, Undo::DELETE);
713 cell(cur.idx())->dispatch(cur, cmd);
717 if (!cur.selection())
719 if (tablemode(cur)) {
723 cell(cur.idx())->dispatch(cur, cmd);
726 case LFUN_CLIPBOARD_PASTE:
727 case LFUN_PRIMARY_SELECTION_PASTE: {
728 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
729 theClipboard().getAsText() :
730 theSelection().get();
733 // pass to InsertPlaintextString, but
734 // only if we have multi-cell content
735 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
736 if (insertPlaintextString(cur.bv(), clip, false)) {
737 // content has been replaced,
738 // so cursor might be invalid
739 cur.pos() = cur.lastpos();
740 bvcur.setCursor(cur);
744 // Let the cell handle normal text
745 cell(cur.idx())->dispatch(cur, cmd);
750 if (tabularStackDirty() && theClipboard().isInternal()) {
751 recordUndoInset(cur, Undo::INSERT);
755 cell(cur.idx())->dispatch(cur, cmd);
760 case LFUN_FONT_ROMAN:
766 case LFUN_FONT_FREE_APPLY:
767 case LFUN_FONT_FREE_UPDATE:
769 case LFUN_FONT_UNDERLINE:
771 case LFUN_WORD_CAPITALIZE:
772 case LFUN_WORD_UPCASE:
773 case LFUN_WORD_LOWCASE:
774 case LFUN_CHARS_TRANSPOSE:
775 if (tablemode(cur)) {
778 getSelection(cur, rs, re, cs, ce);
779 LCursor tmpcur = cur;
780 for (row_type i = rs; i <= re; ++i) {
781 for (col_type j = cs; j <= ce; ++j) {
782 // cursor follows cell:
783 tmpcur.idx() = tabular.getCellNumber(i, j);
784 // select this cell only:
787 tmpcur.resetAnchor();
788 tmpcur.pit() = tmpcur.lastpit();
789 tmpcur.pos() = tmpcur.top().lastpos();
790 tmpcur.setCursor(tmpcur);
791 tmpcur.setSelection();
792 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
797 cell(cur.idx())->dispatch(cur, cmd);
801 // we try to handle this event in the insets dispatch function.
802 cell(cur.idx())->dispatch(cur, cmd);
806 // FIXME: this accesses the position cache before it is initialized
808 InsetTabularMailer(*this).updateDialog(&cur.bv());
812 // function sets an object as defined in func_status.h:
813 // states OK, Unknown, Disabled, On, Off.
814 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
815 FuncStatus & status) const
817 switch (cmd.action) {
818 case LFUN_TABULAR_FEATURE: {
819 int action = LyXTabular::LAST_ACTION;
821 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
822 string const tmp = tabularFeature[i].feature;
823 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
824 action = tabularFeature[i].action;
828 if (action == LyXTabular::LAST_ACTION) {
830 status.unknown(true);
834 string const argument
835 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
837 row_type sel_row_start = 0;
838 row_type sel_row_end = 0;
840 LyXTabular::ltType dummyltt;
843 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
846 case LyXTabular::SET_PWIDTH:
847 case LyXTabular::SET_MPWIDTH:
848 case LyXTabular::SET_SPECIAL_COLUMN:
849 case LyXTabular::SET_SPECIAL_MULTI:
850 case LyXTabular::APPEND_ROW:
851 case LyXTabular::APPEND_COLUMN:
852 case LyXTabular::DELETE_ROW:
853 case LyXTabular::DELETE_COLUMN:
854 case LyXTabular::COPY_ROW:
855 case LyXTabular::COPY_COLUMN:
856 case LyXTabular::SET_ALL_LINES:
857 case LyXTabular::UNSET_ALL_LINES:
858 case LyXTabular::SET_TOP_SPACE:
859 case LyXTabular::SET_BOTTOM_SPACE:
860 case LyXTabular::SET_INTERLINE_SPACE:
864 case LyXTabular::MULTICOLUMN:
865 status.setOnOff(tabular.isMultiColumn(cur.idx()));
868 case LyXTabular::M_TOGGLE_LINE_TOP:
870 case LyXTabular::TOGGLE_LINE_TOP:
871 status.setOnOff(tabular.topLine(cur.idx(), flag));
874 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
876 case LyXTabular::TOGGLE_LINE_BOTTOM:
877 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
880 case LyXTabular::M_TOGGLE_LINE_LEFT:
882 case LyXTabular::TOGGLE_LINE_LEFT:
883 status.setOnOff(tabular.leftLine(cur.idx(), flag));
886 case LyXTabular::M_TOGGLE_LINE_RIGHT:
888 case LyXTabular::TOGGLE_LINE_RIGHT:
889 status.setOnOff(tabular.rightLine(cur.idx(), flag));
892 case LyXTabular::M_ALIGN_LEFT:
894 case LyXTabular::ALIGN_LEFT:
895 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
898 case LyXTabular::M_ALIGN_RIGHT:
900 case LyXTabular::ALIGN_RIGHT:
901 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
904 case LyXTabular::M_ALIGN_CENTER:
906 case LyXTabular::ALIGN_CENTER:
907 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
910 case LyXTabular::ALIGN_BLOCK:
911 status.enabled(!tabular.getPWidth(cur.idx()).zero());
912 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
915 case LyXTabular::M_VALIGN_TOP:
917 case LyXTabular::VALIGN_TOP:
919 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
922 case LyXTabular::M_VALIGN_BOTTOM:
924 case LyXTabular::VALIGN_BOTTOM:
926 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
929 case LyXTabular::M_VALIGN_MIDDLE:
931 case LyXTabular::VALIGN_MIDDLE:
933 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
936 case LyXTabular::SET_LONGTABULAR:
937 status.setOnOff(tabular.isLongTabular());
940 case LyXTabular::UNSET_LONGTABULAR:
941 status.setOnOff(!tabular.isLongTabular());
944 case LyXTabular::SET_ROTATE_TABULAR:
945 status.setOnOff(tabular.getRotateTabular());
948 case LyXTabular::UNSET_ROTATE_TABULAR:
949 status.setOnOff(!tabular.getRotateTabular());
952 case LyXTabular::SET_ROTATE_CELL:
953 status.setOnOff(tabular.getRotateCell(cur.idx()));
956 case LyXTabular::UNSET_ROTATE_CELL:
957 status.setOnOff(!tabular.getRotateCell(cur.idx()));
960 case LyXTabular::SET_USEBOX:
961 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
964 case LyXTabular::SET_LTFIRSTHEAD:
965 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
968 case LyXTabular::UNSET_LTFIRSTHEAD:
969 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
972 case LyXTabular::SET_LTHEAD:
973 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
976 case LyXTabular::UNSET_LTHEAD:
977 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
980 case LyXTabular::SET_LTFOOT:
981 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
984 case LyXTabular::UNSET_LTFOOT:
985 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
988 case LyXTabular::SET_LTLASTFOOT:
989 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
992 case LyXTabular::UNSET_LTLASTFOOT:
993 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
996 case LyXTabular::SET_LTNEWPAGE:
997 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1000 case LyXTabular::SET_BOOKTABS:
1001 status.setOnOff(tabular.useBookTabs());
1004 case LyXTabular::UNSET_BOOKTABS:
1005 status.setOnOff(!tabular.useBookTabs());
1010 status.enabled(false);
1016 // These are only enabled inside tabular
1017 case LFUN_CELL_BACKWARD:
1018 case LFUN_CELL_FORWARD:
1019 status.enabled(true);
1022 // disable these with multiple cells selected
1023 case LFUN_INSET_INSERT:
1024 case LFUN_TABULAR_INSERT:
1025 case LFUN_CHARSTYLE_INSERT:
1026 case LFUN_FLOAT_INSERT:
1027 case LFUN_FLOAT_WIDE_INSERT:
1028 case LFUN_FOOTNOTE_INSERT:
1029 case LFUN_MARGINALNOTE_INSERT:
1030 case LFUN_MATH_INSERT:
1031 case LFUN_MATH_MODE:
1032 case LFUN_MATH_MUTATE:
1033 case LFUN_MATH_DISPLAY:
1034 case LFUN_NOTE_INSERT:
1035 case LFUN_OPTIONAL_INSERT:
1036 case LFUN_BOX_INSERT:
1037 case LFUN_BRANCH_INSERT:
1038 case LFUN_WRAP_INSERT:
1039 case LFUN_ERT_INSERT: {
1040 if (tablemode(cur)) {
1041 status.enabled(false);
1044 return cell(cur.idx())->getStatus(cur, cmd, status);
1047 // disable in non-fixed-width cells
1048 case LFUN_BREAK_LINE:
1049 case LFUN_BREAK_PARAGRAPH:
1050 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1051 case LFUN_BREAK_PARAGRAPH_SKIP: {
1052 if (tabular.getPWidth(cur.idx()).zero()) {
1053 status.enabled(false);
1056 return cell(cur.idx())->getStatus(cur, cmd, status);
1060 if (tabularStackDirty() && theClipboard().isInternal()) {
1061 status.enabled(true);
1064 return cell(cur.idx())->getStatus(cur, cmd, status);
1066 case LFUN_INSET_MODIFY:
1067 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1068 status.enabled(true);
1074 // we try to handle this event in the insets dispatch function.
1075 return cell(cur.idx())->getStatus(cur, cmd, status);
1080 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1081 OutputParams const & runparams) const
1083 return tabular.latex(buf, os, runparams);
1087 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1088 OutputParams const & runparams) const
1090 int const dp = runparams.linelen ? runparams.depth : 0;
1091 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1095 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1096 OutputParams const & runparams) const
1099 InsetBase * master = 0;
1101 #ifdef WITH_WARNINGS
1102 #warning Why not pass a proper DocIterator here?
1105 // if the table is inside a float it doesn't need the informaltable
1106 // wrapper. Search for it.
1107 for (master = owner(); master; master = master->owner())
1108 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1113 os << "<informaltable>";
1116 ret += tabular.docbook(buf, os, runparams);
1118 os << "</informaltable>";
1125 void InsetTabular::validate(LaTeXFeatures & features) const
1127 tabular.validate(features);
1131 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1133 return tabular.getCellInset(idx);
1137 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1139 return tabular.getCellInset(idx);
1143 void InsetTabular::cursorPos(BufferView const & bv,
1144 CursorSlice const & sl, bool boundary, int & x, int & y) const
1146 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1148 // y offset correction
1149 int const row = tabular.row_of_cell(sl.idx());
1150 for (int i = 0; i <= row; ++i) {
1152 y += tabular.getAscentOfRow(i);
1153 y += tabular.getAdditionalHeight(i);
1156 y += tabular.getDescentOfRow(i);
1159 // x offset correction
1160 int const col = tabular.column_of_cell(sl.idx());
1161 int idx = tabular.getCellNumber(row, 0);
1162 for (int j = 0; j < col; ++j) {
1163 if (tabular.isPartOfMultiColumn(row, j))
1165 x += tabular.getWidthOfColumn(idx);
1168 x += tabular.getBeginningOfTextInCell(idx);
1169 x += ADD_TO_TABULAR_WIDTH;
1174 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1178 InsetBase const & inset = *tabular.getCellInset(cell);
1179 Point o = bv.coordCache().getInsets().xy(&inset);
1180 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1181 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1182 row_type const row = tabular.row_of_cell(cell);
1183 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1184 tabular.getAdditionalHeight(row);
1185 int const yend = o.y_ + tabular.getDescentOfRow(row);
1197 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1198 // << " ybeg=" << ybeg << " yend=" << yend
1199 // << " xx=" << xx << " yy=" << yy
1200 // << " dist=" << xx + yy << endl;
1205 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1207 //lyxerr << "InsetTabular::editXY: " << this << endl;
1208 cur.selection() = false;
1210 cur.idx() = getNearestCell(cur.bv(), x, y);
1212 return cell(cur.idx())->text_.editXY(cur, x, y);
1216 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1218 cur.idx() = getNearestCell(cur.bv(), x, y);
1219 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1223 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1225 idx_type idx_min = 0;
1226 int dist_min = std::numeric_limits<int>::max();
1227 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1228 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1229 int const d = dist(bv, i, x, y);
1240 int InsetTabular::getCellXPos(idx_type const cell) const
1244 for (; !tabular.isFirstCellInRow(c); --c)
1246 int lx = tabular.getWidthOfColumn(cell);
1247 for (; c < cell; ++c)
1248 lx += tabular.getWidthOfColumn(c);
1250 return lx - tabular.getWidthOfColumn(cell);
1254 void InsetTabular::resetPos(LCursor & cur) const
1256 BufferView & bv = cur.bv();
1257 int const maxwidth = bv.workWidth();
1259 if (&cur.inset() != this) {
1263 int const X2 = maxwidth;
1264 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1265 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1266 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1269 scx_ = X1 + 20 - x1;
1271 scx_ = X2 - 20 - x2;
1276 cur.updateFlags(Update::Force | Update::FitCursor);
1280 void InsetTabular::moveNextCell(LCursor & cur)
1282 if (isRightToLeft(cur)) {
1283 if (tabular.isFirstCellInRow(cur.idx())) {
1284 row_type const row = tabular.row_of_cell(cur.idx());
1285 if (row == tabular.rows() - 1)
1287 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1294 if (tabular.isLastCell(cur.idx()))
1304 void InsetTabular::movePrevCell(LCursor & cur)
1306 if (isRightToLeft(cur)) {
1307 if (tabular.isLastCellInRow(cur.idx())) {
1308 row_type const row = tabular.row_of_cell(cur.idx());
1311 cur.idx() = tabular.getFirstCellInRow(row);
1312 cur.idx() = tabular.getCellAbove(cur.idx());
1314 if (tabular.isLastCell(cur.idx()))
1319 if (cur.idx() == 0) // first cell
1323 cur.pit() = cur.lastpit();
1324 cur.pos() = cur.lastpos();
1326 // FIXME: this accesses the position cache before it is initialized
1331 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1333 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1336 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1337 string const tmp = tabularFeature[i].feature;
1339 if (tmp == what.substr(0, tmp.length())) {
1340 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1341 //tabularFeatures[i].feature.length())) {
1342 action = tabularFeature[i].action;
1346 if (action == LyXTabular::LAST_ACTION)
1350 ltrim(what.substr(tabularFeature[i].feature.length()));
1351 tabularFeatures(cur, action, val);
1358 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1359 string const & special, bool & flag)
1361 if (special == "dl_above") {
1364 } else if (special == "dl_below") {
1365 ltt.bottomDL = flag;
1367 } else if (special == "empty") {
1379 void InsetTabular::tabularFeatures(LCursor & cur,
1380 LyXTabular::Feature feature, string const & value)
1382 BufferView & bv = cur.bv();
1383 col_type sel_col_start;
1384 col_type sel_col_end;
1385 row_type sel_row_start;
1386 row_type sel_row_end;
1387 bool setLines = false;
1388 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1389 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1393 case LyXTabular::M_ALIGN_LEFT:
1394 case LyXTabular::ALIGN_LEFT:
1395 setAlign = LYX_ALIGN_LEFT;
1398 case LyXTabular::M_ALIGN_RIGHT:
1399 case LyXTabular::ALIGN_RIGHT:
1400 setAlign = LYX_ALIGN_RIGHT;
1403 case LyXTabular::M_ALIGN_CENTER:
1404 case LyXTabular::ALIGN_CENTER:
1405 setAlign = LYX_ALIGN_CENTER;
1408 case LyXTabular::ALIGN_BLOCK:
1409 setAlign = LYX_ALIGN_BLOCK;
1412 case LyXTabular::M_VALIGN_TOP:
1413 case LyXTabular::VALIGN_TOP:
1414 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1417 case LyXTabular::M_VALIGN_BOTTOM:
1418 case LyXTabular::VALIGN_BOTTOM:
1419 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1422 case LyXTabular::M_VALIGN_MIDDLE:
1423 case LyXTabular::VALIGN_MIDDLE:
1424 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1431 recordUndoInset(cur, Undo::ATOMIC);
1433 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1434 row_type const row = tabular.row_of_cell(cur.idx());
1435 col_type const column = tabular.column_of_cell(cur.idx());
1437 LyXTabular::ltType ltt;
1441 case LyXTabular::SET_PWIDTH: {
1442 LyXLength const len(value);
1443 tabular.setColumnPWidth(cur, cur.idx(), len);
1445 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1446 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1450 case LyXTabular::SET_MPWIDTH:
1451 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1454 case LyXTabular::SET_SPECIAL_COLUMN:
1455 case LyXTabular::SET_SPECIAL_MULTI:
1456 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
1459 case LyXTabular::APPEND_ROW:
1460 // append the row into the tabular
1461 tabular.appendRow(bv.buffer()->params(), cur.idx());
1464 case LyXTabular::APPEND_COLUMN:
1465 // append the column into the tabular
1466 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1467 cur.idx() = tabular.getCellNumber(row, column);
1470 case LyXTabular::DELETE_ROW:
1471 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1472 tabular.deleteRow(sel_row_start);
1473 if (sel_row_start >= tabular.rows())
1475 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1478 cur.selection() = false;
1481 case LyXTabular::DELETE_COLUMN:
1482 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1483 tabular.deleteColumn(sel_col_start);
1484 if (sel_col_start >= tabular.columns())
1486 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1489 cur.selection() = false;
1492 case LyXTabular::COPY_ROW:
1493 tabular.copyRow(bv.buffer()->params(), row);
1496 case LyXTabular::COPY_COLUMN:
1497 tabular.copyColumn(bv.buffer()->params(), column);
1498 cur.idx() = tabular.getCellNumber(row, column);
1501 case LyXTabular::M_TOGGLE_LINE_TOP:
1503 case LyXTabular::TOGGLE_LINE_TOP: {
1504 bool lineSet = !tabular.topLine(cur.idx(), flag);
1505 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1506 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508 tabular.getCellNumber(i, j),
1513 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1515 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1516 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1517 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1518 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1519 tabular.setBottomLine(
1520 tabular.getCellNumber(i, j),
1526 case LyXTabular::M_TOGGLE_LINE_LEFT:
1528 case LyXTabular::TOGGLE_LINE_LEFT: {
1529 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1530 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1531 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1532 tabular.setLeftLine(
1533 tabular.getCellNumber(i,j),
1539 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1541 case LyXTabular::TOGGLE_LINE_RIGHT: {
1542 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1543 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1544 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1545 tabular.setRightLine(
1546 tabular.getCellNumber(i,j),
1552 case LyXTabular::M_ALIGN_LEFT:
1553 case LyXTabular::M_ALIGN_RIGHT:
1554 case LyXTabular::M_ALIGN_CENTER:
1556 case LyXTabular::ALIGN_LEFT:
1557 case LyXTabular::ALIGN_RIGHT:
1558 case LyXTabular::ALIGN_CENTER:
1559 case LyXTabular::ALIGN_BLOCK:
1560 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1561 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1562 tabular.setAlignment(
1563 tabular.getCellNumber(i, j),
1568 case LyXTabular::M_VALIGN_TOP:
1569 case LyXTabular::M_VALIGN_BOTTOM:
1570 case LyXTabular::M_VALIGN_MIDDLE:
1572 case LyXTabular::VALIGN_TOP:
1573 case LyXTabular::VALIGN_BOTTOM:
1574 case LyXTabular::VALIGN_MIDDLE:
1575 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1576 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1577 tabular.setVAlignment(
1578 tabular.getCellNumber(i, j),
1582 case LyXTabular::MULTICOLUMN: {
1583 if (sel_row_start != sel_row_end) {
1584 #ifdef WITH_WARNINGS
1585 #warning Need I say it ? This is horrible.
1588 Alert::error(_("Error setting multicolumn"),
1589 _("You cannot set multicolumn vertically."));
1592 if (!cur.selection()) {
1593 // just multicol for one single cell
1594 // check whether we are completely in a multicol
1595 if (tabular.isMultiColumn(cur.idx()))
1596 tabular.unsetMultiColumn(cur.idx());
1598 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1601 // we have a selection so this means we just add all this
1602 // cells to form a multicolumn cell
1603 idx_type const s_start = cur.selBegin().idx();
1604 idx_type const s_end = cur.selEnd().idx();
1605 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1606 cur.idx() = s_start;
1609 cur.selection() = false;
1613 case LyXTabular::SET_ALL_LINES:
1615 case LyXTabular::UNSET_ALL_LINES:
1616 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1617 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1618 tabular.setAllLines(
1619 tabular.getCellNumber(i,j), setLines);
1622 case LyXTabular::SET_LONGTABULAR:
1623 tabular.setLongTabular(true);
1626 case LyXTabular::UNSET_LONGTABULAR:
1627 tabular.setLongTabular(false);
1630 case LyXTabular::SET_ROTATE_TABULAR:
1631 tabular.setRotateTabular(true);
1634 case LyXTabular::UNSET_ROTATE_TABULAR:
1635 tabular.setRotateTabular(false);
1638 case LyXTabular::SET_ROTATE_CELL:
1639 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1640 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1641 tabular.setRotateCell(
1642 tabular.getCellNumber(i, j), true);
1645 case LyXTabular::UNSET_ROTATE_CELL:
1646 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1647 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1648 tabular.setRotateCell(
1649 tabular.getCellNumber(i, j), false);
1652 case LyXTabular::SET_USEBOX: {
1653 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1654 if (val == tabular.getUsebox(cur.idx()))
1655 val = LyXTabular::BOX_NONE;
1656 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1657 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1658 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1662 case LyXTabular::UNSET_LTFIRSTHEAD:
1664 case LyXTabular::SET_LTFIRSTHEAD:
1665 tabular.getRowOfLTFirstHead(row, ltt);
1666 checkLongtableSpecial(ltt, value, flag);
1667 tabular.setLTHead(row, flag, ltt, true);
1670 case LyXTabular::UNSET_LTHEAD:
1672 case LyXTabular::SET_LTHEAD:
1673 tabular.getRowOfLTHead(row, ltt);
1674 checkLongtableSpecial(ltt, value, flag);
1675 tabular.setLTHead(row, flag, ltt, false);
1678 case LyXTabular::UNSET_LTFOOT:
1680 case LyXTabular::SET_LTFOOT:
1681 tabular.getRowOfLTFoot(row, ltt);
1682 checkLongtableSpecial(ltt, value, flag);
1683 tabular.setLTFoot(row, flag, ltt, false);
1686 case LyXTabular::UNSET_LTLASTFOOT:
1688 case LyXTabular::SET_LTLASTFOOT:
1689 tabular.getRowOfLTLastFoot(row, ltt);
1690 checkLongtableSpecial(ltt, value, flag);
1691 tabular.setLTFoot(row, flag, ltt, true);
1694 case LyXTabular::SET_LTNEWPAGE:
1695 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1698 case LyXTabular::SET_BOOKTABS:
1699 tabular.setBookTabs(true);
1702 case LyXTabular::UNSET_BOOKTABS:
1703 tabular.setBookTabs(false);
1706 case LyXTabular::SET_TOP_SPACE: {
1708 if (value == "default")
1709 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1710 tabular.row_info[i].top_space_default = true;
1711 else if (isValidLength(value, &len))
1712 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1713 tabular.row_info[i].top_space_default = false;
1714 tabular.row_info[i].top_space = len;
1717 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1718 tabular.row_info[i].top_space_default = false;
1719 tabular.row_info[i].top_space = len;
1724 case LyXTabular::SET_BOTTOM_SPACE: {
1726 if (value == "default")
1727 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1728 tabular.row_info[i].bottom_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].bottom_space_default = false;
1732 tabular.row_info[i].bottom_space = len;
1735 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1736 tabular.row_info[i].bottom_space_default = false;
1737 tabular.row_info[i].bottom_space = len;
1742 case LyXTabular::SET_INTERLINE_SPACE: {
1744 if (value == "default")
1745 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1746 tabular.row_info[i].interline_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].interline_space_default = false;
1750 tabular.row_info[i].interline_space = len;
1753 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1754 tabular.row_info[i].interline_space_default = false;
1755 tabular.row_info[i].interline_space = len;
1760 // dummy stuff just to avoid warnings
1761 case LyXTabular::LAST_ACTION:
1767 bool InsetTabular::showInsetDialog(BufferView * bv) const
1769 InsetTabularMailer(*this).showDialog(bv);
1774 void InsetTabular::openLayoutDialog(BufferView * bv) const
1776 InsetTabularMailer(*this).showDialog(bv);
1780 bool InsetTabular::copySelection(LCursor & cur)
1782 if (!cur.selection())
1787 getSelection(cur, rs, re, cs, ce);
1789 paste_tabular.reset(new LyXTabular(tabular));
1791 for (row_type i = 0; i < rs; ++i)
1792 paste_tabular->deleteRow(0);
1794 row_type const rows = re - rs + 1;
1795 while (paste_tabular->rows() > rows)
1796 paste_tabular->deleteRow(rows);
1798 paste_tabular->setTopLine(0, true, true);
1799 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1802 for (col_type i = 0; i < cs; ++i)
1803 paste_tabular->deleteColumn(0);
1805 col_type const columns = ce - cs + 1;
1806 while (paste_tabular->columns() > columns)
1807 paste_tabular->deleteColumn(columns);
1809 paste_tabular->setLeftLine(0, true, true);
1810 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1813 odocstringstream os;
1814 OutputParams const runparams;
1815 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1816 // Needed for the "Edit->Paste recent" menu and the system clipboard.
1817 cap::copySelection(cur, os.str());
1819 // mark tabular stack dirty
1820 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1821 // when we (hopefully) have a one-for-all paste mechanism.
1822 // This must be called after cap::copySelection.
1823 dirtyTabularStack(true);
1829 bool InsetTabular::pasteClipboard(LCursor & cur)
1833 col_type const actcol = tabular.column_of_cell(cur.idx());
1834 row_type const actrow = tabular.row_of_cell(cur.idx());
1835 for (row_type r1 = 0, r2 = actrow;
1836 r1 < paste_tabular->rows() && r2 < tabular.rows();
1838 for (col_type c1 = 0, c2 = actcol;
1839 c1 < paste_tabular->columns() && c2 < tabular.columns();
1841 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1842 tabular.isPartOfMultiColumn(r2, c2))
1844 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1848 if (tabular.isPartOfMultiColumn(r2, c2)) {
1852 shared_ptr<InsetText> inset(
1853 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1854 tabular.setCellInset(r2, c2, inset);
1855 // FIXME: change tracking (MG)
1856 inset->setChange(Change(cur.buffer().params().trackChanges ?
1857 Change::INSERTED : Change::UNCHANGED));
1865 void InsetTabular::cutSelection(LCursor & cur)
1867 if (!cur.selection())
1872 getSelection(cur, rs, re, cs, ce);
1873 for (row_type i = rs; i <= re; ++i) {
1874 for (col_type j = cs; j <= ce; ++j) {
1875 shared_ptr<InsetText> t
1876 = cell(tabular.getCellNumber(i, j));
1877 if (cur.buffer().params().trackChanges)
1878 // FIXME: Change tracking (MG)
1879 t->setChange(Change(Change::DELETED));
1885 // cursor position might be invalid now
1886 if (cur.pit() > cur.lastpit())
1887 cur.pit() = cur.lastpit();
1888 if (cur.pos() > cur.lastpos())
1889 cur.pos() = cur.lastpos();
1890 cur.clearSelection();
1891 theSelection().haveSelection(false);
1895 bool InsetTabular::isRightToLeft(LCursor & cur) const
1897 BOOST_ASSERT(cur.depth() > 1);
1898 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1899 pos_type const parentpos = cur[cur.depth() - 2].pos();
1900 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1901 parentpos).language()->rightToLeft();
1905 void InsetTabular::getSelection(LCursor & cur,
1906 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1908 CursorSlice const & beg = cur.selBegin();
1909 CursorSlice const & end = cur.selEnd();
1910 cs = tabular.column_of_cell(beg.idx());
1911 ce = tabular.column_of_cell(end.idx());
1914 cs = tabular.column_of_cell(end.idx());
1916 ce = tabular.right_column_of_cell(end.idx());
1919 rs = tabular.row_of_cell(beg.idx());
1920 re = tabular.row_of_cell(end.idx());
1926 LyXText * InsetTabular::getText(int idx) const
1928 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1932 void InsetTabular::setChange(Change const & change)
1934 for (idx_type idx = 0; idx < nargs(); ++idx)
1935 cell(idx)->setChange(change);
1939 void InsetTabular::acceptChanges(BufferParams const & bparams)
1941 for (idx_type idx = 0; idx < nargs(); ++idx)
1942 cell(idx)->acceptChanges(bparams);
1946 void InsetTabular::rejectChanges(BufferParams const & bparams)
1948 for (idx_type idx = 0; idx < nargs(); ++idx)
1949 cell(idx)->rejectChanges(bparams);
1953 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1955 return tabular.getPWidth(cell).zero();
1959 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
1962 if (buf.length() <= 0)
1965 Buffer const & buffer = *bv.buffer();
1969 col_type maxCols = 1;
1970 docstring::size_type const len = buf.length();
1971 docstring::size_type p = 0;
1974 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
1982 maxCols = max(cols, maxCols);
1988 maxCols = max(cols, maxCols);
1989 LyXTabular * loctab;
1994 paste_tabular.reset(
1995 new LyXTabular(buffer.params(), rows, maxCols));
1996 loctab = paste_tabular.get();
1998 dirtyTabularStack(true);
2001 cell = bv.cursor().idx();
2002 ocol = tabular.column_of_cell(cell);
2003 row = tabular.row_of_cell(cell);
2006 docstring::size_type op = 0;
2007 idx_type const cells = loctab->getNumberOfCells();
2010 rows = loctab->rows();
2011 col_type const columns = loctab->columns();
2013 while (cell < cells && p < len && row < rows &&
2014 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
2020 // we can only set this if we are not too far right
2021 if (cols < columns) {
2022 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2023 Paragraph & par = inset->text_.getPar(0);
2024 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2025 inset->setText(buf.substr(op, p - op), font,
2026 buffer.params().trackChanges);
2032 // we can only set this if we are not too far right
2033 if (cols < columns) {
2034 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2035 Paragraph & par = inset->text_.getPar(0);
2036 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2037 inset->setText(buf.substr(op, p - op), font,
2038 buffer.params().trackChanges);
2043 cell = loctab->getCellNumber(row, cols);
2049 // check for the last cell if there is no trailing '\n'
2050 if (cell < cells && op < len) {
2051 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2052 Paragraph & par = inset->text_.getPar(0);
2053 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2054 inset->setText(buf.substr(op, len - op), font,
2055 buffer.params().trackChanges);
2061 void InsetTabular::addPreview(PreviewLoader & loader) const
2063 row_type const rows = tabular.rows();
2064 col_type const columns = tabular.columns();
2065 for (row_type i = 0; i < rows; ++i) {
2066 for (col_type j = 0; j < columns; ++j)
2067 tabular.getCellInset(i, j)->addPreview(loader);
2072 bool InsetTabular::tablemode(LCursor & cur) const
2074 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2081 string const InsetTabularMailer::name_("tabular");
2083 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2084 : inset_(const_cast<InsetTabular &>(inset))
2088 string const InsetTabularMailer::inset2string(Buffer const &) const
2090 return params2string(inset_);
2094 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2096 istringstream data(in);
2098 lex.setStream(data);
2105 if (!lex || token != name_)
2106 return print_mailer_error("InsetTabularMailer", in, 1,
2109 // This is part of the inset proper that is usually swallowed
2110 // by Buffer::readInset
2112 if (!lex || token != "Tabular")
2113 return print_mailer_error("InsetTabularMailer", in, 2,
2116 Buffer const & buffer = inset.buffer();
2117 inset.read(buffer, lex);
2121 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2124 data << name_ << ' ';
2125 inset.write(inset.buffer(), data);
2126 data << "\\end_inset\n";