3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/Clipboard.h"
41 #include "frontends/Painter.h"
42 #include "frontends/Selection.h"
51 using cap::dirtyTabularStack;
52 using cap::tabularStackDirty;
53 using cap::saveSelection;
55 using graphics::PreviewLoader;
59 using frontend::Painter;
60 using frontend::Clipboard;
62 using boost::shared_ptr;
68 using std::istringstream;
70 using std::ostringstream;
74 namespace Alert = frontend::Alert;
79 int const ADD_TO_HEIGHT = 2;
80 int const ADD_TO_TABULAR_WIDTH = 2;
81 int const default_line_space = 10;
84 boost::scoped_ptr<LyXTabular> paste_tabular;
87 struct TabularFeature {
88 LyXTabular::Feature action;
93 TabularFeature tabularFeature[] =
95 { LyXTabular::APPEND_ROW, "append-row" },
96 { LyXTabular::APPEND_COLUMN, "append-column" },
97 { LyXTabular::DELETE_ROW, "delete-row" },
98 { LyXTabular::DELETE_COLUMN, "delete-column" },
99 { LyXTabular::COPY_ROW, "copy-row" },
100 { LyXTabular::COPY_COLUMN, "copy-column" },
101 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
102 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
103 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
104 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
105 { LyXTabular::ALIGN_LEFT, "align-left" },
106 { LyXTabular::ALIGN_RIGHT, "align-right" },
107 { LyXTabular::ALIGN_CENTER, "align-center" },
108 { LyXTabular::ALIGN_BLOCK, "align-block" },
109 { LyXTabular::VALIGN_TOP, "valign-top" },
110 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
111 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
112 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
113 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
114 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
115 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
116 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
117 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
118 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
119 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
120 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
121 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
122 { LyXTabular::MULTICOLUMN, "multicolumn" },
123 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
124 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
125 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
126 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
127 { LyXTabular::SET_PWIDTH, "set-pwidth" },
128 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
129 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
130 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
131 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
132 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
133 { LyXTabular::SET_USEBOX, "set-usebox" },
134 { LyXTabular::SET_LTHEAD, "set-lthead" },
135 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
136 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
137 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
138 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
139 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
140 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
141 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
142 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
143 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
144 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
145 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
146 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
147 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
148 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
149 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
150 { LyXTabular::LAST_ACTION, "" }
154 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
156 FeatureEqual(LyXTabular::Feature feature)
157 : feature_(feature) {}
158 bool operator()(TabularFeature const & tf) const {
159 return tf.action == feature_;
162 LyXTabular::Feature feature_;
168 string const featureAsString(LyXTabular::Feature feature)
170 TabularFeature * end = tabularFeature +
171 sizeof(tabularFeature) / sizeof(TabularFeature);
172 TabularFeature * it = std::find_if(tabularFeature, end,
173 FeatureEqual(feature));
174 return (it == end) ? string() : it->feature;
178 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
180 : tabular(buf.params(), max(rows, row_type(1)),
181 max(columns, col_type(1))), buffer_(&buf), scx_(0), is_deleted_(false)
185 InsetTabular::InsetTabular(InsetTabular const & tab)
186 : InsetOld(tab), tabular(tab.tabular),
187 buffer_(tab.buffer_), scx_(0), is_deleted_(false)
191 InsetTabular::~InsetTabular()
193 InsetTabularMailer(*this).hideDialog();
198 auto_ptr<InsetBase> InsetTabular::doClone() const
200 return auto_ptr<InsetBase>(new InsetTabular(*this));
204 Buffer const & InsetTabular::buffer() const
210 void InsetTabular::buffer(Buffer const * b)
216 void InsetTabular::write(Buffer const & buf, ostream & os) const
218 os << "Tabular" << endl;
219 tabular.write(buf, os);
223 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
225 bool const old_format = (lex.getString() == "\\LyXTable");
227 tabular.read(buf, lex);
233 string token = lex.getString();
234 while (lex && token != "\\end_inset") {
236 token = lex.getString();
239 lex.printError("Missing \\end_inset at this point. "
245 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
247 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
248 // mi.base.textwidth << "\n";
250 lyxerr << "InsetTabular::metrics: need bv" << endl;
255 for (idx_type cell = 0; i < tabular.rows(); ++i) {
258 for (col_type j = 0; j < tabular.columns(); ++j) {
259 if (tabular.isPartOfMultiColumn(i, j))
260 // Multicolumn cell, but not first one
265 if (tabular.cell_info[i][j].multicolumn ==
266 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
267 p_width = tabular.cellinfo_of_cell(cell).p_width;
269 p_width = tabular.column_info[j].p_width;
271 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
272 tabular.getCellInset(cell)->metrics(m, dim);
274 dim.wid = m.base.textwidth;
275 tabular.setWidthOfCell(cell, dim.wid);
276 if (p_width.zero()) {
277 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
278 // FIXME there must be a way to get rid of
279 // the second metrics call
280 tabular.getCellInset(cell)->metrics(m, dim);
282 maxAsc = max(maxAsc, dim.asc);
283 maxDesc = max(maxDesc, dim.des);
286 int const top_space = tabular.row_info[i].top_space_default ?
288 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
289 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
290 int const bottom_space = tabular.row_info[i].bottom_space_default ?
292 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
293 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
296 dim.asc = tabular.getAscentOfRow(0);
297 dim.des = tabular.getHeightOfTabular() - dim.asc;
298 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
299 bool const changed = dim_ != dim;
305 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
307 setPosCache(pi, x, y);
309 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
310 BufferView * bv = pi.base.bv;
312 resetPos(bv->cursor());
315 x += ADD_TO_TABULAR_WIDTH;
318 first_visible_cell = LyXTabular::npos;
319 for (row_type i = 0; i < tabular.rows(); ++i) {
321 int const a = tabular.getAscentOfRow(i);
322 int const d = tabular.getDescentOfRow(i);
323 idx = tabular.getCellNumber(i, 0);
324 for (col_type j = 0; j < tabular.columns(); ++j) {
325 if (tabular.isPartOfMultiColumn(i, j))
327 if (first_visible_cell == LyXTabular::npos)
328 first_visible_cell = idx;
330 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
331 if (nx + tabular.getWidthOfColumn(idx) < 0
332 || nx > bv->workWidth()
334 || y - a > bv->workHeight()) {
335 pi.pain.setDrawingEnabled(false);
336 cell(idx)->draw(pi, cx, y);
337 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
338 pi.pain.setDrawingEnabled(true);
340 cell(idx)->draw(pi, cx, y);
341 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
343 nx += tabular.getWidthOfColumn(idx);
347 if (i + 1 < tabular.rows())
348 y += d + tabular.getAscentOfRow(i + 1) +
349 tabular.getAdditionalHeight(i + 1);
354 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
356 setPosCache(pi, x, y);
358 LCursor & cur = pi.base.bv->cursor();
360 x += scx_ + ADD_TO_TABULAR_WIDTH;
362 // Paint background of current tabular
363 int const w = tabular.getWidthOfTabular();
364 int const h = tabular.getHeightOfTabular();
365 int yy = y - tabular.getAscentOfRow(0);
366 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
368 if (!cur.selection())
370 if (!ptr_cmp(&cur.inset(), this))
376 if (tablemode(cur)) {
379 getSelection(cur, rs, re, cs, ce);
380 y -= tabular.getAscentOfRow(0);
381 for (row_type j = 0; j < tabular.rows(); ++j) {
382 int const a = tabular.getAscentOfRow(j);
383 int const h = a + tabular.getDescentOfRow(j);
385 y += tabular.getAdditionalHeight(j);
386 for (col_type i = 0; i < tabular.columns(); ++i) {
387 if (tabular.isPartOfMultiColumn(j, i))
389 idx_type const cell =
390 tabular.getCellNumber(j, i);
391 int const w = tabular.getWidthOfColumn(cell);
392 if (i >= cs && i <= ce && j >= rs && j <= re)
393 pi.pain.fillRectangle(xx, y, w, h,
402 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
407 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
408 row_type row, idx_type cell, bool erased) const
410 int x2 = x + tabular.getWidthOfColumn(cell);
412 LColor::color col = LColor::tabularline;
413 LColor::color onoffcol = LColor::tabularonoffline;
416 col = LColor::strikeout;
417 onoffcol = LColor::strikeout;
420 if (!tabular.topAlreadyDrawn(cell)) {
421 on_off = !tabular.topLine(cell);
422 pain.line(x, y - tabular.getAscentOfRow(row),
423 x2, y - tabular.getAscentOfRow(row),
424 on_off ? onoffcol : col,
425 on_off ? Painter::line_onoffdash : Painter::line_solid);
427 on_off = !tabular.bottomLine(cell);
428 pain.line(x, y + tabular.getDescentOfRow(row),
429 x2, y + tabular.getDescentOfRow(row),
430 on_off ? onoffcol : col,
431 on_off ? Painter::line_onoffdash : Painter::line_solid);
432 if (!tabular.leftAlreadyDrawn(cell)) {
433 on_off = !tabular.leftLine(cell);
434 pain.line(x, y - tabular.getAscentOfRow(row),
435 x, y + tabular.getDescentOfRow(row),
436 on_off ? onoffcol : col,
437 on_off ? Painter::line_onoffdash : Painter::line_solid);
439 on_off = !tabular.rightLine(cell);
440 pain.line(x2 - tabular.getAdditionalWidth(cell),
441 y - tabular.getAscentOfRow(row),
442 x2 - tabular.getAdditionalWidth(cell),
443 y + tabular.getDescentOfRow(row),
444 on_off ? onoffcol : col,
445 on_off ? Painter::line_onoffdash : Painter::line_solid);
449 docstring const InsetTabular::editMessage() const
451 return _("Opened table");
455 void InsetTabular::edit(LCursor & cur, bool left)
457 //lyxerr << "InsetTabular::edit: " << this << endl;
459 cur.selection() = false;
462 if (isRightToLeft(cur))
463 cur.idx() = tabular.getLastCellInRow(0);
469 if (isRightToLeft(cur))
470 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
472 cur.idx() = tabular.getNumberOfCells() - 1;
474 cur.pos() = cur.lastpos(); // FIXME crude guess
476 // FIXME: this accesses the position cache before it is initialized
478 //cur.bv().fitCursor();
482 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
484 LYXERR(Debug::DEBUG) << "# InsetTabular::doDispatch: cmd: " << cmd
485 << "\n cur:" << cur << endl;
486 CursorSlice sl = cur.top();
487 LCursor & bvcur = cur.bv().cursor();
489 switch (cmd.action) {
491 case LFUN_MOUSE_PRESS:
492 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
494 if (cmd.button() == mouse_button::button1
495 || (cmd.button() == mouse_button::button3
496 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
497 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
499 cur.selection() = false;
500 setCursorFromCoordinates(cur, cmd.x, cmd.y);
501 cur.bv().mouseSetCursor(cur);
505 if (cmd.button() == mouse_button::button2) {
506 if (cap::selection()) {
507 // See comment in LyXText::dispatch why we
509 // FIXME This does not use paste_tabular,
510 // another reason why paste_tabular should go.
511 cap::copySelectionToStack();
512 cmd = FuncRequest(LFUN_PASTE, "0");
514 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
517 doDispatch(cur, cmd);
521 case LFUN_MOUSE_MOTION:
522 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
523 if (cmd.button() == mouse_button::button1) {
524 // only accept motions to places not deeper nested than the real anchor
525 if (bvcur.anchor_.hasPart(cur)) {
526 // only update if selection changes
527 if (bvcur.idx() == cur.idx() &&
528 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
530 setCursorFromCoordinates(cur, cmd.x, cmd.y);
531 bvcur.setCursor(cur);
532 bvcur.selection() = true;
538 case LFUN_MOUSE_RELEASE:
539 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
540 if (cmd.button() == mouse_button::button1) {
541 if (bvcur.selection()) {
542 // Bug3238: disable persistent selection for table cells for now
543 if (tablemode(bvcur))
544 theSelection().haveSelection(true);
546 saveSelection(bvcur);
548 } else if (cmd.button() == mouse_button::button3)
549 InsetTabularMailer(*this).showDialog(&cur.bv());
552 case LFUN_CELL_BACKWARD:
554 cur.selection() = false;
558 case LFUN_CELL_FORWARD:
560 cur.selection() = false;
564 case LFUN_CHAR_FORWARD_SELECT:
565 case LFUN_CHAR_FORWARD:
566 cell(cur.idx())->dispatch(cur, cmd);
567 if (!cur.result().dispatched()) {
568 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
569 if (cmd.action == LFUN_CHAR_FORWARD_SELECT)
572 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
578 case LFUN_CHAR_BACKWARD_SELECT:
579 case LFUN_CHAR_BACKWARD:
580 cell(cur.idx())->dispatch(cur, cmd);
581 if (!cur.result().dispatched()) {
582 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
583 if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
586 cmd = FuncRequest(LFUN_FINISHED_LEFT);
592 case LFUN_DOWN_SELECT:
594 cell(cur.idx())->dispatch(cur, cmd);
595 cur.dispatched(); // override the cell's decision
597 // if our LyXText didn't do anything to the cursor
598 // then we try to put the cursor into the cell below
599 // setting also the right targetX.
600 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
601 cur.idx() = tabular.getCellBelow(cur.idx());
603 TextMetrics const & tm =
604 cur.bv().textMetrics(cell(cur.idx())->getText(0));
605 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
606 if (cmd.action == LFUN_DOWN_SELECT) {
607 // Bug3238: disable persistent selection for table cells for now
609 theSelection().haveSelection(true);
614 if (sl == cur.top()) {
615 // we trick it to go to the RIGHT after leaving the
617 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
624 cell(cur.idx())->dispatch(cur, cmd);
625 cur.dispatched(); // override the cell's decision
627 // if our LyXText didn't do anything to the cursor
628 // then we try to put the cursor into the cell above
629 // setting also the right targetX.
630 if (tabular.row_of_cell(cur.idx()) != 0) {
631 cur.idx() = tabular.getCellAbove(cur.idx());
632 cur.pit() = cur.lastpit();
633 LyXText const * text = cell(cur.idx())->getText(0);
634 TextMetrics const & tm = cur.bv().textMetrics(text);
635 ParagraphMetrics const & pm =
636 tm.parMetrics(cur.lastpit());
637 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
638 if (cmd.action == LFUN_UP_SELECT) {
639 // Bug3238: disable persistent selection for table cells for now
641 theSelection().haveSelection(true);
646 if (sl == cur.top()) {
647 cmd = FuncRequest(LFUN_FINISHED_UP);
652 // case LFUN_SCREEN_DOWN: {
653 // //if (hasSelection())
654 // // cur.selection() = false;
655 // col_type const col = tabular.column_of_cell(cur.idx());
656 // int const t = cur.bv().top_y() + cur.bv().height();
657 // if (t < yo() + tabular.getHeightOfTabular()) {
658 // cur.bv().scrollDocView(t);
659 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
661 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
668 // case LFUN_SCREEN_UP: {
669 // //if (hasSelection())
670 // // cur.selection() = false;
671 // col_type const col = tabular.column_of_cell(cur.idx());
672 // int const t = cur.bv().top_y() + cur.bv().height();
674 // cur.bv().scrollDocView(t);
678 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
682 // cur.par() = cur.lastpar();
683 // cur.pos() = cur.lastpos();
687 case LFUN_LAYOUT_TABULAR:
688 InsetTabularMailer(*this).showDialog(&cur.bv());
691 case LFUN_INSET_DIALOG_UPDATE:
692 InsetTabularMailer(*this).updateDialog(&cur.bv());
695 case LFUN_TABULAR_FEATURE:
696 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
700 // insert file functions
701 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
702 case LFUN_FILE_INSERT_PLAINTEXT: {
704 string const tmpstr = getContentsOfPlaintextFile(&cur.bv(), to_utf8(cmd.argument()), false);
705 // FIXME: We don't know the encoding of the file
706 if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), from_utf8(tmpstr), false))
712 if (tablemode(cur)) {
713 if (copySelection(cur)) {
714 recordUndoInset(cur, Undo::DELETE);
719 cell(cur.idx())->dispatch(cur, cmd);
722 case LFUN_CHAR_DELETE_BACKWARD:
723 case LFUN_CHAR_DELETE_FORWARD:
724 if (tablemode(cur)) {
725 recordUndoInset(cur, Undo::DELETE);
729 cell(cur.idx())->dispatch(cur, cmd);
733 if (!cur.selection())
735 if (tablemode(cur)) {
739 cell(cur.idx())->dispatch(cur, cmd);
742 case LFUN_CLIPBOARD_PASTE:
743 case LFUN_PRIMARY_SELECTION_PASTE: {
744 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
745 theClipboard().getAsText() :
746 theSelection().get();
749 // pass to InsertPlaintextString, but
750 // only if we have multi-cell content
751 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
752 if (insertPlaintextString(cur.bv(), clip, false)) {
753 // content has been replaced,
754 // so cursor might be invalid
755 cur.pos() = cur.lastpos();
756 bvcur.setCursor(cur);
760 // Let the cell handle normal text
761 cell(cur.idx())->dispatch(cur, cmd);
766 if (tabularStackDirty() && theClipboard().isInternal()) {
767 recordUndoInset(cur, Undo::INSERT);
771 cell(cur.idx())->dispatch(cur, cmd);
776 case LFUN_FONT_ROMAN:
782 case LFUN_FONT_FREE_APPLY:
783 case LFUN_FONT_FREE_UPDATE:
785 case LFUN_FONT_UNDERLINE:
787 case LFUN_WORD_CAPITALIZE:
788 case LFUN_WORD_UPCASE:
789 case LFUN_WORD_LOWCASE:
790 case LFUN_CHARS_TRANSPOSE:
791 if (tablemode(cur)) {
794 getSelection(cur, rs, re, cs, ce);
795 LCursor tmpcur = cur;
796 for (row_type i = rs; i <= re; ++i) {
797 for (col_type j = cs; j <= ce; ++j) {
798 // cursor follows cell:
799 tmpcur.idx() = tabular.getCellNumber(i, j);
800 // select this cell only:
803 tmpcur.resetAnchor();
804 tmpcur.pit() = tmpcur.lastpit();
805 tmpcur.pos() = tmpcur.top().lastpos();
806 tmpcur.setCursor(tmpcur);
807 tmpcur.setSelection();
808 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
813 cell(cur.idx())->dispatch(cur, cmd);
817 // we try to handle this event in the insets dispatch function.
818 cell(cur.idx())->dispatch(cur, cmd);
822 // FIXME: this accesses the position cache before it is initialized
825 InsetTabularMailer(*this).updateDialog(&cur.bv());
829 // function sets an object as defined in func_status.h:
830 // states OK, Unknown, Disabled, On, Off.
831 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
832 FuncStatus & status) const
834 switch (cmd.action) {
835 case LFUN_TABULAR_FEATURE: {
836 int action = LyXTabular::LAST_ACTION;
838 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
839 string const tmp = tabularFeature[i].feature;
840 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
841 action = tabularFeature[i].action;
845 if (action == LyXTabular::LAST_ACTION) {
847 status.unknown(true);
851 string const argument
852 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
854 row_type sel_row_start = 0;
855 row_type sel_row_end = 0;
857 LyXTabular::ltType dummyltt;
860 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
863 case LyXTabular::SET_PWIDTH:
864 case LyXTabular::SET_MPWIDTH:
865 case LyXTabular::SET_SPECIAL_COLUMN:
866 case LyXTabular::SET_SPECIAL_MULTI:
867 case LyXTabular::APPEND_ROW:
868 case LyXTabular::APPEND_COLUMN:
869 case LyXTabular::DELETE_ROW:
870 case LyXTabular::DELETE_COLUMN:
871 case LyXTabular::COPY_ROW:
872 case LyXTabular::COPY_COLUMN:
873 case LyXTabular::SET_ALL_LINES:
874 case LyXTabular::UNSET_ALL_LINES:
875 case LyXTabular::SET_TOP_SPACE:
876 case LyXTabular::SET_BOTTOM_SPACE:
877 case LyXTabular::SET_INTERLINE_SPACE:
881 case LyXTabular::MULTICOLUMN:
882 status.setOnOff(tabular.isMultiColumn(cur.idx()));
885 case LyXTabular::M_TOGGLE_LINE_TOP:
887 case LyXTabular::TOGGLE_LINE_TOP:
888 status.setOnOff(tabular.topLine(cur.idx(), flag));
891 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
893 case LyXTabular::TOGGLE_LINE_BOTTOM:
894 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
897 case LyXTabular::M_TOGGLE_LINE_LEFT:
899 case LyXTabular::TOGGLE_LINE_LEFT:
900 status.setOnOff(tabular.leftLine(cur.idx(), flag));
903 case LyXTabular::M_TOGGLE_LINE_RIGHT:
905 case LyXTabular::TOGGLE_LINE_RIGHT:
906 status.setOnOff(tabular.rightLine(cur.idx(), flag));
909 case LyXTabular::M_ALIGN_LEFT:
911 case LyXTabular::ALIGN_LEFT:
912 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
915 case LyXTabular::M_ALIGN_RIGHT:
917 case LyXTabular::ALIGN_RIGHT:
918 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
921 case LyXTabular::M_ALIGN_CENTER:
923 case LyXTabular::ALIGN_CENTER:
924 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
927 case LyXTabular::ALIGN_BLOCK:
928 status.enabled(!tabular.getPWidth(cur.idx()).zero());
929 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
932 case LyXTabular::M_VALIGN_TOP:
934 case LyXTabular::VALIGN_TOP:
936 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
939 case LyXTabular::M_VALIGN_BOTTOM:
941 case LyXTabular::VALIGN_BOTTOM:
943 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
946 case LyXTabular::M_VALIGN_MIDDLE:
948 case LyXTabular::VALIGN_MIDDLE:
950 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
953 case LyXTabular::SET_LONGTABULAR:
954 status.setOnOff(tabular.isLongTabular());
957 case LyXTabular::UNSET_LONGTABULAR:
958 status.setOnOff(!tabular.isLongTabular());
961 case LyXTabular::SET_ROTATE_TABULAR:
962 status.setOnOff(tabular.getRotateTabular());
965 case LyXTabular::UNSET_ROTATE_TABULAR:
966 status.setOnOff(!tabular.getRotateTabular());
969 case LyXTabular::SET_ROTATE_CELL:
970 status.setOnOff(tabular.getRotateCell(cur.idx()));
973 case LyXTabular::UNSET_ROTATE_CELL:
974 status.setOnOff(!tabular.getRotateCell(cur.idx()));
977 case LyXTabular::SET_USEBOX:
978 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
981 case LyXTabular::SET_LTFIRSTHEAD:
982 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
985 case LyXTabular::UNSET_LTFIRSTHEAD:
986 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
989 case LyXTabular::SET_LTHEAD:
990 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
993 case LyXTabular::UNSET_LTHEAD:
994 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
997 case LyXTabular::SET_LTFOOT:
998 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1001 case LyXTabular::UNSET_LTFOOT:
1002 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1005 case LyXTabular::SET_LTLASTFOOT:
1006 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1009 case LyXTabular::UNSET_LTLASTFOOT:
1010 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1013 case LyXTabular::SET_LTNEWPAGE:
1014 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1017 case LyXTabular::SET_BOOKTABS:
1018 status.setOnOff(tabular.useBookTabs());
1021 case LyXTabular::UNSET_BOOKTABS:
1022 status.setOnOff(!tabular.useBookTabs());
1027 status.enabled(false);
1033 // These are only enabled inside tabular
1034 case LFUN_CELL_BACKWARD:
1035 case LFUN_CELL_FORWARD:
1036 status.enabled(true);
1039 // disable these with multiple cells selected
1040 case LFUN_INSET_INSERT:
1041 case LFUN_TABULAR_INSERT:
1042 case LFUN_CHARSTYLE_INSERT:
1043 case LFUN_FLOAT_INSERT:
1044 case LFUN_FLOAT_WIDE_INSERT:
1045 case LFUN_FOOTNOTE_INSERT:
1046 case LFUN_MARGINALNOTE_INSERT:
1047 case LFUN_MATH_INSERT:
1048 case LFUN_MATH_MODE:
1049 case LFUN_MATH_MUTATE:
1050 case LFUN_MATH_DISPLAY:
1051 case LFUN_NOTE_INSERT:
1052 case LFUN_OPTIONAL_INSERT:
1053 case LFUN_BOX_INSERT:
1054 case LFUN_BRANCH_INSERT:
1055 case LFUN_WRAP_INSERT:
1056 case LFUN_ERT_INSERT: {
1057 if (tablemode(cur)) {
1058 status.enabled(false);
1061 return cell(cur.idx())->getStatus(cur, cmd, status);
1064 // disable in non-fixed-width cells
1065 case LFUN_BREAK_LINE:
1066 case LFUN_BREAK_PARAGRAPH:
1067 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1068 case LFUN_BREAK_PARAGRAPH_SKIP: {
1069 if (tabular.getPWidth(cur.idx()).zero()) {
1070 status.enabled(false);
1073 return cell(cur.idx())->getStatus(cur, cmd, status);
1077 if (tabularStackDirty() && theClipboard().isInternal()) {
1078 status.enabled(true);
1081 return cell(cur.idx())->getStatus(cur, cmd, status);
1083 case LFUN_INSET_MODIFY:
1084 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1085 status.enabled(true);
1091 // we try to handle this event in the insets dispatch function.
1092 return cell(cur.idx())->getStatus(cur, cmd, status);
1097 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1098 OutputParams const & runparams) const
1100 return tabular.latex(buf, os, runparams);
1104 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1105 OutputParams const & runparams) const
1107 os << '\n'; // output table on a new line
1108 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
1109 tabular.plaintext(buf, os, runparams, dp, false, 0);
1110 return PLAINTEXT_NEWLINE;
1114 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1115 OutputParams const & runparams) const
1118 InsetBase * master = 0;
1120 #ifdef WITH_WARNINGS
1121 #warning Why not pass a proper DocIterator here?
1124 // if the table is inside a float it doesn't need the informaltable
1125 // wrapper. Search for it.
1126 for (master = owner(); master; master = master->owner())
1127 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1132 os << "<informaltable>";
1135 ret += tabular.docbook(buf, os, runparams);
1137 os << "</informaltable>";
1144 void InsetTabular::validate(LaTeXFeatures & features) const
1146 tabular.validate(features);
1150 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1152 return tabular.getCellInset(idx);
1156 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1158 return tabular.getCellInset(idx);
1162 void InsetTabular::cursorPos(BufferView const & bv,
1163 CursorSlice const & sl, bool boundary, int & x, int & y) const
1165 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1167 // y offset correction
1168 int const row = tabular.row_of_cell(sl.idx());
1169 for (int i = 0; i <= row; ++i) {
1171 y += tabular.getAscentOfRow(i);
1172 y += tabular.getAdditionalHeight(i);
1175 y += tabular.getDescentOfRow(i);
1178 // x offset correction
1179 int const col = tabular.column_of_cell(sl.idx());
1180 int idx = tabular.getCellNumber(row, 0);
1181 for (int j = 0; j < col; ++j) {
1182 if (tabular.isPartOfMultiColumn(row, j))
1184 x += tabular.getWidthOfColumn(idx);
1187 x += tabular.getBeginningOfTextInCell(idx);
1188 x += ADD_TO_TABULAR_WIDTH;
1193 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1197 InsetBase const & inset = *tabular.getCellInset(cell);
1198 Point o = bv.coordCache().getInsets().xy(&inset);
1199 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1200 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1201 row_type const row = tabular.row_of_cell(cell);
1202 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1203 tabular.getAdditionalHeight(row);
1204 int const yend = o.y_ + tabular.getDescentOfRow(row);
1216 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1217 // << " ybeg=" << ybeg << " yend=" << yend
1218 // << " xx=" << xx << " yy=" << yy
1219 // << " dist=" << xx + yy << endl;
1224 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1226 //lyxerr << "InsetTabular::editXY: " << this << endl;
1227 cur.selection() = false;
1229 cur.idx() = getNearestCell(cur.bv(), x, y);
1231 return cell(cur.idx())->text_.editXY(cur, x, y);
1235 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1237 cur.idx() = getNearestCell(cur.bv(), x, y);
1238 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1242 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1244 idx_type idx_min = 0;
1245 int dist_min = std::numeric_limits<int>::max();
1246 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1247 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1248 int const d = dist(bv, i, x, y);
1259 int InsetTabular::getCellXPos(idx_type const cell) const
1263 for (; !tabular.isFirstCellInRow(c); --c)
1265 int lx = tabular.getWidthOfColumn(cell);
1266 for (; c < cell; ++c)
1267 lx += tabular.getWidthOfColumn(c);
1269 return lx - tabular.getWidthOfColumn(cell);
1273 void InsetTabular::resetPos(LCursor & cur) const
1275 BufferView & bv = cur.bv();
1276 int const maxwidth = bv.workWidth();
1278 if (&cur.inset() != this) {
1282 int const X2 = maxwidth;
1283 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1284 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1285 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1288 scx_ = X1 + 20 - x1;
1290 scx_ = X2 - 20 - x2;
1295 cur.updateFlags(Update::Force | Update::FitCursor);
1299 void InsetTabular::moveNextCell(LCursor & cur)
1301 if (isRightToLeft(cur)) {
1302 if (tabular.isFirstCellInRow(cur.idx())) {
1303 row_type const row = tabular.row_of_cell(cur.idx());
1304 if (row == tabular.rows() - 1)
1306 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1313 if (tabular.isLastCell(cur.idx()))
1323 void InsetTabular::movePrevCell(LCursor & cur)
1325 if (isRightToLeft(cur)) {
1326 if (tabular.isLastCellInRow(cur.idx())) {
1327 row_type const row = tabular.row_of_cell(cur.idx());
1330 cur.idx() = tabular.getFirstCellInRow(row);
1331 cur.idx() = tabular.getCellAbove(cur.idx());
1333 if (tabular.isLastCell(cur.idx()))
1338 if (cur.idx() == 0) // first cell
1342 cur.pit() = cur.lastpit();
1343 cur.pos() = cur.lastpos();
1345 // FIXME: this accesses the position cache before it is initialized
1350 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1352 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1355 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1356 string const tmp = tabularFeature[i].feature;
1358 if (tmp == what.substr(0, tmp.length())) {
1359 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1360 //tabularFeatures[i].feature.length())) {
1361 action = tabularFeature[i].action;
1365 if (action == LyXTabular::LAST_ACTION)
1369 ltrim(what.substr(tabularFeature[i].feature.length()));
1370 tabularFeatures(cur, action, val);
1377 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1378 string const & special, bool & flag)
1380 if (special == "dl_above") {
1383 } else if (special == "dl_below") {
1384 ltt.bottomDL = flag;
1386 } else if (special == "empty") {
1398 void InsetTabular::tabularFeatures(LCursor & cur,
1399 LyXTabular::Feature feature, string const & value)
1401 BufferView & bv = cur.bv();
1402 col_type sel_col_start;
1403 col_type sel_col_end;
1404 row_type sel_row_start;
1405 row_type sel_row_end;
1406 bool setLines = false;
1407 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1408 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1412 case LyXTabular::M_ALIGN_LEFT:
1413 case LyXTabular::ALIGN_LEFT:
1414 setAlign = LYX_ALIGN_LEFT;
1417 case LyXTabular::M_ALIGN_RIGHT:
1418 case LyXTabular::ALIGN_RIGHT:
1419 setAlign = LYX_ALIGN_RIGHT;
1422 case LyXTabular::M_ALIGN_CENTER:
1423 case LyXTabular::ALIGN_CENTER:
1424 setAlign = LYX_ALIGN_CENTER;
1427 case LyXTabular::ALIGN_BLOCK:
1428 setAlign = LYX_ALIGN_BLOCK;
1431 case LyXTabular::M_VALIGN_TOP:
1432 case LyXTabular::VALIGN_TOP:
1433 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1436 case LyXTabular::M_VALIGN_BOTTOM:
1437 case LyXTabular::VALIGN_BOTTOM:
1438 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1441 case LyXTabular::M_VALIGN_MIDDLE:
1442 case LyXTabular::VALIGN_MIDDLE:
1443 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1450 recordUndoInset(cur, Undo::ATOMIC);
1452 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1453 row_type const row = tabular.row_of_cell(cur.idx());
1454 col_type const column = tabular.column_of_cell(cur.idx());
1456 LyXTabular::ltType ltt;
1460 case LyXTabular::SET_PWIDTH: {
1461 LyXLength const len(value);
1462 tabular.setColumnPWidth(cur, cur.idx(), len);
1464 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1465 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1469 case LyXTabular::SET_MPWIDTH:
1470 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1473 case LyXTabular::SET_SPECIAL_COLUMN:
1474 case LyXTabular::SET_SPECIAL_MULTI:
1475 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
1478 case LyXTabular::APPEND_ROW:
1479 // append the row into the tabular
1480 tabular.appendRow(bv.buffer()->params(), cur.idx());
1483 case LyXTabular::APPEND_COLUMN:
1484 // append the column into the tabular
1485 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1486 cur.idx() = tabular.getCellNumber(row, column);
1489 case LyXTabular::DELETE_ROW:
1490 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1491 tabular.deleteRow(sel_row_start);
1492 if (sel_row_start >= tabular.rows())
1494 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1497 cur.selection() = false;
1500 case LyXTabular::DELETE_COLUMN:
1501 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1502 tabular.deleteColumn(sel_col_start);
1503 if (sel_col_start >= tabular.columns())
1505 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1508 cur.selection() = false;
1511 case LyXTabular::COPY_ROW:
1512 tabular.copyRow(bv.buffer()->params(), row);
1515 case LyXTabular::COPY_COLUMN:
1516 tabular.copyColumn(bv.buffer()->params(), column);
1517 cur.idx() = tabular.getCellNumber(row, column);
1520 case LyXTabular::M_TOGGLE_LINE_TOP:
1522 case LyXTabular::TOGGLE_LINE_TOP: {
1523 bool lineSet = !tabular.topLine(cur.idx(), flag);
1524 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1525 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1527 tabular.getCellNumber(i, j),
1532 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1534 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1535 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1536 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1537 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1538 tabular.setBottomLine(
1539 tabular.getCellNumber(i, j),
1545 case LyXTabular::M_TOGGLE_LINE_LEFT:
1547 case LyXTabular::TOGGLE_LINE_LEFT: {
1548 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1549 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1550 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1551 tabular.setLeftLine(
1552 tabular.getCellNumber(i,j),
1558 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1560 case LyXTabular::TOGGLE_LINE_RIGHT: {
1561 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1562 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1563 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1564 tabular.setRightLine(
1565 tabular.getCellNumber(i,j),
1571 case LyXTabular::M_ALIGN_LEFT:
1572 case LyXTabular::M_ALIGN_RIGHT:
1573 case LyXTabular::M_ALIGN_CENTER:
1575 case LyXTabular::ALIGN_LEFT:
1576 case LyXTabular::ALIGN_RIGHT:
1577 case LyXTabular::ALIGN_CENTER:
1578 case LyXTabular::ALIGN_BLOCK:
1579 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1580 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1581 tabular.setAlignment(
1582 tabular.getCellNumber(i, j),
1587 case LyXTabular::M_VALIGN_TOP:
1588 case LyXTabular::M_VALIGN_BOTTOM:
1589 case LyXTabular::M_VALIGN_MIDDLE:
1591 case LyXTabular::VALIGN_TOP:
1592 case LyXTabular::VALIGN_BOTTOM:
1593 case LyXTabular::VALIGN_MIDDLE:
1594 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1595 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1596 tabular.setVAlignment(
1597 tabular.getCellNumber(i, j),
1601 case LyXTabular::MULTICOLUMN: {
1602 if (sel_row_start != sel_row_end) {
1603 #ifdef WITH_WARNINGS
1604 #warning Need I say it ? This is horrible.
1607 Alert::error(_("Error setting multicolumn"),
1608 _("You cannot set multicolumn vertically."));
1611 if (!cur.selection()) {
1612 // just multicol for one single cell
1613 // check whether we are completely in a multicol
1614 if (tabular.isMultiColumn(cur.idx()))
1615 tabular.unsetMultiColumn(cur.idx());
1617 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1620 // we have a selection so this means we just add all this
1621 // cells to form a multicolumn cell
1622 idx_type const s_start = cur.selBegin().idx();
1623 idx_type const s_end = cur.selEnd().idx();
1624 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1625 cur.idx() = s_start;
1628 cur.selection() = false;
1632 case LyXTabular::SET_ALL_LINES:
1634 case LyXTabular::UNSET_ALL_LINES:
1635 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1636 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1637 tabular.setAllLines(
1638 tabular.getCellNumber(i,j), setLines);
1641 case LyXTabular::SET_LONGTABULAR:
1642 tabular.setLongTabular(true);
1645 case LyXTabular::UNSET_LONGTABULAR:
1646 tabular.setLongTabular(false);
1649 case LyXTabular::SET_ROTATE_TABULAR:
1650 tabular.setRotateTabular(true);
1653 case LyXTabular::UNSET_ROTATE_TABULAR:
1654 tabular.setRotateTabular(false);
1657 case LyXTabular::SET_ROTATE_CELL:
1658 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1659 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1660 tabular.setRotateCell(
1661 tabular.getCellNumber(i, j), true);
1664 case LyXTabular::UNSET_ROTATE_CELL:
1665 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1666 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1667 tabular.setRotateCell(
1668 tabular.getCellNumber(i, j), false);
1671 case LyXTabular::SET_USEBOX: {
1672 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1673 if (val == tabular.getUsebox(cur.idx()))
1674 val = LyXTabular::BOX_NONE;
1675 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1676 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1677 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1681 case LyXTabular::UNSET_LTFIRSTHEAD:
1683 case LyXTabular::SET_LTFIRSTHEAD:
1684 tabular.getRowOfLTFirstHead(row, ltt);
1685 checkLongtableSpecial(ltt, value, flag);
1686 tabular.setLTHead(row, flag, ltt, true);
1689 case LyXTabular::UNSET_LTHEAD:
1691 case LyXTabular::SET_LTHEAD:
1692 tabular.getRowOfLTHead(row, ltt);
1693 checkLongtableSpecial(ltt, value, flag);
1694 tabular.setLTHead(row, flag, ltt, false);
1697 case LyXTabular::UNSET_LTFOOT:
1699 case LyXTabular::SET_LTFOOT:
1700 tabular.getRowOfLTFoot(row, ltt);
1701 checkLongtableSpecial(ltt, value, flag);
1702 tabular.setLTFoot(row, flag, ltt, false);
1705 case LyXTabular::UNSET_LTLASTFOOT:
1707 case LyXTabular::SET_LTLASTFOOT:
1708 tabular.getRowOfLTLastFoot(row, ltt);
1709 checkLongtableSpecial(ltt, value, flag);
1710 tabular.setLTFoot(row, flag, ltt, true);
1713 case LyXTabular::SET_LTNEWPAGE:
1714 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1717 case LyXTabular::SET_BOOKTABS:
1718 tabular.setBookTabs(true);
1721 case LyXTabular::UNSET_BOOKTABS:
1722 tabular.setBookTabs(false);
1725 case LyXTabular::SET_TOP_SPACE: {
1727 if (value == "default")
1728 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1729 tabular.row_info[i].top_space_default = true;
1730 else if (isValidLength(value, &len))
1731 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1732 tabular.row_info[i].top_space_default = false;
1733 tabular.row_info[i].top_space = len;
1736 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1737 tabular.row_info[i].top_space_default = false;
1738 tabular.row_info[i].top_space = len;
1743 case LyXTabular::SET_BOTTOM_SPACE: {
1745 if (value == "default")
1746 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1747 tabular.row_info[i].bottom_space_default = true;
1748 else if (isValidLength(value, &len))
1749 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1750 tabular.row_info[i].bottom_space_default = false;
1751 tabular.row_info[i].bottom_space = len;
1754 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1755 tabular.row_info[i].bottom_space_default = false;
1756 tabular.row_info[i].bottom_space = len;
1761 case LyXTabular::SET_INTERLINE_SPACE: {
1763 if (value == "default")
1764 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1765 tabular.row_info[i].interline_space_default = true;
1766 else if (isValidLength(value, &len))
1767 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1768 tabular.row_info[i].interline_space_default = false;
1769 tabular.row_info[i].interline_space = len;
1772 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1773 tabular.row_info[i].interline_space_default = false;
1774 tabular.row_info[i].interline_space = len;
1779 // dummy stuff just to avoid warnings
1780 case LyXTabular::LAST_ACTION:
1786 bool InsetTabular::showInsetDialog(BufferView * bv) const
1788 InsetTabularMailer(*this).showDialog(bv);
1793 void InsetTabular::openLayoutDialog(BufferView * bv) const
1795 InsetTabularMailer(*this).showDialog(bv);
1799 bool InsetTabular::copySelection(LCursor & cur)
1801 if (!cur.selection())
1806 getSelection(cur, rs, re, cs, ce);
1808 paste_tabular.reset(new LyXTabular(tabular));
1810 for (row_type i = 0; i < rs; ++i)
1811 paste_tabular->deleteRow(0);
1813 row_type const rows = re - rs + 1;
1814 while (paste_tabular->rows() > rows)
1815 paste_tabular->deleteRow(rows);
1817 paste_tabular->setTopLine(0, true, true);
1818 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1821 for (col_type i = 0; i < cs; ++i)
1822 paste_tabular->deleteColumn(0);
1824 col_type const columns = ce - cs + 1;
1825 while (paste_tabular->columns() > columns)
1826 paste_tabular->deleteColumn(columns);
1828 paste_tabular->setLeftLine(0, true, true);
1829 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1832 odocstringstream os;
1833 OutputParams const runparams(0);
1834 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1835 // Needed for the "Edit->Paste recent" menu and the system clipboard.
1836 cap::copySelection(cur, os.str());
1838 // mark tabular stack dirty
1839 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1840 // when we (hopefully) have a one-for-all paste mechanism.
1841 // This must be called after cap::copySelection.
1842 dirtyTabularStack(true);
1848 bool InsetTabular::pasteClipboard(LCursor & cur)
1852 col_type const actcol = tabular.column_of_cell(cur.idx());
1853 row_type const actrow = tabular.row_of_cell(cur.idx());
1854 for (row_type r1 = 0, r2 = actrow;
1855 r1 < paste_tabular->rows() && r2 < tabular.rows();
1857 for (col_type c1 = 0, c2 = actcol;
1858 c1 < paste_tabular->columns() && c2 < tabular.columns();
1860 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1861 tabular.isPartOfMultiColumn(r2, c2))
1863 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1867 if (tabular.isPartOfMultiColumn(r2, c2)) {
1871 shared_ptr<InsetText> inset(
1872 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1873 tabular.setCellInset(r2, c2, inset);
1874 // FIXME: change tracking (MG)
1875 inset->setChange(Change(cur.buffer().params().trackChanges ?
1876 Change::INSERTED : Change::UNCHANGED));
1884 void InsetTabular::cutSelection(LCursor & cur)
1886 if (!cur.selection())
1891 getSelection(cur, rs, re, cs, ce);
1892 for (row_type i = rs; i <= re; ++i) {
1893 for (col_type j = cs; j <= ce; ++j) {
1894 shared_ptr<InsetText> t
1895 = cell(tabular.getCellNumber(i, j));
1896 if (cur.buffer().params().trackChanges)
1897 // FIXME: Change tracking (MG)
1898 t->setChange(Change(Change::DELETED));
1904 // cursor position might be invalid now
1905 if (cur.pit() > cur.lastpit())
1906 cur.pit() = cur.lastpit();
1907 if (cur.pos() > cur.lastpos())
1908 cur.pos() = cur.lastpos();
1909 cur.clearSelection();
1910 theSelection().haveSelection(false);
1914 bool InsetTabular::isRightToLeft(LCursor & cur) const
1916 BOOST_ASSERT(cur.depth() > 1);
1917 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1918 pos_type const parentpos = cur[cur.depth() - 2].pos();
1919 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1920 parentpos).language()->rightToLeft();
1924 void InsetTabular::getSelection(LCursor & cur,
1925 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1927 CursorSlice const & beg = cur.selBegin();
1928 CursorSlice const & end = cur.selEnd();
1929 cs = tabular.column_of_cell(beg.idx());
1930 ce = tabular.column_of_cell(end.idx());
1933 cs = tabular.column_of_cell(end.idx());
1935 ce = tabular.right_column_of_cell(end.idx());
1938 rs = tabular.row_of_cell(beg.idx());
1939 re = tabular.row_of_cell(end.idx());
1945 LyXText * InsetTabular::getText(int idx) const
1947 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1951 void InsetTabular::setChange(Change const & change)
1953 for (idx_type idx = 0; idx < nargs(); ++idx)
1954 cell(idx)->setChange(change);
1958 void InsetTabular::acceptChanges(BufferParams const & bparams)
1960 for (idx_type idx = 0; idx < nargs(); ++idx)
1961 cell(idx)->acceptChanges(bparams);
1965 void InsetTabular::rejectChanges(BufferParams const & bparams)
1967 for (idx_type idx = 0; idx < nargs(); ++idx)
1968 cell(idx)->rejectChanges(bparams);
1972 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1974 return tabular.getPWidth(cell).zero();
1978 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
1981 if (buf.length() <= 0)
1984 Buffer const & buffer = *bv.buffer();
1988 col_type maxCols = 1;
1989 docstring::size_type const len = buf.length();
1990 docstring::size_type p = 0;
1993 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
2001 maxCols = max(cols, maxCols);
2007 maxCols = max(cols, maxCols);
2008 LyXTabular * loctab;
2013 paste_tabular.reset(
2014 new LyXTabular(buffer.params(), rows, maxCols));
2015 loctab = paste_tabular.get();
2017 dirtyTabularStack(true);
2020 cell = bv.cursor().idx();
2021 ocol = tabular.column_of_cell(cell);
2022 row = tabular.row_of_cell(cell);
2025 docstring::size_type op = 0;
2026 idx_type const cells = loctab->getNumberOfCells();
2029 rows = loctab->rows();
2030 col_type const columns = loctab->columns();
2032 while (cell < cells && p < len && row < rows &&
2033 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
2039 // we can only set this if we are not too far right
2040 if (cols < columns) {
2041 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2042 Paragraph & par = inset->text_.getPar(0);
2043 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2044 inset->setText(buf.substr(op, p - op), font,
2045 buffer.params().trackChanges);
2051 // we can only set this if we are not too far right
2052 if (cols < columns) {
2053 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2054 Paragraph & par = inset->text_.getPar(0);
2055 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2056 inset->setText(buf.substr(op, p - op), font,
2057 buffer.params().trackChanges);
2062 cell = loctab->getCellNumber(row, cols);
2068 // check for the last cell if there is no trailing '\n'
2069 if (cell < cells && op < len) {
2070 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2071 Paragraph & par = inset->text_.getPar(0);
2072 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2073 inset->setText(buf.substr(op, len - op), font,
2074 buffer.params().trackChanges);
2080 void InsetTabular::addPreview(PreviewLoader & loader) const
2082 row_type const rows = tabular.rows();
2083 col_type const columns = tabular.columns();
2084 for (row_type i = 0; i < rows; ++i) {
2085 for (col_type j = 0; j < columns; ++j)
2086 tabular.getCellInset(i, j)->addPreview(loader);
2091 bool InsetTabular::tablemode(LCursor & cur) const
2093 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2100 string const InsetTabularMailer::name_("tabular");
2102 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2103 : inset_(const_cast<InsetTabular &>(inset))
2107 string const InsetTabularMailer::inset2string(Buffer const &) const
2109 return params2string(inset_);
2113 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2115 istringstream data(in);
2117 lex.setStream(data);
2124 if (!lex || token != name_)
2125 return print_mailer_error("InsetTabularMailer", in, 1,
2128 // This is part of the inset proper that is usually swallowed
2129 // by Buffer::readInset
2131 if (!lex || token != "Tabular")
2132 return print_mailer_error("InsetTabularMailer", in, 2,
2135 Buffer const & buffer = inset.buffer();
2136 inset.read(buffer, lex);
2140 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2143 data << name_ << ' ';
2144 inset.write(inset.buffer(), data);
2145 data << "\\end_inset\n";