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"
43 #include "frontends/nullpainter.h"
52 using cap::dirtyTabularStack;
53 using cap::tabularStackDirty;
55 using graphics::PreviewLoader;
59 using frontend::Painter;
60 using frontend::Clipboard;
62 using boost::shared_ptr;
68 using std::istringstream;
70 using std::ostringstream;
74 namespace Alert = frontend::Alert;
79 int const ADD_TO_HEIGHT = 2;
80 int const ADD_TO_TABULAR_WIDTH = 2;
81 int const default_line_space = 10;
84 boost::scoped_ptr<LyXTabular> paste_tabular;
87 struct TabularFeature {
88 LyXTabular::Feature action;
93 TabularFeature tabularFeature[] =
95 { LyXTabular::APPEND_ROW, "append-row" },
96 { LyXTabular::APPEND_COLUMN, "append-column" },
97 { LyXTabular::DELETE_ROW, "delete-row" },
98 { LyXTabular::DELETE_COLUMN, "delete-column" },
99 { LyXTabular::COPY_ROW, "copy-row" },
100 { LyXTabular::COPY_COLUMN, "copy-column" },
101 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
102 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
103 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
104 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
105 { LyXTabular::ALIGN_LEFT, "align-left" },
106 { LyXTabular::ALIGN_RIGHT, "align-right" },
107 { LyXTabular::ALIGN_CENTER, "align-center" },
108 { LyXTabular::ALIGN_BLOCK, "align-block" },
109 { LyXTabular::VALIGN_TOP, "valign-top" },
110 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
111 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
112 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
113 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
114 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
115 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
116 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
117 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
118 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
119 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
120 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
121 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
122 { LyXTabular::MULTICOLUMN, "multicolumn" },
123 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
124 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
125 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
126 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
127 { LyXTabular::SET_PWIDTH, "set-pwidth" },
128 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
129 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
130 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
131 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
132 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
133 { LyXTabular::SET_USEBOX, "set-usebox" },
134 { LyXTabular::SET_LTHEAD, "set-lthead" },
135 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
136 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
137 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
138 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
139 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
140 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
141 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
142 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
143 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
144 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
145 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
146 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
147 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
148 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
149 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
150 { LyXTabular::LAST_ACTION, "" }
154 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
156 FeatureEqual(LyXTabular::Feature feature)
157 : feature_(feature) {}
158 bool operator()(TabularFeature const & tf) const {
159 return tf.action == feature_;
162 LyXTabular::Feature feature_;
168 string const featureAsString(LyXTabular::Feature feature)
170 TabularFeature * end = tabularFeature +
171 sizeof(tabularFeature) / sizeof(TabularFeature);
172 TabularFeature * it = std::find_if(tabularFeature, end,
173 FeatureEqual(feature));
174 return (it == end) ? string() : it->feature;
178 bool InsetTabular::hasPasteBuffer() const
180 return (paste_tabular.get() != 0);
184 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
186 : tabular(buf.params(), max(rows, row_type(1)),
187 max(columns, col_type(1))), buffer_(&buf), scx_(0)
191 InsetTabular::InsetTabular(InsetTabular const & tab)
192 : InsetOld(tab), tabular(tab.tabular),
193 buffer_(tab.buffer_), scx_(0)
197 InsetTabular::~InsetTabular()
199 InsetTabularMailer(*this).hideDialog();
203 auto_ptr<InsetBase> InsetTabular::doClone() const
205 return auto_ptr<InsetBase>(new InsetTabular(*this));
209 Buffer const & InsetTabular::buffer() const
215 void InsetTabular::buffer(Buffer const * b)
221 void InsetTabular::write(Buffer const & buf, ostream & os) const
223 os << "Tabular" << endl;
224 tabular.write(buf, os);
228 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
230 bool const old_format = (lex.getString() == "\\LyXTable");
232 tabular.read(buf, lex);
238 string token = lex.getString();
239 while (lex.isOK() && (token != "\\end_inset")) {
241 token = lex.getString();
243 if (token != "\\end_inset") {
244 lex.printError("Missing \\end_inset at this point. "
250 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
252 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
253 // mi.base.textwidth << "\n";
255 lyxerr << "InsetTabular::metrics: need bv" << endl;
260 for (idx_type cell = 0; i < tabular.rows(); ++i) {
263 for (col_type j = 0; j < tabular.columns(); ++j) {
264 if (tabular.isPartOfMultiColumn(i, j))
265 // Multicolumn cell, but not first one
270 if (tabular.cell_info[i][j].multicolumn ==
271 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
272 p_width = tabular.cellinfo_of_cell(cell).p_width;
274 p_width = tabular.column_info[j].p_width;
276 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
277 tabular.getCellInset(cell)->metrics(m, dim);
279 dim.wid = m.base.textwidth;
280 tabular.setWidthOfCell(cell, dim.wid);
281 if (p_width.zero()) {
282 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
283 // FIXME there must be a way to get rid of
284 // the second metrics call
285 tabular.getCellInset(cell)->metrics(m, dim);
287 maxAsc = max(maxAsc, dim.asc);
288 maxDesc = max(maxDesc, dim.des);
291 int const top_space = tabular.row_info[i].top_space_default ?
293 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
294 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
295 int const bottom_space = tabular.row_info[i].bottom_space_default ?
297 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
298 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
301 dim.asc = tabular.getAscentOfRow(0);
302 dim.des = tabular.getHeightOfTabular() - dim.asc;
303 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
308 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
310 setPosCache(pi, x, y);
312 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
313 BufferView * bv = pi.base.bv;
315 static frontend::NullPainter nop;
316 static PainterInfo nullpi(bv, nop);
318 resetPos(bv->cursor());
321 x += ADD_TO_TABULAR_WIDTH;
324 first_visible_cell = LyXTabular::npos;
325 for (row_type i = 0; i < tabular.rows(); ++i) {
327 int const a = tabular.getAscentOfRow(i);
328 int const d = tabular.getDescentOfRow(i);
329 idx = tabular.getCellNumber(i, 0);
330 for (col_type j = 0; j < tabular.columns(); ++j) {
331 if (tabular.isPartOfMultiColumn(i, j))
333 if (first_visible_cell == LyXTabular::npos)
334 first_visible_cell = idx;
336 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
337 if (nx + tabular.getWidthOfColumn(idx) < 0
338 || nx > bv->workWidth()
340 || y - a > bv->workHeight()) {
341 cell(idx)->draw(nullpi, cx, y);
342 drawCellLines(nop, nx, y, i, idx, pi.erased_);
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 cur.selection() = false;
501 setCursorFromCoordinates(cur, cmd.x, cmd.y);
507 if (cmd.button() == mouse_button::button2) {
508 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
509 doDispatch(cur, cmd);
513 case LFUN_MOUSE_MOTION:
514 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
515 if (cmd.button() == mouse_button::button1) {
516 // only accept motions to places not deeper nested than the real anchor
517 if (bvcur.anchor_.hasPart(cur)) {
518 setCursorFromCoordinates(cur, cmd.x, cmd.y);
519 bvcur.setCursor(cur);
520 bvcur.selection() = true;
526 case LFUN_MOUSE_RELEASE:
527 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
528 if (cmd.button() == mouse_button::button3)
529 InsetTabularMailer(*this).showDialog(&cur.bv());
532 case LFUN_CELL_BACKWARD:
534 cur.selection() = false;
537 case LFUN_CELL_FORWARD:
539 cur.selection() = false;
542 case LFUN_CHAR_FORWARD_SELECT:
543 case LFUN_CHAR_FORWARD:
544 cell(cur.idx())->dispatch(cur, cmd);
545 if (!cur.result().dispatched()) {
546 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
548 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
554 case LFUN_CHAR_BACKWARD_SELECT:
555 case LFUN_CHAR_BACKWARD:
556 cell(cur.idx())->dispatch(cur, cmd);
557 if (!cur.result().dispatched()) {
558 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
560 cmd = FuncRequest(LFUN_FINISHED_LEFT);
566 case LFUN_DOWN_SELECT:
568 cell(cur.idx())->dispatch(cur, cmd);
569 cur.dispatched(); // override the cell's decision
571 // if our LyXText didn't do anything to the cursor
572 // then we try to put the cursor into the cell below
573 // setting also the right targetX.
574 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
575 cur.idx() = tabular.getCellBelow(cur.idx());
577 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
578 cur.pit(), 0, cur.targetX());
580 if (sl == cur.top()) {
581 // we trick it to go to the RIGHT after leaving the
583 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
590 cell(cur.idx())->dispatch(cur, cmd);
591 cur.dispatched(); // override the cell's decision
593 // if our LyXText didn't do anything to the cursor
594 // then we try to put the cursor into the cell above
595 // setting also the right targetX.
596 if (tabular.row_of_cell(cur.idx()) != 0) {
597 cur.idx() = tabular.getCellAbove(cur.idx());
598 cur.pit() = cur.lastpit();
599 LyXText const * text = cell(cur.idx())->getText(0);
600 cur.pos() = text->x2pos(
602 text->paragraphs().back().rows().size()-1,
605 if (sl == cur.top()) {
606 cmd = FuncRequest(LFUN_FINISHED_UP);
611 // case LFUN_SCREEN_DOWN: {
612 // //if (hasSelection())
613 // // cur.selection() = false;
614 // col_type const col = tabular.column_of_cell(cur.idx());
615 // int const t = cur.bv().top_y() + cur.bv().height();
616 // if (t < yo() + tabular.getHeightOfTabular()) {
617 // cur.bv().scrollDocView(t);
618 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
620 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
627 // case LFUN_SCREEN_UP: {
628 // //if (hasSelection())
629 // // cur.selection() = false;
630 // col_type const col = tabular.column_of_cell(cur.idx());
631 // int const t = cur.bv().top_y() + cur.bv().height();
633 // cur.bv().scrollDocView(t);
637 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
641 // cur.par() = cur.lastpar();
642 // cur.pos() = cur.lastpos();
646 case LFUN_LAYOUT_TABULAR:
647 InsetTabularMailer(*this).showDialog(&cur.bv());
650 case LFUN_INSET_DIALOG_UPDATE:
651 InsetTabularMailer(*this).updateDialog(&cur.bv());
654 case LFUN_TABULAR_FEATURE:
655 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
659 // insert file functions
660 case LFUN_FILE_INSERT_ASCII_PARA:
661 case LFUN_FILE_INSERT_ASCII: {
662 // FIXME: We don't know the encoding of filenames
663 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), to_utf8(cmd.argument()), false);
664 // FIXME: We don't know the encoding of the file
665 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), from_utf8(tmpstr), false))
671 if (tablemode(cur)) {
672 if (copySelection(cur)) {
673 recordUndoInset(cur, Undo::DELETE);
678 cell(cur.idx())->dispatch(cur, cmd);
681 case LFUN_CHAR_DELETE_BACKWARD:
682 case LFUN_CHAR_DELETE_FORWARD:
683 if (tablemode(cur)) {
684 recordUndoInset(cur, Undo::DELETE);
688 cell(cur.idx())->dispatch(cur, cmd);
692 if (!cur.selection())
694 if (tablemode(cur)) {
698 cell(cur.idx())->dispatch(cur, cmd);
701 case LFUN_CLIPBOARD_PASTE:
702 case LFUN_PRIMARY_SELECTION_PASTE: {
703 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
704 theClipboard().get() :
705 theSelection().get();
708 // pass to InsertAsciiString, but
709 // only if we have multi-cell content
710 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
711 if (insertAsciiString(cur.bv(), clip, false)) {
712 // content has been replaced,
713 // so cursor might be invalid
714 cur.pos() = cur.lastpos();
715 bvcur.setCursor(cur);
719 // so that the clipboard is used and it goes on
721 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
722 paste_tabular.reset();
723 dirtyTabularStack(false);
729 if (hasPasteBuffer() && tabularStackDirty()) {
730 recordUndoInset(cur, Undo::INSERT);
734 cell(cur.idx())->dispatch(cur, cmd);
739 case LFUN_FONT_ROMAN:
745 case LFUN_FONT_FREE_APPLY:
746 case LFUN_FONT_FREE_UPDATE:
748 case LFUN_FONT_UNDERLINE:
750 case LFUN_WORD_CAPITALIZE:
751 case LFUN_WORD_UPCASE:
752 case LFUN_WORD_LOWCASE:
753 case LFUN_CHARS_TRANSPOSE:
754 if (tablemode(cur)) {
757 getSelection(cur, rs, re, cs, ce);
758 LCursor tmpcur = cur;
759 for (row_type i = rs; i <= re; ++i) {
760 for (col_type j = cs; j <= ce; ++j) {
761 // cursor follows cell:
762 tmpcur.idx() = tabular.getCellNumber(i, j);
763 // select this cell only:
766 tmpcur.resetAnchor();
767 tmpcur.pit() = tmpcur.lastpit();
768 tmpcur.pos() = tmpcur.top().lastpos();
769 tmpcur.setCursor(tmpcur);
770 tmpcur.setSelection();
771 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
776 cell(cur.idx())->dispatch(cur, cmd);
780 // we try to handle this event in the insets dispatch function.
781 cell(cur.idx())->dispatch(cur, cmd);
785 // FIXME: this accesses the position cache before it is initialized
787 InsetTabularMailer(*this).updateDialog(&cur.bv());
791 // function sets an object as defined in func_status.h:
792 // states OK, Unknown, Disabled, On, Off.
793 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
794 FuncStatus & status) const
796 switch (cmd.action) {
797 case LFUN_TABULAR_FEATURE: {
798 int action = LyXTabular::LAST_ACTION;
800 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
801 string const tmp = tabularFeature[i].feature;
802 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
803 action = tabularFeature[i].action;
807 if (action == LyXTabular::LAST_ACTION) {
809 status.unknown(true);
813 string const argument
814 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
816 row_type sel_row_start = 0;
817 row_type sel_row_end = 0;
819 LyXTabular::ltType dummyltt;
822 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
825 case LyXTabular::SET_PWIDTH:
826 case LyXTabular::SET_MPWIDTH:
827 case LyXTabular::SET_SPECIAL_COLUMN:
828 case LyXTabular::SET_SPECIAL_MULTI:
829 case LyXTabular::APPEND_ROW:
830 case LyXTabular::APPEND_COLUMN:
831 case LyXTabular::DELETE_ROW:
832 case LyXTabular::DELETE_COLUMN:
833 case LyXTabular::COPY_ROW:
834 case LyXTabular::COPY_COLUMN:
835 case LyXTabular::SET_ALL_LINES:
836 case LyXTabular::UNSET_ALL_LINES:
837 case LyXTabular::SET_TOP_SPACE:
838 case LyXTabular::SET_BOTTOM_SPACE:
839 case LyXTabular::SET_INTERLINE_SPACE:
843 case LyXTabular::MULTICOLUMN:
844 status.setOnOff(tabular.isMultiColumn(cur.idx()));
847 case LyXTabular::M_TOGGLE_LINE_TOP:
849 case LyXTabular::TOGGLE_LINE_TOP:
850 status.setOnOff(tabular.topLine(cur.idx(), flag));
853 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
855 case LyXTabular::TOGGLE_LINE_BOTTOM:
856 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
859 case LyXTabular::M_TOGGLE_LINE_LEFT:
861 case LyXTabular::TOGGLE_LINE_LEFT:
862 status.setOnOff(tabular.leftLine(cur.idx(), flag));
865 case LyXTabular::M_TOGGLE_LINE_RIGHT:
867 case LyXTabular::TOGGLE_LINE_RIGHT:
868 status.setOnOff(tabular.rightLine(cur.idx(), flag));
871 case LyXTabular::M_ALIGN_LEFT:
873 case LyXTabular::ALIGN_LEFT:
874 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
877 case LyXTabular::M_ALIGN_RIGHT:
879 case LyXTabular::ALIGN_RIGHT:
880 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
883 case LyXTabular::M_ALIGN_CENTER:
885 case LyXTabular::ALIGN_CENTER:
886 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
889 case LyXTabular::ALIGN_BLOCK:
890 status.enabled(!tabular.getPWidth(cur.idx()).zero());
891 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
894 case LyXTabular::M_VALIGN_TOP:
896 case LyXTabular::VALIGN_TOP:
898 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
901 case LyXTabular::M_VALIGN_BOTTOM:
903 case LyXTabular::VALIGN_BOTTOM:
905 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
908 case LyXTabular::M_VALIGN_MIDDLE:
910 case LyXTabular::VALIGN_MIDDLE:
912 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
915 case LyXTabular::SET_LONGTABULAR:
916 status.setOnOff(tabular.isLongTabular());
919 case LyXTabular::UNSET_LONGTABULAR:
920 status.setOnOff(!tabular.isLongTabular());
923 case LyXTabular::SET_ROTATE_TABULAR:
924 status.setOnOff(tabular.getRotateTabular());
927 case LyXTabular::UNSET_ROTATE_TABULAR:
928 status.setOnOff(!tabular.getRotateTabular());
931 case LyXTabular::SET_ROTATE_CELL:
932 status.setOnOff(tabular.getRotateCell(cur.idx()));
935 case LyXTabular::UNSET_ROTATE_CELL:
936 status.setOnOff(!tabular.getRotateCell(cur.idx()));
939 case LyXTabular::SET_USEBOX:
940 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
943 case LyXTabular::SET_LTFIRSTHEAD:
944 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
947 case LyXTabular::UNSET_LTFIRSTHEAD:
948 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::SET_LTHEAD:
952 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
955 case LyXTabular::UNSET_LTHEAD:
956 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
959 case LyXTabular::SET_LTFOOT:
960 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
963 case LyXTabular::UNSET_LTFOOT:
964 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
967 case LyXTabular::SET_LTLASTFOOT:
968 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
971 case LyXTabular::UNSET_LTLASTFOOT:
972 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
975 case LyXTabular::SET_LTNEWPAGE:
976 status.setOnOff(tabular.getLTNewPage(sel_row_start));
979 case LyXTabular::SET_BOOKTABS:
980 status.setOnOff(tabular.useBookTabs());
983 case LyXTabular::UNSET_BOOKTABS:
984 status.setOnOff(!tabular.useBookTabs());
989 status.enabled(false);
995 // These are only enabled inside tabular
996 case LFUN_CELL_BACKWARD:
997 case LFUN_CELL_FORWARD:
998 status.enabled(true);
1001 // disable these with multiple cells selected
1002 case LFUN_INSET_INSERT:
1003 case LFUN_TABULAR_INSERT:
1004 case LFUN_CHARSTYLE_INSERT:
1005 case LFUN_FLOAT_INSERT:
1006 case LFUN_FLOAT_WIDE_INSERT:
1007 case LFUN_FOOTNOTE_INSERT:
1008 case LFUN_MARGINALNOTE_INSERT:
1009 case LFUN_MATH_INSERT:
1010 case LFUN_MATH_MODE:
1011 case LFUN_MATH_MUTATE:
1012 case LFUN_MATH_DISPLAY:
1013 case LFUN_NOTE_INSERT:
1014 case LFUN_OPTIONAL_INSERT:
1015 case LFUN_BOX_INSERT:
1016 case LFUN_BRANCH_INSERT:
1017 case LFUN_WRAP_INSERT:
1018 case LFUN_ERT_INSERT: {
1019 if (tablemode(cur)) {
1020 status.enabled(false);
1023 return cell(cur.idx())->getStatus(cur, cmd, status);
1026 // disable in non-fixed-width cells
1027 case LFUN_BREAK_LINE:
1028 case LFUN_BREAK_PARAGRAPH:
1029 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1030 case LFUN_BREAK_PARAGRAPH_SKIP: {
1031 if (tabular.getPWidth(cur.idx()).zero()) {
1032 status.enabled(false);
1035 return cell(cur.idx())->getStatus(cur, cmd, status);
1039 if (tabularStackDirty()) {
1040 status.enabled(true);
1044 case LFUN_INSET_MODIFY:
1045 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1046 status.enabled(true);
1052 // we try to handle this event in the insets dispatch function.
1053 return cell(cur.idx())->getStatus(cur, cmd, status);
1058 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1059 OutputParams const & runparams) const
1061 return tabular.latex(buf, os, runparams);
1065 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1066 OutputParams const & runparams) const
1068 int const dp = runparams.linelen ? runparams.depth : 0;
1069 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1073 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1074 OutputParams const & runparams) const
1077 InsetBase * master = 0;
1079 #ifdef WITH_WARNINGS
1080 #warning Why not pass a proper DocIterator here?
1083 // if the table is inside a float it doesn't need the informaltable
1084 // wrapper. Search for it.
1085 for (master = owner(); master; master = master->owner())
1086 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1091 os << "<informaltable>";
1094 ret += tabular.docbook(buf, os, runparams);
1096 os << "</informaltable>";
1103 void InsetTabular::validate(LaTeXFeatures & features) const
1105 tabular.validate(features);
1109 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1111 return tabular.getCellInset(idx);
1115 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1117 return tabular.getCellInset(idx);
1121 void InsetTabular::cursorPos(BufferView const & bv,
1122 CursorSlice const & sl, bool boundary, int & x, int & y) const
1124 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1126 // y offset correction
1127 int const row = tabular.row_of_cell(sl.idx());
1128 for (int i = 0; i <= row; ++i) {
1130 y += tabular.getAscentOfRow(i);
1131 y += tabular.getAdditionalHeight(i);
1134 y += tabular.getDescentOfRow(i);
1137 // x offset correction
1138 int const col = tabular.column_of_cell(sl.idx());
1139 int idx = tabular.getCellNumber(row, 0);
1140 for (int j = 0; j < col; ++j) {
1141 if (tabular.isPartOfMultiColumn(row, j))
1143 x += tabular.getWidthOfColumn(idx);
1146 x += tabular.getBeginningOfTextInCell(idx);
1147 x += ADD_TO_TABULAR_WIDTH;
1152 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1156 InsetBase const & inset = *tabular.getCellInset(cell);
1157 Point o = bv.coordCache().getInsets().xy(&inset);
1158 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1159 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1160 row_type const row = tabular.row_of_cell(cell);
1161 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1162 tabular.getAdditionalHeight(row);
1163 int const yend = o.y_ + tabular.getDescentOfRow(row);
1175 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1176 // << " ybeg=" << ybeg << " yend=" << yend
1177 // << " xx=" << xx << " yy=" << yy
1178 // << " dist=" << xx + yy << endl;
1183 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1185 //lyxerr << "InsetTabular::editXY: " << this << endl;
1186 cur.selection() = false;
1188 cur.idx() = getNearestCell(cur.bv(), x, y);
1190 return cell(cur.idx())->text_.editXY(cur, x, y);
1194 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1196 cur.idx() = getNearestCell(cur.bv(), x, y);
1197 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1201 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1203 idx_type idx_min = 0;
1204 int dist_min = std::numeric_limits<int>::max();
1205 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1206 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1207 int const d = dist(bv, i, x, y);
1218 int InsetTabular::getCellXPos(idx_type const cell) const
1222 for (; !tabular.isFirstCellInRow(c); --c)
1224 int lx = tabular.getWidthOfColumn(cell);
1225 for (; c < cell; ++c)
1226 lx += tabular.getWidthOfColumn(c);
1228 return lx - tabular.getWidthOfColumn(cell);
1232 void InsetTabular::resetPos(LCursor & cur) const
1234 BufferView & bv = cur.bv();
1235 int const maxwidth = bv.workWidth();
1237 if (&cur.inset() != this) {
1241 int const X2 = maxwidth;
1242 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1243 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1244 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1247 scx_ = X1 + 20 - x1;
1249 scx_ = X2 - 20 - x2;
1256 InsetTabularMailer(*this).updateDialog(&bv);
1260 void InsetTabular::moveNextCell(LCursor & cur)
1262 if (isRightToLeft(cur)) {
1263 if (tabular.isFirstCellInRow(cur.idx())) {
1264 row_type const row = tabular.row_of_cell(cur.idx());
1265 if (row == tabular.rows() - 1)
1267 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1274 if (tabular.isLastCell(cur.idx()))
1284 void InsetTabular::movePrevCell(LCursor & cur)
1286 if (isRightToLeft(cur)) {
1287 if (tabular.isLastCellInRow(cur.idx())) {
1288 row_type const row = tabular.row_of_cell(cur.idx());
1291 cur.idx() = tabular.getFirstCellInRow(row);
1292 cur.idx() = tabular.getCellAbove(cur.idx());
1294 if (tabular.isLastCell(cur.idx()))
1299 if (cur.idx() == 0) // first cell
1303 cur.pit() = cur.lastpit();
1304 cur.pos() = cur.lastpos();
1306 // FIXME: this accesses the position cache before it is initialized
1311 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1313 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1316 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1317 string const tmp = tabularFeature[i].feature;
1319 if (tmp == what.substr(0, tmp.length())) {
1320 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1321 //tabularFeatures[i].feature.length())) {
1322 action = tabularFeature[i].action;
1326 if (action == LyXTabular::LAST_ACTION)
1330 ltrim(what.substr(tabularFeature[i].feature.length()));
1331 tabularFeatures(cur, action, val);
1338 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1339 string const & special, bool & flag)
1341 if (special == "dl_above") {
1344 } else if (special == "dl_below") {
1345 ltt.bottomDL = flag;
1347 } else if (special == "empty") {
1359 void InsetTabular::tabularFeatures(LCursor & cur,
1360 LyXTabular::Feature feature, string const & value)
1362 BufferView & bv = cur.bv();
1363 col_type sel_col_start;
1364 col_type sel_col_end;
1365 row_type sel_row_start;
1366 row_type sel_row_end;
1367 bool setLines = false;
1368 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1369 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1373 case LyXTabular::M_ALIGN_LEFT:
1374 case LyXTabular::ALIGN_LEFT:
1375 setAlign = LYX_ALIGN_LEFT;
1378 case LyXTabular::M_ALIGN_RIGHT:
1379 case LyXTabular::ALIGN_RIGHT:
1380 setAlign = LYX_ALIGN_RIGHT;
1383 case LyXTabular::M_ALIGN_CENTER:
1384 case LyXTabular::ALIGN_CENTER:
1385 setAlign = LYX_ALIGN_CENTER;
1388 case LyXTabular::ALIGN_BLOCK:
1389 setAlign = LYX_ALIGN_BLOCK;
1392 case LyXTabular::M_VALIGN_TOP:
1393 case LyXTabular::VALIGN_TOP:
1394 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1397 case LyXTabular::M_VALIGN_BOTTOM:
1398 case LyXTabular::VALIGN_BOTTOM:
1399 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1402 case LyXTabular::M_VALIGN_MIDDLE:
1403 case LyXTabular::VALIGN_MIDDLE:
1404 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1411 recordUndoInset(cur, Undo::ATOMIC);
1413 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1414 row_type const row = tabular.row_of_cell(cur.idx());
1415 col_type const column = tabular.column_of_cell(cur.idx());
1417 LyXTabular::ltType ltt;
1421 case LyXTabular::SET_PWIDTH: {
1422 LyXLength const len(value);
1423 tabular.setColumnPWidth(cur, cur.idx(), len);
1425 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1426 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1430 case LyXTabular::SET_MPWIDTH:
1431 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1434 case LyXTabular::SET_SPECIAL_COLUMN:
1435 case LyXTabular::SET_SPECIAL_MULTI:
1436 tabular.setAlignSpecial(cur.idx(),value,feature);
1439 case LyXTabular::APPEND_ROW:
1440 // append the row into the tabular
1441 tabular.appendRow(bv.buffer()->params(), cur.idx());
1444 case LyXTabular::APPEND_COLUMN:
1445 // append the column into the tabular
1446 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1447 cur.idx() = tabular.getCellNumber(row, column);
1450 case LyXTabular::DELETE_ROW:
1451 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1452 tabular.deleteRow(sel_row_start);
1453 if (sel_row_start >= tabular.rows())
1455 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1458 cur.selection() = false;
1461 case LyXTabular::DELETE_COLUMN:
1462 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1463 tabular.deleteColumn(sel_col_start);
1464 if (sel_col_start >= tabular.columns())
1466 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1469 cur.selection() = false;
1472 case LyXTabular::COPY_ROW:
1473 tabular.copyRow(bv.buffer()->params(), row);
1476 case LyXTabular::COPY_COLUMN:
1477 tabular.copyColumn(bv.buffer()->params(), column);
1478 cur.idx() = tabular.getCellNumber(row, column);
1481 case LyXTabular::M_TOGGLE_LINE_TOP:
1483 case LyXTabular::TOGGLE_LINE_TOP: {
1484 bool lineSet = !tabular.topLine(cur.idx(), flag);
1485 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1486 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1488 tabular.getCellNumber(i, j),
1493 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1495 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1496 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1497 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1498 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1499 tabular.setBottomLine(
1500 tabular.getCellNumber(i, j),
1506 case LyXTabular::M_TOGGLE_LINE_LEFT:
1508 case LyXTabular::TOGGLE_LINE_LEFT: {
1509 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1510 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1511 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1512 tabular.setLeftLine(
1513 tabular.getCellNumber(i,j),
1519 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1521 case LyXTabular::TOGGLE_LINE_RIGHT: {
1522 bool lineSet = !tabular.rightLine(cur.idx(), flag);
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.setRightLine(
1526 tabular.getCellNumber(i,j),
1532 case LyXTabular::M_ALIGN_LEFT:
1533 case LyXTabular::M_ALIGN_RIGHT:
1534 case LyXTabular::M_ALIGN_CENTER:
1536 case LyXTabular::ALIGN_LEFT:
1537 case LyXTabular::ALIGN_RIGHT:
1538 case LyXTabular::ALIGN_CENTER:
1539 case LyXTabular::ALIGN_BLOCK:
1540 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1541 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1542 tabular.setAlignment(
1543 tabular.getCellNumber(i, j),
1548 case LyXTabular::M_VALIGN_TOP:
1549 case LyXTabular::M_VALIGN_BOTTOM:
1550 case LyXTabular::M_VALIGN_MIDDLE:
1552 case LyXTabular::VALIGN_TOP:
1553 case LyXTabular::VALIGN_BOTTOM:
1554 case LyXTabular::VALIGN_MIDDLE:
1555 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1556 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1557 tabular.setVAlignment(
1558 tabular.getCellNumber(i, j),
1562 case LyXTabular::MULTICOLUMN: {
1563 if (sel_row_start != sel_row_end) {
1564 #ifdef WITH_WARNINGS
1565 #warning Need I say it ? This is horrible.
1568 Alert::error(_("Error setting multicolumn"),
1569 _("You cannot set multicolumn vertically."));
1572 if (!cur.selection()) {
1573 // just multicol for one single cell
1574 // check whether we are completely in a multicol
1575 if (tabular.isMultiColumn(cur.idx()))
1576 tabular.unsetMultiColumn(cur.idx());
1578 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1581 // we have a selection so this means we just add all this
1582 // cells to form a multicolumn cell
1583 idx_type const s_start = cur.selBegin().idx();
1584 idx_type const s_end = cur.selEnd().idx();
1585 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1586 cur.idx() = s_start;
1589 cur.selection() = false;
1593 case LyXTabular::SET_ALL_LINES:
1595 case LyXTabular::UNSET_ALL_LINES:
1596 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1597 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1598 tabular.setAllLines(
1599 tabular.getCellNumber(i,j), setLines);
1602 case LyXTabular::SET_LONGTABULAR:
1603 tabular.setLongTabular(true);
1606 case LyXTabular::UNSET_LONGTABULAR:
1607 tabular.setLongTabular(false);
1610 case LyXTabular::SET_ROTATE_TABULAR:
1611 tabular.setRotateTabular(true);
1614 case LyXTabular::UNSET_ROTATE_TABULAR:
1615 tabular.setRotateTabular(false);
1618 case LyXTabular::SET_ROTATE_CELL:
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.setRotateCell(
1622 tabular.getCellNumber(i, j), true);
1625 case LyXTabular::UNSET_ROTATE_CELL:
1626 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1627 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1628 tabular.setRotateCell(
1629 tabular.getCellNumber(i, j), false);
1632 case LyXTabular::SET_USEBOX: {
1633 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1634 if (val == tabular.getUsebox(cur.idx()))
1635 val = LyXTabular::BOX_NONE;
1636 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1637 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1638 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1642 case LyXTabular::UNSET_LTFIRSTHEAD:
1644 case LyXTabular::SET_LTFIRSTHEAD:
1645 tabular.getRowOfLTFirstHead(row, ltt);
1646 checkLongtableSpecial(ltt, value, flag);
1647 tabular.setLTHead(row, flag, ltt, true);
1650 case LyXTabular::UNSET_LTHEAD:
1652 case LyXTabular::SET_LTHEAD:
1653 tabular.getRowOfLTHead(row, ltt);
1654 checkLongtableSpecial(ltt, value, flag);
1655 tabular.setLTHead(row, flag, ltt, false);
1658 case LyXTabular::UNSET_LTFOOT:
1660 case LyXTabular::SET_LTFOOT:
1661 tabular.getRowOfLTFoot(row, ltt);
1662 checkLongtableSpecial(ltt, value, flag);
1663 tabular.setLTFoot(row, flag, ltt, false);
1666 case LyXTabular::UNSET_LTLASTFOOT:
1668 case LyXTabular::SET_LTLASTFOOT:
1669 tabular.getRowOfLTLastFoot(row, ltt);
1670 checkLongtableSpecial(ltt, value, flag);
1671 tabular.setLTFoot(row, flag, ltt, true);
1674 case LyXTabular::SET_LTNEWPAGE:
1675 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1678 case LyXTabular::SET_BOOKTABS:
1679 tabular.setBookTabs(true);
1682 case LyXTabular::UNSET_BOOKTABS:
1683 tabular.setBookTabs(false);
1686 case LyXTabular::SET_TOP_SPACE: {
1688 if (value == "default")
1689 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1690 tabular.row_info[i].top_space_default = true;
1691 else if (isValidLength(value, &len))
1692 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1693 tabular.row_info[i].top_space_default = false;
1694 tabular.row_info[i].top_space = len;
1697 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1698 tabular.row_info[i].top_space_default = false;
1699 tabular.row_info[i].top_space = len;
1704 case LyXTabular::SET_BOTTOM_SPACE: {
1706 if (value == "default")
1707 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1708 tabular.row_info[i].bottom_space_default = true;
1709 else if (isValidLength(value, &len))
1710 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1711 tabular.row_info[i].bottom_space_default = false;
1712 tabular.row_info[i].bottom_space = len;
1715 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1716 tabular.row_info[i].bottom_space_default = false;
1717 tabular.row_info[i].bottom_space = len;
1722 case LyXTabular::SET_INTERLINE_SPACE: {
1724 if (value == "default")
1725 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1726 tabular.row_info[i].interline_space_default = true;
1727 else if (isValidLength(value, &len))
1728 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1729 tabular.row_info[i].interline_space_default = false;
1730 tabular.row_info[i].interline_space = len;
1733 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1734 tabular.row_info[i].interline_space_default = false;
1735 tabular.row_info[i].interline_space = len;
1740 // dummy stuff just to avoid warnings
1741 case LyXTabular::LAST_ACTION:
1745 InsetTabularMailer(*this).updateDialog(&bv);
1749 bool InsetTabular::showInsetDialog(BufferView * bv) const
1751 InsetTabularMailer(*this).showDialog(bv);
1756 void InsetTabular::openLayoutDialog(BufferView * bv) const
1758 InsetTabularMailer(*this).showDialog(bv);
1762 bool InsetTabular::copySelection(LCursor & cur)
1764 if (!cur.selection())
1769 getSelection(cur, rs, re, cs, ce);
1771 paste_tabular.reset(new LyXTabular(tabular));
1773 for (row_type i = 0; i < rs; ++i)
1774 paste_tabular->deleteRow(0);
1776 row_type const rows = re - rs + 1;
1777 while (paste_tabular->rows() > rows)
1778 paste_tabular->deleteRow(rows);
1780 paste_tabular->setTopLine(0, true, true);
1781 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1784 for (col_type i = 0; i < cs; ++i)
1785 paste_tabular->deleteColumn(0);
1787 col_type const columns = ce - cs + 1;
1788 while (paste_tabular->columns() > columns)
1789 paste_tabular->deleteColumn(columns);
1791 paste_tabular->setLeftLine(0, true, true);
1792 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1795 odocstringstream os;
1796 OutputParams const runparams;
1797 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1798 theClipboard().put(os.str());
1799 // mark tabular stack dirty
1800 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1801 // when we (hopefully) have a one-for-all paste mechanism.
1802 cap::dirtyTabularStack(true);
1808 bool InsetTabular::pasteSelection(LCursor & cur)
1812 col_type const actcol = tabular.column_of_cell(cur.idx());
1813 row_type const actrow = tabular.row_of_cell(cur.idx());
1814 for (row_type r1 = 0, r2 = actrow;
1815 r1 < paste_tabular->rows() && r2 < tabular.rows();
1817 for (col_type c1 = 0, c2 = actcol;
1818 c1 < paste_tabular->columns() && c2 < tabular.columns();
1820 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1821 tabular.isPartOfMultiColumn(r2, c2))
1823 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1827 if (tabular.isPartOfMultiColumn(r2, c2)) {
1831 shared_ptr<InsetText> inset(
1832 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1833 tabular.setCellInset(r2, c2, inset);
1834 // FIXME: change tracking (MG)
1835 inset->setChange(Change(cur.buffer().params().trackChanges ?
1836 Change::INSERTED : Change::UNCHANGED));
1844 void InsetTabular::cutSelection(LCursor & cur)
1846 if (!cur.selection())
1851 getSelection(cur, rs, re, cs, ce);
1852 for (row_type i = rs; i <= re; ++i) {
1853 for (col_type j = cs; j <= ce; ++j) {
1854 shared_ptr<InsetText> t
1855 = cell(tabular.getCellNumber(i, j));
1856 if (cur.buffer().params().trackChanges)
1857 // FIXME: Change tracking (MG)
1858 t->setChange(Change(Change::DELETED));
1864 // cursor position might be invalid now
1865 if (cur.pit() > cur.lastpit())
1866 cur.pit() = cur.lastpit();
1867 if (cur.pos() > cur.lastpos())
1868 cur.pos() = cur.lastpos();
1869 cur.clearSelection();
1873 bool InsetTabular::isRightToLeft(LCursor & cur) const
1875 BOOST_ASSERT(cur.depth() > 1);
1876 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1877 pos_type const parentpos = cur[cur.depth() - 2].pos();
1878 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1879 parentpos).language()->rightToLeft();
1883 void InsetTabular::getSelection(LCursor & cur,
1884 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1886 CursorSlice const & beg = cur.selBegin();
1887 CursorSlice const & end = cur.selEnd();
1888 cs = tabular.column_of_cell(beg.idx());
1889 ce = tabular.column_of_cell(end.idx());
1892 cs = tabular.column_of_cell(end.idx());
1894 ce = tabular.right_column_of_cell(end.idx());
1897 rs = tabular.row_of_cell(beg.idx());
1898 re = tabular.row_of_cell(end.idx());
1904 LyXText * InsetTabular::getText(int idx) const
1906 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1910 void InsetTabular::setChange(Change const & change)
1912 for (idx_type idx = 0; idx < nargs(); ++idx)
1913 cell(idx)->setChange(change);
1917 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1919 return tabular.getPWidth(cell).zero();
1923 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1926 if (buf.length() <= 0)
1931 col_type maxCols = 1;
1932 docstring::size_type const len = buf.length();
1933 docstring::size_type p = 0;
1936 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
1944 maxCols = max(cols, maxCols);
1950 maxCols = max(cols, maxCols);
1951 LyXTabular * loctab;
1956 paste_tabular.reset(
1957 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1958 loctab = paste_tabular.get();
1960 dirtyTabularStack(true);
1963 cell = bv.cursor().idx();
1964 ocol = tabular.column_of_cell(cell);
1965 row = tabular.row_of_cell(cell);
1968 docstring::size_type op = 0;
1969 idx_type const cells = loctab->getNumberOfCells();
1972 rows = loctab->rows();
1973 col_type const columns = loctab->columns();
1975 while (cell < cells && p < len && row < rows &&
1976 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
1982 // we can only set this if we are not too far right
1983 if (cols < columns) {
1984 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1985 inset->setViewCache(&bv);
1986 Paragraph & par = inset->text_.getPar(0);
1987 LyXFont const font = inset->text_.getFont(par, 0);
1988 inset->setText(buf.substr(op, p - op), font,
1989 bv.buffer()->params().trackChanges);
1995 // we can only set this if we are not too far right
1996 if (cols < columns) {
1997 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1998 inset->setViewCache(&bv);
1999 Paragraph & par = inset->text_.getPar(0);
2000 LyXFont const font = inset->text_.getFont(par, 0);
2001 inset->setText(buf.substr(op, p - op), font,
2002 bv.buffer()->params().trackChanges);
2007 cell = loctab->getCellNumber(row, cols);
2013 // check for the last cell if there is no trailing '\n'
2014 if (cell < cells && op < len) {
2015 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2016 inset->setViewCache(&bv);
2017 Paragraph & par = inset->text_.getPar(0);
2018 LyXFont const font = inset->text_.getFont(par, 0);
2019 inset->setText(buf.substr(op, len - op), font,
2020 bv.buffer()->params().trackChanges);
2026 void InsetTabular::addPreview(PreviewLoader & loader) const
2028 row_type const rows = tabular.rows();
2029 col_type const columns = tabular.columns();
2030 for (row_type i = 0; i < rows; ++i) {
2031 for (col_type j = 0; j < columns; ++j)
2032 tabular.getCellInset(i, j)->addPreview(loader);
2037 bool InsetTabular::tablemode(LCursor & cur) const
2039 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2046 string const InsetTabularMailer::name_("tabular");
2048 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2049 : inset_(const_cast<InsetTabular &>(inset))
2053 string const InsetTabularMailer::inset2string(Buffer const &) const
2055 return params2string(inset_);
2059 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2061 istringstream data(in);
2063 lex.setStream(data);
2070 if (!lex || token != name_)
2071 return print_mailer_error("InsetTabularMailer", in, 1,
2074 // This is part of the inset proper that is usually swallowed
2075 // by Buffer::readInset
2077 if (!lex || token != "Tabular")
2078 return print_mailer_error("InsetTabularMailer", in, 2,
2081 Buffer const & buffer = inset.buffer();
2082 inset.read(buffer, lex);
2086 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2089 data << name_ << ' ';
2090 inset.write(inset.buffer(), data);
2091 data << "\\end_inset\n";