3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/Clipboard.h"
41 #include "frontends/Painter.h"
42 #include "frontends/Selection.h"
51 using cap::dirtyTabularStack;
52 using cap::tabularStackDirty;
54 using graphics::PreviewLoader;
58 using frontend::Painter;
59 using frontend::Clipboard;
61 using boost::shared_ptr;
67 using std::istringstream;
69 using std::ostringstream;
73 namespace Alert = frontend::Alert;
78 int const ADD_TO_HEIGHT = 2;
79 int const ADD_TO_TABULAR_WIDTH = 2;
80 int const default_line_space = 10;
83 boost::scoped_ptr<LyXTabular> paste_tabular;
86 struct TabularFeature {
87 LyXTabular::Feature action;
92 TabularFeature tabularFeature[] =
94 { LyXTabular::APPEND_ROW, "append-row" },
95 { LyXTabular::APPEND_COLUMN, "append-column" },
96 { LyXTabular::DELETE_ROW, "delete-row" },
97 { LyXTabular::DELETE_COLUMN, "delete-column" },
98 { LyXTabular::COPY_ROW, "copy-row" },
99 { LyXTabular::COPY_COLUMN, "copy-column" },
100 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
101 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
102 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
103 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
104 { LyXTabular::ALIGN_LEFT, "align-left" },
105 { LyXTabular::ALIGN_RIGHT, "align-right" },
106 { LyXTabular::ALIGN_CENTER, "align-center" },
107 { LyXTabular::ALIGN_BLOCK, "align-block" },
108 { LyXTabular::VALIGN_TOP, "valign-top" },
109 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
110 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
111 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
112 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
113 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
114 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
115 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
116 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
117 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
118 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
119 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
120 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
121 { LyXTabular::MULTICOLUMN, "multicolumn" },
122 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
123 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
124 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
125 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
126 { LyXTabular::SET_PWIDTH, "set-pwidth" },
127 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
128 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
129 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
130 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
131 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
132 { LyXTabular::SET_USEBOX, "set-usebox" },
133 { LyXTabular::SET_LTHEAD, "set-lthead" },
134 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
135 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
136 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
137 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
138 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
139 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
140 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
141 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
142 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
143 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
144 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
145 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
146 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
147 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
148 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
149 { LyXTabular::LAST_ACTION, "" }
153 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
155 FeatureEqual(LyXTabular::Feature feature)
156 : feature_(feature) {}
157 bool operator()(TabularFeature const & tf) const {
158 return tf.action == feature_;
161 LyXTabular::Feature feature_;
167 string const featureAsString(LyXTabular::Feature feature)
169 TabularFeature * end = tabularFeature +
170 sizeof(tabularFeature) / sizeof(TabularFeature);
171 TabularFeature * it = std::find_if(tabularFeature, end,
172 FeatureEqual(feature));
173 return (it == end) ? string() : it->feature;
177 bool InsetTabular::hasPasteBuffer() const
179 return (paste_tabular.get() != 0);
183 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
185 : tabular(buf.params(), max(rows, row_type(1)),
186 max(columns, col_type(1))), buffer_(&buf), scx_(0)
190 InsetTabular::InsetTabular(InsetTabular const & tab)
191 : InsetOld(tab), tabular(tab.tabular),
192 buffer_(tab.buffer_), scx_(0)
196 InsetTabular::~InsetTabular()
198 InsetTabularMailer(*this).hideDialog();
202 auto_ptr<InsetBase> InsetTabular::doClone() const
204 return auto_ptr<InsetBase>(new InsetTabular(*this));
208 Buffer const & InsetTabular::buffer() const
214 void InsetTabular::buffer(Buffer const * b)
220 void InsetTabular::write(Buffer const & buf, ostream & os) const
222 os << "Tabular" << endl;
223 tabular.write(buf, os);
227 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
229 bool const old_format = (lex.getString() == "\\LyXTable");
231 tabular.read(buf, lex);
237 string token = lex.getString();
238 while (lex.isOK() && (token != "\\end_inset")) {
240 token = lex.getString();
242 if (token != "\\end_inset") {
243 lex.printError("Missing \\end_inset at this point. "
249 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
251 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
252 // mi.base.textwidth << "\n";
254 lyxerr << "InsetTabular::metrics: need bv" << endl;
259 for (idx_type cell = 0; i < tabular.rows(); ++i) {
262 for (col_type j = 0; j < tabular.columns(); ++j) {
263 if (tabular.isPartOfMultiColumn(i, j))
264 // Multicolumn cell, but not first one
269 if (tabular.cell_info[i][j].multicolumn ==
270 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
271 p_width = tabular.cellinfo_of_cell(cell).p_width;
273 p_width = tabular.column_info[j].p_width;
275 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
276 tabular.getCellInset(cell)->metrics(m, dim);
278 dim.wid = m.base.textwidth;
279 tabular.setWidthOfCell(cell, dim.wid);
280 if (p_width.zero()) {
281 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
282 // FIXME there must be a way to get rid of
283 // the second metrics call
284 tabular.getCellInset(cell)->metrics(m, dim);
286 maxAsc = max(maxAsc, dim.asc);
287 maxDesc = max(maxDesc, dim.des);
290 int const top_space = tabular.row_info[i].top_space_default ?
292 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
293 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
294 int const bottom_space = tabular.row_info[i].bottom_space_default ?
296 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
297 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
300 dim.asc = tabular.getAscentOfRow(0);
301 dim.des = tabular.getHeightOfTabular() - dim.asc;
302 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
303 bool const changed = dim_ != dim;
309 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
311 setPosCache(pi, x, y);
313 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
314 BufferView * bv = pi.base.bv;
316 resetPos(bv->cursor());
319 x += ADD_TO_TABULAR_WIDTH;
322 first_visible_cell = LyXTabular::npos;
323 for (row_type i = 0; i < tabular.rows(); ++i) {
325 int const a = tabular.getAscentOfRow(i);
326 int const d = tabular.getDescentOfRow(i);
327 idx = tabular.getCellNumber(i, 0);
328 for (col_type j = 0; j < tabular.columns(); ++j) {
329 if (tabular.isPartOfMultiColumn(i, j))
331 if (first_visible_cell == LyXTabular::npos)
332 first_visible_cell = idx;
334 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
335 if (nx + tabular.getWidthOfColumn(idx) < 0
336 || nx > bv->workWidth()
338 || y - a > bv->workHeight()) {
339 pi.pain.setDrawingEnabled(false);
340 cell(idx)->draw(pi, cx, y);
341 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
342 pi.pain.setDrawingEnabled(true);
344 cell(idx)->draw(pi, cx, y);
345 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
347 nx += tabular.getWidthOfColumn(idx);
351 if (i + 1 < tabular.rows())
352 y += d + tabular.getAscentOfRow(i + 1) +
353 tabular.getAdditionalHeight(i + 1);
358 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
360 setPosCache(pi, x, y);
362 LCursor & cur = pi.base.bv->cursor();
364 x += scx_ + ADD_TO_TABULAR_WIDTH;
366 // Paint background of current tabular
367 int const w = tabular.getWidthOfTabular();
368 int const h = tabular.getHeightOfTabular();
369 int yy = y - tabular.getAscentOfRow(0);
370 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
372 if (!cur.selection())
374 if (!ptr_cmp(&cur.inset(), this))
380 if (tablemode(cur)) {
383 getSelection(cur, rs, re, cs, ce);
384 y -= tabular.getAscentOfRow(0);
385 for (row_type j = 0; j < tabular.rows(); ++j) {
386 int const a = tabular.getAscentOfRow(j);
387 int const h = a + tabular.getDescentOfRow(j);
389 y += tabular.getAdditionalHeight(j);
390 for (col_type i = 0; i < tabular.columns(); ++i) {
391 if (tabular.isPartOfMultiColumn(j, i))
393 idx_type const cell =
394 tabular.getCellNumber(j, i);
395 int const w = tabular.getWidthOfColumn(cell);
396 if (i >= cs && i <= ce && j >= rs && j <= re)
397 pi.pain.fillRectangle(xx, y, w, h,
406 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
411 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
412 row_type row, idx_type cell, bool erased) const
414 int x2 = x + tabular.getWidthOfColumn(cell);
416 LColor::color col = LColor::tabularline;
417 LColor::color onoffcol = LColor::tabularonoffline;
420 col = LColor::strikeout;
421 onoffcol = LColor::strikeout;
424 if (!tabular.topAlreadyDrawn(cell)) {
425 on_off = !tabular.topLine(cell);
426 pain.line(x, y - tabular.getAscentOfRow(row),
427 x2, y - tabular.getAscentOfRow(row),
428 on_off ? onoffcol : col,
429 on_off ? Painter::line_onoffdash : Painter::line_solid);
431 on_off = !tabular.bottomLine(cell);
432 pain.line(x, y + tabular.getDescentOfRow(row),
433 x2, y + tabular.getDescentOfRow(row),
434 on_off ? onoffcol : col,
435 on_off ? Painter::line_onoffdash : Painter::line_solid);
436 if (!tabular.leftAlreadyDrawn(cell)) {
437 on_off = !tabular.leftLine(cell);
438 pain.line(x, y - tabular.getAscentOfRow(row),
439 x, y + tabular.getDescentOfRow(row),
440 on_off ? onoffcol : col,
441 on_off ? Painter::line_onoffdash : Painter::line_solid);
443 on_off = !tabular.rightLine(cell);
444 pain.line(x2 - tabular.getAdditionalWidth(cell),
445 y - tabular.getAscentOfRow(row),
446 x2 - tabular.getAdditionalWidth(cell),
447 y + tabular.getDescentOfRow(row),
448 on_off ? onoffcol : col,
449 on_off ? Painter::line_onoffdash : Painter::line_solid);
453 docstring const InsetTabular::editMessage() const
455 return _("Opened table");
459 void InsetTabular::edit(LCursor & cur, bool left)
461 //lyxerr << "InsetTabular::edit: " << this << endl;
463 cur.selection() = false;
466 if (isRightToLeft(cur))
467 cur.idx() = tabular.getLastCellInRow(0);
473 if (isRightToLeft(cur))
474 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
476 cur.idx() = tabular.getNumberOfCells() - 1;
478 cur.pos() = cur.lastpos(); // FIXME crude guess
480 // FIXME: this accesses the position cache before it is initialized
482 //cur.bv().fitCursor();
486 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
488 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
489 << "\n cur:" << cur << endl;
490 CursorSlice sl = cur.top();
491 LCursor & bvcur = cur.bv().cursor();
493 switch (cmd.action) {
495 case LFUN_MOUSE_PRESS:
496 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
498 if (cmd.button() == mouse_button::button1
499 || (cmd.button() == mouse_button::button3
500 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
501 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
503 cur.selection() = false;
504 setCursorFromCoordinates(cur, cmd.x, cmd.y);
505 cur.bv().mouseSetCursor(cur);
509 if (cmd.button() == mouse_button::button2) {
510 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
511 doDispatch(cur, cmd);
515 case LFUN_MOUSE_MOTION:
516 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
517 if (cmd.button() == mouse_button::button1) {
518 // only accept motions to places not deeper nested than the real anchor
519 if (bvcur.anchor_.hasPart(cur)) {
520 // only update if selection changes
521 if (bvcur.idx() == cur.idx() &&
522 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
524 setCursorFromCoordinates(cur, cmd.x, cmd.y);
525 bvcur.setCursor(cur);
526 bvcur.selection() = true;
532 case LFUN_MOUSE_RELEASE:
533 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
534 if (cmd.button() == mouse_button::button3)
535 InsetTabularMailer(*this).showDialog(&cur.bv());
538 case LFUN_CELL_BACKWARD:
540 cur.selection() = false;
543 case LFUN_CELL_FORWARD:
545 cur.selection() = false;
548 case LFUN_CHAR_FORWARD_SELECT:
549 case LFUN_CHAR_FORWARD:
550 cell(cur.idx())->dispatch(cur, cmd);
551 if (!cur.result().dispatched()) {
552 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
554 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
560 case LFUN_CHAR_BACKWARD_SELECT:
561 case LFUN_CHAR_BACKWARD:
562 cell(cur.idx())->dispatch(cur, cmd);
563 if (!cur.result().dispatched()) {
564 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
566 cmd = FuncRequest(LFUN_FINISHED_LEFT);
572 case LFUN_DOWN_SELECT:
574 cell(cur.idx())->dispatch(cur, cmd);
575 cur.dispatched(); // override the cell's decision
577 // if our LyXText didn't do anything to the cursor
578 // then we try to put the cursor into the cell below
579 // setting also the right targetX.
580 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
581 cur.idx() = tabular.getCellBelow(cur.idx());
583 TextMetrics const & tm =
584 cur.bv().textMetrics(cell(cur.idx())->getText(0));
585 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
587 if (sl == cur.top()) {
588 // we trick it to go to the RIGHT after leaving the
590 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
597 cell(cur.idx())->dispatch(cur, cmd);
598 cur.dispatched(); // override the cell's decision
600 // if our LyXText didn't do anything to the cursor
601 // then we try to put the cursor into the cell above
602 // setting also the right targetX.
603 if (tabular.row_of_cell(cur.idx()) != 0) {
604 cur.idx() = tabular.getCellAbove(cur.idx());
605 cur.pit() = cur.lastpit();
606 LyXText const * text = cell(cur.idx())->getText(0);
607 TextMetrics const & tm = cur.bv().textMetrics(text);
608 ParagraphMetrics const & pm =
609 tm.parMetrics(cur.lastpit());
610 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
612 if (sl == cur.top()) {
613 cmd = FuncRequest(LFUN_FINISHED_UP);
618 // case LFUN_SCREEN_DOWN: {
619 // //if (hasSelection())
620 // // cur.selection() = false;
621 // col_type const col = tabular.column_of_cell(cur.idx());
622 // int const t = cur.bv().top_y() + cur.bv().height();
623 // if (t < yo() + tabular.getHeightOfTabular()) {
624 // cur.bv().scrollDocView(t);
625 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
627 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
634 // case LFUN_SCREEN_UP: {
635 // //if (hasSelection())
636 // // cur.selection() = false;
637 // col_type const col = tabular.column_of_cell(cur.idx());
638 // int const t = cur.bv().top_y() + cur.bv().height();
640 // cur.bv().scrollDocView(t);
644 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
648 // cur.par() = cur.lastpar();
649 // cur.pos() = cur.lastpos();
653 case LFUN_LAYOUT_TABULAR:
654 InsetTabularMailer(*this).showDialog(&cur.bv());
657 case LFUN_INSET_DIALOG_UPDATE:
658 InsetTabularMailer(*this).updateDialog(&cur.bv());
661 case LFUN_TABULAR_FEATURE:
662 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
666 // insert file functions
667 case LFUN_FILE_INSERT_ASCII_PARA:
668 case LFUN_FILE_INSERT_ASCII: {
670 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), to_utf8(cmd.argument()), false);
671 // FIXME: We don't know the encoding of the file
672 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), from_utf8(tmpstr), false))
678 if (tablemode(cur)) {
679 if (copySelection(cur)) {
680 recordUndoInset(cur, Undo::DELETE);
685 cell(cur.idx())->dispatch(cur, cmd);
688 case LFUN_CHAR_DELETE_BACKWARD:
689 case LFUN_CHAR_DELETE_FORWARD:
690 if (tablemode(cur)) {
691 recordUndoInset(cur, Undo::DELETE);
695 cell(cur.idx())->dispatch(cur, cmd);
699 if (!cur.selection())
701 if (tablemode(cur)) {
705 cell(cur.idx())->dispatch(cur, cmd);
708 case LFUN_CLIPBOARD_PASTE:
709 case LFUN_PRIMARY_SELECTION_PASTE: {
710 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
711 theClipboard().get() :
712 theSelection().get();
715 // pass to InsertAsciiString, but
716 // only if we have multi-cell content
717 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
718 if (insertAsciiString(cur.bv(), clip, false)) {
719 // content has been replaced,
720 // so cursor might be invalid
721 cur.pos() = cur.lastpos();
722 bvcur.setCursor(cur);
726 // Let the cell handle normal text
727 cell(cur.idx())->dispatch(cur, cmd);
732 if (hasPasteBuffer() && tabularStackDirty()) {
733 recordUndoInset(cur, Undo::INSERT);
737 cell(cur.idx())->dispatch(cur, cmd);
742 case LFUN_FONT_ROMAN:
748 case LFUN_FONT_FREE_APPLY:
749 case LFUN_FONT_FREE_UPDATE:
751 case LFUN_FONT_UNDERLINE:
753 case LFUN_WORD_CAPITALIZE:
754 case LFUN_WORD_UPCASE:
755 case LFUN_WORD_LOWCASE:
756 case LFUN_CHARS_TRANSPOSE:
757 if (tablemode(cur)) {
760 getSelection(cur, rs, re, cs, ce);
761 LCursor tmpcur = cur;
762 for (row_type i = rs; i <= re; ++i) {
763 for (col_type j = cs; j <= ce; ++j) {
764 // cursor follows cell:
765 tmpcur.idx() = tabular.getCellNumber(i, j);
766 // select this cell only:
769 tmpcur.resetAnchor();
770 tmpcur.pit() = tmpcur.lastpit();
771 tmpcur.pos() = tmpcur.top().lastpos();
772 tmpcur.setCursor(tmpcur);
773 tmpcur.setSelection();
774 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
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);
788 // FIXME: this accesses the position cache before it is initialized
790 InsetTabularMailer(*this).updateDialog(&cur.bv());
794 // function sets an object as defined in func_status.h:
795 // states OK, Unknown, Disabled, On, Off.
796 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
797 FuncStatus & status) const
799 switch (cmd.action) {
800 case LFUN_TABULAR_FEATURE: {
801 int action = LyXTabular::LAST_ACTION;
803 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
804 string const tmp = tabularFeature[i].feature;
805 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
806 action = tabularFeature[i].action;
810 if (action == LyXTabular::LAST_ACTION) {
812 status.unknown(true);
816 string const argument
817 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
819 row_type sel_row_start = 0;
820 row_type sel_row_end = 0;
822 LyXTabular::ltType dummyltt;
825 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
828 case LyXTabular::SET_PWIDTH:
829 case LyXTabular::SET_MPWIDTH:
830 case LyXTabular::SET_SPECIAL_COLUMN:
831 case LyXTabular::SET_SPECIAL_MULTI:
832 case LyXTabular::APPEND_ROW:
833 case LyXTabular::APPEND_COLUMN:
834 case LyXTabular::DELETE_ROW:
835 case LyXTabular::DELETE_COLUMN:
836 case LyXTabular::COPY_ROW:
837 case LyXTabular::COPY_COLUMN:
838 case LyXTabular::SET_ALL_LINES:
839 case LyXTabular::UNSET_ALL_LINES:
840 case LyXTabular::SET_TOP_SPACE:
841 case LyXTabular::SET_BOTTOM_SPACE:
842 case LyXTabular::SET_INTERLINE_SPACE:
846 case LyXTabular::MULTICOLUMN:
847 status.setOnOff(tabular.isMultiColumn(cur.idx()));
850 case LyXTabular::M_TOGGLE_LINE_TOP:
852 case LyXTabular::TOGGLE_LINE_TOP:
853 status.setOnOff(tabular.topLine(cur.idx(), flag));
856 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
858 case LyXTabular::TOGGLE_LINE_BOTTOM:
859 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
862 case LyXTabular::M_TOGGLE_LINE_LEFT:
864 case LyXTabular::TOGGLE_LINE_LEFT:
865 status.setOnOff(tabular.leftLine(cur.idx(), flag));
868 case LyXTabular::M_TOGGLE_LINE_RIGHT:
870 case LyXTabular::TOGGLE_LINE_RIGHT:
871 status.setOnOff(tabular.rightLine(cur.idx(), flag));
874 case LyXTabular::M_ALIGN_LEFT:
876 case LyXTabular::ALIGN_LEFT:
877 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
880 case LyXTabular::M_ALIGN_RIGHT:
882 case LyXTabular::ALIGN_RIGHT:
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
886 case LyXTabular::M_ALIGN_CENTER:
888 case LyXTabular::ALIGN_CENTER:
889 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
892 case LyXTabular::ALIGN_BLOCK:
893 status.enabled(!tabular.getPWidth(cur.idx()).zero());
894 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
897 case LyXTabular::M_VALIGN_TOP:
899 case LyXTabular::VALIGN_TOP:
901 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
904 case LyXTabular::M_VALIGN_BOTTOM:
906 case LyXTabular::VALIGN_BOTTOM:
908 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
911 case LyXTabular::M_VALIGN_MIDDLE:
913 case LyXTabular::VALIGN_MIDDLE:
915 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
918 case LyXTabular::SET_LONGTABULAR:
919 status.setOnOff(tabular.isLongTabular());
922 case LyXTabular::UNSET_LONGTABULAR:
923 status.setOnOff(!tabular.isLongTabular());
926 case LyXTabular::SET_ROTATE_TABULAR:
927 status.setOnOff(tabular.getRotateTabular());
930 case LyXTabular::UNSET_ROTATE_TABULAR:
931 status.setOnOff(!tabular.getRotateTabular());
934 case LyXTabular::SET_ROTATE_CELL:
935 status.setOnOff(tabular.getRotateCell(cur.idx()));
938 case LyXTabular::UNSET_ROTATE_CELL:
939 status.setOnOff(!tabular.getRotateCell(cur.idx()));
942 case LyXTabular::SET_USEBOX:
943 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
946 case LyXTabular::SET_LTFIRSTHEAD:
947 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
950 case LyXTabular::UNSET_LTFIRSTHEAD:
951 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
954 case LyXTabular::SET_LTHEAD:
955 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
958 case LyXTabular::UNSET_LTHEAD:
959 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
962 case LyXTabular::SET_LTFOOT:
963 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
966 case LyXTabular::UNSET_LTFOOT:
967 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
970 case LyXTabular::SET_LTLASTFOOT:
971 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
974 case LyXTabular::UNSET_LTLASTFOOT:
975 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
978 case LyXTabular::SET_LTNEWPAGE:
979 status.setOnOff(tabular.getLTNewPage(sel_row_start));
982 case LyXTabular::SET_BOOKTABS:
983 status.setOnOff(tabular.useBookTabs());
986 case LyXTabular::UNSET_BOOKTABS:
987 status.setOnOff(!tabular.useBookTabs());
992 status.enabled(false);
998 // These are only enabled inside tabular
999 case LFUN_CELL_BACKWARD:
1000 case LFUN_CELL_FORWARD:
1001 status.enabled(true);
1004 // disable these with multiple cells selected
1005 case LFUN_INSET_INSERT:
1006 case LFUN_TABULAR_INSERT:
1007 case LFUN_CHARSTYLE_INSERT:
1008 case LFUN_FLOAT_INSERT:
1009 case LFUN_FLOAT_WIDE_INSERT:
1010 case LFUN_FOOTNOTE_INSERT:
1011 case LFUN_MARGINALNOTE_INSERT:
1012 case LFUN_MATH_INSERT:
1013 case LFUN_MATH_MODE:
1014 case LFUN_MATH_MUTATE:
1015 case LFUN_MATH_DISPLAY:
1016 case LFUN_NOTE_INSERT:
1017 case LFUN_OPTIONAL_INSERT:
1018 case LFUN_BOX_INSERT:
1019 case LFUN_BRANCH_INSERT:
1020 case LFUN_WRAP_INSERT:
1021 case LFUN_ERT_INSERT: {
1022 if (tablemode(cur)) {
1023 status.enabled(false);
1026 return cell(cur.idx())->getStatus(cur, cmd, status);
1029 // disable in non-fixed-width cells
1030 case LFUN_BREAK_LINE:
1031 case LFUN_BREAK_PARAGRAPH:
1032 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1033 case LFUN_BREAK_PARAGRAPH_SKIP: {
1034 if (tabular.getPWidth(cur.idx()).zero()) {
1035 status.enabled(false);
1038 return cell(cur.idx())->getStatus(cur, cmd, status);
1042 if (tabularStackDirty()) {
1043 status.enabled(true);
1047 case LFUN_INSET_MODIFY:
1048 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1049 status.enabled(true);
1055 // we try to handle this event in the insets dispatch function.
1056 return cell(cur.idx())->getStatus(cur, cmd, status);
1061 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1062 OutputParams const & runparams) const
1064 return tabular.latex(buf, os, runparams);
1068 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1069 OutputParams const & runparams) const
1071 int const dp = runparams.linelen ? runparams.depth : 0;
1072 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1076 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1077 OutputParams const & runparams) const
1080 InsetBase * master = 0;
1082 #ifdef WITH_WARNINGS
1083 #warning Why not pass a proper DocIterator here?
1086 // if the table is inside a float it doesn't need the informaltable
1087 // wrapper. Search for it.
1088 for (master = owner(); master; master = master->owner())
1089 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1094 os << "<informaltable>";
1097 ret += tabular.docbook(buf, os, runparams);
1099 os << "</informaltable>";
1106 void InsetTabular::validate(LaTeXFeatures & features) const
1108 tabular.validate(features);
1112 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1114 return tabular.getCellInset(idx);
1118 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1120 return tabular.getCellInset(idx);
1124 void InsetTabular::cursorPos(BufferView const & bv,
1125 CursorSlice const & sl, bool boundary, int & x, int & y) const
1127 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1129 // y offset correction
1130 int const row = tabular.row_of_cell(sl.idx());
1131 for (int i = 0; i <= row; ++i) {
1133 y += tabular.getAscentOfRow(i);
1134 y += tabular.getAdditionalHeight(i);
1137 y += tabular.getDescentOfRow(i);
1140 // x offset correction
1141 int const col = tabular.column_of_cell(sl.idx());
1142 int idx = tabular.getCellNumber(row, 0);
1143 for (int j = 0; j < col; ++j) {
1144 if (tabular.isPartOfMultiColumn(row, j))
1146 x += tabular.getWidthOfColumn(idx);
1149 x += tabular.getBeginningOfTextInCell(idx);
1150 x += ADD_TO_TABULAR_WIDTH;
1155 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1159 InsetBase const & inset = *tabular.getCellInset(cell);
1160 Point o = bv.coordCache().getInsets().xy(&inset);
1161 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1162 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1163 row_type const row = tabular.row_of_cell(cell);
1164 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1165 tabular.getAdditionalHeight(row);
1166 int const yend = o.y_ + tabular.getDescentOfRow(row);
1178 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1179 // << " ybeg=" << ybeg << " yend=" << yend
1180 // << " xx=" << xx << " yy=" << yy
1181 // << " dist=" << xx + yy << endl;
1186 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1188 //lyxerr << "InsetTabular::editXY: " << this << endl;
1189 cur.selection() = false;
1191 cur.idx() = getNearestCell(cur.bv(), x, y);
1193 return cell(cur.idx())->text_.editXY(cur, x, y);
1197 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1199 cur.idx() = getNearestCell(cur.bv(), x, y);
1200 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1204 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1206 idx_type idx_min = 0;
1207 int dist_min = std::numeric_limits<int>::max();
1208 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1209 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1210 int const d = dist(bv, i, x, y);
1221 int InsetTabular::getCellXPos(idx_type const cell) const
1225 for (; !tabular.isFirstCellInRow(c); --c)
1227 int lx = tabular.getWidthOfColumn(cell);
1228 for (; c < cell; ++c)
1229 lx += tabular.getWidthOfColumn(c);
1231 return lx - tabular.getWidthOfColumn(cell);
1235 void InsetTabular::resetPos(LCursor & cur) const
1237 BufferView & bv = cur.bv();
1238 int const maxwidth = bv.workWidth();
1240 if (&cur.inset() != this) {
1244 int const X2 = maxwidth;
1245 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1246 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1247 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1250 scx_ = X1 + 20 - x1;
1252 scx_ = X2 - 20 - x2;
1257 cur.updateFlags(Update::Force | Update::FitCursor);
1259 InsetTabularMailer(*this).updateDialog(&bv);
1263 void InsetTabular::moveNextCell(LCursor & cur)
1265 if (isRightToLeft(cur)) {
1266 if (tabular.isFirstCellInRow(cur.idx())) {
1267 row_type const row = tabular.row_of_cell(cur.idx());
1268 if (row == tabular.rows() - 1)
1270 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1277 if (tabular.isLastCell(cur.idx()))
1287 void InsetTabular::movePrevCell(LCursor & cur)
1289 if (isRightToLeft(cur)) {
1290 if (tabular.isLastCellInRow(cur.idx())) {
1291 row_type const row = tabular.row_of_cell(cur.idx());
1294 cur.idx() = tabular.getFirstCellInRow(row);
1295 cur.idx() = tabular.getCellAbove(cur.idx());
1297 if (tabular.isLastCell(cur.idx()))
1302 if (cur.idx() == 0) // first cell
1306 cur.pit() = cur.lastpit();
1307 cur.pos() = cur.lastpos();
1309 // FIXME: this accesses the position cache before it is initialized
1314 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1316 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1319 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1320 string const tmp = tabularFeature[i].feature;
1322 if (tmp == what.substr(0, tmp.length())) {
1323 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1324 //tabularFeatures[i].feature.length())) {
1325 action = tabularFeature[i].action;
1329 if (action == LyXTabular::LAST_ACTION)
1333 ltrim(what.substr(tabularFeature[i].feature.length()));
1334 tabularFeatures(cur, action, val);
1341 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1342 string const & special, bool & flag)
1344 if (special == "dl_above") {
1347 } else if (special == "dl_below") {
1348 ltt.bottomDL = flag;
1350 } else if (special == "empty") {
1362 void InsetTabular::tabularFeatures(LCursor & cur,
1363 LyXTabular::Feature feature, string const & value)
1365 BufferView & bv = cur.bv();
1366 col_type sel_col_start;
1367 col_type sel_col_end;
1368 row_type sel_row_start;
1369 row_type sel_row_end;
1370 bool setLines = false;
1371 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1372 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1376 case LyXTabular::M_ALIGN_LEFT:
1377 case LyXTabular::ALIGN_LEFT:
1378 setAlign = LYX_ALIGN_LEFT;
1381 case LyXTabular::M_ALIGN_RIGHT:
1382 case LyXTabular::ALIGN_RIGHT:
1383 setAlign = LYX_ALIGN_RIGHT;
1386 case LyXTabular::M_ALIGN_CENTER:
1387 case LyXTabular::ALIGN_CENTER:
1388 setAlign = LYX_ALIGN_CENTER;
1391 case LyXTabular::ALIGN_BLOCK:
1392 setAlign = LYX_ALIGN_BLOCK;
1395 case LyXTabular::M_VALIGN_TOP:
1396 case LyXTabular::VALIGN_TOP:
1397 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1400 case LyXTabular::M_VALIGN_BOTTOM:
1401 case LyXTabular::VALIGN_BOTTOM:
1402 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1405 case LyXTabular::M_VALIGN_MIDDLE:
1406 case LyXTabular::VALIGN_MIDDLE:
1407 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1414 recordUndoInset(cur, Undo::ATOMIC);
1416 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1417 row_type const row = tabular.row_of_cell(cur.idx());
1418 col_type const column = tabular.column_of_cell(cur.idx());
1420 LyXTabular::ltType ltt;
1424 case LyXTabular::SET_PWIDTH: {
1425 LyXLength const len(value);
1426 tabular.setColumnPWidth(cur, cur.idx(), len);
1428 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1429 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1433 case LyXTabular::SET_MPWIDTH:
1434 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1437 case LyXTabular::SET_SPECIAL_COLUMN:
1438 case LyXTabular::SET_SPECIAL_MULTI:
1439 tabular.setAlignSpecial(cur.idx(),value,feature);
1442 case LyXTabular::APPEND_ROW:
1443 // append the row into the tabular
1444 tabular.appendRow(bv.buffer()->params(), cur.idx());
1447 case LyXTabular::APPEND_COLUMN:
1448 // append the column into the tabular
1449 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1450 cur.idx() = tabular.getCellNumber(row, column);
1453 case LyXTabular::DELETE_ROW:
1454 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1455 tabular.deleteRow(sel_row_start);
1456 if (sel_row_start >= tabular.rows())
1458 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1461 cur.selection() = false;
1464 case LyXTabular::DELETE_COLUMN:
1465 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1466 tabular.deleteColumn(sel_col_start);
1467 if (sel_col_start >= tabular.columns())
1469 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1472 cur.selection() = false;
1475 case LyXTabular::COPY_ROW:
1476 tabular.copyRow(bv.buffer()->params(), row);
1479 case LyXTabular::COPY_COLUMN:
1480 tabular.copyColumn(bv.buffer()->params(), column);
1481 cur.idx() = tabular.getCellNumber(row, column);
1484 case LyXTabular::M_TOGGLE_LINE_TOP:
1486 case LyXTabular::TOGGLE_LINE_TOP: {
1487 bool lineSet = !tabular.topLine(cur.idx(), flag);
1488 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1489 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1491 tabular.getCellNumber(i, j),
1496 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1498 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1499 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1500 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1501 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1502 tabular.setBottomLine(
1503 tabular.getCellNumber(i, j),
1509 case LyXTabular::M_TOGGLE_LINE_LEFT:
1511 case LyXTabular::TOGGLE_LINE_LEFT: {
1512 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1513 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1514 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1515 tabular.setLeftLine(
1516 tabular.getCellNumber(i,j),
1522 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1524 case LyXTabular::TOGGLE_LINE_RIGHT: {
1525 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1526 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1527 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1528 tabular.setRightLine(
1529 tabular.getCellNumber(i,j),
1535 case LyXTabular::M_ALIGN_LEFT:
1536 case LyXTabular::M_ALIGN_RIGHT:
1537 case LyXTabular::M_ALIGN_CENTER:
1539 case LyXTabular::ALIGN_LEFT:
1540 case LyXTabular::ALIGN_RIGHT:
1541 case LyXTabular::ALIGN_CENTER:
1542 case LyXTabular::ALIGN_BLOCK:
1543 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1544 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1545 tabular.setAlignment(
1546 tabular.getCellNumber(i, j),
1551 case LyXTabular::M_VALIGN_TOP:
1552 case LyXTabular::M_VALIGN_BOTTOM:
1553 case LyXTabular::M_VALIGN_MIDDLE:
1555 case LyXTabular::VALIGN_TOP:
1556 case LyXTabular::VALIGN_BOTTOM:
1557 case LyXTabular::VALIGN_MIDDLE:
1558 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1559 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1560 tabular.setVAlignment(
1561 tabular.getCellNumber(i, j),
1565 case LyXTabular::MULTICOLUMN: {
1566 if (sel_row_start != sel_row_end) {
1567 #ifdef WITH_WARNINGS
1568 #warning Need I say it ? This is horrible.
1571 Alert::error(_("Error setting multicolumn"),
1572 _("You cannot set multicolumn vertically."));
1575 if (!cur.selection()) {
1576 // just multicol for one single cell
1577 // check whether we are completely in a multicol
1578 if (tabular.isMultiColumn(cur.idx()))
1579 tabular.unsetMultiColumn(cur.idx());
1581 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1584 // we have a selection so this means we just add all this
1585 // cells to form a multicolumn cell
1586 idx_type const s_start = cur.selBegin().idx();
1587 idx_type const s_end = cur.selEnd().idx();
1588 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1589 cur.idx() = s_start;
1592 cur.selection() = false;
1596 case LyXTabular::SET_ALL_LINES:
1598 case LyXTabular::UNSET_ALL_LINES:
1599 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1600 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1601 tabular.setAllLines(
1602 tabular.getCellNumber(i,j), setLines);
1605 case LyXTabular::SET_LONGTABULAR:
1606 tabular.setLongTabular(true);
1609 case LyXTabular::UNSET_LONGTABULAR:
1610 tabular.setLongTabular(false);
1613 case LyXTabular::SET_ROTATE_TABULAR:
1614 tabular.setRotateTabular(true);
1617 case LyXTabular::UNSET_ROTATE_TABULAR:
1618 tabular.setRotateTabular(false);
1621 case LyXTabular::SET_ROTATE_CELL:
1622 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1623 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1624 tabular.setRotateCell(
1625 tabular.getCellNumber(i, j), true);
1628 case LyXTabular::UNSET_ROTATE_CELL:
1629 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1630 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1631 tabular.setRotateCell(
1632 tabular.getCellNumber(i, j), false);
1635 case LyXTabular::SET_USEBOX: {
1636 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1637 if (val == tabular.getUsebox(cur.idx()))
1638 val = LyXTabular::BOX_NONE;
1639 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1640 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1641 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1645 case LyXTabular::UNSET_LTFIRSTHEAD:
1647 case LyXTabular::SET_LTFIRSTHEAD:
1648 tabular.getRowOfLTFirstHead(row, ltt);
1649 checkLongtableSpecial(ltt, value, flag);
1650 tabular.setLTHead(row, flag, ltt, true);
1653 case LyXTabular::UNSET_LTHEAD:
1655 case LyXTabular::SET_LTHEAD:
1656 tabular.getRowOfLTHead(row, ltt);
1657 checkLongtableSpecial(ltt, value, flag);
1658 tabular.setLTHead(row, flag, ltt, false);
1661 case LyXTabular::UNSET_LTFOOT:
1663 case LyXTabular::SET_LTFOOT:
1664 tabular.getRowOfLTFoot(row, ltt);
1665 checkLongtableSpecial(ltt, value, flag);
1666 tabular.setLTFoot(row, flag, ltt, false);
1669 case LyXTabular::UNSET_LTLASTFOOT:
1671 case LyXTabular::SET_LTLASTFOOT:
1672 tabular.getRowOfLTLastFoot(row, ltt);
1673 checkLongtableSpecial(ltt, value, flag);
1674 tabular.setLTFoot(row, flag, ltt, true);
1677 case LyXTabular::SET_LTNEWPAGE:
1678 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1681 case LyXTabular::SET_BOOKTABS:
1682 tabular.setBookTabs(true);
1685 case LyXTabular::UNSET_BOOKTABS:
1686 tabular.setBookTabs(false);
1689 case LyXTabular::SET_TOP_SPACE: {
1691 if (value == "default")
1692 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1693 tabular.row_info[i].top_space_default = true;
1694 else if (isValidLength(value, &len))
1695 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1696 tabular.row_info[i].top_space_default = false;
1697 tabular.row_info[i].top_space = len;
1700 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1701 tabular.row_info[i].top_space_default = false;
1702 tabular.row_info[i].top_space = len;
1707 case LyXTabular::SET_BOTTOM_SPACE: {
1709 if (value == "default")
1710 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1711 tabular.row_info[i].bottom_space_default = true;
1712 else if (isValidLength(value, &len))
1713 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1714 tabular.row_info[i].bottom_space_default = false;
1715 tabular.row_info[i].bottom_space = len;
1718 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1719 tabular.row_info[i].bottom_space_default = false;
1720 tabular.row_info[i].bottom_space = len;
1725 case LyXTabular::SET_INTERLINE_SPACE: {
1727 if (value == "default")
1728 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1729 tabular.row_info[i].interline_space_default = true;
1730 else if (isValidLength(value, &len))
1731 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1732 tabular.row_info[i].interline_space_default = false;
1733 tabular.row_info[i].interline_space = len;
1736 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1737 tabular.row_info[i].interline_space_default = false;
1738 tabular.row_info[i].interline_space = len;
1743 // dummy stuff just to avoid warnings
1744 case LyXTabular::LAST_ACTION:
1748 InsetTabularMailer(*this).updateDialog(&bv);
1752 bool InsetTabular::showInsetDialog(BufferView * bv) const
1754 InsetTabularMailer(*this).showDialog(bv);
1759 void InsetTabular::openLayoutDialog(BufferView * bv) const
1761 InsetTabularMailer(*this).showDialog(bv);
1765 bool InsetTabular::copySelection(LCursor & cur)
1767 if (!cur.selection())
1772 getSelection(cur, rs, re, cs, ce);
1774 paste_tabular.reset(new LyXTabular(tabular));
1776 for (row_type i = 0; i < rs; ++i)
1777 paste_tabular->deleteRow(0);
1779 row_type const rows = re - rs + 1;
1780 while (paste_tabular->rows() > rows)
1781 paste_tabular->deleteRow(rows);
1783 paste_tabular->setTopLine(0, true, true);
1784 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1787 for (col_type i = 0; i < cs; ++i)
1788 paste_tabular->deleteColumn(0);
1790 col_type const columns = ce - cs + 1;
1791 while (paste_tabular->columns() > columns)
1792 paste_tabular->deleteColumn(columns);
1794 paste_tabular->setLeftLine(0, true, true);
1795 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1798 odocstringstream os;
1799 OutputParams const runparams;
1800 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1801 theClipboard().put(os.str());
1802 // mark tabular stack dirty
1803 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1804 // when we (hopefully) have a one-for-all paste mechanism.
1805 cap::dirtyTabularStack(true);
1811 bool InsetTabular::pasteSelection(LCursor & cur)
1815 col_type const actcol = tabular.column_of_cell(cur.idx());
1816 row_type const actrow = tabular.row_of_cell(cur.idx());
1817 for (row_type r1 = 0, r2 = actrow;
1818 r1 < paste_tabular->rows() && r2 < tabular.rows();
1820 for (col_type c1 = 0, c2 = actcol;
1821 c1 < paste_tabular->columns() && c2 < tabular.columns();
1823 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1824 tabular.isPartOfMultiColumn(r2, c2))
1826 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1830 if (tabular.isPartOfMultiColumn(r2, c2)) {
1834 shared_ptr<InsetText> inset(
1835 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1836 tabular.setCellInset(r2, c2, inset);
1837 // FIXME: change tracking (MG)
1838 inset->setChange(Change(cur.buffer().params().trackChanges ?
1839 Change::INSERTED : Change::UNCHANGED));
1847 void InsetTabular::cutSelection(LCursor & cur)
1849 if (!cur.selection())
1854 getSelection(cur, rs, re, cs, ce);
1855 for (row_type i = rs; i <= re; ++i) {
1856 for (col_type j = cs; j <= ce; ++j) {
1857 shared_ptr<InsetText> t
1858 = cell(tabular.getCellNumber(i, j));
1859 if (cur.buffer().params().trackChanges)
1860 // FIXME: Change tracking (MG)
1861 t->setChange(Change(Change::DELETED));
1867 // cursor position might be invalid now
1868 if (cur.pit() > cur.lastpit())
1869 cur.pit() = cur.lastpit();
1870 if (cur.pos() > cur.lastpos())
1871 cur.pos() = cur.lastpos();
1872 cur.clearSelection();
1876 bool InsetTabular::isRightToLeft(LCursor & cur) const
1878 BOOST_ASSERT(cur.depth() > 1);
1879 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1880 pos_type const parentpos = cur[cur.depth() - 2].pos();
1881 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1882 parentpos).language()->rightToLeft();
1886 void InsetTabular::getSelection(LCursor & cur,
1887 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1889 CursorSlice const & beg = cur.selBegin();
1890 CursorSlice const & end = cur.selEnd();
1891 cs = tabular.column_of_cell(beg.idx());
1892 ce = tabular.column_of_cell(end.idx());
1895 cs = tabular.column_of_cell(end.idx());
1897 ce = tabular.right_column_of_cell(end.idx());
1900 rs = tabular.row_of_cell(beg.idx());
1901 re = tabular.row_of_cell(end.idx());
1907 LyXText * InsetTabular::getText(int idx) const
1909 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1913 void InsetTabular::setChange(Change const & change)
1915 for (idx_type idx = 0; idx < nargs(); ++idx)
1916 cell(idx)->setChange(change);
1920 void InsetTabular::acceptChanges()
1922 for (idx_type idx = 0; idx < nargs(); ++idx)
1923 cell(idx)->acceptChanges();
1927 void InsetTabular::rejectChanges()
1929 for (idx_type idx = 0; idx < nargs(); ++idx)
1930 cell(idx)->rejectChanges();
1934 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1936 return tabular.getPWidth(cell).zero();
1940 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1943 if (buf.length() <= 0)
1946 Buffer const & buffer = *bv.buffer();
1950 col_type maxCols = 1;
1951 docstring::size_type const len = buf.length();
1952 docstring::size_type p = 0;
1955 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
1963 maxCols = max(cols, maxCols);
1969 maxCols = max(cols, maxCols);
1970 LyXTabular * loctab;
1975 paste_tabular.reset(
1976 new LyXTabular(buffer.params(), rows, maxCols));
1977 loctab = paste_tabular.get();
1979 dirtyTabularStack(true);
1982 cell = bv.cursor().idx();
1983 ocol = tabular.column_of_cell(cell);
1984 row = tabular.row_of_cell(cell);
1987 docstring::size_type op = 0;
1988 idx_type const cells = loctab->getNumberOfCells();
1991 rows = loctab->rows();
1992 col_type const columns = loctab->columns();
1994 while (cell < cells && p < len && row < rows &&
1995 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
2001 // we can only set this if we are not too far right
2002 if (cols < columns) {
2003 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2004 Paragraph & par = inset->text_.getPar(0);
2005 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2006 inset->setText(buf.substr(op, p - op), font,
2007 buffer.params().trackChanges);
2013 // we can only set this if we are not too far right
2014 if (cols < columns) {
2015 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2016 Paragraph & par = inset->text_.getPar(0);
2017 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2018 inset->setText(buf.substr(op, p - op), font,
2019 buffer.params().trackChanges);
2024 cell = loctab->getCellNumber(row, cols);
2030 // check for the last cell if there is no trailing '\n'
2031 if (cell < cells && op < len) {
2032 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2033 Paragraph & par = inset->text_.getPar(0);
2034 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2035 inset->setText(buf.substr(op, len - op), font,
2036 buffer.params().trackChanges);
2042 void InsetTabular::addPreview(PreviewLoader & loader) const
2044 row_type const rows = tabular.rows();
2045 col_type const columns = tabular.columns();
2046 for (row_type i = 0; i < rows; ++i) {
2047 for (col_type j = 0; j < columns; ++j)
2048 tabular.getCellInset(i, j)->addPreview(loader);
2053 bool InsetTabular::tablemode(LCursor & cur) const
2055 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2062 string const InsetTabularMailer::name_("tabular");
2064 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2065 : inset_(const_cast<InsetTabular &>(inset))
2069 string const InsetTabularMailer::inset2string(Buffer const &) const
2071 return params2string(inset_);
2075 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2077 istringstream data(in);
2079 lex.setStream(data);
2086 if (!lex || token != name_)
2087 return print_mailer_error("InsetTabularMailer", in, 1,
2090 // This is part of the inset proper that is usually swallowed
2091 // by Buffer::readInset
2093 if (!lex || token != "Tabular")
2094 return print_mailer_error("InsetTabularMailer", in, 2,
2097 Buffer const & buffer = inset.buffer();
2098 inset.read(buffer, lex);
2102 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2105 data << name_ << ' ';
2106 inset.write(inset.buffer(), data);
2107 data << "\\end_inset\n";