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/font_metrics.h"
41 #include "frontends/Gui.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/Clipboard.h"
44 #include "frontends/Painter.h"
45 #include "frontends/Selection.h"
46 #include "frontends/nullpainter.h"
52 using lyx::cap::dirtyTabularStack;
53 using lyx::cap::tabularStackDirty;
55 using lyx::graphics::PreviewLoader;
57 using lyx::support::ltrim;
59 using lyx::frontend::Painter;
60 using lyx::frontend::Gui;
61 using lyx::frontend::Clipboard;
63 using boost::shared_ptr;
69 using std::istringstream;
71 using std::ostringstream;
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::TOGGLE_LINE_TOP, "toggle-line-top" },
99 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
100 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
101 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
102 { LyXTabular::ALIGN_LEFT, "align-left" },
103 { LyXTabular::ALIGN_RIGHT, "align-right" },
104 { LyXTabular::ALIGN_CENTER, "align-center" },
105 { LyXTabular::ALIGN_BLOCK, "align-block" },
106 { LyXTabular::VALIGN_TOP, "valign-top" },
107 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
108 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
109 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
110 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
111 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
112 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
113 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
114 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
115 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
116 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
117 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
118 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
119 { LyXTabular::MULTICOLUMN, "multicolumn" },
120 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
121 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
122 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
123 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
124 { LyXTabular::SET_PWIDTH, "set-pwidth" },
125 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
126 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
127 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
128 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
129 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
130 { LyXTabular::SET_USEBOX, "set-usebox" },
131 { LyXTabular::SET_LTHEAD, "set-lthead" },
132 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
133 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
134 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
135 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
136 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
137 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
138 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
139 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
140 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
141 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
142 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
143 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
144 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
145 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
146 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
147 { LyXTabular::LAST_ACTION, "" }
151 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
153 FeatureEqual(LyXTabular::Feature feature)
154 : feature_(feature) {}
155 bool operator()(TabularFeature const & tf) const {
156 return tf.action == feature_;
159 LyXTabular::Feature feature_;
165 string const featureAsString(LyXTabular::Feature feature)
167 TabularFeature * end = tabularFeature +
168 sizeof(tabularFeature) / sizeof(TabularFeature);
169 TabularFeature * it = std::find_if(tabularFeature, end,
170 FeatureEqual(feature));
171 return (it == end) ? string() : it->feature;
175 bool InsetTabular::hasPasteBuffer() const
177 return (paste_tabular.get() != 0);
181 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
183 : tabular(buf.params(), max(rows, row_type(1)),
184 max(columns, col_type(1))), buffer_(&buf), scx_(0)
188 InsetTabular::InsetTabular(InsetTabular const & tab)
189 : InsetOld(tab), tabular(tab.tabular),
190 buffer_(tab.buffer_), scx_(0)
194 InsetTabular::~InsetTabular()
196 InsetTabularMailer(*this).hideDialog();
200 auto_ptr<InsetBase> InsetTabular::doClone() const
202 return auto_ptr<InsetBase>(new InsetTabular(*this));
206 Buffer const & InsetTabular::buffer() const
212 void InsetTabular::buffer(Buffer const * b)
218 void InsetTabular::write(Buffer const & buf, ostream & os) const
220 os << "Tabular" << endl;
221 tabular.write(buf, os);
225 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
227 bool const old_format = (lex.getString() == "\\LyXTable");
229 tabular.read(buf, lex);
235 string token = lex.getString();
236 while (lex.isOK() && (token != "\\end_inset")) {
238 token = lex.getString();
240 if (token != "\\end_inset") {
241 lex.printError("Missing \\end_inset at this point. "
247 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
249 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
250 // mi.base.textwidth << "\n";
252 lyxerr << "InsetTabular::metrics: need bv" << endl;
257 for (idx_type cell = 0; i < tabular.rows(); ++i) {
260 for (col_type j = 0; j < tabular.columns(); ++j) {
261 if (tabular.isPartOfMultiColumn(i, j))
262 // Multicolumn cell, but not first one
267 if (tabular.cell_info[i][j].multicolumn ==
268 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
269 p_width = tabular.cellinfo_of_cell(cell).p_width;
271 p_width = tabular.column_info[j].p_width;
273 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
274 tabular.getCellInset(cell)->metrics(m, dim);
276 dim.wid = m.base.textwidth;
277 tabular.setWidthOfCell(cell, dim.wid);
278 if (p_width.zero()) {
279 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
280 // FIXME there must be a way to get rid of
281 // the second metrics call
282 tabular.getCellInset(cell)->metrics(m, dim);
284 maxAsc = max(maxAsc, dim.asc);
285 maxDesc = max(maxDesc, dim.des);
288 int const top_space = tabular.row_info[i].top_space_default ?
290 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
291 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
292 int const bottom_space = tabular.row_info[i].bottom_space_default ?
294 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
295 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
298 dim.asc = tabular.getAscentOfRow(0);
299 dim.des = tabular.getHeightOfTabular() - dim.asc;
300 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
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 static lyx::frontend::NullPainter nop;
313 static PainterInfo nullpi(bv, nop);
315 //resetPos(bv->cursor());
318 x += ADD_TO_TABULAR_WIDTH;
321 first_visible_cell = LyXTabular::npos;
322 for (row_type i = 0; i < tabular.rows(); ++i) {
324 int const a = tabular.getAscentOfRow(i);
325 int const d = tabular.getDescentOfRow(i);
326 idx = tabular.getCellNumber(i, 0);
327 for (col_type j = 0; j < tabular.columns(); ++j) {
328 if (tabular.isPartOfMultiColumn(i, j))
330 if (first_visible_cell == LyXTabular::npos)
331 first_visible_cell = idx;
333 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
334 if (nx + tabular.getWidthOfColumn(idx) < 0
335 || nx > bv->workWidth()
337 || y - a > bv->workHeight()) {
338 cell(idx)->draw(nullpi, cx, y);
339 drawCellLines(nop, nx, y, i, idx, pi.erased_);
341 cell(idx)->draw(pi, cx, y);
342 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
344 nx += tabular.getWidthOfColumn(idx);
348 if (i + 1 < tabular.rows())
349 y += d + tabular.getAscentOfRow(i + 1) +
350 tabular.getAdditionalHeight(i + 1);
355 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
357 setPosCache(pi, x, y);
359 LCursor & cur = pi.base.bv->cursor();
361 x += scx_ + ADD_TO_TABULAR_WIDTH;
363 // Paint background of current tabular
364 int const w = tabular.getWidthOfTabular();
365 int const h = tabular.getHeightOfTabular();
366 int yy = y - tabular.getAscentOfRow(0);
367 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
369 if (!cur.selection())
371 if (!ptr_cmp(&cur.inset(), this))
377 if (tablemode(cur)) {
380 getSelection(cur, rs, re, cs, ce);
381 y -= tabular.getAscentOfRow(0);
382 for (row_type j = 0; j < tabular.rows(); ++j) {
383 int const a = tabular.getAscentOfRow(j);
384 int const h = a + tabular.getDescentOfRow(j);
386 y += tabular.getAdditionalHeight(j);
387 for (col_type i = 0; i < tabular.columns(); ++i) {
388 if (tabular.isPartOfMultiColumn(j, i))
390 idx_type const cell =
391 tabular.getCellNumber(j, i);
392 int const w = tabular.getWidthOfColumn(cell);
393 if (i >= cs && i <= ce && j >= rs && j <= re)
394 pi.pain.fillRectangle(xx, y, w, h,
403 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
408 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
409 row_type row, idx_type cell, bool erased) const
411 int x2 = x + tabular.getWidthOfColumn(cell);
413 LColor::color col = LColor::tabularline;
414 LColor::color onoffcol = LColor::tabularonoffline;
417 col = LColor::strikeout;
418 onoffcol = LColor::strikeout;
421 if (!tabular.topAlreadyDrawn(cell)) {
422 on_off = !tabular.topLine(cell);
423 pain.line(x, y - tabular.getAscentOfRow(row),
424 x2, y - tabular.getAscentOfRow(row),
425 on_off ? onoffcol : col,
426 on_off ? Painter::line_onoffdash : Painter::line_solid);
428 on_off = !tabular.bottomLine(cell);
429 pain.line(x, y + tabular.getDescentOfRow(row),
430 x2, y + tabular.getDescentOfRow(row),
431 on_off ? onoffcol : col,
432 on_off ? Painter::line_onoffdash : Painter::line_solid);
433 if (!tabular.leftAlreadyDrawn(cell)) {
434 on_off = !tabular.leftLine(cell);
435 pain.line(x, y - tabular.getAscentOfRow(row),
436 x, y + tabular.getDescentOfRow(row),
437 on_off ? onoffcol : col,
438 on_off ? Painter::line_onoffdash : Painter::line_solid);
440 on_off = !tabular.rightLine(cell);
441 pain.line(x2 - tabular.getAdditionalWidth(cell),
442 y - tabular.getAscentOfRow(row),
443 x2 - tabular.getAdditionalWidth(cell),
444 y + tabular.getDescentOfRow(row),
445 on_off ? onoffcol : col,
446 on_off ? Painter::line_onoffdash : Painter::line_solid);
450 string const InsetTabular::editMessage() const
452 return _("Opened table");
456 void InsetTabular::edit(LCursor & cur, bool left)
458 //lyxerr << "InsetTabular::edit: " << this << endl;
460 cur.selection() = false;
463 if (isRightToLeft(cur))
464 cur.idx() = tabular.getLastCellInRow(0);
470 if (isRightToLeft(cur))
471 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
473 cur.idx() = tabular.getNumberOfCells() - 1;
475 cur.pos() = cur.lastpos(); // FIXME crude guess
477 // this accesses the position cache before it is initialized
479 //cur.bv().fitCursor();
483 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
485 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
486 << "\n cur:" << cur << endl;
487 CursorSlice sl = cur.top();
488 LCursor & bvcur = cur.bv().cursor();
490 switch (cmd.action) {
492 case LFUN_MOUSE_PRESS:
493 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
495 if (cmd.button() == mouse_button::button1
496 || cmd.button() == mouse_button::button3) {
497 cur.selection() = false;
498 setCursorFromCoordinates(cur, cmd.x, cmd.y);
504 if (cmd.button() == mouse_button::button2) {
505 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
506 doDispatch(cur, cmd);
510 case LFUN_MOUSE_MOTION:
511 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
512 if (cmd.button() == mouse_button::button1) {
513 // only accept motions to places not deeper nested than the real anchor
514 if (bvcur.anchor_.hasPart(cur)) {
515 setCursorFromCoordinates(cur, cmd.x, cmd.y);
516 bvcur.setCursor(cur);
517 bvcur.selection() = true;
523 case LFUN_MOUSE_RELEASE:
524 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
525 if (cmd.button() == mouse_button::button3)
526 InsetTabularMailer(*this).showDialog(&cur.bv());
529 case LFUN_CELL_BACKWARD:
531 cur.selection() = false;
534 case LFUN_CELL_FORWARD:
536 cur.selection() = false;
539 case LFUN_CHAR_FORWARD_SELECT:
540 case LFUN_CHAR_FORWARD:
541 cell(cur.idx())->dispatch(cur, cmd);
542 if (!cur.result().dispatched()) {
543 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
545 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
551 case LFUN_CHAR_BACKWARD_SELECT:
552 case LFUN_CHAR_BACKWARD:
553 cell(cur.idx())->dispatch(cur, cmd);
554 if (!cur.result().dispatched()) {
555 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
557 cmd = FuncRequest(LFUN_FINISHED_LEFT);
563 case LFUN_DOWN_SELECT:
565 cell(cur.idx())->dispatch(cur, cmd);
566 cur.dispatched(); // override the cell's decision
568 // if our LyXText didn't do anything to the cursor
569 // then we try to put the cursor into the cell below
570 // setting also the right targetX.
571 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
572 cur.idx() = tabular.getCellBelow(cur.idx());
574 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
575 cur.pit(), 0, cur.targetX());
577 if (sl == cur.top()) {
578 // we trick it to go to the RIGHT after leaving the
580 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
587 cell(cur.idx())->dispatch(cur, cmd);
588 cur.dispatched(); // override the cell's decision
590 // if our LyXText didn't do anything to the cursor
591 // then we try to put the cursor into the cell above
592 // setting also the right targetX.
593 if (tabular.row_of_cell(cur.idx()) != 0) {
594 cur.idx() = tabular.getCellAbove(cur.idx());
595 cur.pit() = cur.lastpit();
596 LyXText const * text = cell(cur.idx())->getText(0);
597 cur.pos() = text->x2pos(
599 text->paragraphs().back().rows().size()-1,
602 if (sl == cur.top()) {
603 cmd = FuncRequest(LFUN_FINISHED_UP);
608 // case LFUN_SCREEN_DOWN: {
609 // //if (hasSelection())
610 // // cur.selection() = false;
611 // col_type const col = tabular.column_of_cell(cur.idx());
612 // int const t = cur.bv().top_y() + cur.bv().height();
613 // if (t < yo() + tabular.getHeightOfTabular()) {
614 // cur.bv().scrollDocView(t);
615 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
617 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
624 // case LFUN_SCREEN_UP: {
625 // //if (hasSelection())
626 // // cur.selection() = false;
627 // col_type const col = tabular.column_of_cell(cur.idx());
628 // int const t = cur.bv().top_y() + cur.bv().height();
630 // cur.bv().scrollDocView(t);
634 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
638 // cur.par() = cur.lastpar();
639 // cur.pos() = cur.lastpos();
643 case LFUN_LAYOUT_TABULAR:
644 InsetTabularMailer(*this).showDialog(&cur.bv());
647 case LFUN_INSET_DIALOG_UPDATE:
648 InsetTabularMailer(*this).updateDialog(&cur.bv());
651 case LFUN_TABULAR_FEATURE:
652 if (!tabularFeatures(cur, cmd.argument))
656 // insert file functions
657 case LFUN_FILE_INSERT_ASCII_PARA:
658 case LFUN_FILE_INSERT_ASCII: {
659 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
660 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
666 if (tablemode(cur)) {
667 if (copySelection(cur)) {
668 recordUndoInset(cur, Undo::DELETE);
673 cell(cur.idx())->dispatch(cur, cmd);
676 case LFUN_CHAR_DELETE_BACKWARD:
677 case LFUN_CHAR_DELETE_FORWARD:
678 if (tablemode(cur)) {
679 recordUndoInset(cur, Undo::DELETE);
683 cell(cur.idx())->dispatch(cur, cmd);
687 if (!cur.selection())
689 if (tablemode(cur)) {
693 cell(cur.idx())->dispatch(cur, cmd);
696 case LFUN_CLIPBOARD_PASTE:
697 case LFUN_PRIMARY_SELECTION_PASTE: {
698 string const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
699 cur.bv().owner()->gui().clipboard().get() :
700 cur.bv().owner()->gui().selection().get();
703 // pass to InsertAsciiString, but
704 // only if we have multi-cell content
705 if (clip.find_first_of("\t\n") != string::npos) {
706 if (insertAsciiString(cur.bv(), clip, false)) {
707 // content has been replaced,
708 // so cursor might be invalid
709 cur.pos() = cur.lastpos();
710 bvcur.setCursor(cur);
714 // so that the clipboard is used and it goes on
716 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
717 paste_tabular.reset();
718 dirtyTabularStack(false);
724 if (hasPasteBuffer() && tabularStackDirty()) {
725 recordUndoInset(cur, Undo::INSERT);
729 cell(cur.idx())->dispatch(cur, cmd);
730 // Reset pasted paragraphs:
731 if (tabular.getPWidth(cur.idx()).zero())
732 cell(cur.idx())->forceParagraphsToDefault(cur);
737 case LFUN_FONT_ROMAN:
743 case LFUN_FONT_FREE_APPLY:
744 case LFUN_FONT_FREE_UPDATE:
746 case LFUN_FONT_UNDERLINE:
748 case LFUN_WORD_CAPITALIZE:
749 case LFUN_WORD_UPCASE:
750 case LFUN_WORD_LOWCASE:
751 case LFUN_CHARS_TRANSPOSE:
752 if (tablemode(cur)) {
755 getSelection(cur, rs, re, cs, ce);
756 for (row_type i = rs; i <= re; ++i) {
757 for (col_type j = cs; j <= ce; ++j) {
758 // cursor follows cell:
759 cur.idx() = tabular.getCellNumber(i, j);
760 // select this cell only:
763 cur.pos() = cur.top().lastpos();
766 cell(cur.idx())->dispatch(cur, cmd);
769 // Restore original selection
770 cur.idx() = tabular.getCellNumber(rs, cs);
773 cur.idx() = tabular.getCellNumber(re, ce);
774 cur.pos() = cur.top().lastpos();
779 cell(cur.idx())->dispatch(cur, cmd);
783 // we try to handle this event in the insets dispatch function.
784 cell(cur.idx())->dispatch(cur, cmd);
789 InsetTabularMailer(*this).updateDialog(&cur.bv());
793 // function sets an object as defined in func_status.h:
794 // states OK, Unknown, Disabled, On, Off.
795 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
796 FuncStatus & status) const
798 switch (cmd.action) {
799 case LFUN_TABULAR_FEATURE: {
800 int action = LyXTabular::LAST_ACTION;
802 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
803 string const tmp = tabularFeature[i].feature;
804 if (tmp == cmd.argument.substr(0, tmp.length())) {
805 action = tabularFeature[i].action;
809 if (action == LyXTabular::LAST_ACTION) {
811 status.unknown(true);
815 string const argument
816 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
818 row_type sel_row_start = 0;
819 row_type sel_row_end = 0;
821 LyXTabular::ltType dummyltt;
824 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
827 case LyXTabular::SET_PWIDTH:
828 case LyXTabular::SET_MPWIDTH:
829 case LyXTabular::SET_SPECIAL_COLUMN:
830 case LyXTabular::SET_SPECIAL_MULTI:
831 case LyXTabular::APPEND_ROW:
832 case LyXTabular::APPEND_COLUMN:
833 case LyXTabular::DELETE_ROW:
834 case LyXTabular::DELETE_COLUMN:
835 case LyXTabular::SET_ALL_LINES:
836 case LyXTabular::UNSET_ALL_LINES:
837 case LyXTabular::SET_TOP_SPACE:
838 case LyXTabular::SET_BOTTOM_SPACE:
839 case LyXTabular::SET_INTERLINE_SPACE:
843 case LyXTabular::MULTICOLUMN:
844 status.setOnOff(tabular.isMultiColumn(cur.idx()));
847 case LyXTabular::M_TOGGLE_LINE_TOP:
849 case LyXTabular::TOGGLE_LINE_TOP:
850 status.setOnOff(tabular.topLine(cur.idx(), flag));
853 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
855 case LyXTabular::TOGGLE_LINE_BOTTOM:
856 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
859 case LyXTabular::M_TOGGLE_LINE_LEFT:
861 case LyXTabular::TOGGLE_LINE_LEFT:
862 status.setOnOff(tabular.leftLine(cur.idx(), flag));
865 case LyXTabular::M_TOGGLE_LINE_RIGHT:
867 case LyXTabular::TOGGLE_LINE_RIGHT:
868 status.setOnOff(tabular.rightLine(cur.idx(), flag));
871 case LyXTabular::M_ALIGN_LEFT:
873 case LyXTabular::ALIGN_LEFT:
874 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
877 case LyXTabular::M_ALIGN_RIGHT:
879 case LyXTabular::ALIGN_RIGHT:
880 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
883 case LyXTabular::M_ALIGN_CENTER:
885 case LyXTabular::ALIGN_CENTER:
886 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
889 case LyXTabular::ALIGN_BLOCK:
890 status.enabled(!tabular.getPWidth(cur.idx()).zero());
891 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
894 case LyXTabular::M_VALIGN_TOP:
896 case LyXTabular::VALIGN_TOP:
898 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
901 case LyXTabular::M_VALIGN_BOTTOM:
903 case LyXTabular::VALIGN_BOTTOM:
905 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
908 case LyXTabular::M_VALIGN_MIDDLE:
910 case LyXTabular::VALIGN_MIDDLE:
912 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
915 case LyXTabular::SET_LONGTABULAR:
916 status.setOnOff(tabular.isLongTabular());
919 case LyXTabular::UNSET_LONGTABULAR:
920 status.setOnOff(!tabular.isLongTabular());
923 case LyXTabular::SET_ROTATE_TABULAR:
924 status.setOnOff(tabular.getRotateTabular());
927 case LyXTabular::UNSET_ROTATE_TABULAR:
928 status.setOnOff(!tabular.getRotateTabular());
931 case LyXTabular::SET_ROTATE_CELL:
932 status.setOnOff(tabular.getRotateCell(cur.idx()));
935 case LyXTabular::UNSET_ROTATE_CELL:
936 status.setOnOff(!tabular.getRotateCell(cur.idx()));
939 case LyXTabular::SET_USEBOX:
940 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
943 case LyXTabular::SET_LTFIRSTHEAD:
944 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
947 case LyXTabular::UNSET_LTFIRSTHEAD:
948 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::SET_LTHEAD:
952 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
955 case LyXTabular::UNSET_LTHEAD:
956 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
959 case LyXTabular::SET_LTFOOT:
960 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
963 case LyXTabular::UNSET_LTFOOT:
964 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
967 case LyXTabular::SET_LTLASTFOOT:
968 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
971 case LyXTabular::UNSET_LTLASTFOOT:
972 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
975 case LyXTabular::SET_LTNEWPAGE:
976 status.setOnOff(tabular.getLTNewPage(sel_row_start));
979 case LyXTabular::SET_BOOKTABS:
980 status.setOnOff(tabular.useBookTabs());
983 case LyXTabular::UNSET_BOOKTABS:
984 status.setOnOff(!tabular.useBookTabs());
989 status.enabled(false);
995 // These are only enabled inside tabular
996 case LFUN_CELL_BACKWARD:
997 case LFUN_CELL_FORWARD:
998 status.enabled(true);
1001 // disable these with multiple cells selected
1002 case LFUN_INSET_INSERT:
1003 case LFUN_TABULAR_INSERT:
1004 case LFUN_CHARSTYLE_INSERT:
1005 case LFUN_FLOAT_INSERT:
1006 case LFUN_FLOAT_WIDE_INSERT:
1007 case LFUN_FOOTNOTE_INSERT:
1008 case LFUN_MARGINALNOTE_INSERT:
1009 case LFUN_MATH_INSERT:
1010 case LFUN_MATH_MODE:
1011 case LFUN_MATH_MUTATE:
1012 case LFUN_MATH_DISPLAY:
1013 case LFUN_NOTE_INSERT:
1014 case LFUN_OPTIONAL_INSERT:
1015 case LFUN_BOX_INSERT:
1016 case LFUN_BRANCH_INSERT:
1017 case LFUN_WRAP_INSERT:
1018 case LFUN_ERT_INSERT: {
1019 if (tablemode(cur)) {
1020 status.enabled(false);
1023 return cell(cur.idx())->getStatus(cur, cmd, status);
1026 // disable in non-fixed-width cells
1027 case LFUN_BREAK_LINE:
1028 case LFUN_BREAK_PARAGRAPH:
1029 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1030 case LFUN_BREAK_PARAGRAPH_SKIP: {
1031 if (tabular.getPWidth(cur.idx()).zero()) {
1032 status.enabled(false);
1035 return cell(cur.idx())->getStatus(cur, cmd, status);
1039 if (tabularStackDirty()) {
1040 status.enabled(true);
1044 case LFUN_INSET_MODIFY:
1045 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1046 status.enabled(true);
1052 // we try to handle this event in the insets dispatch function.
1053 return cell(cur.idx())->getStatus(cur, cmd, status);
1058 int InsetTabular::latex(Buffer const & buf, ostream & os,
1059 OutputParams const & runparams) const
1061 return tabular.latex(buf, os, runparams);
1065 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1066 OutputParams const & runparams) const
1068 int const dp = runparams.linelen ? runparams.depth : 0;
1069 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1073 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1074 OutputParams const & runparams) const
1077 InsetBase * master = 0;
1079 #ifdef WITH_WARNINGS
1080 #warning Why not pass a proper DocIterator here?
1083 // if the table is inside a float it doesn't need the informaltable
1084 // wrapper. Search for it.
1085 for (master = owner(); master; master = master->owner())
1086 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1091 os << "<informaltable>";
1094 ret += tabular.docbook(buf, os, runparams);
1096 os << "</informaltable>";
1103 void InsetTabular::validate(LaTeXFeatures & features) const
1105 tabular.validate(features);
1109 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1111 return tabular.getCellInset(idx);
1115 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1117 return tabular.getCellInset(idx);
1121 void InsetTabular::cursorPos
1122 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1124 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1126 // y offset correction
1127 int const row = tabular.row_of_cell(sl.idx());
1128 for (int i = 0; i <= row; ++i) {
1130 y += tabular.getAscentOfRow(i);
1131 y += tabular.getAdditionalHeight(i);
1134 y += tabular.getDescentOfRow(i);
1137 // x offset correction
1138 int const col = tabular.column_of_cell(sl.idx());
1139 int idx = tabular.getCellNumber(row, 0);
1140 for (int j = 0; j < col; ++j) {
1141 if (tabular.isPartOfMultiColumn(row, j))
1143 x += tabular.getWidthOfColumn(idx);
1146 x += tabular.getBeginningOfTextInCell(idx);
1147 x += ADD_TO_TABULAR_WIDTH;
1152 int InsetTabular::dist(idx_type const cell, int x, int y) const
1156 InsetBase const & inset = *tabular.getCellInset(cell);
1157 Point o = theCoords.getInsets().xy(&inset);
1158 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1159 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1160 row_type const row = tabular.row_of_cell(cell);
1161 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1162 tabular.getAdditionalHeight(row);
1163 int const yend = o.y_ + tabular.getDescentOfRow(row);
1175 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1176 // << " ybeg=" << ybeg << " yend=" << yend
1177 // << " xx=" << xx << " yy=" << yy
1178 // << " dist=" << xx + yy << endl;
1183 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1185 //lyxerr << "InsetTabular::editXY: " << this << endl;
1186 cur.selection() = false;
1188 cur.idx() = getNearestCell(x, y);
1190 return cell(cur.idx())->text_.editXY(cur, x, y);
1194 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1196 cur.idx() = getNearestCell(x, y);
1197 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1201 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1203 idx_type idx_min = 0;
1204 int dist_min = std::numeric_limits<int>::max();
1205 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1206 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1207 int const d = dist(i, x, y);
1218 int InsetTabular::getCellXPos(idx_type const cell) const
1222 for (; !tabular.isFirstCellInRow(c); --c)
1224 int lx = tabular.getWidthOfColumn(cell);
1225 for (; c < cell; ++c)
1226 lx += tabular.getWidthOfColumn(c);
1228 return lx - tabular.getWidthOfColumn(cell);
1232 void InsetTabular::resetPos(LCursor & cur) const
1234 BufferView & bv = cur.bv();
1235 int const maxwidth = bv.workWidth();
1237 if (&cur.inset() != this) {
1241 int const X2 = maxwidth;
1242 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1243 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1244 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1247 scx_ = X1 + 20 - x1;
1249 scx_ = X2 - 20 - x2;
1256 InsetTabularMailer(*this).updateDialog(&bv);
1260 void InsetTabular::moveNextCell(LCursor & cur)
1262 if (isRightToLeft(cur)) {
1263 if (tabular.isFirstCellInRow(cur.idx())) {
1264 row_type const row = tabular.row_of_cell(cur.idx());
1265 if (row == tabular.rows() - 1)
1267 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1274 if (tabular.isLastCell(cur.idx()))
1284 void InsetTabular::movePrevCell(LCursor & cur)
1286 if (isRightToLeft(cur)) {
1287 if (tabular.isLastCellInRow(cur.idx())) {
1288 row_type const row = tabular.row_of_cell(cur.idx());
1291 cur.idx() = tabular.getFirstCellInRow(row);
1292 cur.idx() = tabular.getCellAbove(cur.idx());
1294 if (tabular.isLastCell(cur.idx()))
1299 if (cur.idx() == 0) // first cell
1303 cur.pit() = cur.lastpit();
1304 cur.pos() = cur.lastpos();
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::M_TOGGLE_LINE_TOP:
1472 case LyXTabular::TOGGLE_LINE_TOP: {
1473 bool lineSet = !tabular.topLine(cur.idx(), flag);
1474 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1475 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1477 tabular.getCellNumber(i, j),
1482 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1484 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1485 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1486 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1487 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1488 tabular.setBottomLine(
1489 tabular.getCellNumber(i, j),
1495 case LyXTabular::M_TOGGLE_LINE_LEFT:
1497 case LyXTabular::TOGGLE_LINE_LEFT: {
1498 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1499 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1500 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1501 tabular.setLeftLine(
1502 tabular.getCellNumber(i,j),
1508 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1510 case LyXTabular::TOGGLE_LINE_RIGHT: {
1511 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1512 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1513 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1514 tabular.setRightLine(
1515 tabular.getCellNumber(i,j),
1521 case LyXTabular::M_ALIGN_LEFT:
1522 case LyXTabular::M_ALIGN_RIGHT:
1523 case LyXTabular::M_ALIGN_CENTER:
1525 case LyXTabular::ALIGN_LEFT:
1526 case LyXTabular::ALIGN_RIGHT:
1527 case LyXTabular::ALIGN_CENTER:
1528 case LyXTabular::ALIGN_BLOCK:
1529 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1530 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1531 tabular.setAlignment(
1532 tabular.getCellNumber(i, j),
1537 case LyXTabular::M_VALIGN_TOP:
1538 case LyXTabular::M_VALIGN_BOTTOM:
1539 case LyXTabular::M_VALIGN_MIDDLE:
1541 case LyXTabular::VALIGN_TOP:
1542 case LyXTabular::VALIGN_BOTTOM:
1543 case LyXTabular::VALIGN_MIDDLE:
1544 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1545 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1546 tabular.setVAlignment(
1547 tabular.getCellNumber(i, j),
1551 case LyXTabular::MULTICOLUMN: {
1552 if (sel_row_start != sel_row_end) {
1553 #ifdef WITH_WARNINGS
1554 #warning Need I say it ? This is horrible.
1556 Alert::error(_("Error setting multicolumn"),
1557 _("You cannot set multicolumn vertically."));
1560 if (!cur.selection()) {
1561 // just multicol for one single cell
1562 // check whether we are completely in a multicol
1563 if (tabular.isMultiColumn(cur.idx()))
1564 tabular.unsetMultiColumn(cur.idx());
1566 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1569 // we have a selection so this means we just add all this
1570 // cells to form a multicolumn cell
1571 idx_type const s_start = cur.selBegin().idx();
1572 idx_type const s_end = cur.selEnd().idx();
1573 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1574 cur.idx() = s_start;
1577 cur.selection() = false;
1581 case LyXTabular::SET_ALL_LINES:
1583 case LyXTabular::UNSET_ALL_LINES:
1584 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1585 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1586 tabular.setAllLines(
1587 tabular.getCellNumber(i,j), setLines);
1590 case LyXTabular::SET_LONGTABULAR:
1591 tabular.setLongTabular(true);
1594 case LyXTabular::UNSET_LONGTABULAR:
1595 tabular.setLongTabular(false);
1598 case LyXTabular::SET_ROTATE_TABULAR:
1599 tabular.setRotateTabular(true);
1602 case LyXTabular::UNSET_ROTATE_TABULAR:
1603 tabular.setRotateTabular(false);
1606 case LyXTabular::SET_ROTATE_CELL:
1607 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1608 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1609 tabular.setRotateCell(
1610 tabular.getCellNumber(i, j), true);
1613 case LyXTabular::UNSET_ROTATE_CELL:
1614 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1615 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1616 tabular.setRotateCell(
1617 tabular.getCellNumber(i, j), false);
1620 case LyXTabular::SET_USEBOX: {
1621 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1622 if (val == tabular.getUsebox(cur.idx()))
1623 val = LyXTabular::BOX_NONE;
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.setUsebox(tabular.getCellNumber(i, j), val);
1630 case LyXTabular::UNSET_LTFIRSTHEAD:
1632 case LyXTabular::SET_LTFIRSTHEAD:
1633 tabular.getRowOfLTFirstHead(row, ltt);
1634 checkLongtableSpecial(ltt, value, flag);
1635 tabular.setLTHead(row, flag, ltt, true);
1638 case LyXTabular::UNSET_LTHEAD:
1640 case LyXTabular::SET_LTHEAD:
1641 tabular.getRowOfLTHead(row, ltt);
1642 checkLongtableSpecial(ltt, value, flag);
1643 tabular.setLTHead(row, flag, ltt, false);
1646 case LyXTabular::UNSET_LTFOOT:
1648 case LyXTabular::SET_LTFOOT:
1649 tabular.getRowOfLTFoot(row, ltt);
1650 checkLongtableSpecial(ltt, value, flag);
1651 tabular.setLTFoot(row, flag, ltt, false);
1654 case LyXTabular::UNSET_LTLASTFOOT:
1656 case LyXTabular::SET_LTLASTFOOT:
1657 tabular.getRowOfLTLastFoot(row, ltt);
1658 checkLongtableSpecial(ltt, value, flag);
1659 tabular.setLTFoot(row, flag, ltt, true);
1662 case LyXTabular::SET_LTNEWPAGE:
1663 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1666 case LyXTabular::SET_BOOKTABS:
1667 tabular.setBookTabs(true);
1670 case LyXTabular::UNSET_BOOKTABS:
1671 tabular.setBookTabs(false);
1674 case LyXTabular::SET_TOP_SPACE: {
1676 if (value == "default")
1677 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1678 tabular.row_info[i].top_space_default = true;
1679 else if (isValidLength(value, &len))
1680 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1681 tabular.row_info[i].top_space_default = false;
1682 tabular.row_info[i].top_space = len;
1685 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1686 tabular.row_info[i].top_space_default = false;
1687 tabular.row_info[i].top_space = len;
1692 case LyXTabular::SET_BOTTOM_SPACE: {
1694 if (value == "default")
1695 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1696 tabular.row_info[i].bottom_space_default = true;
1697 else if (isValidLength(value, &len))
1698 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1699 tabular.row_info[i].bottom_space_default = false;
1700 tabular.row_info[i].bottom_space = len;
1703 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1704 tabular.row_info[i].bottom_space_default = false;
1705 tabular.row_info[i].bottom_space = len;
1710 case LyXTabular::SET_INTERLINE_SPACE: {
1712 if (value == "default")
1713 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1714 tabular.row_info[i].interline_space_default = true;
1715 else if (isValidLength(value, &len))
1716 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1717 tabular.row_info[i].interline_space_default = false;
1718 tabular.row_info[i].interline_space = len;
1721 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1722 tabular.row_info[i].interline_space_default = false;
1723 tabular.row_info[i].interline_space = len;
1728 // dummy stuff just to avoid warnings
1729 case LyXTabular::LAST_ACTION:
1733 InsetTabularMailer(*this).updateDialog(&bv);
1737 bool InsetTabular::showInsetDialog(BufferView * bv) const
1739 InsetTabularMailer(*this).showDialog(bv);
1744 void InsetTabular::openLayoutDialog(BufferView * bv) const
1746 InsetTabularMailer(*this).showDialog(bv);
1750 bool InsetTabular::copySelection(LCursor & cur)
1752 if (!cur.selection())
1757 getSelection(cur, rs, re, cs, ce);
1759 paste_tabular.reset(new LyXTabular(tabular));
1761 for (row_type i = 0; i < rs; ++i)
1762 paste_tabular->deleteRow(0);
1764 row_type const rows = re - rs + 1;
1765 while (paste_tabular->rows() > rows)
1766 paste_tabular->deleteRow(rows);
1768 paste_tabular->setTopLine(0, true, true);
1769 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1772 for (col_type i = 0; i < cs; ++i)
1773 paste_tabular->deleteColumn(0);
1775 col_type const columns = ce - cs + 1;
1776 while (paste_tabular->columns() > columns)
1777 paste_tabular->deleteColumn(columns);
1779 paste_tabular->setLeftLine(0, true, true);
1780 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1784 OutputParams const runparams;
1785 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1786 cur.bv().owner()->gui().clipboard().put(os.str());
1787 // mark tabular stack dirty
1788 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1789 // when we (hopefully) have a one-for-all paste mechanism.
1790 lyx::cap::dirtyTabularStack(true);
1796 bool InsetTabular::pasteSelection(LCursor & cur)
1800 col_type const actcol = tabular.column_of_cell(cur.idx());
1801 row_type const actrow = tabular.row_of_cell(cur.idx());
1802 for (row_type r1 = 0, r2 = actrow;
1803 r1 < paste_tabular->rows() && r2 < tabular.rows();
1805 for (col_type c1 = 0, c2 = actcol;
1806 c1 < paste_tabular->columns() && c2 < tabular.columns();
1808 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1809 tabular.isPartOfMultiColumn(r2, c2))
1811 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1815 if (tabular.isPartOfMultiColumn(r2, c2)) {
1819 shared_ptr<InsetText> inset(
1820 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1821 tabular.setCellInset(r2, c2, inset);
1830 void InsetTabular::cutSelection(LCursor & cur)
1832 if (!cur.selection())
1837 getSelection(cur, rs, re, cs, ce);
1838 for (row_type i = rs; i <= re; ++i) {
1839 for (col_type j = cs; j <= ce; ++j) {
1840 shared_ptr<InsetText> t
1841 = cell(tabular.getCellNumber(i, j));
1842 if (cur.buffer().params().tracking_changes)
1843 t->markErased(true);
1849 // cursor position might be invalid now
1850 cur.pos() = cur.lastpos();
1851 cur.clearSelection();
1855 bool InsetTabular::isRightToLeft(LCursor & cur) const
1857 BOOST_ASSERT(cur.depth() > 1);
1858 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1859 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1860 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1861 parentpos).language()->rightToLeft();
1865 void InsetTabular::getSelection(LCursor & cur,
1866 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1868 CursorSlice const & beg = cur.selBegin();
1869 CursorSlice const & end = cur.selEnd();
1870 cs = tabular.column_of_cell(beg.idx());
1871 ce = tabular.column_of_cell(end.idx());
1874 cs = tabular.column_of_cell(end.idx());
1876 ce = tabular.right_column_of_cell(end.idx());
1879 rs = tabular.row_of_cell(beg.idx());
1880 re = tabular.row_of_cell(end.idx());
1886 LyXText * InsetTabular::getText(int idx) const
1888 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1892 void InsetTabular::markErased(bool erased)
1894 for (idx_type idx = 0; idx < nargs(); ++idx)
1895 cell(idx)->markErased(erased);
1899 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1901 return tabular.getPWidth(cell).zero();
1905 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1908 if (buf.length() <= 0)
1913 col_type maxCols = 1;
1914 string::size_type const len = buf.length();
1915 string::size_type p = 0;
1917 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1925 maxCols = max(cols, maxCols);
1931 maxCols = max(cols, maxCols);
1932 LyXTabular * loctab;
1937 paste_tabular.reset(
1938 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1939 loctab = paste_tabular.get();
1941 dirtyTabularStack(true);
1944 cell = bv.cursor().idx();
1945 ocol = tabular.column_of_cell(cell);
1946 row = tabular.row_of_cell(cell);
1949 string::size_type op = 0;
1950 idx_type const cells = loctab->getNumberOfCells();
1953 rows = loctab->rows();
1954 col_type const columns = loctab->columns();
1956 while (cell < cells && p < len && row < rows &&
1957 (p = buf.find_first_of("\t\n", p)) != string::npos)
1963 // we can only set this if we are not too far right
1964 if (cols < columns) {
1965 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1966 inset->setViewCache(&bv);
1967 Paragraph & par = inset->text_.getPar(0);
1968 LyXFont const font = inset->text_.getFont(par, 0);
1969 inset->setText(buf.substr(op, p - op), font);
1975 // we can only set this if we are not too far right
1976 if (cols < columns) {
1977 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1978 inset->setViewCache(&bv);
1979 Paragraph & par = inset->text_.getPar(0);
1980 LyXFont const font = inset->text_.getFont(par, 0);
1981 inset->setText(buf.substr(op, p - op), font);
1986 cell = loctab->getCellNumber(row, cols);
1992 // check for the last cell if there is no trailing '\n'
1993 if (cell < cells && op < len) {
1994 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1995 inset->setViewCache(&bv);
1996 Paragraph & par = inset->text_.getPar(0);
1997 LyXFont const font = inset->text_.getFont(par, 0);
1998 inset->setText(buf.substr(op, len - op), font);
2004 void InsetTabular::addPreview(PreviewLoader & loader) const
2006 row_type const rows = tabular.rows();
2007 col_type const columns = tabular.columns();
2008 for (row_type i = 0; i < rows; ++i) {
2009 for (col_type j = 0; j < columns; ++j)
2010 tabular.getCellInset(i, j)->addPreview(loader);
2015 bool InsetTabular::tablemode(LCursor & cur) const
2017 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2024 string const InsetTabularMailer::name_("tabular");
2026 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2027 : inset_(const_cast<InsetTabular &>(inset))
2031 string const InsetTabularMailer::inset2string(Buffer const &) const
2033 return params2string(inset_);
2037 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2039 istringstream data(in);
2041 lex.setStream(data);
2048 if (!lex || token != name_)
2049 return print_mailer_error("InsetTabularMailer", in, 1,
2052 // This is part of the inset proper that is usually swallowed
2053 // by Buffer::readInset
2055 if (!lex || token != "Tabular")
2056 return print_mailer_error("InsetTabularMailer", in, 2,
2059 Buffer const & buffer = inset.buffer();
2060 inset.read(buffer, lex);
2064 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2067 data << name_ << ' ';
2068 inset.write(inset.buffer(), data);
2069 data << "\\end_inset\n";