3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/Clipboard.h"
41 #include "frontends/Painter.h"
42 #include "frontends/Selection.h"
51 using cap::dirtyTabularStack;
52 using cap::tabularStackDirty;
54 using graphics::PreviewLoader;
58 using frontend::Painter;
59 using frontend::Clipboard;
61 using boost::shared_ptr;
67 using std::istringstream;
69 using std::ostringstream;
73 namespace Alert = frontend::Alert;
78 int const ADD_TO_HEIGHT = 2;
79 int const ADD_TO_TABULAR_WIDTH = 2;
80 int const default_line_space = 10;
83 boost::scoped_ptr<LyXTabular> paste_tabular;
86 struct TabularFeature {
87 LyXTabular::Feature action;
92 TabularFeature tabularFeature[] =
94 { LyXTabular::APPEND_ROW, "append-row" },
95 { LyXTabular::APPEND_COLUMN, "append-column" },
96 { LyXTabular::DELETE_ROW, "delete-row" },
97 { LyXTabular::DELETE_COLUMN, "delete-column" },
98 { LyXTabular::COPY_ROW, "copy-row" },
99 { LyXTabular::COPY_COLUMN, "copy-column" },
100 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
101 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
102 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
103 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
104 { LyXTabular::ALIGN_LEFT, "align-left" },
105 { LyXTabular::ALIGN_RIGHT, "align-right" },
106 { LyXTabular::ALIGN_CENTER, "align-center" },
107 { LyXTabular::ALIGN_BLOCK, "align-block" },
108 { LyXTabular::VALIGN_TOP, "valign-top" },
109 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
110 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
111 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
112 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
113 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
114 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
115 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
116 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
117 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
118 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
119 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
120 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
121 { LyXTabular::MULTICOLUMN, "multicolumn" },
122 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
123 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
124 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
125 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
126 { LyXTabular::SET_PWIDTH, "set-pwidth" },
127 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
128 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
129 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
130 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
131 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
132 { LyXTabular::SET_USEBOX, "set-usebox" },
133 { LyXTabular::SET_LTHEAD, "set-lthead" },
134 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
135 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
136 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
137 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
138 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
139 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
140 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
141 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
142 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
143 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
144 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
145 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
146 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
147 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
148 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
149 { LyXTabular::LAST_ACTION, "" }
153 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
155 FeatureEqual(LyXTabular::Feature feature)
156 : feature_(feature) {}
157 bool operator()(TabularFeature const & tf) const {
158 return tf.action == feature_;
161 LyXTabular::Feature feature_;
167 string const featureAsString(LyXTabular::Feature feature)
169 TabularFeature * end = tabularFeature +
170 sizeof(tabularFeature) / sizeof(TabularFeature);
171 TabularFeature * it = std::find_if(tabularFeature, end,
172 FeatureEqual(feature));
173 return (it == end) ? string() : it->feature;
177 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
179 : tabular(buf.params(), max(rows, row_type(1)),
180 max(columns, col_type(1))), buffer_(&buf), scx_(0)
184 InsetTabular::InsetTabular(InsetTabular const & tab)
185 : InsetOld(tab), tabular(tab.tabular),
186 buffer_(tab.buffer_), scx_(0)
190 InsetTabular::~InsetTabular()
192 InsetTabularMailer(*this).hideDialog();
196 auto_ptr<InsetBase> InsetTabular::doClone() const
198 return auto_ptr<InsetBase>(new InsetTabular(*this));
202 Buffer const & InsetTabular::buffer() const
208 void InsetTabular::buffer(Buffer const * b)
214 void InsetTabular::write(Buffer const & buf, ostream & os) const
216 os << "Tabular" << endl;
217 tabular.write(buf, os);
221 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
223 bool const old_format = (lex.getString() == "\\LyXTable");
225 tabular.read(buf, lex);
231 string token = lex.getString();
232 while (lex.isOK() && (token != "\\end_inset")) {
234 token = lex.getString();
236 if (token != "\\end_inset") {
237 lex.printError("Missing \\end_inset at this point. "
243 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
245 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
246 // mi.base.textwidth << "\n";
248 lyxerr << "InsetTabular::metrics: need bv" << endl;
253 for (idx_type cell = 0; i < tabular.rows(); ++i) {
256 for (col_type j = 0; j < tabular.columns(); ++j) {
257 if (tabular.isPartOfMultiColumn(i, j))
258 // Multicolumn cell, but not first one
263 if (tabular.cell_info[i][j].multicolumn ==
264 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
265 p_width = tabular.cellinfo_of_cell(cell).p_width;
267 p_width = tabular.column_info[j].p_width;
269 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
270 tabular.getCellInset(cell)->metrics(m, dim);
272 dim.wid = m.base.textwidth;
273 tabular.setWidthOfCell(cell, dim.wid);
274 if (p_width.zero()) {
275 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
276 // FIXME there must be a way to get rid of
277 // the second metrics call
278 tabular.getCellInset(cell)->metrics(m, dim);
280 maxAsc = max(maxAsc, dim.asc);
281 maxDesc = max(maxDesc, dim.des);
284 int const top_space = tabular.row_info[i].top_space_default ?
286 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
287 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
288 int const bottom_space = tabular.row_info[i].bottom_space_default ?
290 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
291 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
294 dim.asc = tabular.getAscentOfRow(0);
295 dim.des = tabular.getHeightOfTabular() - dim.asc;
296 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
297 bool const changed = dim_ != dim;
303 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
305 setPosCache(pi, x, y);
307 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
308 BufferView * bv = pi.base.bv;
310 resetPos(bv->cursor());
313 x += ADD_TO_TABULAR_WIDTH;
316 first_visible_cell = LyXTabular::npos;
317 for (row_type i = 0; i < tabular.rows(); ++i) {
319 int const a = tabular.getAscentOfRow(i);
320 int const d = tabular.getDescentOfRow(i);
321 idx = tabular.getCellNumber(i, 0);
322 for (col_type j = 0; j < tabular.columns(); ++j) {
323 if (tabular.isPartOfMultiColumn(i, j))
325 if (first_visible_cell == LyXTabular::npos)
326 first_visible_cell = idx;
328 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
329 if (nx + tabular.getWidthOfColumn(idx) < 0
330 || nx > bv->workWidth()
332 || y - a > bv->workHeight()) {
333 pi.pain.setDrawingEnabled(false);
334 cell(idx)->draw(pi, cx, y);
335 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
336 pi.pain.setDrawingEnabled(true);
338 cell(idx)->draw(pi, cx, y);
339 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
341 nx += tabular.getWidthOfColumn(idx);
345 if (i + 1 < tabular.rows())
346 y += d + tabular.getAscentOfRow(i + 1) +
347 tabular.getAdditionalHeight(i + 1);
352 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
354 setPosCache(pi, x, y);
356 LCursor & cur = pi.base.bv->cursor();
358 x += scx_ + ADD_TO_TABULAR_WIDTH;
360 // Paint background of current tabular
361 int const w = tabular.getWidthOfTabular();
362 int const h = tabular.getHeightOfTabular();
363 int yy = y - tabular.getAscentOfRow(0);
364 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
366 if (!cur.selection())
368 if (!ptr_cmp(&cur.inset(), this))
374 if (tablemode(cur)) {
377 getSelection(cur, rs, re, cs, ce);
378 y -= tabular.getAscentOfRow(0);
379 for (row_type j = 0; j < tabular.rows(); ++j) {
380 int const a = tabular.getAscentOfRow(j);
381 int const h = a + tabular.getDescentOfRow(j);
383 y += tabular.getAdditionalHeight(j);
384 for (col_type i = 0; i < tabular.columns(); ++i) {
385 if (tabular.isPartOfMultiColumn(j, i))
387 idx_type const cell =
388 tabular.getCellNumber(j, i);
389 int const w = tabular.getWidthOfColumn(cell);
390 if (i >= cs && i <= ce && j >= rs && j <= re)
391 pi.pain.fillRectangle(xx, y, w, h,
400 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
405 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
406 row_type row, idx_type cell, bool erased) const
408 int x2 = x + tabular.getWidthOfColumn(cell);
410 LColor::color col = LColor::tabularline;
411 LColor::color onoffcol = LColor::tabularonoffline;
414 col = LColor::strikeout;
415 onoffcol = LColor::strikeout;
418 if (!tabular.topAlreadyDrawn(cell)) {
419 on_off = !tabular.topLine(cell);
420 pain.line(x, y - tabular.getAscentOfRow(row),
421 x2, y - tabular.getAscentOfRow(row),
422 on_off ? onoffcol : col,
423 on_off ? Painter::line_onoffdash : Painter::line_solid);
425 on_off = !tabular.bottomLine(cell);
426 pain.line(x, y + tabular.getDescentOfRow(row),
427 x2, y + tabular.getDescentOfRow(row),
428 on_off ? onoffcol : col,
429 on_off ? Painter::line_onoffdash : Painter::line_solid);
430 if (!tabular.leftAlreadyDrawn(cell)) {
431 on_off = !tabular.leftLine(cell);
432 pain.line(x, y - tabular.getAscentOfRow(row),
433 x, y + tabular.getDescentOfRow(row),
434 on_off ? onoffcol : col,
435 on_off ? Painter::line_onoffdash : Painter::line_solid);
437 on_off = !tabular.rightLine(cell);
438 pain.line(x2 - tabular.getAdditionalWidth(cell),
439 y - tabular.getAscentOfRow(row),
440 x2 - tabular.getAdditionalWidth(cell),
441 y + tabular.getDescentOfRow(row),
442 on_off ? onoffcol : col,
443 on_off ? Painter::line_onoffdash : Painter::line_solid);
447 docstring const InsetTabular::editMessage() const
449 return _("Opened table");
453 void InsetTabular::edit(LCursor & cur, bool left)
455 //lyxerr << "InsetTabular::edit: " << this << endl;
457 cur.selection() = false;
460 if (isRightToLeft(cur))
461 cur.idx() = tabular.getLastCellInRow(0);
467 if (isRightToLeft(cur))
468 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
470 cur.idx() = tabular.getNumberOfCells() - 1;
472 cur.pos() = cur.lastpos(); // FIXME crude guess
474 // FIXME: this accesses the position cache before it is initialized
476 //cur.bv().fitCursor();
480 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
482 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
483 << "\n cur:" << cur << endl;
484 CursorSlice sl = cur.top();
485 LCursor & bvcur = cur.bv().cursor();
487 switch (cmd.action) {
489 case LFUN_MOUSE_PRESS:
490 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
492 if (cmd.button() == mouse_button::button1
493 || (cmd.button() == mouse_button::button3
494 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
495 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
497 cur.selection() = false;
498 setCursorFromCoordinates(cur, cmd.x, cmd.y);
499 cur.bv().mouseSetCursor(cur);
503 if (cmd.button() == mouse_button::button2) {
504 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
505 doDispatch(cur, cmd);
509 case LFUN_MOUSE_MOTION:
510 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
511 if (cmd.button() == mouse_button::button1) {
512 // only accept motions to places not deeper nested than the real anchor
513 if (bvcur.anchor_.hasPart(cur)) {
514 // only update if selection changes
515 if (bvcur.idx() == cur.idx() &&
516 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
518 setCursorFromCoordinates(cur, cmd.x, cmd.y);
519 bvcur.setCursor(cur);
520 bvcur.selection() = true;
526 case LFUN_MOUSE_RELEASE:
527 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
528 if (cmd.button() == mouse_button::button3)
529 InsetTabularMailer(*this).showDialog(&cur.bv());
532 case LFUN_CELL_BACKWARD:
534 cur.selection() = false;
537 case LFUN_CELL_FORWARD:
539 cur.selection() = false;
542 case LFUN_CHAR_FORWARD_SELECT:
543 case LFUN_CHAR_FORWARD:
544 cell(cur.idx())->dispatch(cur, cmd);
545 if (!cur.result().dispatched()) {
546 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
548 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
554 case LFUN_CHAR_BACKWARD_SELECT:
555 case LFUN_CHAR_BACKWARD:
556 cell(cur.idx())->dispatch(cur, cmd);
557 if (!cur.result().dispatched()) {
558 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
560 cmd = FuncRequest(LFUN_FINISHED_LEFT);
566 case LFUN_DOWN_SELECT:
568 cell(cur.idx())->dispatch(cur, cmd);
569 cur.dispatched(); // override the cell's decision
571 // if our LyXText didn't do anything to the cursor
572 // then we try to put the cursor into the cell below
573 // setting also the right targetX.
574 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
575 cur.idx() = tabular.getCellBelow(cur.idx());
577 TextMetrics const & tm =
578 cur.bv().textMetrics(cell(cur.idx())->getText(0));
579 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
581 if (sl == cur.top()) {
582 // we trick it to go to the RIGHT after leaving the
584 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
591 cell(cur.idx())->dispatch(cur, cmd);
592 cur.dispatched(); // override the cell's decision
594 // if our LyXText didn't do anything to the cursor
595 // then we try to put the cursor into the cell above
596 // setting also the right targetX.
597 if (tabular.row_of_cell(cur.idx()) != 0) {
598 cur.idx() = tabular.getCellAbove(cur.idx());
599 cur.pit() = cur.lastpit();
600 LyXText const * text = cell(cur.idx())->getText(0);
601 TextMetrics const & tm = cur.bv().textMetrics(text);
602 ParagraphMetrics const & pm =
603 tm.parMetrics(cur.lastpit());
604 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
606 if (sl == cur.top()) {
607 cmd = FuncRequest(LFUN_FINISHED_UP);
612 // case LFUN_SCREEN_DOWN: {
613 // //if (hasSelection())
614 // // cur.selection() = false;
615 // col_type const col = tabular.column_of_cell(cur.idx());
616 // int const t = cur.bv().top_y() + cur.bv().height();
617 // if (t < yo() + tabular.getHeightOfTabular()) {
618 // cur.bv().scrollDocView(t);
619 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
621 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
628 // case LFUN_SCREEN_UP: {
629 // //if (hasSelection())
630 // // cur.selection() = false;
631 // col_type const col = tabular.column_of_cell(cur.idx());
632 // int const t = cur.bv().top_y() + cur.bv().height();
634 // cur.bv().scrollDocView(t);
638 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
642 // cur.par() = cur.lastpar();
643 // cur.pos() = cur.lastpos();
647 case LFUN_LAYOUT_TABULAR:
648 InsetTabularMailer(*this).showDialog(&cur.bv());
651 case LFUN_INSET_DIALOG_UPDATE:
652 InsetTabularMailer(*this).updateDialog(&cur.bv());
655 case LFUN_TABULAR_FEATURE:
656 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
660 // insert file functions
661 case LFUN_FILE_INSERT_ASCII_PARA:
662 case LFUN_FILE_INSERT_ASCII: {
664 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), to_utf8(cmd.argument()), false);
665 // FIXME: We don't know the encoding of the file
666 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), from_utf8(tmpstr), false))
672 if (tablemode(cur)) {
673 if (copySelection(cur)) {
674 recordUndoInset(cur, Undo::DELETE);
679 cell(cur.idx())->dispatch(cur, cmd);
682 case LFUN_CHAR_DELETE_BACKWARD:
683 case LFUN_CHAR_DELETE_FORWARD:
684 if (tablemode(cur)) {
685 recordUndoInset(cur, Undo::DELETE);
689 cell(cur.idx())->dispatch(cur, cmd);
693 if (!cur.selection())
695 if (tablemode(cur)) {
699 cell(cur.idx())->dispatch(cur, cmd);
702 case LFUN_CLIPBOARD_PASTE:
703 case LFUN_PRIMARY_SELECTION_PASTE: {
704 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
705 theClipboard().get() :
706 theSelection().get();
709 // pass to InsertAsciiString, but
710 // only if we have multi-cell content
711 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
712 if (insertAsciiString(cur.bv(), clip, false)) {
713 // content has been replaced,
714 // so cursor might be invalid
715 cur.pos() = cur.lastpos();
716 bvcur.setCursor(cur);
720 // Let the cell handle normal text
721 cell(cur.idx())->dispatch(cur, cmd);
726 if (tabularStackDirty() && theClipboard().isInternal()) {
727 recordUndoInset(cur, Undo::INSERT);
731 cell(cur.idx())->dispatch(cur, cmd);
736 case LFUN_FONT_ROMAN:
742 case LFUN_FONT_FREE_APPLY:
743 case LFUN_FONT_FREE_UPDATE:
745 case LFUN_FONT_UNDERLINE:
747 case LFUN_WORD_CAPITALIZE:
748 case LFUN_WORD_UPCASE:
749 case LFUN_WORD_LOWCASE:
750 case LFUN_CHARS_TRANSPOSE:
751 if (tablemode(cur)) {
754 getSelection(cur, rs, re, cs, ce);
755 LCursor tmpcur = cur;
756 for (row_type i = rs; i <= re; ++i) {
757 for (col_type j = cs; j <= ce; ++j) {
758 // cursor follows cell:
759 tmpcur.idx() = tabular.getCellNumber(i, j);
760 // select this cell only:
763 tmpcur.resetAnchor();
764 tmpcur.pit() = tmpcur.lastpit();
765 tmpcur.pos() = tmpcur.top().lastpos();
766 tmpcur.setCursor(tmpcur);
767 tmpcur.setSelection();
768 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
773 cell(cur.idx())->dispatch(cur, cmd);
777 // we try to handle this event in the insets dispatch function.
778 cell(cur.idx())->dispatch(cur, cmd);
782 // FIXME: this accesses the position cache before it is initialized
784 InsetTabularMailer(*this).updateDialog(&cur.bv());
788 // function sets an object as defined in func_status.h:
789 // states OK, Unknown, Disabled, On, Off.
790 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
791 FuncStatus & status) const
793 switch (cmd.action) {
794 case LFUN_TABULAR_FEATURE: {
795 int action = LyXTabular::LAST_ACTION;
797 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
798 string const tmp = tabularFeature[i].feature;
799 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
800 action = tabularFeature[i].action;
804 if (action == LyXTabular::LAST_ACTION) {
806 status.unknown(true);
810 string const argument
811 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
813 row_type sel_row_start = 0;
814 row_type sel_row_end = 0;
816 LyXTabular::ltType dummyltt;
819 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
822 case LyXTabular::SET_PWIDTH:
823 case LyXTabular::SET_MPWIDTH:
824 case LyXTabular::SET_SPECIAL_COLUMN:
825 case LyXTabular::SET_SPECIAL_MULTI:
826 case LyXTabular::APPEND_ROW:
827 case LyXTabular::APPEND_COLUMN:
828 case LyXTabular::DELETE_ROW:
829 case LyXTabular::DELETE_COLUMN:
830 case LyXTabular::COPY_ROW:
831 case LyXTabular::COPY_COLUMN:
832 case LyXTabular::SET_ALL_LINES:
833 case LyXTabular::UNSET_ALL_LINES:
834 case LyXTabular::SET_TOP_SPACE:
835 case LyXTabular::SET_BOTTOM_SPACE:
836 case LyXTabular::SET_INTERLINE_SPACE:
840 case LyXTabular::MULTICOLUMN:
841 status.setOnOff(tabular.isMultiColumn(cur.idx()));
844 case LyXTabular::M_TOGGLE_LINE_TOP:
846 case LyXTabular::TOGGLE_LINE_TOP:
847 status.setOnOff(tabular.topLine(cur.idx(), flag));
850 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
852 case LyXTabular::TOGGLE_LINE_BOTTOM:
853 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
856 case LyXTabular::M_TOGGLE_LINE_LEFT:
858 case LyXTabular::TOGGLE_LINE_LEFT:
859 status.setOnOff(tabular.leftLine(cur.idx(), flag));
862 case LyXTabular::M_TOGGLE_LINE_RIGHT:
864 case LyXTabular::TOGGLE_LINE_RIGHT:
865 status.setOnOff(tabular.rightLine(cur.idx(), flag));
868 case LyXTabular::M_ALIGN_LEFT:
870 case LyXTabular::ALIGN_LEFT:
871 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
874 case LyXTabular::M_ALIGN_RIGHT:
876 case LyXTabular::ALIGN_RIGHT:
877 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
880 case LyXTabular::M_ALIGN_CENTER:
882 case LyXTabular::ALIGN_CENTER:
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
886 case LyXTabular::ALIGN_BLOCK:
887 status.enabled(!tabular.getPWidth(cur.idx()).zero());
888 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
891 case LyXTabular::M_VALIGN_TOP:
893 case LyXTabular::VALIGN_TOP:
895 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
898 case LyXTabular::M_VALIGN_BOTTOM:
900 case LyXTabular::VALIGN_BOTTOM:
902 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
905 case LyXTabular::M_VALIGN_MIDDLE:
907 case LyXTabular::VALIGN_MIDDLE:
909 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
912 case LyXTabular::SET_LONGTABULAR:
913 status.setOnOff(tabular.isLongTabular());
916 case LyXTabular::UNSET_LONGTABULAR:
917 status.setOnOff(!tabular.isLongTabular());
920 case LyXTabular::SET_ROTATE_TABULAR:
921 status.setOnOff(tabular.getRotateTabular());
924 case LyXTabular::UNSET_ROTATE_TABULAR:
925 status.setOnOff(!tabular.getRotateTabular());
928 case LyXTabular::SET_ROTATE_CELL:
929 status.setOnOff(tabular.getRotateCell(cur.idx()));
932 case LyXTabular::UNSET_ROTATE_CELL:
933 status.setOnOff(!tabular.getRotateCell(cur.idx()));
936 case LyXTabular::SET_USEBOX:
937 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
940 case LyXTabular::SET_LTFIRSTHEAD:
941 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
944 case LyXTabular::UNSET_LTFIRSTHEAD:
945 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
948 case LyXTabular::SET_LTHEAD:
949 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
952 case LyXTabular::UNSET_LTHEAD:
953 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
956 case LyXTabular::SET_LTFOOT:
957 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
960 case LyXTabular::UNSET_LTFOOT:
961 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
964 case LyXTabular::SET_LTLASTFOOT:
965 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
968 case LyXTabular::UNSET_LTLASTFOOT:
969 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
972 case LyXTabular::SET_LTNEWPAGE:
973 status.setOnOff(tabular.getLTNewPage(sel_row_start));
976 case LyXTabular::SET_BOOKTABS:
977 status.setOnOff(tabular.useBookTabs());
980 case LyXTabular::UNSET_BOOKTABS:
981 status.setOnOff(!tabular.useBookTabs());
986 status.enabled(false);
992 // These are only enabled inside tabular
993 case LFUN_CELL_BACKWARD:
994 case LFUN_CELL_FORWARD:
995 status.enabled(true);
998 // disable these with multiple cells selected
999 case LFUN_INSET_INSERT:
1000 case LFUN_TABULAR_INSERT:
1001 case LFUN_CHARSTYLE_INSERT:
1002 case LFUN_FLOAT_INSERT:
1003 case LFUN_FLOAT_WIDE_INSERT:
1004 case LFUN_FOOTNOTE_INSERT:
1005 case LFUN_MARGINALNOTE_INSERT:
1006 case LFUN_MATH_INSERT:
1007 case LFUN_MATH_MODE:
1008 case LFUN_MATH_MUTATE:
1009 case LFUN_MATH_DISPLAY:
1010 case LFUN_NOTE_INSERT:
1011 case LFUN_OPTIONAL_INSERT:
1012 case LFUN_BOX_INSERT:
1013 case LFUN_BRANCH_INSERT:
1014 case LFUN_WRAP_INSERT:
1015 case LFUN_ERT_INSERT: {
1016 if (tablemode(cur)) {
1017 status.enabled(false);
1020 return cell(cur.idx())->getStatus(cur, cmd, status);
1023 // disable in non-fixed-width cells
1024 case LFUN_BREAK_LINE:
1025 case LFUN_BREAK_PARAGRAPH:
1026 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1027 case LFUN_BREAK_PARAGRAPH_SKIP: {
1028 if (tabular.getPWidth(cur.idx()).zero()) {
1029 status.enabled(false);
1032 return cell(cur.idx())->getStatus(cur, cmd, status);
1036 if (tabularStackDirty() && theClipboard().isInternal()) {
1037 status.enabled(true);
1040 return cell(cur.idx())->getStatus(cur, cmd, status);
1042 case LFUN_INSET_MODIFY:
1043 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1044 status.enabled(true);
1050 // we try to handle this event in the insets dispatch function.
1051 return cell(cur.idx())->getStatus(cur, cmd, status);
1056 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1057 OutputParams const & runparams) const
1059 return tabular.latex(buf, os, runparams);
1063 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1064 OutputParams const & runparams) const
1066 int const dp = runparams.linelen ? runparams.depth : 0;
1067 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1071 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1072 OutputParams const & runparams) const
1075 InsetBase * master = 0;
1077 #ifdef WITH_WARNINGS
1078 #warning Why not pass a proper DocIterator here?
1081 // if the table is inside a float it doesn't need the informaltable
1082 // wrapper. Search for it.
1083 for (master = owner(); master; master = master->owner())
1084 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1089 os << "<informaltable>";
1092 ret += tabular.docbook(buf, os, runparams);
1094 os << "</informaltable>";
1101 void InsetTabular::validate(LaTeXFeatures & features) const
1103 tabular.validate(features);
1107 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1109 return tabular.getCellInset(idx);
1113 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1115 return tabular.getCellInset(idx);
1119 void InsetTabular::cursorPos(BufferView const & bv,
1120 CursorSlice const & sl, bool boundary, int & x, int & y) const
1122 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1124 // y offset correction
1125 int const row = tabular.row_of_cell(sl.idx());
1126 for (int i = 0; i <= row; ++i) {
1128 y += tabular.getAscentOfRow(i);
1129 y += tabular.getAdditionalHeight(i);
1132 y += tabular.getDescentOfRow(i);
1135 // x offset correction
1136 int const col = tabular.column_of_cell(sl.idx());
1137 int idx = tabular.getCellNumber(row, 0);
1138 for (int j = 0; j < col; ++j) {
1139 if (tabular.isPartOfMultiColumn(row, j))
1141 x += tabular.getWidthOfColumn(idx);
1144 x += tabular.getBeginningOfTextInCell(idx);
1145 x += ADD_TO_TABULAR_WIDTH;
1150 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1154 InsetBase const & inset = *tabular.getCellInset(cell);
1155 Point o = bv.coordCache().getInsets().xy(&inset);
1156 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1157 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1158 row_type const row = tabular.row_of_cell(cell);
1159 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1160 tabular.getAdditionalHeight(row);
1161 int const yend = o.y_ + tabular.getDescentOfRow(row);
1173 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1174 // << " ybeg=" << ybeg << " yend=" << yend
1175 // << " xx=" << xx << " yy=" << yy
1176 // << " dist=" << xx + yy << endl;
1181 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1183 //lyxerr << "InsetTabular::editXY: " << this << endl;
1184 cur.selection() = false;
1186 cur.idx() = getNearestCell(cur.bv(), x, y);
1188 return cell(cur.idx())->text_.editXY(cur, x, y);
1192 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1194 cur.idx() = getNearestCell(cur.bv(), x, y);
1195 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1199 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1201 idx_type idx_min = 0;
1202 int dist_min = std::numeric_limits<int>::max();
1203 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1204 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1205 int const d = dist(bv, i, x, y);
1216 int InsetTabular::getCellXPos(idx_type const cell) const
1220 for (; !tabular.isFirstCellInRow(c); --c)
1222 int lx = tabular.getWidthOfColumn(cell);
1223 for (; c < cell; ++c)
1224 lx += tabular.getWidthOfColumn(c);
1226 return lx - tabular.getWidthOfColumn(cell);
1230 void InsetTabular::resetPos(LCursor & cur) const
1232 BufferView & bv = cur.bv();
1233 int const maxwidth = bv.workWidth();
1235 if (&cur.inset() != this) {
1239 int const X2 = maxwidth;
1240 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1241 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1242 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1245 scx_ = X1 + 20 - x1;
1247 scx_ = X2 - 20 - x2;
1252 cur.updateFlags(Update::Force | Update::FitCursor);
1254 InsetTabularMailer(*this).updateDialog(&bv);
1258 void InsetTabular::moveNextCell(LCursor & cur)
1260 if (isRightToLeft(cur)) {
1261 if (tabular.isFirstCellInRow(cur.idx())) {
1262 row_type const row = tabular.row_of_cell(cur.idx());
1263 if (row == tabular.rows() - 1)
1265 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1272 if (tabular.isLastCell(cur.idx()))
1282 void InsetTabular::movePrevCell(LCursor & cur)
1284 if (isRightToLeft(cur)) {
1285 if (tabular.isLastCellInRow(cur.idx())) {
1286 row_type const row = tabular.row_of_cell(cur.idx());
1289 cur.idx() = tabular.getFirstCellInRow(row);
1290 cur.idx() = tabular.getCellAbove(cur.idx());
1292 if (tabular.isLastCell(cur.idx()))
1297 if (cur.idx() == 0) // first cell
1301 cur.pit() = cur.lastpit();
1302 cur.pos() = cur.lastpos();
1304 // FIXME: this accesses the position cache before it is initialized
1309 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1311 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1314 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1315 string const tmp = tabularFeature[i].feature;
1317 if (tmp == what.substr(0, tmp.length())) {
1318 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1319 //tabularFeatures[i].feature.length())) {
1320 action = tabularFeature[i].action;
1324 if (action == LyXTabular::LAST_ACTION)
1328 ltrim(what.substr(tabularFeature[i].feature.length()));
1329 tabularFeatures(cur, action, val);
1336 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1337 string const & special, bool & flag)
1339 if (special == "dl_above") {
1342 } else if (special == "dl_below") {
1343 ltt.bottomDL = flag;
1345 } else if (special == "empty") {
1357 void InsetTabular::tabularFeatures(LCursor & cur,
1358 LyXTabular::Feature feature, string const & value)
1360 BufferView & bv = cur.bv();
1361 col_type sel_col_start;
1362 col_type sel_col_end;
1363 row_type sel_row_start;
1364 row_type sel_row_end;
1365 bool setLines = false;
1366 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1367 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1371 case LyXTabular::M_ALIGN_LEFT:
1372 case LyXTabular::ALIGN_LEFT:
1373 setAlign = LYX_ALIGN_LEFT;
1376 case LyXTabular::M_ALIGN_RIGHT:
1377 case LyXTabular::ALIGN_RIGHT:
1378 setAlign = LYX_ALIGN_RIGHT;
1381 case LyXTabular::M_ALIGN_CENTER:
1382 case LyXTabular::ALIGN_CENTER:
1383 setAlign = LYX_ALIGN_CENTER;
1386 case LyXTabular::ALIGN_BLOCK:
1387 setAlign = LYX_ALIGN_BLOCK;
1390 case LyXTabular::M_VALIGN_TOP:
1391 case LyXTabular::VALIGN_TOP:
1392 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1395 case LyXTabular::M_VALIGN_BOTTOM:
1396 case LyXTabular::VALIGN_BOTTOM:
1397 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1400 case LyXTabular::M_VALIGN_MIDDLE:
1401 case LyXTabular::VALIGN_MIDDLE:
1402 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1409 recordUndoInset(cur, Undo::ATOMIC);
1411 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1412 row_type const row = tabular.row_of_cell(cur.idx());
1413 col_type const column = tabular.column_of_cell(cur.idx());
1415 LyXTabular::ltType ltt;
1419 case LyXTabular::SET_PWIDTH: {
1420 LyXLength const len(value);
1421 tabular.setColumnPWidth(cur, cur.idx(), len);
1423 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1424 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1428 case LyXTabular::SET_MPWIDTH:
1429 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1432 case LyXTabular::SET_SPECIAL_COLUMN:
1433 case LyXTabular::SET_SPECIAL_MULTI:
1434 tabular.setAlignSpecial(cur.idx(),value,feature);
1437 case LyXTabular::APPEND_ROW:
1438 // append the row into the tabular
1439 tabular.appendRow(bv.buffer()->params(), cur.idx());
1442 case LyXTabular::APPEND_COLUMN:
1443 // append the column into the tabular
1444 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1445 cur.idx() = tabular.getCellNumber(row, column);
1448 case LyXTabular::DELETE_ROW:
1449 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1450 tabular.deleteRow(sel_row_start);
1451 if (sel_row_start >= tabular.rows())
1453 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1456 cur.selection() = false;
1459 case LyXTabular::DELETE_COLUMN:
1460 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1461 tabular.deleteColumn(sel_col_start);
1462 if (sel_col_start >= tabular.columns())
1464 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1467 cur.selection() = false;
1470 case LyXTabular::COPY_ROW:
1471 tabular.copyRow(bv.buffer()->params(), row);
1474 case LyXTabular::COPY_COLUMN:
1475 tabular.copyColumn(bv.buffer()->params(), column);
1476 cur.idx() = tabular.getCellNumber(row, column);
1479 case LyXTabular::M_TOGGLE_LINE_TOP:
1481 case LyXTabular::TOGGLE_LINE_TOP: {
1482 bool lineSet = !tabular.topLine(cur.idx(), flag);
1483 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1484 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1486 tabular.getCellNumber(i, j),
1491 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1493 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1494 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1495 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1496 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1497 tabular.setBottomLine(
1498 tabular.getCellNumber(i, j),
1504 case LyXTabular::M_TOGGLE_LINE_LEFT:
1506 case LyXTabular::TOGGLE_LINE_LEFT: {
1507 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1508 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1509 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1510 tabular.setLeftLine(
1511 tabular.getCellNumber(i,j),
1517 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1519 case LyXTabular::TOGGLE_LINE_RIGHT: {
1520 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1521 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1522 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1523 tabular.setRightLine(
1524 tabular.getCellNumber(i,j),
1530 case LyXTabular::M_ALIGN_LEFT:
1531 case LyXTabular::M_ALIGN_RIGHT:
1532 case LyXTabular::M_ALIGN_CENTER:
1534 case LyXTabular::ALIGN_LEFT:
1535 case LyXTabular::ALIGN_RIGHT:
1536 case LyXTabular::ALIGN_CENTER:
1537 case LyXTabular::ALIGN_BLOCK:
1538 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1539 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1540 tabular.setAlignment(
1541 tabular.getCellNumber(i, j),
1546 case LyXTabular::M_VALIGN_TOP:
1547 case LyXTabular::M_VALIGN_BOTTOM:
1548 case LyXTabular::M_VALIGN_MIDDLE:
1550 case LyXTabular::VALIGN_TOP:
1551 case LyXTabular::VALIGN_BOTTOM:
1552 case LyXTabular::VALIGN_MIDDLE:
1553 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1554 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1555 tabular.setVAlignment(
1556 tabular.getCellNumber(i, j),
1560 case LyXTabular::MULTICOLUMN: {
1561 if (sel_row_start != sel_row_end) {
1562 #ifdef WITH_WARNINGS
1563 #warning Need I say it ? This is horrible.
1566 Alert::error(_("Error setting multicolumn"),
1567 _("You cannot set multicolumn vertically."));
1570 if (!cur.selection()) {
1571 // just multicol for one single cell
1572 // check whether we are completely in a multicol
1573 if (tabular.isMultiColumn(cur.idx()))
1574 tabular.unsetMultiColumn(cur.idx());
1576 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1579 // we have a selection so this means we just add all this
1580 // cells to form a multicolumn cell
1581 idx_type const s_start = cur.selBegin().idx();
1582 idx_type const s_end = cur.selEnd().idx();
1583 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1584 cur.idx() = s_start;
1587 cur.selection() = false;
1591 case LyXTabular::SET_ALL_LINES:
1593 case LyXTabular::UNSET_ALL_LINES:
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.setAllLines(
1597 tabular.getCellNumber(i,j), setLines);
1600 case LyXTabular::SET_LONGTABULAR:
1601 tabular.setLongTabular(true);
1604 case LyXTabular::UNSET_LONGTABULAR:
1605 tabular.setLongTabular(false);
1608 case LyXTabular::SET_ROTATE_TABULAR:
1609 tabular.setRotateTabular(true);
1612 case LyXTabular::UNSET_ROTATE_TABULAR:
1613 tabular.setRotateTabular(false);
1616 case LyXTabular::SET_ROTATE_CELL:
1617 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1618 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1619 tabular.setRotateCell(
1620 tabular.getCellNumber(i, j), true);
1623 case LyXTabular::UNSET_ROTATE_CELL:
1624 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1625 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1626 tabular.setRotateCell(
1627 tabular.getCellNumber(i, j), false);
1630 case LyXTabular::SET_USEBOX: {
1631 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1632 if (val == tabular.getUsebox(cur.idx()))
1633 val = LyXTabular::BOX_NONE;
1634 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1635 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1636 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1640 case LyXTabular::UNSET_LTFIRSTHEAD:
1642 case LyXTabular::SET_LTFIRSTHEAD:
1643 tabular.getRowOfLTFirstHead(row, ltt);
1644 checkLongtableSpecial(ltt, value, flag);
1645 tabular.setLTHead(row, flag, ltt, true);
1648 case LyXTabular::UNSET_LTHEAD:
1650 case LyXTabular::SET_LTHEAD:
1651 tabular.getRowOfLTHead(row, ltt);
1652 checkLongtableSpecial(ltt, value, flag);
1653 tabular.setLTHead(row, flag, ltt, false);
1656 case LyXTabular::UNSET_LTFOOT:
1658 case LyXTabular::SET_LTFOOT:
1659 tabular.getRowOfLTFoot(row, ltt);
1660 checkLongtableSpecial(ltt, value, flag);
1661 tabular.setLTFoot(row, flag, ltt, false);
1664 case LyXTabular::UNSET_LTLASTFOOT:
1666 case LyXTabular::SET_LTLASTFOOT:
1667 tabular.getRowOfLTLastFoot(row, ltt);
1668 checkLongtableSpecial(ltt, value, flag);
1669 tabular.setLTFoot(row, flag, ltt, true);
1672 case LyXTabular::SET_LTNEWPAGE:
1673 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1676 case LyXTabular::SET_BOOKTABS:
1677 tabular.setBookTabs(true);
1680 case LyXTabular::UNSET_BOOKTABS:
1681 tabular.setBookTabs(false);
1684 case LyXTabular::SET_TOP_SPACE: {
1686 if (value == "default")
1687 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1688 tabular.row_info[i].top_space_default = true;
1689 else if (isValidLength(value, &len))
1690 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1691 tabular.row_info[i].top_space_default = false;
1692 tabular.row_info[i].top_space = len;
1695 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1696 tabular.row_info[i].top_space_default = false;
1697 tabular.row_info[i].top_space = len;
1702 case LyXTabular::SET_BOTTOM_SPACE: {
1704 if (value == "default")
1705 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1706 tabular.row_info[i].bottom_space_default = true;
1707 else if (isValidLength(value, &len))
1708 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1709 tabular.row_info[i].bottom_space_default = false;
1710 tabular.row_info[i].bottom_space = len;
1713 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1714 tabular.row_info[i].bottom_space_default = false;
1715 tabular.row_info[i].bottom_space = len;
1720 case LyXTabular::SET_INTERLINE_SPACE: {
1722 if (value == "default")
1723 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1724 tabular.row_info[i].interline_space_default = true;
1725 else if (isValidLength(value, &len))
1726 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1727 tabular.row_info[i].interline_space_default = false;
1728 tabular.row_info[i].interline_space = len;
1731 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1732 tabular.row_info[i].interline_space_default = false;
1733 tabular.row_info[i].interline_space = len;
1738 // dummy stuff just to avoid warnings
1739 case LyXTabular::LAST_ACTION:
1743 InsetTabularMailer(*this).updateDialog(&bv);
1747 bool InsetTabular::showInsetDialog(BufferView * bv) const
1749 InsetTabularMailer(*this).showDialog(bv);
1754 void InsetTabular::openLayoutDialog(BufferView * bv) const
1756 InsetTabularMailer(*this).showDialog(bv);
1760 bool InsetTabular::copySelection(LCursor & cur)
1762 if (!cur.selection())
1767 getSelection(cur, rs, re, cs, ce);
1769 paste_tabular.reset(new LyXTabular(tabular));
1771 for (row_type i = 0; i < rs; ++i)
1772 paste_tabular->deleteRow(0);
1774 row_type const rows = re - rs + 1;
1775 while (paste_tabular->rows() > rows)
1776 paste_tabular->deleteRow(rows);
1778 paste_tabular->setTopLine(0, true, true);
1779 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1782 for (col_type i = 0; i < cs; ++i)
1783 paste_tabular->deleteColumn(0);
1785 col_type const columns = ce - cs + 1;
1786 while (paste_tabular->columns() > columns)
1787 paste_tabular->deleteColumn(columns);
1789 paste_tabular->setLeftLine(0, true, true);
1790 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1793 odocstringstream os;
1794 OutputParams const runparams;
1795 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1796 theClipboard().put(os.str());
1797 // mark tabular stack dirty
1798 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1799 // when we (hopefully) have a one-for-all paste mechanism.
1800 dirtyTabularStack(true);
1806 bool InsetTabular::pasteSelection(LCursor & cur)
1810 col_type const actcol = tabular.column_of_cell(cur.idx());
1811 row_type const actrow = tabular.row_of_cell(cur.idx());
1812 for (row_type r1 = 0, r2 = actrow;
1813 r1 < paste_tabular->rows() && r2 < tabular.rows();
1815 for (col_type c1 = 0, c2 = actcol;
1816 c1 < paste_tabular->columns() && c2 < tabular.columns();
1818 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1819 tabular.isPartOfMultiColumn(r2, c2))
1821 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1825 if (tabular.isPartOfMultiColumn(r2, c2)) {
1829 shared_ptr<InsetText> inset(
1830 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1831 tabular.setCellInset(r2, c2, inset);
1832 // FIXME: change tracking (MG)
1833 inset->setChange(Change(cur.buffer().params().trackChanges ?
1834 Change::INSERTED : Change::UNCHANGED));
1842 void InsetTabular::cutSelection(LCursor & cur)
1844 if (!cur.selection())
1849 getSelection(cur, rs, re, cs, ce);
1850 for (row_type i = rs; i <= re; ++i) {
1851 for (col_type j = cs; j <= ce; ++j) {
1852 shared_ptr<InsetText> t
1853 = cell(tabular.getCellNumber(i, j));
1854 if (cur.buffer().params().trackChanges)
1855 // FIXME: Change tracking (MG)
1856 t->setChange(Change(Change::DELETED));
1862 // cursor position might be invalid now
1863 if (cur.pit() > cur.lastpit())
1864 cur.pit() = cur.lastpit();
1865 if (cur.pos() > cur.lastpos())
1866 cur.pos() = cur.lastpos();
1867 cur.clearSelection();
1871 bool InsetTabular::isRightToLeft(LCursor & cur) const
1873 BOOST_ASSERT(cur.depth() > 1);
1874 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1875 pos_type const parentpos = cur[cur.depth() - 2].pos();
1876 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1877 parentpos).language()->rightToLeft();
1881 void InsetTabular::getSelection(LCursor & cur,
1882 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1884 CursorSlice const & beg = cur.selBegin();
1885 CursorSlice const & end = cur.selEnd();
1886 cs = tabular.column_of_cell(beg.idx());
1887 ce = tabular.column_of_cell(end.idx());
1890 cs = tabular.column_of_cell(end.idx());
1892 ce = tabular.right_column_of_cell(end.idx());
1895 rs = tabular.row_of_cell(beg.idx());
1896 re = tabular.row_of_cell(end.idx());
1902 LyXText * InsetTabular::getText(int idx) const
1904 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1908 void InsetTabular::setChange(Change const & change)
1910 for (idx_type idx = 0; idx < nargs(); ++idx)
1911 cell(idx)->setChange(change);
1915 void InsetTabular::acceptChanges()
1917 for (idx_type idx = 0; idx < nargs(); ++idx)
1918 cell(idx)->acceptChanges();
1922 void InsetTabular::rejectChanges()
1924 for (idx_type idx = 0; idx < nargs(); ++idx)
1925 cell(idx)->rejectChanges();
1929 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1931 return tabular.getPWidth(cell).zero();
1935 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1938 if (buf.length() <= 0)
1941 Buffer const & buffer = *bv.buffer();
1945 col_type maxCols = 1;
1946 docstring::size_type const len = buf.length();
1947 docstring::size_type p = 0;
1950 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
1958 maxCols = max(cols, maxCols);
1964 maxCols = max(cols, maxCols);
1965 LyXTabular * loctab;
1970 paste_tabular.reset(
1971 new LyXTabular(buffer.params(), rows, maxCols));
1972 loctab = paste_tabular.get();
1974 dirtyTabularStack(true);
1977 cell = bv.cursor().idx();
1978 ocol = tabular.column_of_cell(cell);
1979 row = tabular.row_of_cell(cell);
1982 docstring::size_type op = 0;
1983 idx_type const cells = loctab->getNumberOfCells();
1986 rows = loctab->rows();
1987 col_type const columns = loctab->columns();
1989 while (cell < cells && p < len && row < rows &&
1990 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
1996 // we can only set this if we are not too far right
1997 if (cols < columns) {
1998 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1999 Paragraph & par = inset->text_.getPar(0);
2000 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2001 inset->setText(buf.substr(op, p - op), font,
2002 buffer.params().trackChanges);
2008 // we can only set this if we are not too far right
2009 if (cols < columns) {
2010 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2011 Paragraph & par = inset->text_.getPar(0);
2012 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2013 inset->setText(buf.substr(op, p - op), font,
2014 buffer.params().trackChanges);
2019 cell = loctab->getCellNumber(row, cols);
2025 // check for the last cell if there is no trailing '\n'
2026 if (cell < cells && op < len) {
2027 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2028 Paragraph & par = inset->text_.getPar(0);
2029 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2030 inset->setText(buf.substr(op, len - op), font,
2031 buffer.params().trackChanges);
2037 void InsetTabular::addPreview(PreviewLoader & loader) const
2039 row_type const rows = tabular.rows();
2040 col_type const columns = tabular.columns();
2041 for (row_type i = 0; i < rows; ++i) {
2042 for (col_type j = 0; j < columns; ++j)
2043 tabular.getCellInset(i, j)->addPreview(loader);
2048 bool InsetTabular::tablemode(LCursor & cur) const
2050 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2057 string const InsetTabularMailer::name_("tabular");
2059 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2060 : inset_(const_cast<InsetTabular &>(inset))
2064 string const InsetTabularMailer::inset2string(Buffer const &) const
2066 return params2string(inset_);
2070 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2072 istringstream data(in);
2074 lex.setStream(data);
2081 if (!lex || token != name_)
2082 return print_mailer_error("InsetTabularMailer", in, 1,
2085 // This is part of the inset proper that is usually swallowed
2086 // by Buffer::readInset
2088 if (!lex || token != "Tabular")
2089 return print_mailer_error("InsetTabularMailer", in, 2,
2092 Buffer const & buffer = inset.buffer();
2093 inset.read(buffer, lex);
2097 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2100 data << name_ << ' ';
2101 inset.write(inset.buffer(), data);
2102 data << "\\end_inset\n";