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/Application.h"
41 #include "frontends/font_metrics.h"
42 #include "frontends/Clipboard.h"
43 #include "frontends/Painter.h"
44 #include "frontends/Selection.h"
45 #include "frontends/nullpainter.h"
53 using lyx::cap::dirtyTabularStack;
54 using lyx::cap::tabularStackDirty;
56 using lyx::graphics::PreviewLoader;
58 using lyx::support::ltrim;
60 using lyx::frontend::Painter;
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 docstring 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, lyx::to_utf8(cmd.argument())))
656 // insert file functions
657 case LFUN_FILE_INSERT_ASCII_PARA:
658 case LFUN_FILE_INSERT_ASCII: {
659 // FIXME: We don't know the encoding of filenames
660 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), lyx::to_utf8(cmd.argument()), false);
661 // FIXME: We don't know the encoding of the file
662 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), lyx::from_utf8(tmpstr), false))
668 if (tablemode(cur)) {
669 if (copySelection(cur)) {
670 recordUndoInset(cur, Undo::DELETE);
675 cell(cur.idx())->dispatch(cur, cmd);
678 case LFUN_CHAR_DELETE_BACKWARD:
679 case LFUN_CHAR_DELETE_FORWARD:
680 if (tablemode(cur)) {
681 recordUndoInset(cur, Undo::DELETE);
685 cell(cur.idx())->dispatch(cur, cmd);
689 if (!cur.selection())
691 if (tablemode(cur)) {
695 cell(cur.idx())->dispatch(cur, cmd);
698 case LFUN_CLIPBOARD_PASTE:
699 case LFUN_PRIMARY_SELECTION_PASTE: {
700 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
701 theApp->clipboard().get() :
702 theApp->selection().get();
705 // pass to InsertAsciiString, but
706 // only if we have multi-cell content
707 if (clip.find_first_of(lyx::from_ascii("\t\n")) != docstring::npos) {
708 if (insertAsciiString(cur.bv(), clip, false)) {
709 // content has been replaced,
710 // so cursor might be invalid
711 cur.pos() = cur.lastpos();
712 bvcur.setCursor(cur);
716 // so that the clipboard is used and it goes on
718 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
719 paste_tabular.reset();
720 dirtyTabularStack(false);
726 if (hasPasteBuffer() && tabularStackDirty()) {
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);
783 InsetTabularMailer(*this).updateDialog(&cur.bv());
787 // function sets an object as defined in func_status.h:
788 // states OK, Unknown, Disabled, On, Off.
789 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
790 FuncStatus & status) const
792 switch (cmd.action) {
793 case LFUN_TABULAR_FEATURE: {
794 int action = LyXTabular::LAST_ACTION;
796 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
797 string const tmp = tabularFeature[i].feature;
798 if (tmp == lyx::to_utf8(cmd.argument()).substr(0, tmp.length())) {
799 action = tabularFeature[i].action;
803 if (action == LyXTabular::LAST_ACTION) {
805 status.unknown(true);
809 string const argument
810 = ltrim(lyx::to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
812 row_type sel_row_start = 0;
813 row_type sel_row_end = 0;
815 LyXTabular::ltType dummyltt;
818 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
821 case LyXTabular::SET_PWIDTH:
822 case LyXTabular::SET_MPWIDTH:
823 case LyXTabular::SET_SPECIAL_COLUMN:
824 case LyXTabular::SET_SPECIAL_MULTI:
825 case LyXTabular::APPEND_ROW:
826 case LyXTabular::APPEND_COLUMN:
827 case LyXTabular::DELETE_ROW:
828 case LyXTabular::DELETE_COLUMN:
829 case LyXTabular::SET_ALL_LINES:
830 case LyXTabular::UNSET_ALL_LINES:
831 case LyXTabular::SET_TOP_SPACE:
832 case LyXTabular::SET_BOTTOM_SPACE:
833 case LyXTabular::SET_INTERLINE_SPACE:
837 case LyXTabular::MULTICOLUMN:
838 status.setOnOff(tabular.isMultiColumn(cur.idx()));
841 case LyXTabular::M_TOGGLE_LINE_TOP:
843 case LyXTabular::TOGGLE_LINE_TOP:
844 status.setOnOff(tabular.topLine(cur.idx(), flag));
847 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
849 case LyXTabular::TOGGLE_LINE_BOTTOM:
850 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
853 case LyXTabular::M_TOGGLE_LINE_LEFT:
855 case LyXTabular::TOGGLE_LINE_LEFT:
856 status.setOnOff(tabular.leftLine(cur.idx(), flag));
859 case LyXTabular::M_TOGGLE_LINE_RIGHT:
861 case LyXTabular::TOGGLE_LINE_RIGHT:
862 status.setOnOff(tabular.rightLine(cur.idx(), flag));
865 case LyXTabular::M_ALIGN_LEFT:
867 case LyXTabular::ALIGN_LEFT:
868 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
871 case LyXTabular::M_ALIGN_RIGHT:
873 case LyXTabular::ALIGN_RIGHT:
874 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
877 case LyXTabular::M_ALIGN_CENTER:
879 case LyXTabular::ALIGN_CENTER:
880 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
883 case LyXTabular::ALIGN_BLOCK:
884 status.enabled(!tabular.getPWidth(cur.idx()).zero());
885 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
888 case LyXTabular::M_VALIGN_TOP:
890 case LyXTabular::VALIGN_TOP:
892 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
895 case LyXTabular::M_VALIGN_BOTTOM:
897 case LyXTabular::VALIGN_BOTTOM:
899 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
902 case LyXTabular::M_VALIGN_MIDDLE:
904 case LyXTabular::VALIGN_MIDDLE:
906 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
909 case LyXTabular::SET_LONGTABULAR:
910 status.setOnOff(tabular.isLongTabular());
913 case LyXTabular::UNSET_LONGTABULAR:
914 status.setOnOff(!tabular.isLongTabular());
917 case LyXTabular::SET_ROTATE_TABULAR:
918 status.setOnOff(tabular.getRotateTabular());
921 case LyXTabular::UNSET_ROTATE_TABULAR:
922 status.setOnOff(!tabular.getRotateTabular());
925 case LyXTabular::SET_ROTATE_CELL:
926 status.setOnOff(tabular.getRotateCell(cur.idx()));
929 case LyXTabular::UNSET_ROTATE_CELL:
930 status.setOnOff(!tabular.getRotateCell(cur.idx()));
933 case LyXTabular::SET_USEBOX:
934 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
937 case LyXTabular::SET_LTFIRSTHEAD:
938 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
941 case LyXTabular::UNSET_LTFIRSTHEAD:
942 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
945 case LyXTabular::SET_LTHEAD:
946 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
949 case LyXTabular::UNSET_LTHEAD:
950 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
953 case LyXTabular::SET_LTFOOT:
954 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
957 case LyXTabular::UNSET_LTFOOT:
958 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
961 case LyXTabular::SET_LTLASTFOOT:
962 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
965 case LyXTabular::UNSET_LTLASTFOOT:
966 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
969 case LyXTabular::SET_LTNEWPAGE:
970 status.setOnOff(tabular.getLTNewPage(sel_row_start));
973 case LyXTabular::SET_BOOKTABS:
974 status.setOnOff(tabular.useBookTabs());
977 case LyXTabular::UNSET_BOOKTABS:
978 status.setOnOff(!tabular.useBookTabs());
983 status.enabled(false);
989 // These are only enabled inside tabular
990 case LFUN_CELL_BACKWARD:
991 case LFUN_CELL_FORWARD:
992 status.enabled(true);
995 // disable these with multiple cells selected
996 case LFUN_INSET_INSERT:
997 case LFUN_TABULAR_INSERT:
998 case LFUN_CHARSTYLE_INSERT:
999 case LFUN_FLOAT_INSERT:
1000 case LFUN_FLOAT_WIDE_INSERT:
1001 case LFUN_FOOTNOTE_INSERT:
1002 case LFUN_MARGINALNOTE_INSERT:
1003 case LFUN_MATH_INSERT:
1004 case LFUN_MATH_MODE:
1005 case LFUN_MATH_MUTATE:
1006 case LFUN_MATH_DISPLAY:
1007 case LFUN_NOTE_INSERT:
1008 case LFUN_OPTIONAL_INSERT:
1009 case LFUN_BOX_INSERT:
1010 case LFUN_BRANCH_INSERT:
1011 case LFUN_WRAP_INSERT:
1012 case LFUN_ERT_INSERT: {
1013 if (tablemode(cur)) {
1014 status.enabled(false);
1017 return cell(cur.idx())->getStatus(cur, cmd, status);
1020 // disable in non-fixed-width cells
1021 case LFUN_BREAK_LINE:
1022 case LFUN_BREAK_PARAGRAPH:
1023 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1024 case LFUN_BREAK_PARAGRAPH_SKIP: {
1025 if (tabular.getPWidth(cur.idx()).zero()) {
1026 status.enabled(false);
1029 return cell(cur.idx())->getStatus(cur, cmd, status);
1033 if (tabularStackDirty()) {
1034 status.enabled(true);
1038 case LFUN_INSET_MODIFY:
1039 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1040 status.enabled(true);
1046 // we try to handle this event in the insets dispatch function.
1047 return cell(cur.idx())->getStatus(cur, cmd, status);
1052 int InsetTabular::latex(Buffer const & buf, ostream & os,
1053 OutputParams const & runparams) const
1055 return tabular.latex(buf, os, runparams);
1059 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1060 OutputParams const & runparams) const
1062 int const dp = runparams.linelen ? runparams.depth : 0;
1063 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1067 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1068 OutputParams const & runparams) const
1071 InsetBase * master = 0;
1073 #ifdef WITH_WARNINGS
1074 #warning Why not pass a proper DocIterator here?
1077 // if the table is inside a float it doesn't need the informaltable
1078 // wrapper. Search for it.
1079 for (master = owner(); master; master = master->owner())
1080 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1085 os << "<informaltable>";
1088 ret += tabular.docbook(buf, os, runparams);
1090 os << "</informaltable>";
1097 void InsetTabular::validate(LaTeXFeatures & features) const
1099 tabular.validate(features);
1103 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1105 return tabular.getCellInset(idx);
1109 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1111 return tabular.getCellInset(idx);
1115 void InsetTabular::cursorPos
1116 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1118 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1120 // y offset correction
1121 int const row = tabular.row_of_cell(sl.idx());
1122 for (int i = 0; i <= row; ++i) {
1124 y += tabular.getAscentOfRow(i);
1125 y += tabular.getAdditionalHeight(i);
1128 y += tabular.getDescentOfRow(i);
1131 // x offset correction
1132 int const col = tabular.column_of_cell(sl.idx());
1133 int idx = tabular.getCellNumber(row, 0);
1134 for (int j = 0; j < col; ++j) {
1135 if (tabular.isPartOfMultiColumn(row, j))
1137 x += tabular.getWidthOfColumn(idx);
1140 x += tabular.getBeginningOfTextInCell(idx);
1141 x += ADD_TO_TABULAR_WIDTH;
1146 int InsetTabular::dist(idx_type const cell, int x, int y) const
1150 InsetBase const & inset = *tabular.getCellInset(cell);
1151 Point o = theCoords.getInsets().xy(&inset);
1152 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1153 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1154 row_type const row = tabular.row_of_cell(cell);
1155 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1156 tabular.getAdditionalHeight(row);
1157 int const yend = o.y_ + tabular.getDescentOfRow(row);
1169 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1170 // << " ybeg=" << ybeg << " yend=" << yend
1171 // << " xx=" << xx << " yy=" << yy
1172 // << " dist=" << xx + yy << endl;
1177 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1179 //lyxerr << "InsetTabular::editXY: " << this << endl;
1180 cur.selection() = false;
1182 cur.idx() = getNearestCell(x, y);
1184 return cell(cur.idx())->text_.editXY(cur, x, y);
1188 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1190 cur.idx() = getNearestCell(x, y);
1191 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1195 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1197 idx_type idx_min = 0;
1198 int dist_min = std::numeric_limits<int>::max();
1199 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1200 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1201 int const d = dist(i, x, y);
1212 int InsetTabular::getCellXPos(idx_type const cell) const
1216 for (; !tabular.isFirstCellInRow(c); --c)
1218 int lx = tabular.getWidthOfColumn(cell);
1219 for (; c < cell; ++c)
1220 lx += tabular.getWidthOfColumn(c);
1222 return lx - tabular.getWidthOfColumn(cell);
1226 void InsetTabular::resetPos(LCursor & cur) const
1228 BufferView & bv = cur.bv();
1229 int const maxwidth = bv.workWidth();
1231 if (&cur.inset() != this) {
1235 int const X2 = maxwidth;
1236 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1237 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1238 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1241 scx_ = X1 + 20 - x1;
1243 scx_ = X2 - 20 - x2;
1250 InsetTabularMailer(*this).updateDialog(&bv);
1254 void InsetTabular::moveNextCell(LCursor & cur)
1256 if (isRightToLeft(cur)) {
1257 if (tabular.isFirstCellInRow(cur.idx())) {
1258 row_type const row = tabular.row_of_cell(cur.idx());
1259 if (row == tabular.rows() - 1)
1261 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1268 if (tabular.isLastCell(cur.idx()))
1278 void InsetTabular::movePrevCell(LCursor & cur)
1280 if (isRightToLeft(cur)) {
1281 if (tabular.isLastCellInRow(cur.idx())) {
1282 row_type const row = tabular.row_of_cell(cur.idx());
1285 cur.idx() = tabular.getFirstCellInRow(row);
1286 cur.idx() = tabular.getCellAbove(cur.idx());
1288 if (tabular.isLastCell(cur.idx()))
1293 if (cur.idx() == 0) // first cell
1297 cur.pit() = cur.lastpit();
1298 cur.pos() = cur.lastpos();
1303 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1305 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1308 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1309 string const tmp = tabularFeature[i].feature;
1311 if (tmp == what.substr(0, tmp.length())) {
1312 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1313 //tabularFeatures[i].feature.length())) {
1314 action = tabularFeature[i].action;
1318 if (action == LyXTabular::LAST_ACTION)
1322 ltrim(what.substr(tabularFeature[i].feature.length()));
1323 tabularFeatures(cur, action, val);
1330 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1331 string const & special, bool & flag)
1333 if (special == "dl_above") {
1336 } else if (special == "dl_below") {
1337 ltt.bottomDL = flag;
1339 } else if (special == "empty") {
1351 void InsetTabular::tabularFeatures(LCursor & cur,
1352 LyXTabular::Feature feature, string const & value)
1354 BufferView & bv = cur.bv();
1355 col_type sel_col_start;
1356 col_type sel_col_end;
1357 row_type sel_row_start;
1358 row_type sel_row_end;
1359 bool setLines = false;
1360 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1361 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1365 case LyXTabular::M_ALIGN_LEFT:
1366 case LyXTabular::ALIGN_LEFT:
1367 setAlign = LYX_ALIGN_LEFT;
1370 case LyXTabular::M_ALIGN_RIGHT:
1371 case LyXTabular::ALIGN_RIGHT:
1372 setAlign = LYX_ALIGN_RIGHT;
1375 case LyXTabular::M_ALIGN_CENTER:
1376 case LyXTabular::ALIGN_CENTER:
1377 setAlign = LYX_ALIGN_CENTER;
1380 case LyXTabular::ALIGN_BLOCK:
1381 setAlign = LYX_ALIGN_BLOCK;
1384 case LyXTabular::M_VALIGN_TOP:
1385 case LyXTabular::VALIGN_TOP:
1386 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1389 case LyXTabular::M_VALIGN_BOTTOM:
1390 case LyXTabular::VALIGN_BOTTOM:
1391 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1394 case LyXTabular::M_VALIGN_MIDDLE:
1395 case LyXTabular::VALIGN_MIDDLE:
1396 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1403 recordUndoInset(cur, Undo::ATOMIC);
1405 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1406 row_type const row = tabular.row_of_cell(cur.idx());
1407 col_type const column = tabular.column_of_cell(cur.idx());
1409 LyXTabular::ltType ltt;
1413 case LyXTabular::SET_PWIDTH: {
1414 LyXLength const len(value);
1415 tabular.setColumnPWidth(cur, cur.idx(), len);
1417 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1418 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1422 case LyXTabular::SET_MPWIDTH:
1423 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1426 case LyXTabular::SET_SPECIAL_COLUMN:
1427 case LyXTabular::SET_SPECIAL_MULTI:
1428 tabular.setAlignSpecial(cur.idx(),value,feature);
1431 case LyXTabular::APPEND_ROW:
1432 // append the row into the tabular
1433 tabular.appendRow(bv.buffer()->params(), cur.idx());
1436 case LyXTabular::APPEND_COLUMN:
1437 // append the column into the tabular
1438 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1439 cur.idx() = tabular.getCellNumber(row, column);
1442 case LyXTabular::DELETE_ROW:
1443 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1444 tabular.deleteRow(sel_row_start);
1445 if (sel_row_start >= tabular.rows())
1447 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1450 cur.selection() = false;
1453 case LyXTabular::DELETE_COLUMN:
1454 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1455 tabular.deleteColumn(sel_col_start);
1456 if (sel_col_start >= tabular.columns())
1458 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1461 cur.selection() = false;
1464 case LyXTabular::M_TOGGLE_LINE_TOP:
1466 case LyXTabular::TOGGLE_LINE_TOP: {
1467 bool lineSet = !tabular.topLine(cur.idx(), flag);
1468 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1469 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1471 tabular.getCellNumber(i, j),
1476 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1478 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1479 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1480 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1481 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1482 tabular.setBottomLine(
1483 tabular.getCellNumber(i, j),
1489 case LyXTabular::M_TOGGLE_LINE_LEFT:
1491 case LyXTabular::TOGGLE_LINE_LEFT: {
1492 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1493 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1494 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1495 tabular.setLeftLine(
1496 tabular.getCellNumber(i,j),
1502 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1504 case LyXTabular::TOGGLE_LINE_RIGHT: {
1505 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1506 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1507 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508 tabular.setRightLine(
1509 tabular.getCellNumber(i,j),
1515 case LyXTabular::M_ALIGN_LEFT:
1516 case LyXTabular::M_ALIGN_RIGHT:
1517 case LyXTabular::M_ALIGN_CENTER:
1519 case LyXTabular::ALIGN_LEFT:
1520 case LyXTabular::ALIGN_RIGHT:
1521 case LyXTabular::ALIGN_CENTER:
1522 case LyXTabular::ALIGN_BLOCK:
1523 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1524 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1525 tabular.setAlignment(
1526 tabular.getCellNumber(i, j),
1531 case LyXTabular::M_VALIGN_TOP:
1532 case LyXTabular::M_VALIGN_BOTTOM:
1533 case LyXTabular::M_VALIGN_MIDDLE:
1535 case LyXTabular::VALIGN_TOP:
1536 case LyXTabular::VALIGN_BOTTOM:
1537 case LyXTabular::VALIGN_MIDDLE:
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.setVAlignment(
1541 tabular.getCellNumber(i, j),
1545 case LyXTabular::MULTICOLUMN: {
1546 if (sel_row_start != sel_row_end) {
1547 #ifdef WITH_WARNINGS
1548 #warning Need I say it ? This is horrible.
1551 Alert::error(_("Error setting multicolumn"),
1552 _("You cannot set multicolumn vertically."));
1555 if (!cur.selection()) {
1556 // just multicol for one single cell
1557 // check whether we are completely in a multicol
1558 if (tabular.isMultiColumn(cur.idx()))
1559 tabular.unsetMultiColumn(cur.idx());
1561 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1564 // we have a selection so this means we just add all this
1565 // cells to form a multicolumn cell
1566 idx_type const s_start = cur.selBegin().idx();
1567 idx_type const s_end = cur.selEnd().idx();
1568 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1569 cur.idx() = s_start;
1572 cur.selection() = false;
1576 case LyXTabular::SET_ALL_LINES:
1578 case LyXTabular::UNSET_ALL_LINES:
1579 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1580 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1581 tabular.setAllLines(
1582 tabular.getCellNumber(i,j), setLines);
1585 case LyXTabular::SET_LONGTABULAR:
1586 tabular.setLongTabular(true);
1589 case LyXTabular::UNSET_LONGTABULAR:
1590 tabular.setLongTabular(false);
1593 case LyXTabular::SET_ROTATE_TABULAR:
1594 tabular.setRotateTabular(true);
1597 case LyXTabular::UNSET_ROTATE_TABULAR:
1598 tabular.setRotateTabular(false);
1601 case LyXTabular::SET_ROTATE_CELL:
1602 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1603 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1604 tabular.setRotateCell(
1605 tabular.getCellNumber(i, j), true);
1608 case LyXTabular::UNSET_ROTATE_CELL:
1609 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1610 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1611 tabular.setRotateCell(
1612 tabular.getCellNumber(i, j), false);
1615 case LyXTabular::SET_USEBOX: {
1616 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1617 if (val == tabular.getUsebox(cur.idx()))
1618 val = LyXTabular::BOX_NONE;
1619 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1620 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1621 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1625 case LyXTabular::UNSET_LTFIRSTHEAD:
1627 case LyXTabular::SET_LTFIRSTHEAD:
1628 tabular.getRowOfLTFirstHead(row, ltt);
1629 checkLongtableSpecial(ltt, value, flag);
1630 tabular.setLTHead(row, flag, ltt, true);
1633 case LyXTabular::UNSET_LTHEAD:
1635 case LyXTabular::SET_LTHEAD:
1636 tabular.getRowOfLTHead(row, ltt);
1637 checkLongtableSpecial(ltt, value, flag);
1638 tabular.setLTHead(row, flag, ltt, false);
1641 case LyXTabular::UNSET_LTFOOT:
1643 case LyXTabular::SET_LTFOOT:
1644 tabular.getRowOfLTFoot(row, ltt);
1645 checkLongtableSpecial(ltt, value, flag);
1646 tabular.setLTFoot(row, flag, ltt, false);
1649 case LyXTabular::UNSET_LTLASTFOOT:
1651 case LyXTabular::SET_LTLASTFOOT:
1652 tabular.getRowOfLTLastFoot(row, ltt);
1653 checkLongtableSpecial(ltt, value, flag);
1654 tabular.setLTFoot(row, flag, ltt, true);
1657 case LyXTabular::SET_LTNEWPAGE:
1658 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1661 case LyXTabular::SET_BOOKTABS:
1662 tabular.setBookTabs(true);
1665 case LyXTabular::UNSET_BOOKTABS:
1666 tabular.setBookTabs(false);
1669 case LyXTabular::SET_TOP_SPACE: {
1671 if (value == "default")
1672 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1673 tabular.row_info[i].top_space_default = true;
1674 else if (isValidLength(value, &len))
1675 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1676 tabular.row_info[i].top_space_default = false;
1677 tabular.row_info[i].top_space = 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;
1687 case LyXTabular::SET_BOTTOM_SPACE: {
1689 if (value == "default")
1690 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1691 tabular.row_info[i].bottom_space_default = true;
1692 else if (isValidLength(value, &len))
1693 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1694 tabular.row_info[i].bottom_space_default = false;
1695 tabular.row_info[i].bottom_space = 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;
1705 case LyXTabular::SET_INTERLINE_SPACE: {
1707 if (value == "default")
1708 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1709 tabular.row_info[i].interline_space_default = true;
1710 else if (isValidLength(value, &len))
1711 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1712 tabular.row_info[i].interline_space_default = false;
1713 tabular.row_info[i].interline_space = 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;
1723 // dummy stuff just to avoid warnings
1724 case LyXTabular::LAST_ACTION:
1728 InsetTabularMailer(*this).updateDialog(&bv);
1732 bool InsetTabular::showInsetDialog(BufferView * bv) const
1734 InsetTabularMailer(*this).showDialog(bv);
1739 void InsetTabular::openLayoutDialog(BufferView * bv) const
1741 InsetTabularMailer(*this).showDialog(bv);
1745 bool InsetTabular::copySelection(LCursor & cur)
1747 if (!cur.selection())
1752 getSelection(cur, rs, re, cs, ce);
1754 paste_tabular.reset(new LyXTabular(tabular));
1756 for (row_type i = 0; i < rs; ++i)
1757 paste_tabular->deleteRow(0);
1759 row_type const rows = re - rs + 1;
1760 while (paste_tabular->rows() > rows)
1761 paste_tabular->deleteRow(rows);
1763 paste_tabular->setTopLine(0, true, true);
1764 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1767 for (col_type i = 0; i < cs; ++i)
1768 paste_tabular->deleteColumn(0);
1770 col_type const columns = ce - cs + 1;
1771 while (paste_tabular->columns() > columns)
1772 paste_tabular->deleteColumn(columns);
1774 paste_tabular->setLeftLine(0, true, true);
1775 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1779 OutputParams const runparams;
1780 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1781 theApp->clipboard().put(lyx::from_utf8(os.str()));
1782 // mark tabular stack dirty
1783 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1784 // when we (hopefully) have a one-for-all paste mechanism.
1785 lyx::cap::dirtyTabularStack(true);
1791 bool InsetTabular::pasteSelection(LCursor & cur)
1795 col_type const actcol = tabular.column_of_cell(cur.idx());
1796 row_type const actrow = tabular.row_of_cell(cur.idx());
1797 for (row_type r1 = 0, r2 = actrow;
1798 r1 < paste_tabular->rows() && r2 < tabular.rows();
1800 for (col_type c1 = 0, c2 = actcol;
1801 c1 < paste_tabular->columns() && c2 < tabular.columns();
1803 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1804 tabular.isPartOfMultiColumn(r2, c2))
1806 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1810 if (tabular.isPartOfMultiColumn(r2, c2)) {
1814 shared_ptr<InsetText> inset(
1815 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1816 tabular.setCellInset(r2, c2, inset);
1825 void InsetTabular::cutSelection(LCursor & cur)
1827 if (!cur.selection())
1832 getSelection(cur, rs, re, cs, ce);
1833 for (row_type i = rs; i <= re; ++i) {
1834 for (col_type j = cs; j <= ce; ++j) {
1835 shared_ptr<InsetText> t
1836 = cell(tabular.getCellNumber(i, j));
1837 if (cur.buffer().params().tracking_changes)
1838 t->markErased(true);
1844 // cursor position might be invalid now
1845 if (cur.pit() > cur.lastpit())
1846 cur.pit() = cur.lastpit();
1847 if (cur.pos() > cur.lastpos())
1848 cur.pos() = cur.lastpos();
1849 cur.clearSelection();
1853 bool InsetTabular::isRightToLeft(LCursor & cur) const
1855 BOOST_ASSERT(cur.depth() > 1);
1856 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1857 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1858 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1859 parentpos).language()->rightToLeft();
1863 void InsetTabular::getSelection(LCursor & cur,
1864 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1866 CursorSlice const & beg = cur.selBegin();
1867 CursorSlice const & end = cur.selEnd();
1868 cs = tabular.column_of_cell(beg.idx());
1869 ce = tabular.column_of_cell(end.idx());
1872 cs = tabular.column_of_cell(end.idx());
1874 ce = tabular.right_column_of_cell(end.idx());
1877 rs = tabular.row_of_cell(beg.idx());
1878 re = tabular.row_of_cell(end.idx());
1884 LyXText * InsetTabular::getText(int idx) const
1886 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1890 void InsetTabular::markErased(bool erased)
1892 for (idx_type idx = 0; idx < nargs(); ++idx)
1893 cell(idx)->markErased(erased);
1897 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1899 return tabular.getPWidth(cell).zero();
1903 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1906 if (buf.length() <= 0)
1911 col_type maxCols = 1;
1912 docstring::size_type const len = buf.length();
1913 docstring::size_type p = 0;
1916 (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos) {
1924 maxCols = max(cols, maxCols);
1930 maxCols = max(cols, maxCols);
1931 LyXTabular * loctab;
1936 paste_tabular.reset(
1937 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1938 loctab = paste_tabular.get();
1940 dirtyTabularStack(true);
1943 cell = bv.cursor().idx();
1944 ocol = tabular.column_of_cell(cell);
1945 row = tabular.row_of_cell(cell);
1948 docstring::size_type op = 0;
1949 idx_type const cells = loctab->getNumberOfCells();
1952 rows = loctab->rows();
1953 col_type const columns = loctab->columns();
1955 while (cell < cells && p < len && row < rows &&
1956 (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos)
1962 // we can only set this if we are not too far right
1963 if (cols < columns) {
1964 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1965 inset->setViewCache(&bv);
1966 Paragraph & par = inset->text_.getPar(0);
1967 LyXFont const font = inset->text_.getFont(par, 0);
1968 inset->setText(buf.substr(op, p - op), font);
1974 // we can only set this if we are not too far right
1975 if (cols < columns) {
1976 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1977 inset->setViewCache(&bv);
1978 Paragraph & par = inset->text_.getPar(0);
1979 LyXFont const font = inset->text_.getFont(par, 0);
1980 inset->setText(buf.substr(op, p - op), font);
1985 cell = loctab->getCellNumber(row, cols);
1991 // check for the last cell if there is no trailing '\n'
1992 if (cell < cells && op < len) {
1993 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1994 inset->setViewCache(&bv);
1995 Paragraph & par = inset->text_.getPar(0);
1996 LyXFont const font = inset->text_.getFont(par, 0);
1997 inset->setText(buf.substr(op, len - op), font);
2003 void InsetTabular::addPreview(PreviewLoader & loader) const
2005 row_type const rows = tabular.rows();
2006 col_type const columns = tabular.columns();
2007 for (row_type i = 0; i < rows; ++i) {
2008 for (col_type j = 0; j < columns; ++j)
2009 tabular.getCellInset(i, j)->addPreview(loader);
2014 bool InsetTabular::tablemode(LCursor & cur) const
2016 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2023 string const InsetTabularMailer::name_("tabular");
2025 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2026 : inset_(const_cast<InsetTabular &>(inset))
2030 string const InsetTabularMailer::inset2string(Buffer const &) const
2032 return params2string(inset_);
2036 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2038 istringstream data(in);
2040 lex.setStream(data);
2047 if (!lex || token != name_)
2048 return print_mailer_error("InsetTabularMailer", in, 1,
2051 // This is part of the inset proper that is usually swallowed
2052 // by Buffer::readInset
2054 if (!lex || token != "Tabular")
2055 return print_mailer_error("InsetTabularMailer", in, 2,
2058 Buffer const & buffer = inset.buffer();
2059 inset.read(buffer, lex);
2063 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2066 data << name_ << ' ';
2067 inset.write(inset.buffer(), data);
2068 data << "\\end_inset\n";