3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/Gui.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/Clipboard.h"
44 #include "frontends/Painter.h"
45 #include "frontends/Selection.h"
46 #include "frontends/nullpainter.h"
54 using lyx::cap::dirtyTabularStack;
55 using lyx::cap::tabularStackDirty;
57 using lyx::graphics::PreviewLoader;
59 using lyx::support::ltrim;
61 using lyx::frontend::Painter;
62 using lyx::frontend::Gui;
63 using lyx::frontend::Clipboard;
65 using boost::shared_ptr;
71 using std::istringstream;
73 using std::ostringstream;
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::TOGGLE_LINE_TOP, "toggle-line-top" },
101 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
102 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
103 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
104 { LyXTabular::ALIGN_LEFT, "align-left" },
105 { LyXTabular::ALIGN_RIGHT, "align-right" },
106 { LyXTabular::ALIGN_CENTER, "align-center" },
107 { LyXTabular::ALIGN_BLOCK, "align-block" },
108 { LyXTabular::VALIGN_TOP, "valign-top" },
109 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
110 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
111 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
112 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
113 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
114 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
115 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
116 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
117 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
118 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
119 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
120 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
121 { LyXTabular::MULTICOLUMN, "multicolumn" },
122 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
123 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
124 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
125 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
126 { LyXTabular::SET_PWIDTH, "set-pwidth" },
127 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
128 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
129 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
130 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
131 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
132 { LyXTabular::SET_USEBOX, "set-usebox" },
133 { LyXTabular::SET_LTHEAD, "set-lthead" },
134 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
135 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
136 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
137 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
138 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
139 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
140 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
141 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
142 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
143 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
144 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
145 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
146 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
147 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
148 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
149 { LyXTabular::LAST_ACTION, "" }
153 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
155 FeatureEqual(LyXTabular::Feature feature)
156 : feature_(feature) {}
157 bool operator()(TabularFeature const & tf) const {
158 return tf.action == feature_;
161 LyXTabular::Feature feature_;
167 string const featureAsString(LyXTabular::Feature feature)
169 TabularFeature * end = tabularFeature +
170 sizeof(tabularFeature) / sizeof(TabularFeature);
171 TabularFeature * it = std::find_if(tabularFeature, end,
172 FeatureEqual(feature));
173 return (it == end) ? string() : it->feature;
177 bool InsetTabular::hasPasteBuffer() const
179 return (paste_tabular.get() != 0);
183 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
185 : tabular(buf.params(), max(rows, row_type(1)),
186 max(columns, col_type(1))), buffer_(&buf), scx_(0)
190 InsetTabular::InsetTabular(InsetTabular const & tab)
191 : InsetOld(tab), tabular(tab.tabular),
192 buffer_(tab.buffer_), scx_(0)
196 InsetTabular::~InsetTabular()
198 InsetTabularMailer(*this).hideDialog();
202 auto_ptr<InsetBase> InsetTabular::doClone() const
204 return auto_ptr<InsetBase>(new InsetTabular(*this));
208 Buffer const & InsetTabular::buffer() const
214 void InsetTabular::buffer(Buffer const * b)
220 void InsetTabular::write(Buffer const & buf, ostream & os) const
222 os << "Tabular" << endl;
223 tabular.write(buf, os);
227 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
229 bool const old_format = (lex.getString() == "\\LyXTable");
231 tabular.read(buf, lex);
237 string token = lex.getString();
238 while (lex.isOK() && (token != "\\end_inset")) {
240 token = lex.getString();
242 if (token != "\\end_inset") {
243 lex.printError("Missing \\end_inset at this point. "
249 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
251 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
252 // mi.base.textwidth << "\n";
254 lyxerr << "InsetTabular::metrics: need bv" << endl;
259 for (idx_type cell = 0; i < tabular.rows(); ++i) {
262 for (col_type j = 0; j < tabular.columns(); ++j) {
263 if (tabular.isPartOfMultiColumn(i, j))
264 // Multicolumn cell, but not first one
269 if (tabular.cell_info[i][j].multicolumn ==
270 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
271 p_width = tabular.cellinfo_of_cell(cell).p_width;
273 p_width = tabular.column_info[j].p_width;
275 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
276 tabular.getCellInset(cell)->metrics(m, dim);
278 dim.wid = m.base.textwidth;
279 tabular.setWidthOfCell(cell, dim.wid);
280 if (p_width.zero()) {
281 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
282 // FIXME there must be a way to get rid of
283 // the second metrics call
284 tabular.getCellInset(cell)->metrics(m, dim);
286 maxAsc = max(maxAsc, dim.asc);
287 maxDesc = max(maxDesc, dim.des);
290 int const top_space = tabular.row_info[i].top_space_default ?
292 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
293 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
294 int const bottom_space = tabular.row_info[i].bottom_space_default ?
296 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
297 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
300 dim.asc = tabular.getAscentOfRow(0);
301 dim.des = tabular.getHeightOfTabular() - dim.asc;
302 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
307 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
309 setPosCache(pi, x, y);
311 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
312 BufferView * bv = pi.base.bv;
314 static lyx::frontend::NullPainter nop;
315 static PainterInfo nullpi(bv, nop);
317 //resetPos(bv->cursor());
320 x += ADD_TO_TABULAR_WIDTH;
323 first_visible_cell = LyXTabular::npos;
324 for (row_type i = 0; i < tabular.rows(); ++i) {
326 int const a = tabular.getAscentOfRow(i);
327 int const d = tabular.getDescentOfRow(i);
328 idx = tabular.getCellNumber(i, 0);
329 for (col_type j = 0; j < tabular.columns(); ++j) {
330 if (tabular.isPartOfMultiColumn(i, j))
332 if (first_visible_cell == LyXTabular::npos)
333 first_visible_cell = idx;
335 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
336 if (nx + tabular.getWidthOfColumn(idx) < 0
337 || nx > bv->workWidth()
339 || y - a > bv->workHeight()) {
340 cell(idx)->draw(nullpi, cx, y);
341 drawCellLines(nop, nx, y, i, idx, pi.erased_);
343 cell(idx)->draw(pi, cx, y);
344 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
346 nx += tabular.getWidthOfColumn(idx);
350 if (i + 1 < tabular.rows())
351 y += d + tabular.getAscentOfRow(i + 1) +
352 tabular.getAdditionalHeight(i + 1);
357 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
359 setPosCache(pi, x, y);
361 LCursor & cur = pi.base.bv->cursor();
363 x += scx_ + ADD_TO_TABULAR_WIDTH;
365 // Paint background of current tabular
366 int const w = tabular.getWidthOfTabular();
367 int const h = tabular.getHeightOfTabular();
368 int yy = y - tabular.getAscentOfRow(0);
369 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
371 if (!cur.selection())
373 if (!ptr_cmp(&cur.inset(), this))
379 if (tablemode(cur)) {
382 getSelection(cur, rs, re, cs, ce);
383 y -= tabular.getAscentOfRow(0);
384 for (row_type j = 0; j < tabular.rows(); ++j) {
385 int const a = tabular.getAscentOfRow(j);
386 int const h = a + tabular.getDescentOfRow(j);
388 y += tabular.getAdditionalHeight(j);
389 for (col_type i = 0; i < tabular.columns(); ++i) {
390 if (tabular.isPartOfMultiColumn(j, i))
392 idx_type const cell =
393 tabular.getCellNumber(j, i);
394 int const w = tabular.getWidthOfColumn(cell);
395 if (i >= cs && i <= ce && j >= rs && j <= re)
396 pi.pain.fillRectangle(xx, y, w, h,
405 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
410 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
411 row_type row, idx_type cell, bool erased) const
413 int x2 = x + tabular.getWidthOfColumn(cell);
415 LColor::color col = LColor::tabularline;
416 LColor::color onoffcol = LColor::tabularonoffline;
419 col = LColor::strikeout;
420 onoffcol = LColor::strikeout;
423 if (!tabular.topAlreadyDrawn(cell)) {
424 on_off = !tabular.topLine(cell);
425 pain.line(x, y - tabular.getAscentOfRow(row),
426 x2, y - tabular.getAscentOfRow(row),
427 on_off ? onoffcol : col,
428 on_off ? Painter::line_onoffdash : Painter::line_solid);
430 on_off = !tabular.bottomLine(cell);
431 pain.line(x, y + tabular.getDescentOfRow(row),
432 x2, y + tabular.getDescentOfRow(row),
433 on_off ? onoffcol : col,
434 on_off ? Painter::line_onoffdash : Painter::line_solid);
435 if (!tabular.leftAlreadyDrawn(cell)) {
436 on_off = !tabular.leftLine(cell);
437 pain.line(x, y - tabular.getAscentOfRow(row),
438 x, y + tabular.getDescentOfRow(row),
439 on_off ? onoffcol : col,
440 on_off ? Painter::line_onoffdash : Painter::line_solid);
442 on_off = !tabular.rightLine(cell);
443 pain.line(x2 - tabular.getAdditionalWidth(cell),
444 y - tabular.getAscentOfRow(row),
445 x2 - tabular.getAdditionalWidth(cell),
446 y + tabular.getDescentOfRow(row),
447 on_off ? onoffcol : col,
448 on_off ? Painter::line_onoffdash : Painter::line_solid);
452 docstring const InsetTabular::editMessage() const
454 return _("Opened table");
458 void InsetTabular::edit(LCursor & cur, bool left)
460 //lyxerr << "InsetTabular::edit: " << this << endl;
462 cur.selection() = false;
465 if (isRightToLeft(cur))
466 cur.idx() = tabular.getLastCellInRow(0);
472 if (isRightToLeft(cur))
473 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
475 cur.idx() = tabular.getNumberOfCells() - 1;
477 cur.pos() = cur.lastpos(); // FIXME crude guess
479 // this accesses the position cache before it is initialized
481 //cur.bv().fitCursor();
485 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
487 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
488 << "\n cur:" << cur << endl;
489 CursorSlice sl = cur.top();
490 LCursor & bvcur = cur.bv().cursor();
492 switch (cmd.action) {
494 case LFUN_MOUSE_PRESS:
495 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
497 if (cmd.button() == mouse_button::button1
498 || cmd.button() == mouse_button::button3) {
499 cur.selection() = false;
500 setCursorFromCoordinates(cur, cmd.x, cmd.y);
506 if (cmd.button() == mouse_button::button2) {
507 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
508 doDispatch(cur, cmd);
512 case LFUN_MOUSE_MOTION:
513 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
514 if (cmd.button() == mouse_button::button1) {
515 // only accept motions to places not deeper nested than the real anchor
516 if (bvcur.anchor_.hasPart(cur)) {
517 setCursorFromCoordinates(cur, cmd.x, cmd.y);
518 bvcur.setCursor(cur);
519 bvcur.selection() = true;
525 case LFUN_MOUSE_RELEASE:
526 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
527 if (cmd.button() == mouse_button::button3)
528 InsetTabularMailer(*this).showDialog(&cur.bv());
531 case LFUN_CELL_BACKWARD:
533 cur.selection() = false;
536 case LFUN_CELL_FORWARD:
538 cur.selection() = false;
541 case LFUN_CHAR_FORWARD_SELECT:
542 case LFUN_CHAR_FORWARD:
543 cell(cur.idx())->dispatch(cur, cmd);
544 if (!cur.result().dispatched()) {
545 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
547 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
553 case LFUN_CHAR_BACKWARD_SELECT:
554 case LFUN_CHAR_BACKWARD:
555 cell(cur.idx())->dispatch(cur, cmd);
556 if (!cur.result().dispatched()) {
557 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
559 cmd = FuncRequest(LFUN_FINISHED_LEFT);
565 case LFUN_DOWN_SELECT:
567 cell(cur.idx())->dispatch(cur, cmd);
568 cur.dispatched(); // override the cell's decision
570 // if our LyXText didn't do anything to the cursor
571 // then we try to put the cursor into the cell below
572 // setting also the right targetX.
573 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
574 cur.idx() = tabular.getCellBelow(cur.idx());
576 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
577 cur.pit(), 0, cur.targetX());
579 if (sl == cur.top()) {
580 // we trick it to go to the RIGHT after leaving the
582 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
589 cell(cur.idx())->dispatch(cur, cmd);
590 cur.dispatched(); // override the cell's decision
592 // if our LyXText didn't do anything to the cursor
593 // then we try to put the cursor into the cell above
594 // setting also the right targetX.
595 if (tabular.row_of_cell(cur.idx()) != 0) {
596 cur.idx() = tabular.getCellAbove(cur.idx());
597 cur.pit() = cur.lastpit();
598 LyXText const * text = cell(cur.idx())->getText(0);
599 cur.pos() = text->x2pos(
601 text->paragraphs().back().rows().size()-1,
604 if (sl == cur.top()) {
605 cmd = FuncRequest(LFUN_FINISHED_UP);
610 // case LFUN_SCREEN_DOWN: {
611 // //if (hasSelection())
612 // // cur.selection() = false;
613 // col_type const col = tabular.column_of_cell(cur.idx());
614 // int const t = cur.bv().top_y() + cur.bv().height();
615 // if (t < yo() + tabular.getHeightOfTabular()) {
616 // cur.bv().scrollDocView(t);
617 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
619 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
626 // case LFUN_SCREEN_UP: {
627 // //if (hasSelection())
628 // // cur.selection() = false;
629 // col_type const col = tabular.column_of_cell(cur.idx());
630 // int const t = cur.bv().top_y() + cur.bv().height();
632 // cur.bv().scrollDocView(t);
636 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
640 // cur.par() = cur.lastpar();
641 // cur.pos() = cur.lastpos();
645 case LFUN_LAYOUT_TABULAR:
646 InsetTabularMailer(*this).showDialog(&cur.bv());
649 case LFUN_INSET_DIALOG_UPDATE:
650 InsetTabularMailer(*this).updateDialog(&cur.bv());
653 case LFUN_TABULAR_FEATURE:
654 if (!tabularFeatures(cur, lyx::to_utf8(cmd.argument())))
658 // insert file functions
659 case LFUN_FILE_INSERT_ASCII_PARA:
660 case LFUN_FILE_INSERT_ASCII: {
661 // FIXME: We don't know the encoding of filenames
662 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), lyx::to_utf8(cmd.argument()), false);
663 // FIXME: We don't know the encoding of the file
664 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), lyx::from_utf8(tmpstr), false))
670 if (tablemode(cur)) {
671 if (copySelection(cur)) {
672 recordUndoInset(cur, Undo::DELETE);
677 cell(cur.idx())->dispatch(cur, cmd);
680 case LFUN_CHAR_DELETE_BACKWARD:
681 case LFUN_CHAR_DELETE_FORWARD:
682 if (tablemode(cur)) {
683 recordUndoInset(cur, Undo::DELETE);
687 cell(cur.idx())->dispatch(cur, cmd);
691 if (!cur.selection())
693 if (tablemode(cur)) {
697 cell(cur.idx())->dispatch(cur, cmd);
700 case LFUN_CLIPBOARD_PASTE:
701 case LFUN_PRIMARY_SELECTION_PASTE: {
702 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
703 cur.bv().owner()->gui().clipboard().get() :
704 cur.bv().owner()->gui().selection().get();
707 // pass to InsertAsciiString, but
708 // only if we have multi-cell content
709 if (clip.find_first_of(lyx::from_ascii("\t\n")) != docstring::npos) {
710 if (insertAsciiString(cur.bv(), clip, false)) {
711 // content has been replaced,
712 // so cursor might be invalid
713 cur.pos() = cur.lastpos();
714 bvcur.setCursor(cur);
718 // so that the clipboard is used and it goes on
720 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
721 paste_tabular.reset();
722 dirtyTabularStack(false);
728 if (hasPasteBuffer() && tabularStackDirty()) {
729 recordUndoInset(cur, Undo::INSERT);
733 cell(cur.idx())->dispatch(cur, cmd);
734 // Reset pasted paragraphs:
735 if (tabular.getPWidth(cur.idx()).zero())
736 cell(cur.idx())->forceParagraphsToDefault(cur);
741 case LFUN_FONT_ROMAN:
747 case LFUN_FONT_FREE_APPLY:
748 case LFUN_FONT_FREE_UPDATE:
750 case LFUN_FONT_UNDERLINE:
752 case LFUN_WORD_CAPITALIZE:
753 case LFUN_WORD_UPCASE:
754 case LFUN_WORD_LOWCASE:
755 case LFUN_CHARS_TRANSPOSE:
756 if (tablemode(cur)) {
759 getSelection(cur, rs, re, cs, ce);
760 for (row_type i = rs; i <= re; ++i) {
761 for (col_type j = cs; j <= ce; ++j) {
762 // cursor follows cell:
763 cur.idx() = tabular.getCellNumber(i, j);
764 // select this cell only:
767 cur.pos() = cur.top().lastpos();
770 cell(cur.idx())->dispatch(cur, cmd);
773 // Restore original selection
774 cur.idx() = tabular.getCellNumber(rs, cs);
777 cur.idx() = tabular.getCellNumber(re, ce);
778 cur.pos() = cur.top().lastpos();
783 cell(cur.idx())->dispatch(cur, cmd);
787 // we try to handle this event in the insets dispatch function.
788 cell(cur.idx())->dispatch(cur, cmd);
793 InsetTabularMailer(*this).updateDialog(&cur.bv());
797 // function sets an object as defined in func_status.h:
798 // states OK, Unknown, Disabled, On, Off.
799 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
800 FuncStatus & status) const
802 switch (cmd.action) {
803 case LFUN_TABULAR_FEATURE: {
804 int action = LyXTabular::LAST_ACTION;
806 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
807 string const tmp = tabularFeature[i].feature;
808 if (tmp == lyx::to_utf8(cmd.argument()).substr(0, tmp.length())) {
809 action = tabularFeature[i].action;
813 if (action == LyXTabular::LAST_ACTION) {
815 status.unknown(true);
819 string const argument
820 = ltrim(lyx::to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
822 row_type sel_row_start = 0;
823 row_type sel_row_end = 0;
825 LyXTabular::ltType dummyltt;
828 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
831 case LyXTabular::SET_PWIDTH:
832 case LyXTabular::SET_MPWIDTH:
833 case LyXTabular::SET_SPECIAL_COLUMN:
834 case LyXTabular::SET_SPECIAL_MULTI:
835 case LyXTabular::APPEND_ROW:
836 case LyXTabular::APPEND_COLUMN:
837 case LyXTabular::DELETE_ROW:
838 case LyXTabular::DELETE_COLUMN:
839 case LyXTabular::SET_ALL_LINES:
840 case LyXTabular::UNSET_ALL_LINES:
841 case LyXTabular::SET_TOP_SPACE:
842 case LyXTabular::SET_BOTTOM_SPACE:
843 case LyXTabular::SET_INTERLINE_SPACE:
847 case LyXTabular::MULTICOLUMN:
848 status.setOnOff(tabular.isMultiColumn(cur.idx()));
851 case LyXTabular::M_TOGGLE_LINE_TOP:
853 case LyXTabular::TOGGLE_LINE_TOP:
854 status.setOnOff(tabular.topLine(cur.idx(), flag));
857 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
859 case LyXTabular::TOGGLE_LINE_BOTTOM:
860 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
863 case LyXTabular::M_TOGGLE_LINE_LEFT:
865 case LyXTabular::TOGGLE_LINE_LEFT:
866 status.setOnOff(tabular.leftLine(cur.idx(), flag));
869 case LyXTabular::M_TOGGLE_LINE_RIGHT:
871 case LyXTabular::TOGGLE_LINE_RIGHT:
872 status.setOnOff(tabular.rightLine(cur.idx(), flag));
875 case LyXTabular::M_ALIGN_LEFT:
877 case LyXTabular::ALIGN_LEFT:
878 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
881 case LyXTabular::M_ALIGN_RIGHT:
883 case LyXTabular::ALIGN_RIGHT:
884 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
887 case LyXTabular::M_ALIGN_CENTER:
889 case LyXTabular::ALIGN_CENTER:
890 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
893 case LyXTabular::ALIGN_BLOCK:
894 status.enabled(!tabular.getPWidth(cur.idx()).zero());
895 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
898 case LyXTabular::M_VALIGN_TOP:
900 case LyXTabular::VALIGN_TOP:
902 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
905 case LyXTabular::M_VALIGN_BOTTOM:
907 case LyXTabular::VALIGN_BOTTOM:
909 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
912 case LyXTabular::M_VALIGN_MIDDLE:
914 case LyXTabular::VALIGN_MIDDLE:
916 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
919 case LyXTabular::SET_LONGTABULAR:
920 status.setOnOff(tabular.isLongTabular());
923 case LyXTabular::UNSET_LONGTABULAR:
924 status.setOnOff(!tabular.isLongTabular());
927 case LyXTabular::SET_ROTATE_TABULAR:
928 status.setOnOff(tabular.getRotateTabular());
931 case LyXTabular::UNSET_ROTATE_TABULAR:
932 status.setOnOff(!tabular.getRotateTabular());
935 case LyXTabular::SET_ROTATE_CELL:
936 status.setOnOff(tabular.getRotateCell(cur.idx()));
939 case LyXTabular::UNSET_ROTATE_CELL:
940 status.setOnOff(!tabular.getRotateCell(cur.idx()));
943 case LyXTabular::SET_USEBOX:
944 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
947 case LyXTabular::SET_LTFIRSTHEAD:
948 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::UNSET_LTFIRSTHEAD:
952 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
955 case LyXTabular::SET_LTHEAD:
956 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
959 case LyXTabular::UNSET_LTHEAD:
960 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
963 case LyXTabular::SET_LTFOOT:
964 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
967 case LyXTabular::UNSET_LTFOOT:
968 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
971 case LyXTabular::SET_LTLASTFOOT:
972 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
975 case LyXTabular::UNSET_LTLASTFOOT:
976 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
979 case LyXTabular::SET_LTNEWPAGE:
980 status.setOnOff(tabular.getLTNewPage(sel_row_start));
983 case LyXTabular::SET_BOOKTABS:
984 status.setOnOff(tabular.useBookTabs());
987 case LyXTabular::UNSET_BOOKTABS:
988 status.setOnOff(!tabular.useBookTabs());
993 status.enabled(false);
999 // These are only enabled inside tabular
1000 case LFUN_CELL_BACKWARD:
1001 case LFUN_CELL_FORWARD:
1002 status.enabled(true);
1005 // disable these with multiple cells selected
1006 case LFUN_INSET_INSERT:
1007 case LFUN_TABULAR_INSERT:
1008 case LFUN_CHARSTYLE_INSERT:
1009 case LFUN_FLOAT_INSERT:
1010 case LFUN_FLOAT_WIDE_INSERT:
1011 case LFUN_FOOTNOTE_INSERT:
1012 case LFUN_MARGINALNOTE_INSERT:
1013 case LFUN_MATH_INSERT:
1014 case LFUN_MATH_MODE:
1015 case LFUN_MATH_MUTATE:
1016 case LFUN_MATH_DISPLAY:
1017 case LFUN_NOTE_INSERT:
1018 case LFUN_OPTIONAL_INSERT:
1019 case LFUN_BOX_INSERT:
1020 case LFUN_BRANCH_INSERT:
1021 case LFUN_WRAP_INSERT:
1022 case LFUN_ERT_INSERT: {
1023 if (tablemode(cur)) {
1024 status.enabled(false);
1027 return cell(cur.idx())->getStatus(cur, cmd, status);
1030 // disable in non-fixed-width cells
1031 case LFUN_BREAK_LINE:
1032 case LFUN_BREAK_PARAGRAPH:
1033 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1034 case LFUN_BREAK_PARAGRAPH_SKIP: {
1035 if (tabular.getPWidth(cur.idx()).zero()) {
1036 status.enabled(false);
1039 return cell(cur.idx())->getStatus(cur, cmd, status);
1043 if (tabularStackDirty()) {
1044 status.enabled(true);
1048 case LFUN_INSET_MODIFY:
1049 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1050 status.enabled(true);
1056 // we try to handle this event in the insets dispatch function.
1057 return cell(cur.idx())->getStatus(cur, cmd, status);
1062 int InsetTabular::latex(Buffer const & buf, ostream & os,
1063 OutputParams const & runparams) const
1065 return tabular.latex(buf, os, runparams);
1069 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1070 OutputParams const & runparams) const
1072 int const dp = runparams.linelen ? runparams.depth : 0;
1073 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1077 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1078 OutputParams const & runparams) const
1081 InsetBase * master = 0;
1083 #ifdef WITH_WARNINGS
1084 #warning Why not pass a proper DocIterator here?
1087 // if the table is inside a float it doesn't need the informaltable
1088 // wrapper. Search for it.
1089 for (master = owner(); master; master = master->owner())
1090 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1095 os << "<informaltable>";
1098 ret += tabular.docbook(buf, os, runparams);
1100 os << "</informaltable>";
1107 void InsetTabular::validate(LaTeXFeatures & features) const
1109 tabular.validate(features);
1113 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1115 return tabular.getCellInset(idx);
1119 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1121 return tabular.getCellInset(idx);
1125 void InsetTabular::cursorPos
1126 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1128 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1130 // y offset correction
1131 int const row = tabular.row_of_cell(sl.idx());
1132 for (int i = 0; i <= row; ++i) {
1134 y += tabular.getAscentOfRow(i);
1135 y += tabular.getAdditionalHeight(i);
1138 y += tabular.getDescentOfRow(i);
1141 // x offset correction
1142 int const col = tabular.column_of_cell(sl.idx());
1143 int idx = tabular.getCellNumber(row, 0);
1144 for (int j = 0; j < col; ++j) {
1145 if (tabular.isPartOfMultiColumn(row, j))
1147 x += tabular.getWidthOfColumn(idx);
1150 x += tabular.getBeginningOfTextInCell(idx);
1151 x += ADD_TO_TABULAR_WIDTH;
1156 int InsetTabular::dist(idx_type const cell, int x, int y) const
1160 InsetBase const & inset = *tabular.getCellInset(cell);
1161 Point o = theCoords.getInsets().xy(&inset);
1162 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1163 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1164 row_type const row = tabular.row_of_cell(cell);
1165 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1166 tabular.getAdditionalHeight(row);
1167 int const yend = o.y_ + tabular.getDescentOfRow(row);
1179 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1180 // << " ybeg=" << ybeg << " yend=" << yend
1181 // << " xx=" << xx << " yy=" << yy
1182 // << " dist=" << xx + yy << endl;
1187 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1189 //lyxerr << "InsetTabular::editXY: " << this << endl;
1190 cur.selection() = false;
1192 cur.idx() = getNearestCell(x, y);
1194 return cell(cur.idx())->text_.editXY(cur, x, y);
1198 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1200 cur.idx() = getNearestCell(x, y);
1201 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1205 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1207 idx_type idx_min = 0;
1208 int dist_min = std::numeric_limits<int>::max();
1209 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1210 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1211 int const d = dist(i, x, y);
1222 int InsetTabular::getCellXPos(idx_type const cell) const
1226 for (; !tabular.isFirstCellInRow(c); --c)
1228 int lx = tabular.getWidthOfColumn(cell);
1229 for (; c < cell; ++c)
1230 lx += tabular.getWidthOfColumn(c);
1232 return lx - tabular.getWidthOfColumn(cell);
1236 void InsetTabular::resetPos(LCursor & cur) const
1238 BufferView & bv = cur.bv();
1239 int const maxwidth = bv.workWidth();
1241 if (&cur.inset() != this) {
1245 int const X2 = maxwidth;
1246 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1247 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1248 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1251 scx_ = X1 + 20 - x1;
1253 scx_ = X2 - 20 - x2;
1260 InsetTabularMailer(*this).updateDialog(&bv);
1264 void InsetTabular::moveNextCell(LCursor & cur)
1266 if (isRightToLeft(cur)) {
1267 if (tabular.isFirstCellInRow(cur.idx())) {
1268 row_type const row = tabular.row_of_cell(cur.idx());
1269 if (row == tabular.rows() - 1)
1271 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1278 if (tabular.isLastCell(cur.idx()))
1288 void InsetTabular::movePrevCell(LCursor & cur)
1290 if (isRightToLeft(cur)) {
1291 if (tabular.isLastCellInRow(cur.idx())) {
1292 row_type const row = tabular.row_of_cell(cur.idx());
1295 cur.idx() = tabular.getFirstCellInRow(row);
1296 cur.idx() = tabular.getCellAbove(cur.idx());
1298 if (tabular.isLastCell(cur.idx()))
1303 if (cur.idx() == 0) // first cell
1307 cur.pit() = cur.lastpit();
1308 cur.pos() = cur.lastpos();
1313 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1315 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1318 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1319 string const tmp = tabularFeature[i].feature;
1321 if (tmp == what.substr(0, tmp.length())) {
1322 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1323 //tabularFeatures[i].feature.length())) {
1324 action = tabularFeature[i].action;
1328 if (action == LyXTabular::LAST_ACTION)
1332 ltrim(what.substr(tabularFeature[i].feature.length()));
1333 tabularFeatures(cur, action, val);
1340 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1341 string const & special, bool & flag)
1343 if (special == "dl_above") {
1346 } else if (special == "dl_below") {
1347 ltt.bottomDL = flag;
1349 } else if (special == "empty") {
1361 void InsetTabular::tabularFeatures(LCursor & cur,
1362 LyXTabular::Feature feature, string const & value)
1364 BufferView & bv = cur.bv();
1365 col_type sel_col_start;
1366 col_type sel_col_end;
1367 row_type sel_row_start;
1368 row_type sel_row_end;
1369 bool setLines = false;
1370 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1371 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1375 case LyXTabular::M_ALIGN_LEFT:
1376 case LyXTabular::ALIGN_LEFT:
1377 setAlign = LYX_ALIGN_LEFT;
1380 case LyXTabular::M_ALIGN_RIGHT:
1381 case LyXTabular::ALIGN_RIGHT:
1382 setAlign = LYX_ALIGN_RIGHT;
1385 case LyXTabular::M_ALIGN_CENTER:
1386 case LyXTabular::ALIGN_CENTER:
1387 setAlign = LYX_ALIGN_CENTER;
1390 case LyXTabular::ALIGN_BLOCK:
1391 setAlign = LYX_ALIGN_BLOCK;
1394 case LyXTabular::M_VALIGN_TOP:
1395 case LyXTabular::VALIGN_TOP:
1396 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1399 case LyXTabular::M_VALIGN_BOTTOM:
1400 case LyXTabular::VALIGN_BOTTOM:
1401 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1404 case LyXTabular::M_VALIGN_MIDDLE:
1405 case LyXTabular::VALIGN_MIDDLE:
1406 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1413 recordUndoInset(cur, Undo::ATOMIC);
1415 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1416 row_type const row = tabular.row_of_cell(cur.idx());
1417 col_type const column = tabular.column_of_cell(cur.idx());
1419 LyXTabular::ltType ltt;
1423 case LyXTabular::SET_PWIDTH: {
1424 LyXLength const len(value);
1425 tabular.setColumnPWidth(cur, cur.idx(), len);
1427 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1428 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1432 case LyXTabular::SET_MPWIDTH:
1433 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1436 case LyXTabular::SET_SPECIAL_COLUMN:
1437 case LyXTabular::SET_SPECIAL_MULTI:
1438 tabular.setAlignSpecial(cur.idx(),value,feature);
1441 case LyXTabular::APPEND_ROW:
1442 // append the row into the tabular
1443 tabular.appendRow(bv.buffer()->params(), cur.idx());
1446 case LyXTabular::APPEND_COLUMN:
1447 // append the column into the tabular
1448 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1449 cur.idx() = tabular.getCellNumber(row, column);
1452 case LyXTabular::DELETE_ROW:
1453 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1454 tabular.deleteRow(sel_row_start);
1455 if (sel_row_start >= tabular.rows())
1457 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1460 cur.selection() = false;
1463 case LyXTabular::DELETE_COLUMN:
1464 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1465 tabular.deleteColumn(sel_col_start);
1466 if (sel_col_start >= tabular.columns())
1468 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1471 cur.selection() = false;
1474 case LyXTabular::M_TOGGLE_LINE_TOP:
1476 case LyXTabular::TOGGLE_LINE_TOP: {
1477 bool lineSet = !tabular.topLine(cur.idx(), flag);
1478 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1479 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1481 tabular.getCellNumber(i, j),
1486 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1488 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1489 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1490 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1491 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1492 tabular.setBottomLine(
1493 tabular.getCellNumber(i, j),
1499 case LyXTabular::M_TOGGLE_LINE_LEFT:
1501 case LyXTabular::TOGGLE_LINE_LEFT: {
1502 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1503 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1504 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1505 tabular.setLeftLine(
1506 tabular.getCellNumber(i,j),
1512 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1514 case LyXTabular::TOGGLE_LINE_RIGHT: {
1515 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1516 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1517 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1518 tabular.setRightLine(
1519 tabular.getCellNumber(i,j),
1525 case LyXTabular::M_ALIGN_LEFT:
1526 case LyXTabular::M_ALIGN_RIGHT:
1527 case LyXTabular::M_ALIGN_CENTER:
1529 case LyXTabular::ALIGN_LEFT:
1530 case LyXTabular::ALIGN_RIGHT:
1531 case LyXTabular::ALIGN_CENTER:
1532 case LyXTabular::ALIGN_BLOCK:
1533 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1534 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1535 tabular.setAlignment(
1536 tabular.getCellNumber(i, j),
1541 case LyXTabular::M_VALIGN_TOP:
1542 case LyXTabular::M_VALIGN_BOTTOM:
1543 case LyXTabular::M_VALIGN_MIDDLE:
1545 case LyXTabular::VALIGN_TOP:
1546 case LyXTabular::VALIGN_BOTTOM:
1547 case LyXTabular::VALIGN_MIDDLE:
1548 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1549 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1550 tabular.setVAlignment(
1551 tabular.getCellNumber(i, j),
1555 case LyXTabular::MULTICOLUMN: {
1556 if (sel_row_start != sel_row_end) {
1557 #ifdef WITH_WARNINGS
1558 #warning Need I say it ? This is horrible.
1561 Alert::error(lyx::to_utf8(_("Error setting multicolumn")),
1562 lyx::to_utf8(_("You cannot set multicolumn vertically.")));
1565 if (!cur.selection()) {
1566 // just multicol for one single cell
1567 // check whether we are completely in a multicol
1568 if (tabular.isMultiColumn(cur.idx()))
1569 tabular.unsetMultiColumn(cur.idx());
1571 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1574 // we have a selection so this means we just add all this
1575 // cells to form a multicolumn cell
1576 idx_type const s_start = cur.selBegin().idx();
1577 idx_type const s_end = cur.selEnd().idx();
1578 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1579 cur.idx() = s_start;
1582 cur.selection() = false;
1586 case LyXTabular::SET_ALL_LINES:
1588 case LyXTabular::UNSET_ALL_LINES:
1589 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1590 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1591 tabular.setAllLines(
1592 tabular.getCellNumber(i,j), setLines);
1595 case LyXTabular::SET_LONGTABULAR:
1596 tabular.setLongTabular(true);
1599 case LyXTabular::UNSET_LONGTABULAR:
1600 tabular.setLongTabular(false);
1603 case LyXTabular::SET_ROTATE_TABULAR:
1604 tabular.setRotateTabular(true);
1607 case LyXTabular::UNSET_ROTATE_TABULAR:
1608 tabular.setRotateTabular(false);
1611 case LyXTabular::SET_ROTATE_CELL:
1612 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1613 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1614 tabular.setRotateCell(
1615 tabular.getCellNumber(i, j), true);
1618 case LyXTabular::UNSET_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), false);
1625 case LyXTabular::SET_USEBOX: {
1626 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1627 if (val == tabular.getUsebox(cur.idx()))
1628 val = LyXTabular::BOX_NONE;
1629 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1630 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1631 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1635 case LyXTabular::UNSET_LTFIRSTHEAD:
1637 case LyXTabular::SET_LTFIRSTHEAD:
1638 tabular.getRowOfLTFirstHead(row, ltt);
1639 checkLongtableSpecial(ltt, value, flag);
1640 tabular.setLTHead(row, flag, ltt, true);
1643 case LyXTabular::UNSET_LTHEAD:
1645 case LyXTabular::SET_LTHEAD:
1646 tabular.getRowOfLTHead(row, ltt);
1647 checkLongtableSpecial(ltt, value, flag);
1648 tabular.setLTHead(row, flag, ltt, false);
1651 case LyXTabular::UNSET_LTFOOT:
1653 case LyXTabular::SET_LTFOOT:
1654 tabular.getRowOfLTFoot(row, ltt);
1655 checkLongtableSpecial(ltt, value, flag);
1656 tabular.setLTFoot(row, flag, ltt, false);
1659 case LyXTabular::UNSET_LTLASTFOOT:
1661 case LyXTabular::SET_LTLASTFOOT:
1662 tabular.getRowOfLTLastFoot(row, ltt);
1663 checkLongtableSpecial(ltt, value, flag);
1664 tabular.setLTFoot(row, flag, ltt, true);
1667 case LyXTabular::SET_LTNEWPAGE:
1668 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1671 case LyXTabular::SET_BOOKTABS:
1672 tabular.setBookTabs(true);
1675 case LyXTabular::UNSET_BOOKTABS:
1676 tabular.setBookTabs(false);
1679 case LyXTabular::SET_TOP_SPACE: {
1681 if (value == "default")
1682 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1683 tabular.row_info[i].top_space_default = true;
1684 else if (isValidLength(value, &len))
1685 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1686 tabular.row_info[i].top_space_default = false;
1687 tabular.row_info[i].top_space = len;
1690 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1691 tabular.row_info[i].top_space_default = false;
1692 tabular.row_info[i].top_space = len;
1697 case LyXTabular::SET_BOTTOM_SPACE: {
1699 if (value == "default")
1700 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1701 tabular.row_info[i].bottom_space_default = true;
1702 else if (isValidLength(value, &len))
1703 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1704 tabular.row_info[i].bottom_space_default = false;
1705 tabular.row_info[i].bottom_space = len;
1708 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1709 tabular.row_info[i].bottom_space_default = false;
1710 tabular.row_info[i].bottom_space = len;
1715 case LyXTabular::SET_INTERLINE_SPACE: {
1717 if (value == "default")
1718 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1719 tabular.row_info[i].interline_space_default = true;
1720 else if (isValidLength(value, &len))
1721 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1722 tabular.row_info[i].interline_space_default = false;
1723 tabular.row_info[i].interline_space = len;
1726 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1727 tabular.row_info[i].interline_space_default = false;
1728 tabular.row_info[i].interline_space = len;
1733 // dummy stuff just to avoid warnings
1734 case LyXTabular::LAST_ACTION:
1738 InsetTabularMailer(*this).updateDialog(&bv);
1742 bool InsetTabular::showInsetDialog(BufferView * bv) const
1744 InsetTabularMailer(*this).showDialog(bv);
1749 void InsetTabular::openLayoutDialog(BufferView * bv) const
1751 InsetTabularMailer(*this).showDialog(bv);
1755 bool InsetTabular::copySelection(LCursor & cur)
1757 if (!cur.selection())
1762 getSelection(cur, rs, re, cs, ce);
1764 paste_tabular.reset(new LyXTabular(tabular));
1766 for (row_type i = 0; i < rs; ++i)
1767 paste_tabular->deleteRow(0);
1769 row_type const rows = re - rs + 1;
1770 while (paste_tabular->rows() > rows)
1771 paste_tabular->deleteRow(rows);
1773 paste_tabular->setTopLine(0, true, true);
1774 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1777 for (col_type i = 0; i < cs; ++i)
1778 paste_tabular->deleteColumn(0);
1780 col_type const columns = ce - cs + 1;
1781 while (paste_tabular->columns() > columns)
1782 paste_tabular->deleteColumn(columns);
1784 paste_tabular->setLeftLine(0, true, true);
1785 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1789 OutputParams const runparams;
1790 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1791 cur.bv().owner()->gui().clipboard().put(lyx::from_utf8(os.str()));
1792 // mark tabular stack dirty
1793 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1794 // when we (hopefully) have a one-for-all paste mechanism.
1795 lyx::cap::dirtyTabularStack(true);
1801 bool InsetTabular::pasteSelection(LCursor & cur)
1805 col_type const actcol = tabular.column_of_cell(cur.idx());
1806 row_type const actrow = tabular.row_of_cell(cur.idx());
1807 for (row_type r1 = 0, r2 = actrow;
1808 r1 < paste_tabular->rows() && r2 < tabular.rows();
1810 for (col_type c1 = 0, c2 = actcol;
1811 c1 < paste_tabular->columns() && c2 < tabular.columns();
1813 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1814 tabular.isPartOfMultiColumn(r2, c2))
1816 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1820 if (tabular.isPartOfMultiColumn(r2, c2)) {
1824 shared_ptr<InsetText> inset(
1825 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1826 tabular.setCellInset(r2, c2, inset);
1835 void InsetTabular::cutSelection(LCursor & cur)
1837 if (!cur.selection())
1842 getSelection(cur, rs, re, cs, ce);
1843 for (row_type i = rs; i <= re; ++i) {
1844 for (col_type j = cs; j <= ce; ++j) {
1845 shared_ptr<InsetText> t
1846 = cell(tabular.getCellNumber(i, j));
1847 if (cur.buffer().params().tracking_changes)
1848 t->markErased(true);
1854 // cursor position might be invalid now
1855 cur.pos() = cur.lastpos();
1856 cur.clearSelection();
1860 bool InsetTabular::isRightToLeft(LCursor & cur) const
1862 BOOST_ASSERT(cur.depth() > 1);
1863 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1864 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1865 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1866 parentpos).language()->rightToLeft();
1870 void InsetTabular::getSelection(LCursor & cur,
1871 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1873 CursorSlice const & beg = cur.selBegin();
1874 CursorSlice const & end = cur.selEnd();
1875 cs = tabular.column_of_cell(beg.idx());
1876 ce = tabular.column_of_cell(end.idx());
1879 cs = tabular.column_of_cell(end.idx());
1881 ce = tabular.right_column_of_cell(end.idx());
1884 rs = tabular.row_of_cell(beg.idx());
1885 re = tabular.row_of_cell(end.idx());
1891 LyXText * InsetTabular::getText(int idx) const
1893 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1897 void InsetTabular::markErased(bool erased)
1899 for (idx_type idx = 0; idx < nargs(); ++idx)
1900 cell(idx)->markErased(erased);
1904 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1906 return tabular.getPWidth(cell).zero();
1910 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1913 if (buf.length() <= 0)
1918 col_type maxCols = 1;
1919 docstring::size_type const len = buf.length();
1920 docstring::size_type p = 0;
1923 (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos) {
1931 maxCols = max(cols, maxCols);
1937 maxCols = max(cols, maxCols);
1938 LyXTabular * loctab;
1943 paste_tabular.reset(
1944 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1945 loctab = paste_tabular.get();
1947 dirtyTabularStack(true);
1950 cell = bv.cursor().idx();
1951 ocol = tabular.column_of_cell(cell);
1952 row = tabular.row_of_cell(cell);
1955 docstring::size_type op = 0;
1956 idx_type const cells = loctab->getNumberOfCells();
1959 rows = loctab->rows();
1960 col_type const columns = loctab->columns();
1962 while (cell < cells && p < len && row < rows &&
1963 (p = buf.find_first_of(lyx::from_ascii("\t\n"), p)) != docstring::npos)
1969 // we can only set this if we are not too far right
1970 if (cols < columns) {
1971 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1972 inset->setViewCache(&bv);
1973 Paragraph & par = inset->text_.getPar(0);
1974 LyXFont const font = inset->text_.getFont(par, 0);
1975 inset->setText(buf.substr(op, p - op), font);
1981 // we can only set this if we are not too far right
1982 if (cols < columns) {
1983 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1984 inset->setViewCache(&bv);
1985 Paragraph & par = inset->text_.getPar(0);
1986 LyXFont const font = inset->text_.getFont(par, 0);
1987 inset->setText(buf.substr(op, p - op), font);
1992 cell = loctab->getCellNumber(row, cols);
1998 // check for the last cell if there is no trailing '\n'
1999 if (cell < cells && op < len) {
2000 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2001 inset->setViewCache(&bv);
2002 Paragraph & par = inset->text_.getPar(0);
2003 LyXFont const font = inset->text_.getFont(par, 0);
2004 inset->setText(buf.substr(op, len - op), font);
2010 void InsetTabular::addPreview(PreviewLoader & loader) const
2012 row_type const rows = tabular.rows();
2013 col_type const columns = tabular.columns();
2014 for (row_type i = 0; i < rows; ++i) {
2015 for (col_type j = 0; j < columns; ++j)
2016 tabular.getCellInset(i, j)->addPreview(loader);
2021 bool InsetTabular::tablemode(LCursor & cur) const
2023 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2030 string const InsetTabularMailer::name_("tabular");
2032 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2033 : inset_(const_cast<InsetTabular &>(inset))
2037 string const InsetTabularMailer::inset2string(Buffer const &) const
2039 return params2string(inset_);
2043 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2045 istringstream data(in);
2047 lex.setStream(data);
2054 if (!lex || token != name_)
2055 return print_mailer_error("InsetTabularMailer", in, 1,
2058 // This is part of the inset proper that is usually swallowed
2059 // by Buffer::readInset
2061 if (!lex || token != "Tabular")
2062 return print_mailer_error("InsetTabularMailer", in, 2,
2065 Buffer const & buffer = inset.buffer();
2066 inset.read(buffer, lex);
2070 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2073 data << name_ << ' ';
2074 inset.write(inset.buffer(), data);
2075 data << "\\end_inset\n";