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"
50 using lyx::odocstream;
53 using lyx::cap::dirtyTabularStack;
54 using lyx::cap::tabularStackDirty;
56 using lyx::graphics::PreviewLoader;
58 using lyx::support::ltrim;
60 using lyx::frontend::Painter;
61 using lyx::frontend::Clipboard;
63 using boost::shared_ptr;
69 using std::istringstream;
71 using std::ostringstream;
75 namespace Alert = lyx::frontend::Alert;
80 int const ADD_TO_HEIGHT = 2;
81 int const ADD_TO_TABULAR_WIDTH = 2;
82 int const default_line_space = 10;
85 boost::scoped_ptr<LyXTabular> paste_tabular;
88 struct TabularFeature {
89 LyXTabular::Feature action;
94 TabularFeature tabularFeature[] =
96 { LyXTabular::APPEND_ROW, "append-row" },
97 { LyXTabular::APPEND_COLUMN, "append-column" },
98 { LyXTabular::DELETE_ROW, "delete-row" },
99 { LyXTabular::DELETE_COLUMN, "delete-column" },
100 { LyXTabular::COPY_ROW, "copy-row" },
101 { LyXTabular::COPY_COLUMN, "copy-column" },
102 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
103 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
104 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
105 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
106 { LyXTabular::ALIGN_LEFT, "align-left" },
107 { LyXTabular::ALIGN_RIGHT, "align-right" },
108 { LyXTabular::ALIGN_CENTER, "align-center" },
109 { LyXTabular::ALIGN_BLOCK, "align-block" },
110 { LyXTabular::VALIGN_TOP, "valign-top" },
111 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
112 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
113 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
114 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
115 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
116 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
117 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
118 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
119 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
120 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
121 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
122 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
123 { LyXTabular::MULTICOLUMN, "multicolumn" },
124 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
125 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
126 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
127 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
128 { LyXTabular::SET_PWIDTH, "set-pwidth" },
129 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
130 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
131 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
132 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
133 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
134 { LyXTabular::SET_USEBOX, "set-usebox" },
135 { LyXTabular::SET_LTHEAD, "set-lthead" },
136 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
137 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
138 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
139 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
140 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
141 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
142 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
143 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
144 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
145 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
146 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
147 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
148 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
149 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
150 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
151 { LyXTabular::LAST_ACTION, "" }
155 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
157 FeatureEqual(LyXTabular::Feature feature)
158 : feature_(feature) {}
159 bool operator()(TabularFeature const & tf) const {
160 return tf.action == feature_;
163 LyXTabular::Feature feature_;
169 string const featureAsString(LyXTabular::Feature feature)
171 TabularFeature * end = tabularFeature +
172 sizeof(tabularFeature) / sizeof(TabularFeature);
173 TabularFeature * it = std::find_if(tabularFeature, end,
174 FeatureEqual(feature));
175 return (it == end) ? string() : it->feature;
179 bool InsetTabular::hasPasteBuffer() const
181 return (paste_tabular.get() != 0);
185 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
187 : tabular(buf.params(), max(rows, row_type(1)),
188 max(columns, col_type(1))), buffer_(&buf), scx_(0)
192 InsetTabular::InsetTabular(InsetTabular const & tab)
193 : InsetOld(tab), tabular(tab.tabular),
194 buffer_(tab.buffer_), scx_(0)
198 InsetTabular::~InsetTabular()
200 InsetTabularMailer(*this).hideDialog();
204 auto_ptr<InsetBase> InsetTabular::doClone() const
206 return auto_ptr<InsetBase>(new InsetTabular(*this));
210 Buffer const & InsetTabular::buffer() const
216 void InsetTabular::buffer(Buffer const * b)
222 void InsetTabular::write(Buffer const & buf, ostream & os) const
224 os << "Tabular" << endl;
225 tabular.write(buf, os);
229 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
231 bool const old_format = (lex.getString() == "\\LyXTable");
233 tabular.read(buf, lex);
239 string token = lex.getString();
240 while (lex.isOK() && (token != "\\end_inset")) {
242 token = lex.getString();
244 if (token != "\\end_inset") {
245 lex.printError("Missing \\end_inset at this point. "
251 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
253 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
254 // mi.base.textwidth << "\n";
256 lyxerr << "InsetTabular::metrics: need bv" << endl;
261 for (idx_type cell = 0; i < tabular.rows(); ++i) {
264 for (col_type j = 0; j < tabular.columns(); ++j) {
265 if (tabular.isPartOfMultiColumn(i, j))
266 // Multicolumn cell, but not first one
271 if (tabular.cell_info[i][j].multicolumn ==
272 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
273 p_width = tabular.cellinfo_of_cell(cell).p_width;
275 p_width = tabular.column_info[j].p_width;
277 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
278 tabular.getCellInset(cell)->metrics(m, dim);
280 dim.wid = m.base.textwidth;
281 tabular.setWidthOfCell(cell, dim.wid);
282 if (p_width.zero()) {
283 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
284 // FIXME there must be a way to get rid of
285 // the second metrics call
286 tabular.getCellInset(cell)->metrics(m, dim);
288 maxAsc = max(maxAsc, dim.asc);
289 maxDesc = max(maxDesc, dim.des);
292 int const top_space = tabular.row_info[i].top_space_default ?
294 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
295 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
296 int const bottom_space = tabular.row_info[i].bottom_space_default ?
298 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
299 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
302 dim.asc = tabular.getAscentOfRow(0);
303 dim.des = tabular.getHeightOfTabular() - dim.asc;
304 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
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 static lyx::frontend::NullPainter nop;
317 static PainterInfo nullpi(bv, nop);
319 //resetPos(bv->cursor());
322 x += ADD_TO_TABULAR_WIDTH;
325 first_visible_cell = LyXTabular::npos;
326 for (row_type i = 0; i < tabular.rows(); ++i) {
328 int const a = tabular.getAscentOfRow(i);
329 int const d = tabular.getDescentOfRow(i);
330 idx = tabular.getCellNumber(i, 0);
331 for (col_type j = 0; j < tabular.columns(); ++j) {
332 if (tabular.isPartOfMultiColumn(i, j))
334 if (first_visible_cell == LyXTabular::npos)
335 first_visible_cell = idx;
337 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
338 if (nx + tabular.getWidthOfColumn(idx) < 0
339 || nx > bv->workWidth()
341 || y - a > bv->workHeight()) {
342 cell(idx)->draw(nullpi, cx, y);
343 drawCellLines(nop, nx, y, i, idx, pi.erased_);
345 cell(idx)->draw(pi, cx, y);
346 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
348 nx += tabular.getWidthOfColumn(idx);
352 if (i + 1 < tabular.rows())
353 y += d + tabular.getAscentOfRow(i + 1) +
354 tabular.getAdditionalHeight(i + 1);
359 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
361 setPosCache(pi, x, y);
363 LCursor & cur = pi.base.bv->cursor();
365 x += scx_ + ADD_TO_TABULAR_WIDTH;
367 // Paint background of current tabular
368 int const w = tabular.getWidthOfTabular();
369 int const h = tabular.getHeightOfTabular();
370 int yy = y - tabular.getAscentOfRow(0);
371 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
373 if (!cur.selection())
375 if (!ptr_cmp(&cur.inset(), this))
381 if (tablemode(cur)) {
384 getSelection(cur, rs, re, cs, ce);
385 y -= tabular.getAscentOfRow(0);
386 for (row_type j = 0; j < tabular.rows(); ++j) {
387 int const a = tabular.getAscentOfRow(j);
388 int const h = a + tabular.getDescentOfRow(j);
390 y += tabular.getAdditionalHeight(j);
391 for (col_type i = 0; i < tabular.columns(); ++i) {
392 if (tabular.isPartOfMultiColumn(j, i))
394 idx_type const cell =
395 tabular.getCellNumber(j, i);
396 int const w = tabular.getWidthOfColumn(cell);
397 if (i >= cs && i <= ce && j >= rs && j <= re)
398 pi.pain.fillRectangle(xx, y, w, h,
407 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
412 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
413 row_type row, idx_type cell, bool erased) const
415 int x2 = x + tabular.getWidthOfColumn(cell);
417 LColor::color col = LColor::tabularline;
418 LColor::color onoffcol = LColor::tabularonoffline;
421 col = LColor::strikeout;
422 onoffcol = LColor::strikeout;
425 if (!tabular.topAlreadyDrawn(cell)) {
426 on_off = !tabular.topLine(cell);
427 pain.line(x, y - tabular.getAscentOfRow(row),
428 x2, y - tabular.getAscentOfRow(row),
429 on_off ? onoffcol : col,
430 on_off ? Painter::line_onoffdash : Painter::line_solid);
432 on_off = !tabular.bottomLine(cell);
433 pain.line(x, y + tabular.getDescentOfRow(row),
434 x2, y + tabular.getDescentOfRow(row),
435 on_off ? onoffcol : col,
436 on_off ? Painter::line_onoffdash : Painter::line_solid);
437 if (!tabular.leftAlreadyDrawn(cell)) {
438 on_off = !tabular.leftLine(cell);
439 pain.line(x, y - tabular.getAscentOfRow(row),
440 x, y + tabular.getDescentOfRow(row),
441 on_off ? onoffcol : col,
442 on_off ? Painter::line_onoffdash : Painter::line_solid);
444 on_off = !tabular.rightLine(cell);
445 pain.line(x2 - tabular.getAdditionalWidth(cell),
446 y - tabular.getAscentOfRow(row),
447 x2 - tabular.getAdditionalWidth(cell),
448 y + tabular.getDescentOfRow(row),
449 on_off ? onoffcol : col,
450 on_off ? Painter::line_onoffdash : Painter::line_solid);
454 docstring const InsetTabular::editMessage() const
456 return _("Opened table");
460 void InsetTabular::edit(LCursor & cur, bool left)
462 //lyxerr << "InsetTabular::edit: " << this << endl;
464 cur.selection() = false;
467 if (isRightToLeft(cur))
468 cur.idx() = tabular.getLastCellInRow(0);
474 if (isRightToLeft(cur))
475 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
477 cur.idx() = tabular.getNumberOfCells() - 1;
479 cur.pos() = cur.lastpos(); // FIXME crude guess
481 // FIXME: this accesses the position cache before it is initialized
483 //cur.bv().fitCursor();
487 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
489 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
490 << "\n cur:" << cur << endl;
491 CursorSlice sl = cur.top();
492 LCursor & bvcur = cur.bv().cursor();
494 switch (cmd.action) {
496 case LFUN_MOUSE_PRESS:
497 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
499 if (cmd.button() == mouse_button::button1
500 || cmd.button() == mouse_button::button3) {
501 cur.selection() = false;
502 setCursorFromCoordinates(cur, cmd.x, cmd.y);
508 if (cmd.button() == mouse_button::button2) {
509 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
510 doDispatch(cur, cmd);
514 case LFUN_MOUSE_MOTION:
515 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
516 if (cmd.button() == mouse_button::button1) {
517 // only accept motions to places not deeper nested than the real anchor
518 if (bvcur.anchor_.hasPart(cur)) {
519 setCursorFromCoordinates(cur, cmd.x, cmd.y);
520 bvcur.setCursor(cur);
521 bvcur.selection() = true;
527 case LFUN_MOUSE_RELEASE:
528 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
529 if (cmd.button() == mouse_button::button3)
530 InsetTabularMailer(*this).showDialog(&cur.bv());
533 case LFUN_CELL_BACKWARD:
535 cur.selection() = false;
538 case LFUN_CELL_FORWARD:
540 cur.selection() = false;
543 case LFUN_CHAR_FORWARD_SELECT:
544 case LFUN_CHAR_FORWARD:
545 cell(cur.idx())->dispatch(cur, cmd);
546 if (!cur.result().dispatched()) {
547 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
549 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
555 case LFUN_CHAR_BACKWARD_SELECT:
556 case LFUN_CHAR_BACKWARD:
557 cell(cur.idx())->dispatch(cur, cmd);
558 if (!cur.result().dispatched()) {
559 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
561 cmd = FuncRequest(LFUN_FINISHED_LEFT);
567 case LFUN_DOWN_SELECT:
569 cell(cur.idx())->dispatch(cur, cmd);
570 cur.dispatched(); // override the cell's decision
572 // if our LyXText didn't do anything to the cursor
573 // then we try to put the cursor into the cell below
574 // setting also the right targetX.
575 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
576 cur.idx() = tabular.getCellBelow(cur.idx());
578 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
579 cur.pit(), 0, cur.targetX());
581 if (sl == cur.top()) {
582 // we trick it to go to the RIGHT after leaving the
584 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
591 cell(cur.idx())->dispatch(cur, cmd);
592 cur.dispatched(); // override the cell's decision
594 // if our LyXText didn't do anything to the cursor
595 // then we try to put the cursor into the cell above
596 // setting also the right targetX.
597 if (tabular.row_of_cell(cur.idx()) != 0) {
598 cur.idx() = tabular.getCellAbove(cur.idx());
599 cur.pit() = cur.lastpit();
600 LyXText const * text = cell(cur.idx())->getText(0);
601 cur.pos() = text->x2pos(
603 text->paragraphs().back().rows().size()-1,
606 if (sl == cur.top()) {
607 cmd = FuncRequest(LFUN_FINISHED_UP);
612 // case LFUN_SCREEN_DOWN: {
613 // //if (hasSelection())
614 // // cur.selection() = false;
615 // col_type const col = tabular.column_of_cell(cur.idx());
616 // int const t = cur.bv().top_y() + cur.bv().height();
617 // if (t < yo() + tabular.getHeightOfTabular()) {
618 // cur.bv().scrollDocView(t);
619 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
621 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
628 // case LFUN_SCREEN_UP: {
629 // //if (hasSelection())
630 // // cur.selection() = false;
631 // col_type const col = tabular.column_of_cell(cur.idx());
632 // int const t = cur.bv().top_y() + cur.bv().height();
634 // cur.bv().scrollDocView(t);
638 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
642 // cur.par() = cur.lastpar();
643 // cur.pos() = cur.lastpos();
647 case LFUN_LAYOUT_TABULAR:
648 InsetTabularMailer(*this).showDialog(&cur.bv());
651 case LFUN_INSET_DIALOG_UPDATE:
652 InsetTabularMailer(*this).updateDialog(&cur.bv());
655 case LFUN_TABULAR_FEATURE:
656 if (!tabularFeatures(cur, lyx::to_utf8(cmd.argument())))
660 // insert file functions
661 case LFUN_FILE_INSERT_ASCII_PARA:
662 case LFUN_FILE_INSERT_ASCII: {
663 // FIXME: We don't know the encoding of filenames
664 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), lyx::to_utf8(cmd.argument()), false);
665 // FIXME: We don't know the encoding of the file
666 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), lyx::from_utf8(tmpstr), false))
672 if (tablemode(cur)) {
673 if (copySelection(cur)) {
674 recordUndoInset(cur, Undo::DELETE);
679 cell(cur.idx())->dispatch(cur, cmd);
682 case LFUN_CHAR_DELETE_BACKWARD:
683 case LFUN_CHAR_DELETE_FORWARD:
684 if (tablemode(cur)) {
685 recordUndoInset(cur, Undo::DELETE);
689 cell(cur.idx())->dispatch(cur, cmd);
693 if (!cur.selection())
695 if (tablemode(cur)) {
699 cell(cur.idx())->dispatch(cur, cmd);
702 case LFUN_CLIPBOARD_PASTE:
703 case LFUN_PRIMARY_SELECTION_PASTE: {
704 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
705 theClipboard().get() :
706 theSelection().get();
709 // pass to InsertAsciiString, but
710 // only if we have multi-cell content
711 if (clip.find_first_of(lyx::from_ascii("\t\n")) != docstring::npos) {
712 if (insertAsciiString(cur.bv(), clip, false)) {
713 // content has been replaced,
714 // so cursor might be invalid
715 cur.pos() = cur.lastpos();
716 bvcur.setCursor(cur);
720 // so that the clipboard is used and it goes on
722 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
723 paste_tabular.reset();
724 dirtyTabularStack(false);
730 if (hasPasteBuffer() && tabularStackDirty()) {
731 recordUndoInset(cur, Undo::INSERT);
735 cell(cur.idx())->dispatch(cur, cmd);
740 case LFUN_FONT_ROMAN:
746 case LFUN_FONT_FREE_APPLY:
747 case LFUN_FONT_FREE_UPDATE:
749 case LFUN_FONT_UNDERLINE:
751 case LFUN_WORD_CAPITALIZE:
752 case LFUN_WORD_UPCASE:
753 case LFUN_WORD_LOWCASE:
754 case LFUN_CHARS_TRANSPOSE:
755 if (tablemode(cur)) {
758 getSelection(cur, rs, re, cs, ce);
759 LCursor tmpcur = cur;
760 for (row_type i = rs; i <= re; ++i) {
761 for (col_type j = cs; j <= ce; ++j) {
762 // cursor follows cell:
763 tmpcur.idx() = tabular.getCellNumber(i, j);
764 // select this cell only:
767 tmpcur.resetAnchor();
768 tmpcur.pit() = tmpcur.lastpit();
769 tmpcur.pos() = tmpcur.top().lastpos();
770 tmpcur.setCursor(tmpcur);
771 tmpcur.setSelection();
772 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
777 cell(cur.idx())->dispatch(cur, cmd);
781 // we try to handle this event in the insets dispatch function.
782 cell(cur.idx())->dispatch(cur, cmd);
786 // FIXME: this accesses the position cache before it is initialized
788 InsetTabularMailer(*this).updateDialog(&cur.bv());
792 // function sets an object as defined in func_status.h:
793 // states OK, Unknown, Disabled, On, Off.
794 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
795 FuncStatus & status) const
797 switch (cmd.action) {
798 case LFUN_TABULAR_FEATURE: {
799 int action = LyXTabular::LAST_ACTION;
801 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
802 string const tmp = tabularFeature[i].feature;
803 if (tmp == lyx::to_utf8(cmd.argument()).substr(0, tmp.length())) {
804 action = tabularFeature[i].action;
808 if (action == LyXTabular::LAST_ACTION) {
810 status.unknown(true);
814 string const argument
815 = ltrim(lyx::to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
817 row_type sel_row_start = 0;
818 row_type sel_row_end = 0;
820 LyXTabular::ltType dummyltt;
823 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
826 case LyXTabular::SET_PWIDTH:
827 case LyXTabular::SET_MPWIDTH:
828 case LyXTabular::SET_SPECIAL_COLUMN:
829 case LyXTabular::SET_SPECIAL_MULTI:
830 case LyXTabular::APPEND_ROW:
831 case LyXTabular::APPEND_COLUMN:
832 case LyXTabular::DELETE_ROW:
833 case LyXTabular::DELETE_COLUMN:
834 case LyXTabular::COPY_ROW:
835 case LyXTabular::COPY_COLUMN:
836 case LyXTabular::SET_ALL_LINES:
837 case LyXTabular::UNSET_ALL_LINES:
838 case LyXTabular::SET_TOP_SPACE:
839 case LyXTabular::SET_BOTTOM_SPACE:
840 case LyXTabular::SET_INTERLINE_SPACE:
844 case LyXTabular::MULTICOLUMN:
845 status.setOnOff(tabular.isMultiColumn(cur.idx()));
848 case LyXTabular::M_TOGGLE_LINE_TOP:
850 case LyXTabular::TOGGLE_LINE_TOP:
851 status.setOnOff(tabular.topLine(cur.idx(), flag));
854 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
856 case LyXTabular::TOGGLE_LINE_BOTTOM:
857 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
860 case LyXTabular::M_TOGGLE_LINE_LEFT:
862 case LyXTabular::TOGGLE_LINE_LEFT:
863 status.setOnOff(tabular.leftLine(cur.idx(), flag));
866 case LyXTabular::M_TOGGLE_LINE_RIGHT:
868 case LyXTabular::TOGGLE_LINE_RIGHT:
869 status.setOnOff(tabular.rightLine(cur.idx(), flag));
872 case LyXTabular::M_ALIGN_LEFT:
874 case LyXTabular::ALIGN_LEFT:
875 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
878 case LyXTabular::M_ALIGN_RIGHT:
880 case LyXTabular::ALIGN_RIGHT:
881 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
884 case LyXTabular::M_ALIGN_CENTER:
886 case LyXTabular::ALIGN_CENTER:
887 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
890 case LyXTabular::ALIGN_BLOCK:
891 status.enabled(!tabular.getPWidth(cur.idx()).zero());
892 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
895 case LyXTabular::M_VALIGN_TOP:
897 case LyXTabular::VALIGN_TOP:
899 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
902 case LyXTabular::M_VALIGN_BOTTOM:
904 case LyXTabular::VALIGN_BOTTOM:
906 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
909 case LyXTabular::M_VALIGN_MIDDLE:
911 case LyXTabular::VALIGN_MIDDLE:
913 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
916 case LyXTabular::SET_LONGTABULAR:
917 status.setOnOff(tabular.isLongTabular());
920 case LyXTabular::UNSET_LONGTABULAR:
921 status.setOnOff(!tabular.isLongTabular());
924 case LyXTabular::SET_ROTATE_TABULAR:
925 status.setOnOff(tabular.getRotateTabular());
928 case LyXTabular::UNSET_ROTATE_TABULAR:
929 status.setOnOff(!tabular.getRotateTabular());
932 case LyXTabular::SET_ROTATE_CELL:
933 status.setOnOff(tabular.getRotateCell(cur.idx()));
936 case LyXTabular::UNSET_ROTATE_CELL:
937 status.setOnOff(!tabular.getRotateCell(cur.idx()));
940 case LyXTabular::SET_USEBOX:
941 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
944 case LyXTabular::SET_LTFIRSTHEAD:
945 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
948 case LyXTabular::UNSET_LTFIRSTHEAD:
949 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
952 case LyXTabular::SET_LTHEAD:
953 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
956 case LyXTabular::UNSET_LTHEAD:
957 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
960 case LyXTabular::SET_LTFOOT:
961 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
964 case LyXTabular::UNSET_LTFOOT:
965 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
968 case LyXTabular::SET_LTLASTFOOT:
969 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
972 case LyXTabular::UNSET_LTLASTFOOT:
973 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
976 case LyXTabular::SET_LTNEWPAGE:
977 status.setOnOff(tabular.getLTNewPage(sel_row_start));
980 case LyXTabular::SET_BOOKTABS:
981 status.setOnOff(tabular.useBookTabs());
984 case LyXTabular::UNSET_BOOKTABS:
985 status.setOnOff(!tabular.useBookTabs());
990 status.enabled(false);
996 // These are only enabled inside tabular
997 case LFUN_CELL_BACKWARD:
998 case LFUN_CELL_FORWARD:
999 status.enabled(true);
1002 // disable these with multiple cells selected
1003 case LFUN_INSET_INSERT:
1004 case LFUN_TABULAR_INSERT:
1005 case LFUN_CHARSTYLE_INSERT:
1006 case LFUN_FLOAT_INSERT:
1007 case LFUN_FLOAT_WIDE_INSERT:
1008 case LFUN_FOOTNOTE_INSERT:
1009 case LFUN_MARGINALNOTE_INSERT:
1010 case LFUN_MATH_INSERT:
1011 case LFUN_MATH_MODE:
1012 case LFUN_MATH_MUTATE:
1013 case LFUN_MATH_DISPLAY:
1014 case LFUN_NOTE_INSERT:
1015 case LFUN_OPTIONAL_INSERT:
1016 case LFUN_BOX_INSERT:
1017 case LFUN_BRANCH_INSERT:
1018 case LFUN_WRAP_INSERT:
1019 case LFUN_ERT_INSERT: {
1020 if (tablemode(cur)) {
1021 status.enabled(false);
1024 return cell(cur.idx())->getStatus(cur, cmd, status);
1027 // disable in non-fixed-width cells
1028 case LFUN_BREAK_LINE:
1029 case LFUN_BREAK_PARAGRAPH:
1030 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1031 case LFUN_BREAK_PARAGRAPH_SKIP: {
1032 if (tabular.getPWidth(cur.idx()).zero()) {
1033 status.enabled(false);
1036 return cell(cur.idx())->getStatus(cur, cmd, status);
1040 if (tabularStackDirty()) {
1041 status.enabled(true);
1045 case LFUN_INSET_MODIFY:
1046 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1047 status.enabled(true);
1053 // we try to handle this event in the insets dispatch function.
1054 return cell(cur.idx())->getStatus(cur, cmd, status);
1059 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1060 OutputParams const & runparams) const
1062 return tabular.latex(buf, os, runparams);
1066 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1067 OutputParams const & runparams) const
1069 int const dp = runparams.linelen ? runparams.depth : 0;
1070 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1074 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1075 OutputParams const & runparams) const
1078 InsetBase * master = 0;
1080 #ifdef WITH_WARNINGS
1081 #warning Why not pass a proper DocIterator here?
1084 // if the table is inside a float it doesn't need the informaltable
1085 // wrapper. Search for it.
1086 for (master = owner(); master; master = master->owner())
1087 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1092 os << "<informaltable>";
1095 ret += tabular.docbook(buf, os, runparams);
1097 os << "</informaltable>";
1104 void InsetTabular::validate(LaTeXFeatures & features) const
1106 tabular.validate(features);
1110 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1112 return tabular.getCellInset(idx);
1116 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1118 return tabular.getCellInset(idx);
1122 void InsetTabular::cursorPos(BufferView const & bv,
1123 CursorSlice const & sl, bool boundary, int & x, int & y) const
1125 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1127 // y offset correction
1128 int const row = tabular.row_of_cell(sl.idx());
1129 for (int i = 0; i <= row; ++i) {
1131 y += tabular.getAscentOfRow(i);
1132 y += tabular.getAdditionalHeight(i);
1135 y += tabular.getDescentOfRow(i);
1138 // x offset correction
1139 int const col = tabular.column_of_cell(sl.idx());
1140 int idx = tabular.getCellNumber(row, 0);
1141 for (int j = 0; j < col; ++j) {
1142 if (tabular.isPartOfMultiColumn(row, j))
1144 x += tabular.getWidthOfColumn(idx);
1147 x += tabular.getBeginningOfTextInCell(idx);
1148 x += ADD_TO_TABULAR_WIDTH;
1153 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1157 InsetBase const & inset = *tabular.getCellInset(cell);
1158 Point o = bv.coordCache().getInsets().xy(&inset);
1159 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1160 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1161 row_type const row = tabular.row_of_cell(cell);
1162 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1163 tabular.getAdditionalHeight(row);
1164 int const yend = o.y_ + tabular.getDescentOfRow(row);
1176 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1177 // << " ybeg=" << ybeg << " yend=" << yend
1178 // << " xx=" << xx << " yy=" << yy
1179 // << " dist=" << xx + yy << endl;
1184 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1186 //lyxerr << "InsetTabular::editXY: " << this << endl;
1187 cur.selection() = false;
1189 cur.idx() = getNearestCell(cur.bv(), x, y);
1191 return cell(cur.idx())->text_.editXY(cur, x, y);
1195 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1197 cur.idx() = getNearestCell(cur.bv(), x, y);
1198 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1202 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1204 idx_type idx_min = 0;
1205 int dist_min = std::numeric_limits<int>::max();
1206 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1207 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1208 int const d = dist(bv, i, x, y);
1219 int InsetTabular::getCellXPos(idx_type const cell) const
1223 for (; !tabular.isFirstCellInRow(c); --c)
1225 int lx = tabular.getWidthOfColumn(cell);
1226 for (; c < cell; ++c)
1227 lx += tabular.getWidthOfColumn(c);
1229 return lx - tabular.getWidthOfColumn(cell);
1233 void InsetTabular::resetPos(LCursor & cur) const
1235 BufferView & bv = cur.bv();
1236 int const maxwidth = bv.workWidth();
1238 if (&cur.inset() != this) {
1242 int const X2 = maxwidth;
1243 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1244 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1245 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1248 scx_ = X1 + 20 - x1;
1250 scx_ = X2 - 20 - x2;
1257 InsetTabularMailer(*this).updateDialog(&bv);
1261 void InsetTabular::moveNextCell(LCursor & cur)
1263 if (isRightToLeft(cur)) {
1264 if (tabular.isFirstCellInRow(cur.idx())) {
1265 row_type const row = tabular.row_of_cell(cur.idx());
1266 if (row == tabular.rows() - 1)
1268 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1275 if (tabular.isLastCell(cur.idx()))
1285 void InsetTabular::movePrevCell(LCursor & cur)
1287 if (isRightToLeft(cur)) {
1288 if (tabular.isLastCellInRow(cur.idx())) {
1289 row_type const row = tabular.row_of_cell(cur.idx());
1292 cur.idx() = tabular.getFirstCellInRow(row);
1293 cur.idx() = tabular.getCellAbove(cur.idx());
1295 if (tabular.isLastCell(cur.idx()))
1300 if (cur.idx() == 0) // first cell
1304 cur.pit() = cur.lastpit();
1305 cur.pos() = cur.lastpos();
1307 // FIXME: this accesses the position cache before it is initialized
1312 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1314 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1317 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1318 string const tmp = tabularFeature[i].feature;
1320 if (tmp == what.substr(0, tmp.length())) {
1321 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1322 //tabularFeatures[i].feature.length())) {
1323 action = tabularFeature[i].action;
1327 if (action == LyXTabular::LAST_ACTION)
1331 ltrim(what.substr(tabularFeature[i].feature.length()));
1332 tabularFeatures(cur, action, val);
1339 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1340 string const & special, bool & flag)
1342 if (special == "dl_above") {
1345 } else if (special == "dl_below") {
1346 ltt.bottomDL = flag;
1348 } else if (special == "empty") {
1360 void InsetTabular::tabularFeatures(LCursor & cur,
1361 LyXTabular::Feature feature, string const & value)
1363 BufferView & bv = cur.bv();
1364 col_type sel_col_start;
1365 col_type sel_col_end;
1366 row_type sel_row_start;
1367 row_type sel_row_end;
1368 bool setLines = false;
1369 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1370 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1374 case LyXTabular::M_ALIGN_LEFT:
1375 case LyXTabular::ALIGN_LEFT:
1376 setAlign = LYX_ALIGN_LEFT;
1379 case LyXTabular::M_ALIGN_RIGHT:
1380 case LyXTabular::ALIGN_RIGHT:
1381 setAlign = LYX_ALIGN_RIGHT;
1384 case LyXTabular::M_ALIGN_CENTER:
1385 case LyXTabular::ALIGN_CENTER:
1386 setAlign = LYX_ALIGN_CENTER;
1389 case LyXTabular::ALIGN_BLOCK:
1390 setAlign = LYX_ALIGN_BLOCK;
1393 case LyXTabular::M_VALIGN_TOP:
1394 case LyXTabular::VALIGN_TOP:
1395 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1398 case LyXTabular::M_VALIGN_BOTTOM:
1399 case LyXTabular::VALIGN_BOTTOM:
1400 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1403 case LyXTabular::M_VALIGN_MIDDLE:
1404 case LyXTabular::VALIGN_MIDDLE:
1405 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1412 recordUndoInset(cur, Undo::ATOMIC);
1414 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1415 row_type const row = tabular.row_of_cell(cur.idx());
1416 col_type const column = tabular.column_of_cell(cur.idx());
1418 LyXTabular::ltType ltt;
1422 case LyXTabular::SET_PWIDTH: {
1423 LyXLength const len(value);
1424 tabular.setColumnPWidth(cur, cur.idx(), len);
1426 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1427 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1431 case LyXTabular::SET_MPWIDTH:
1432 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1435 case LyXTabular::SET_SPECIAL_COLUMN:
1436 case LyXTabular::SET_SPECIAL_MULTI:
1437 tabular.setAlignSpecial(cur.idx(),value,feature);
1440 case LyXTabular::APPEND_ROW:
1441 // append the row into the tabular
1442 tabular.appendRow(bv.buffer()->params(), cur.idx());
1445 case LyXTabular::APPEND_COLUMN:
1446 // append the column into the tabular
1447 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1448 cur.idx() = tabular.getCellNumber(row, column);
1451 case LyXTabular::DELETE_ROW:
1452 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1453 tabular.deleteRow(sel_row_start);
1454 if (sel_row_start >= tabular.rows())
1456 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1459 cur.selection() = false;
1462 case LyXTabular::DELETE_COLUMN:
1463 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1464 tabular.deleteColumn(sel_col_start);
1465 if (sel_col_start >= tabular.columns())
1467 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1470 cur.selection() = false;
1473 case LyXTabular::COPY_ROW:
1474 tabular.copyRow(bv.buffer()->params(), row);
1477 case LyXTabular::COPY_COLUMN:
1478 tabular.copyColumn(bv.buffer()->params(), column);
1479 cur.idx() = tabular.getCellNumber(row, column);
1482 case LyXTabular::M_TOGGLE_LINE_TOP:
1484 case LyXTabular::TOGGLE_LINE_TOP: {
1485 bool lineSet = !tabular.topLine(cur.idx(), flag);
1486 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1487 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1489 tabular.getCellNumber(i, j),
1494 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1496 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1497 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1498 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1499 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1500 tabular.setBottomLine(
1501 tabular.getCellNumber(i, j),
1507 case LyXTabular::M_TOGGLE_LINE_LEFT:
1509 case LyXTabular::TOGGLE_LINE_LEFT: {
1510 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1511 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1512 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1513 tabular.setLeftLine(
1514 tabular.getCellNumber(i,j),
1520 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1522 case LyXTabular::TOGGLE_LINE_RIGHT: {
1523 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1524 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1525 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1526 tabular.setRightLine(
1527 tabular.getCellNumber(i,j),
1533 case LyXTabular::M_ALIGN_LEFT:
1534 case LyXTabular::M_ALIGN_RIGHT:
1535 case LyXTabular::M_ALIGN_CENTER:
1537 case LyXTabular::ALIGN_LEFT:
1538 case LyXTabular::ALIGN_RIGHT:
1539 case LyXTabular::ALIGN_CENTER:
1540 case LyXTabular::ALIGN_BLOCK:
1541 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1542 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1543 tabular.setAlignment(
1544 tabular.getCellNumber(i, j),
1549 case LyXTabular::M_VALIGN_TOP:
1550 case LyXTabular::M_VALIGN_BOTTOM:
1551 case LyXTabular::M_VALIGN_MIDDLE:
1553 case LyXTabular::VALIGN_TOP:
1554 case LyXTabular::VALIGN_BOTTOM:
1555 case LyXTabular::VALIGN_MIDDLE:
1556 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1557 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1558 tabular.setVAlignment(
1559 tabular.getCellNumber(i, j),
1563 case LyXTabular::MULTICOLUMN: {
1564 if (sel_row_start != sel_row_end) {
1565 #ifdef WITH_WARNINGS
1566 #warning Need I say it ? This is horrible.
1569 Alert::error(_("Error setting multicolumn"),
1570 _("You cannot set multicolumn vertically."));
1573 if (!cur.selection()) {
1574 // just multicol for one single cell
1575 // check whether we are completely in a multicol
1576 if (tabular.isMultiColumn(cur.idx()))
1577 tabular.unsetMultiColumn(cur.idx());
1579 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1582 // we have a selection so this means we just add all this
1583 // cells to form a multicolumn cell
1584 idx_type const s_start = cur.selBegin().idx();
1585 idx_type const s_end = cur.selEnd().idx();
1586 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1587 cur.idx() = s_start;
1590 cur.selection() = false;
1594 case LyXTabular::SET_ALL_LINES:
1596 case LyXTabular::UNSET_ALL_LINES:
1597 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1598 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1599 tabular.setAllLines(
1600 tabular.getCellNumber(i,j), setLines);
1603 case LyXTabular::SET_LONGTABULAR:
1604 tabular.setLongTabular(true);
1607 case LyXTabular::UNSET_LONGTABULAR:
1608 tabular.setLongTabular(false);
1611 case LyXTabular::SET_ROTATE_TABULAR:
1612 tabular.setRotateTabular(true);
1615 case LyXTabular::UNSET_ROTATE_TABULAR:
1616 tabular.setRotateTabular(false);
1619 case LyXTabular::SET_ROTATE_CELL:
1620 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1621 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1622 tabular.setRotateCell(
1623 tabular.getCellNumber(i, j), true);
1626 case LyXTabular::UNSET_ROTATE_CELL:
1627 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1628 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1629 tabular.setRotateCell(
1630 tabular.getCellNumber(i, j), false);
1633 case LyXTabular::SET_USEBOX: {
1634 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1635 if (val == tabular.getUsebox(cur.idx()))
1636 val = LyXTabular::BOX_NONE;
1637 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1638 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1639 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1643 case LyXTabular::UNSET_LTFIRSTHEAD:
1645 case LyXTabular::SET_LTFIRSTHEAD:
1646 tabular.getRowOfLTFirstHead(row, ltt);
1647 checkLongtableSpecial(ltt, value, flag);
1648 tabular.setLTHead(row, flag, ltt, true);
1651 case LyXTabular::UNSET_LTHEAD:
1653 case LyXTabular::SET_LTHEAD:
1654 tabular.getRowOfLTHead(row, ltt);
1655 checkLongtableSpecial(ltt, value, flag);
1656 tabular.setLTHead(row, flag, ltt, false);
1659 case LyXTabular::UNSET_LTFOOT:
1661 case LyXTabular::SET_LTFOOT:
1662 tabular.getRowOfLTFoot(row, ltt);
1663 checkLongtableSpecial(ltt, value, flag);
1664 tabular.setLTFoot(row, flag, ltt, false);
1667 case LyXTabular::UNSET_LTLASTFOOT:
1669 case LyXTabular::SET_LTLASTFOOT:
1670 tabular.getRowOfLTLastFoot(row, ltt);
1671 checkLongtableSpecial(ltt, value, flag);
1672 tabular.setLTFoot(row, flag, ltt, true);
1675 case LyXTabular::SET_LTNEWPAGE:
1676 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1679 case LyXTabular::SET_BOOKTABS:
1680 tabular.setBookTabs(true);
1683 case LyXTabular::UNSET_BOOKTABS:
1684 tabular.setBookTabs(false);
1687 case LyXTabular::SET_TOP_SPACE: {
1689 if (value == "default")
1690 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1691 tabular.row_info[i].top_space_default = true;
1692 else if (isValidLength(value, &len))
1693 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1694 tabular.row_info[i].top_space_default = false;
1695 tabular.row_info[i].top_space = len;
1698 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1699 tabular.row_info[i].top_space_default = false;
1700 tabular.row_info[i].top_space = len;
1705 case LyXTabular::SET_BOTTOM_SPACE: {
1707 if (value == "default")
1708 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1709 tabular.row_info[i].bottom_space_default = true;
1710 else if (isValidLength(value, &len))
1711 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1712 tabular.row_info[i].bottom_space_default = false;
1713 tabular.row_info[i].bottom_space = len;
1716 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1717 tabular.row_info[i].bottom_space_default = false;
1718 tabular.row_info[i].bottom_space = len;
1723 case LyXTabular::SET_INTERLINE_SPACE: {
1725 if (value == "default")
1726 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1727 tabular.row_info[i].interline_space_default = true;
1728 else if (isValidLength(value, &len))
1729 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1730 tabular.row_info[i].interline_space_default = false;
1731 tabular.row_info[i].interline_space = len;
1734 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1735 tabular.row_info[i].interline_space_default = false;
1736 tabular.row_info[i].interline_space = len;
1741 // dummy stuff just to avoid warnings
1742 case LyXTabular::LAST_ACTION:
1746 InsetTabularMailer(*this).updateDialog(&bv);
1750 bool InsetTabular::showInsetDialog(BufferView * bv) const
1752 InsetTabularMailer(*this).showDialog(bv);
1757 void InsetTabular::openLayoutDialog(BufferView * bv) const
1759 InsetTabularMailer(*this).showDialog(bv);
1763 bool InsetTabular::copySelection(LCursor & cur)
1765 if (!cur.selection())
1770 getSelection(cur, rs, re, cs, ce);
1772 paste_tabular.reset(new LyXTabular(tabular));
1774 for (row_type i = 0; i < rs; ++i)
1775 paste_tabular->deleteRow(0);
1777 row_type const rows = re - rs + 1;
1778 while (paste_tabular->rows() > rows)
1779 paste_tabular->deleteRow(rows);
1781 paste_tabular->setTopLine(0, true, true);
1782 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1785 for (col_type i = 0; i < cs; ++i)
1786 paste_tabular->deleteColumn(0);
1788 col_type const columns = ce - cs + 1;
1789 while (paste_tabular->columns() > columns)
1790 paste_tabular->deleteColumn(columns);
1792 paste_tabular->setLeftLine(0, true, true);
1793 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1796 lyx::odocstringstream os;
1797 OutputParams const runparams;
1798 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1799 theClipboard().put(os.str());
1800 // mark tabular stack dirty
1801 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1802 // when we (hopefully) have a one-for-all paste mechanism.
1803 lyx::cap::dirtyTabularStack(true);
1809 bool InsetTabular::pasteSelection(LCursor & cur)
1813 col_type const actcol = tabular.column_of_cell(cur.idx());
1814 row_type const actrow = tabular.row_of_cell(cur.idx());
1815 for (row_type r1 = 0, r2 = actrow;
1816 r1 < paste_tabular->rows() && r2 < tabular.rows();
1818 for (col_type c1 = 0, c2 = actcol;
1819 c1 < paste_tabular->columns() && c2 < tabular.columns();
1821 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1822 tabular.isPartOfMultiColumn(r2, c2))
1824 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1828 if (tabular.isPartOfMultiColumn(r2, c2)) {
1832 shared_ptr<InsetText> inset(
1833 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1834 tabular.setCellInset(r2, c2, inset);
1835 // FIXME: change tracking (MG)
1836 inset->setChange(Change(cur.buffer().params().trackChanges ?
1837 Change::INSERTED : Change::UNCHANGED));
1845 void InsetTabular::cutSelection(LCursor & cur)
1847 if (!cur.selection())
1852 getSelection(cur, rs, re, cs, ce);
1853 for (row_type i = rs; i <= re; ++i) {
1854 for (col_type j = cs; j <= ce; ++j) {
1855 shared_ptr<InsetText> t
1856 = cell(tabular.getCellNumber(i, j));
1857 if (cur.buffer().params().trackChanges)
1858 // FIXME: Change tracking (MG)
1859 t->setChange(Change(Change::DELETED));
1865 // cursor position might be invalid now
1866 if (cur.pit() > cur.lastpit())
1867 cur.pit() = cur.lastpit();
1868 if (cur.pos() > cur.lastpos())
1869 cur.pos() = cur.lastpos();
1870 cur.clearSelection();
1874 bool InsetTabular::isRightToLeft(LCursor & cur) const
1876 BOOST_ASSERT(cur.depth() > 1);
1877 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1878 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1879 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1880 parentpos).language()->rightToLeft();
1884 void InsetTabular::getSelection(LCursor & cur,
1885 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1887 CursorSlice const & beg = cur.selBegin();
1888 CursorSlice const & end = cur.selEnd();
1889 cs = tabular.column_of_cell(beg.idx());
1890 ce = tabular.column_of_cell(end.idx());
1893 cs = tabular.column_of_cell(end.idx());
1895 ce = tabular.right_column_of_cell(end.idx());
1898 rs = tabular.row_of_cell(beg.idx());
1899 re = tabular.row_of_cell(end.idx());
1905 LyXText * InsetTabular::getText(int idx) const
1907 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1911 void InsetTabular::setChange(Change const & change)
1913 for (idx_type idx = 0; idx < nargs(); ++idx)
1914 cell(idx)->setChange(change);
1918 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1920 return tabular.getPWidth(cell).zero();
1924 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1927 if (buf.length() <= 0)
1932 col_type maxCols = 1;
1933 docstring::size_type const len = buf.length();
1934 docstring::size_type p = 0;
1937 (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos) {
1945 maxCols = max(cols, maxCols);
1951 maxCols = max(cols, maxCols);
1952 LyXTabular * loctab;
1957 paste_tabular.reset(
1958 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1959 loctab = paste_tabular.get();
1961 dirtyTabularStack(true);
1964 cell = bv.cursor().idx();
1965 ocol = tabular.column_of_cell(cell);
1966 row = tabular.row_of_cell(cell);
1969 docstring::size_type op = 0;
1970 idx_type const cells = loctab->getNumberOfCells();
1973 rows = loctab->rows();
1974 col_type const columns = loctab->columns();
1976 while (cell < cells && p < len && row < rows &&
1977 (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos)
1983 // we can only set this if we are not too far right
1984 if (cols < columns) {
1985 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1986 inset->setViewCache(&bv);
1987 Paragraph & par = inset->text_.getPar(0);
1988 LyXFont const font = inset->text_.getFont(par, 0);
1989 inset->setText(buf.substr(op, p - op), font,
1990 bv.buffer()->params().trackChanges);
1996 // we can only set this if we are not too far right
1997 if (cols < columns) {
1998 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1999 inset->setViewCache(&bv);
2000 Paragraph & par = inset->text_.getPar(0);
2001 LyXFont const font = inset->text_.getFont(par, 0);
2002 inset->setText(buf.substr(op, p - op), font,
2003 bv.buffer()->params().trackChanges);
2008 cell = loctab->getCellNumber(row, cols);
2014 // check for the last cell if there is no trailing '\n'
2015 if (cell < cells && op < len) {
2016 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2017 inset->setViewCache(&bv);
2018 Paragraph & par = inset->text_.getPar(0);
2019 LyXFont const font = inset->text_.getFont(par, 0);
2020 inset->setText(buf.substr(op, len - op), font,
2021 bv.buffer()->params().trackChanges);
2027 void InsetTabular::addPreview(PreviewLoader & loader) const
2029 row_type const rows = tabular.rows();
2030 col_type const columns = tabular.columns();
2031 for (row_type i = 0; i < rows; ++i) {
2032 for (col_type j = 0; j < columns; ++j)
2033 tabular.getCellInset(i, j)->addPreview(loader);
2038 bool InsetTabular::tablemode(LCursor & cur) const
2040 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2047 string const InsetTabularMailer::name_("tabular");
2049 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2050 : inset_(const_cast<InsetTabular &>(inset))
2054 string const InsetTabularMailer::inset2string(Buffer const &) const
2056 return params2string(inset_);
2060 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2062 istringstream data(in);
2064 lex.setStream(data);
2071 if (!lex || token != name_)
2072 return print_mailer_error("InsetTabularMailer", in, 1,
2075 // This is part of the inset proper that is usually swallowed
2076 // by Buffer::readInset
2078 if (!lex || token != "Tabular")
2079 return print_mailer_error("InsetTabularMailer", in, 2,
2082 Buffer const & buffer = inset.buffer();
2083 inset.read(buffer, lex);
2087 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2090 data << name_ << ' ';
2091 inset.write(inset.buffer(), data);
2092 data << "\\end_inset\n";