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/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
49 using lyx::cap::dirtyTabularStack;
50 using lyx::cap::tabularStackDirty;
52 using lyx::graphics::PreviewLoader;
54 using lyx::support::ltrim;
56 using lyx::frontend::Painter;
58 using boost::shared_ptr;
64 using std::istringstream;
66 using std::ostringstream;
73 int const ADD_TO_HEIGHT = 2;
74 int const ADD_TO_TABULAR_WIDTH = 2;
77 boost::scoped_ptr<LyXTabular> paste_tabular;
80 struct TabularFeature {
81 LyXTabular::Feature action;
86 TabularFeature tabularFeature[] =
88 { LyXTabular::APPEND_ROW, "append-row" },
89 { LyXTabular::APPEND_COLUMN, "append-column" },
90 { LyXTabular::DELETE_ROW, "delete-row" },
91 { LyXTabular::DELETE_COLUMN, "delete-column" },
92 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
93 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
94 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
95 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
96 { LyXTabular::ALIGN_LEFT, "align-left" },
97 { LyXTabular::ALIGN_RIGHT, "align-right" },
98 { LyXTabular::ALIGN_CENTER, "align-center" },
99 { LyXTabular::ALIGN_BLOCK, "align-block" },
100 { LyXTabular::VALIGN_TOP, "valign-top" },
101 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
102 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
103 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
104 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
105 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
106 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
107 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
108 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
109 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
110 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
111 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
112 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
113 { LyXTabular::MULTICOLUMN, "multicolumn" },
114 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
115 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
116 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
117 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
118 { LyXTabular::SET_PWIDTH, "set-pwidth" },
119 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
120 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
121 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
122 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
123 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
124 { LyXTabular::SET_USEBOX, "set-usebox" },
125 { LyXTabular::SET_LTHEAD, "set-lthead" },
126 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
127 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
128 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
129 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
130 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
131 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
132 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
133 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
134 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
135 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
136 { LyXTabular::LAST_ACTION, "" }
140 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
142 FeatureEqual(LyXTabular::Feature feature)
143 : feature_(feature) {}
144 bool operator()(TabularFeature const & tf) const {
145 return tf.action == feature_;
148 LyXTabular::Feature feature_;
154 string const featureAsString(LyXTabular::Feature feature)
156 TabularFeature * end = tabularFeature +
157 sizeof(tabularFeature) / sizeof(TabularFeature);
158 TabularFeature * it = std::find_if(tabularFeature, end,
159 FeatureEqual(feature));
160 return (it == end) ? string() : it->feature;
164 bool InsetTabular::hasPasteBuffer() const
166 return (paste_tabular.get() != 0);
170 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
172 : tabular(buf.params(), max(rows, row_type(1)),
173 max(columns, col_type(1))), buffer_(&buf), scx_(0)
177 InsetTabular::InsetTabular(InsetTabular const & tab)
178 : InsetOld(tab), tabular(tab.tabular),
179 buffer_(tab.buffer_), scx_(0)
183 InsetTabular::~InsetTabular()
185 InsetTabularMailer(*this).hideDialog();
189 auto_ptr<InsetBase> InsetTabular::doClone() const
191 return auto_ptr<InsetBase>(new InsetTabular(*this));
195 Buffer const & InsetTabular::buffer() const
201 void InsetTabular::buffer(Buffer const * b)
207 void InsetTabular::write(Buffer const & buf, ostream & os) const
209 os << "Tabular" << endl;
210 tabular.write(buf, os);
214 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
216 bool const old_format = (lex.getString() == "\\LyXTable");
218 tabular.read(buf, lex);
224 string token = lex.getString();
225 while (lex.isOK() && (token != "\\end_inset")) {
227 token = lex.getString();
229 if (token != "\\end_inset") {
230 lex.printError("Missing \\end_inset at this point. "
236 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
238 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
239 // mi.base.textwidth << "\n";
241 lyxerr << "InsetTabular::metrics: need bv" << endl;
246 for (idx_type cell = 0; i < tabular.rows(); ++i) {
249 for (col_type j = 0; j < tabular.columns(); ++j) {
250 if (tabular.isPartOfMultiColumn(i, j))
251 // Multicolumn cell, but not first one
256 if (tabular.cell_info[i][j].multicolumn ==
257 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
258 p_width = tabular.cellinfo_of_cell(cell).p_width;
260 p_width = tabular.column_info[j].p_width;
262 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
263 tabular.getCellInset(cell)->metrics(m, dim);
265 dim.wid = m.base.textwidth;
266 maxAsc = max(maxAsc, dim.asc);
267 maxDesc = max(maxDesc, dim.des);
268 tabular.setWidthOfCell(cell, dim.wid);
271 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
272 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
275 dim.asc = tabular.getAscentOfRow(0);
276 dim.des = tabular.getHeightOfTabular() - dim.asc;
277 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
282 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
284 setPosCache(pi, x, y);
286 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
287 BufferView * bv = pi.base.bv;
289 static lyx::frontend::NullPainter nop;
290 static PainterInfo nullpi(bv, nop);
292 //resetPos(bv->cursor());
295 x += ADD_TO_TABULAR_WIDTH;
298 first_visible_cell = LyXTabular::npos;
299 for (row_type i = 0; i < tabular.rows(); ++i) {
301 int const a = tabular.getAscentOfRow(i);
302 int const d = tabular.getDescentOfRow(i);
303 idx = tabular.getCellNumber(i, 0);
304 for (col_type j = 0; j < tabular.columns(); ++j) {
305 if (tabular.isPartOfMultiColumn(i, j))
307 if (first_visible_cell == LyXTabular::npos)
308 first_visible_cell = idx;
310 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
311 if (nx + tabular.getWidthOfColumn(idx) < 0
312 || nx > bv->workWidth()
314 || y - a > bv->workHeight()) {
315 cell(idx)->draw(nullpi, cx, y);
316 drawCellLines(nop, nx, y, i, idx, pi.erased_);
318 cell(idx)->draw(pi, cx, y);
319 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
321 nx += tabular.getWidthOfColumn(idx);
325 if (i + 1 < tabular.rows())
326 y += d + tabular.getAscentOfRow(i + 1) +
327 tabular.getAdditionalHeight(i + 1);
332 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
334 setPosCache(pi, x, y);
336 LCursor & cur = pi.base.bv->cursor();
338 x += scx_ + ADD_TO_TABULAR_WIDTH;
340 // Here we take the cursor slice that is the tabular.
341 lyx::size_type d = 0;
343 for (; d < cur.depth(); d++) {
344 if (ptr_cmp(&cur[d].inset(), this)) {
351 CursorSlice const & sl = cur[d];
352 // Paint background of current cell
353 int const w = tabular.getWidthOfColumn(sl.idx());
354 int yy = y - tabular.getAscentOfRow(0) + ADD_TO_HEIGHT;
356 for (; tabular.getCellNumber(j, tabular.columns() - 1) < sl.idx(); ++j) {
357 int const a = tabular.getAscentOfRow(j);
358 int const h = a + tabular.getDescentOfRow(j);
360 yy += tabular.getAdditionalHeight(j + 1);
362 int const h = tabular.getAscentOfRow(j) + tabular.getDescentOfRow(j);
363 pi.pain.fillRectangle(x + getCellXPos(sl.idx()), yy, w, h,
367 if (!cur.selection())
369 if (!ptr_cmp(&cur.inset(), this))
375 if (tablemode(cur)) {
378 getSelection(cur, rs, re, cs, ce);
379 y -= tabular.getAscentOfRow(0);
380 for (row_type j = 0; j < tabular.rows(); ++j) {
381 int const a = tabular.getAscentOfRow(j);
382 int const h = a + tabular.getDescentOfRow(j);
384 y += tabular.getAdditionalHeight(j);
385 for (col_type i = 0; i < tabular.columns(); ++i) {
386 if (tabular.isPartOfMultiColumn(j, i))
388 idx_type const cell =
389 tabular.getCellNumber(j, i);
390 int const w = tabular.getWidthOfColumn(cell);
391 if (i >= cs && i <= ce && j >= rs && j <= re)
392 pi.pain.fillRectangle(xx, y, w, h,
401 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
406 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
407 row_type row, idx_type cell, bool erased) const
409 int x2 = x + tabular.getWidthOfColumn(cell);
411 LColor::color col = LColor::tabularline;
412 LColor::color onoffcol = LColor::tabularonoffline;
415 col = LColor::strikeout;
416 onoffcol = LColor::strikeout;
419 if (!tabular.topAlreadyDrawn(cell)) {
420 on_off = !tabular.topLine(cell);
421 pain.line(x, y - tabular.getAscentOfRow(row),
422 x2, y - tabular.getAscentOfRow(row),
423 on_off ? onoffcol : col,
424 on_off ? Painter::line_onoffdash : Painter::line_solid);
426 on_off = !tabular.bottomLine(cell);
427 pain.line(x, y + tabular.getDescentOfRow(row),
428 x2, y + tabular.getDescentOfRow(row),
429 on_off ? onoffcol : col,
430 on_off ? Painter::line_onoffdash : Painter::line_solid);
431 if (!tabular.leftAlreadyDrawn(cell)) {
432 on_off = !tabular.leftLine(cell);
433 pain.line(x, y - tabular.getAscentOfRow(row),
434 x, y + tabular.getDescentOfRow(row),
435 on_off ? onoffcol : col,
436 on_off ? Painter::line_onoffdash : Painter::line_solid);
438 on_off = !tabular.rightLine(cell);
439 pain.line(x2 - tabular.getAdditionalWidth(cell),
440 y - tabular.getAscentOfRow(row),
441 x2 - tabular.getAdditionalWidth(cell),
442 y + tabular.getDescentOfRow(row),
443 on_off ? onoffcol : col,
444 on_off ? Painter::line_onoffdash : Painter::line_solid);
448 string const InsetTabular::editMessage() const
450 return _("Opened table");
454 void InsetTabular::edit(LCursor & cur, bool left)
456 //lyxerr << "InsetTabular::edit: " << this << endl;
458 cur.selection() = false;
461 if (isRightToLeft(cur))
462 cur.idx() = tabular.getLastCellInRow(0);
468 if (isRightToLeft(cur))
469 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
471 cur.idx() = tabular.getNumberOfCells() - 1;
473 cur.pos() = cur.lastpos(); // FIXME crude guess
475 // this accesses the position cache before it is initialized
477 //cur.bv().fitCursor();
481 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
483 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
484 << "\n cur:" << cur << endl;
485 CursorSlice sl = cur.top();
486 LCursor & bvcur = cur.bv().cursor();
488 switch (cmd.action) {
490 case LFUN_MOUSE_PRESS:
491 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
493 if (cmd.button() == mouse_button::button1) {
494 cur.selection() = false;
495 setCursorFromCoordinates(cur, cmd.x, cmd.y);
501 if (cmd.button() == mouse_button::button2) {
502 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
503 doDispatch(cur, cmd);
507 if (cmd.button() == mouse_button::button3)
508 bvcur.setCursor(cur);
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().painter().paperHeight();
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().painter().paperHeight();
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, cmd.argument))
658 // insert file functions
659 case LFUN_FILE_INSERT_ASCII_PARA:
660 case LFUN_FILE_INSERT_ASCII: {
661 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
662 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
668 if (tablemode(cur)) {
669 if (copySelection(cur)) {
670 recordUndoInset(cur, Undo::DELETE);
675 cell(cur.idx())->dispatch(cur, cmd);
678 case LFUN_CHAR_DELETE_BACKWARD:
679 case LFUN_CHAR_DELETE_FORWARD:
680 if (tablemode(cur)) {
681 recordUndoInset(cur, Undo::DELETE);
685 cell(cur.idx())->dispatch(cur, cmd);
689 if (!cur.selection())
691 if (tablemode(cur)) {
695 cell(cur.idx())->dispatch(cur, cmd);
698 case LFUN_PRIMARY_SELECTION_PASTE: {
699 string const clip = cur.bv().getClipboard();
702 // pass to InsertAsciiString, but
703 // only if we have multi-cell content
704 if (clip.find_first_of("\t\n") != string::npos) {
705 if (insertAsciiString(cur.bv(), clip, false)) {
706 // content has been replaced,
707 // so cursor might be invalid
708 cur.pos() = cur.lastpos();
709 bvcur.setCursor(cur);
713 // so that the clipboard is used and it goes on
715 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
716 paste_tabular.reset();
717 dirtyTabularStack(false);
723 if (hasPasteBuffer() && tabularStackDirty()) {
724 recordUndoInset(cur, Undo::INSERT);
728 cell(cur.idx())->dispatch(cur, cmd);
729 // Reset pasted paragraphs:
730 if (tabular.getPWidth(cur.idx()).zero())
731 cell(cur.idx())->forceParagraphsToDefault(cur);
736 case LFUN_FONT_ROMAN:
742 case LFUN_FONT_FREE_APPLY:
743 case LFUN_FONT_FREE_UPDATE:
745 case LFUN_FONT_UNDERLINE:
747 case LFUN_WORD_CAPITALIZE:
748 case LFUN_WORD_UPCASE:
749 case LFUN_WORD_LOWCASE:
750 case LFUN_CHARS_TRANSPOSE:
751 if (tablemode(cur)) {
754 getSelection(cur, rs, re, cs, ce);
755 for (row_type i = rs; i <= re; ++i) {
756 for (col_type j = cs; j <= ce; ++j) {
757 // cursor follows cell:
758 cur.idx() = tabular.getCellNumber(i, j);
759 // select this cell only:
762 cur.pos() = cur.top().lastpos();
765 cell(cur.idx())->dispatch(cur, cmd);
768 // Restore original selection
769 cur.idx() = tabular.getCellNumber(rs, cs);
772 cur.idx() = tabular.getCellNumber(re, ce);
773 cur.pos() = cur.top().lastpos();
778 cell(cur.idx())->dispatch(cur, cmd);
782 // we try to handle this event in the insets dispatch function.
783 cell(cur.idx())->dispatch(cur, cmd);
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 == 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(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::SET_ALL_LINES:
835 case LyXTabular::UNSET_ALL_LINES:
839 case LyXTabular::MULTICOLUMN:
840 status.setOnOff(tabular.isMultiColumn(cur.idx()));
843 case LyXTabular::M_TOGGLE_LINE_TOP:
845 case LyXTabular::TOGGLE_LINE_TOP:
846 status.setOnOff(tabular.topLine(cur.idx(), flag));
849 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
851 case LyXTabular::TOGGLE_LINE_BOTTOM:
852 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
855 case LyXTabular::M_TOGGLE_LINE_LEFT:
857 case LyXTabular::TOGGLE_LINE_LEFT:
858 status.setOnOff(tabular.leftLine(cur.idx(), flag));
861 case LyXTabular::M_TOGGLE_LINE_RIGHT:
863 case LyXTabular::TOGGLE_LINE_RIGHT:
864 status.setOnOff(tabular.rightLine(cur.idx(), flag));
867 case LyXTabular::M_ALIGN_LEFT:
869 case LyXTabular::ALIGN_LEFT:
870 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
873 case LyXTabular::M_ALIGN_RIGHT:
875 case LyXTabular::ALIGN_RIGHT:
876 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
879 case LyXTabular::M_ALIGN_CENTER:
881 case LyXTabular::ALIGN_CENTER:
882 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
885 case LyXTabular::ALIGN_BLOCK:
886 status.enabled(!tabular.getPWidth(cur.idx()).zero());
887 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
890 case LyXTabular::M_VALIGN_TOP:
892 case LyXTabular::VALIGN_TOP:
894 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
897 case LyXTabular::M_VALIGN_BOTTOM:
899 case LyXTabular::VALIGN_BOTTOM:
901 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
904 case LyXTabular::M_VALIGN_MIDDLE:
906 case LyXTabular::VALIGN_MIDDLE:
908 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
911 case LyXTabular::SET_LONGTABULAR:
912 status.setOnOff(tabular.isLongTabular());
915 case LyXTabular::UNSET_LONGTABULAR:
916 status.setOnOff(!tabular.isLongTabular());
919 case LyXTabular::SET_ROTATE_TABULAR:
920 status.setOnOff(tabular.getRotateTabular());
923 case LyXTabular::UNSET_ROTATE_TABULAR:
924 status.setOnOff(!tabular.getRotateTabular());
927 case LyXTabular::SET_ROTATE_CELL:
928 status.setOnOff(tabular.getRotateCell(cur.idx()));
931 case LyXTabular::UNSET_ROTATE_CELL:
932 status.setOnOff(!tabular.getRotateCell(cur.idx()));
935 case LyXTabular::SET_USEBOX:
936 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
939 case LyXTabular::SET_LTFIRSTHEAD:
940 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
943 case LyXTabular::UNSET_LTFIRSTHEAD:
944 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
947 case LyXTabular::SET_LTHEAD:
948 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::UNSET_LTHEAD:
952 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
955 case LyXTabular::SET_LTFOOT:
956 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
959 case LyXTabular::UNSET_LTFOOT:
960 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
963 case LyXTabular::SET_LTLASTFOOT:
964 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
967 case LyXTabular::UNSET_LTLASTFOOT:
968 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
971 case LyXTabular::SET_LTNEWPAGE:
972 status.setOnOff(tabular.getLTNewPage(sel_row_start));
977 status.enabled(false);
983 // These are only enabled inside tabular
984 case LFUN_CELL_BACKWARD:
985 case LFUN_CELL_FORWARD:
986 status.enabled(true);
989 // disable these with multiple cells selected
990 case LFUN_INSET_INSERT:
991 case LFUN_TABULAR_INSERT:
992 case LFUN_CHARSTYLE_INSERT:
993 case LFUN_FLOAT_INSERT:
994 case LFUN_FLOAT_WIDE_INSERT:
995 case LFUN_FOOTNOTE_INSERT:
996 case LFUN_MARGINALNOTE_INSERT:
997 case LFUN_MATH_INSERT:
999 case LFUN_MATH_MUTATE:
1000 case LFUN_MATH_DISPLAY:
1001 case LFUN_NOTE_INSERT:
1002 case LFUN_OPTIONAL_INSERT:
1003 case LFUN_BOX_INSERT:
1004 case LFUN_BRANCH_INSERT:
1005 case LFUN_WRAP_INSERT:
1006 case LFUN_ERT_INSERT: {
1007 if (tablemode(cur)) {
1008 status.enabled(false);
1011 return cell(cur.idx())->getStatus(cur, cmd, status);
1014 // disable in non-fixed-width cells
1015 case LFUN_BREAK_LINE:
1016 case LFUN_BREAK_PARAGRAPH:
1017 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1018 case LFUN_BREAK_PARAGRAPH_SKIP: {
1019 if (tabular.getPWidth(cur.idx()).zero()) {
1020 status.enabled(false);
1023 return cell(cur.idx())->getStatus(cur, cmd, status);
1027 if (tabularStackDirty()) {
1028 status.enabled(true);
1032 case LFUN_INSET_MODIFY:
1033 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1034 status.enabled(true);
1040 // we try to handle this event in the insets dispatch function.
1041 return cell(cur.idx())->getStatus(cur, cmd, status);
1046 int InsetTabular::latex(Buffer const & buf, ostream & os,
1047 OutputParams const & runparams) const
1049 return tabular.latex(buf, os, runparams);
1053 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1054 OutputParams const & runparams) const
1056 int const dp = runparams.linelen ? runparams.depth : 0;
1057 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1061 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1062 OutputParams const & runparams) const
1064 return tabular.linuxdoc(buf,os, runparams);
1068 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1069 OutputParams const & runparams) const
1072 InsetBase * master = 0;
1074 #ifdef WITH_WARNINGS
1075 #warning Why not pass a proper DocIterator here?
1078 // if the table is inside a float it doesn't need the informaltable
1079 // wrapper. Search for it.
1080 for (master = owner(); master; master = master->owner())
1081 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1086 os << "<informaltable>";
1089 ret += tabular.docbook(buf, os, runparams);
1091 os << "</informaltable>";
1098 void InsetTabular::validate(LaTeXFeatures & features) const
1100 tabular.validate(features);
1104 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1106 return tabular.getCellInset(idx);
1110 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1112 return tabular.getCellInset(idx);
1116 void InsetTabular::cursorPos
1117 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1119 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1121 // y offset correction
1122 int const row = tabular.row_of_cell(sl.idx());
1123 for (int i = 0; i <= row; ++i) {
1125 y += tabular.getAscentOfRow(i);
1126 y += tabular.getAdditionalHeight(i);
1129 y += tabular.getDescentOfRow(i);
1132 // x offset correction
1133 int const col = tabular.column_of_cell(sl.idx());
1134 int idx = tabular.getCellNumber(row, 0);
1135 for (int j = 0; j < col; ++j) {
1136 if (tabular.isPartOfMultiColumn(row, j))
1138 x += tabular.getWidthOfColumn(idx);
1141 x += tabular.getBeginningOfTextInCell(idx);
1142 x += ADD_TO_TABULAR_WIDTH;
1147 int InsetTabular::dist(idx_type const cell, int x, int y) const
1151 InsetBase const & inset = *tabular.getCellInset(cell);
1152 Point o = theCoords.getInsets().xy(&inset);
1153 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1154 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1155 int const ybeg = o.y_ - inset.ascent();
1156 row_type const row = tabular.row_of_cell(cell);
1157 int const rowheight = tabular.getAscentOfRow(row)
1158 + tabular.getDescentOfRow(row);
1159 int const yend = ybeg + rowheight;
1171 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1172 // << " ybeg=" << ybeg << " yend=" << yend
1173 // << " xx=" << xx << " yy=" << yy
1174 // << " dist=" << xx + yy << endl;
1179 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1181 //lyxerr << "InsetTabular::editXY: " << this << endl;
1182 cur.selection() = false;
1184 cur.idx() = getNearestCell(x, y);
1186 return cell(cur.idx())->text_.editXY(cur, x, y);
1190 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1192 cur.idx() = getNearestCell(x, y);
1193 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1197 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1199 idx_type idx_min = 0;
1200 int dist_min = std::numeric_limits<int>::max();
1201 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1202 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1203 int const d = dist(i, x, y);
1214 int InsetTabular::getCellXPos(idx_type const cell) const
1218 for (; !tabular.isFirstCellInRow(c); --c)
1220 int lx = tabular.getWidthOfColumn(cell);
1221 for (; c < cell; ++c)
1222 lx += tabular.getWidthOfColumn(c);
1224 return lx - tabular.getWidthOfColumn(cell);
1228 void InsetTabular::resetPos(LCursor & cur) const
1230 BufferView & bv = cur.bv();
1231 int const maxwidth = bv.workWidth();
1233 if (&cur.inset() != this) {
1237 int const X2 = maxwidth;
1238 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1239 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1240 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1243 scx_ = X1 + 20 - x1;
1245 scx_ = X2 - 20 - x2;
1252 InsetTabularMailer(*this).updateDialog(&bv);
1256 void InsetTabular::moveNextCell(LCursor & cur)
1258 if (isRightToLeft(cur)) {
1259 if (tabular.isFirstCellInRow(cur.idx())) {
1260 row_type const row = tabular.row_of_cell(cur.idx());
1261 if (row == tabular.rows() - 1)
1263 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1270 if (tabular.isLastCell(cur.idx()))
1280 void InsetTabular::movePrevCell(LCursor & cur)
1282 if (isRightToLeft(cur)) {
1283 if (tabular.isLastCellInRow(cur.idx())) {
1284 row_type const row = tabular.row_of_cell(cur.idx());
1287 cur.idx() = tabular.getFirstCellInRow(row);
1288 cur.idx() = tabular.getCellAbove(cur.idx());
1290 if (tabular.isLastCell(cur.idx()))
1295 if (cur.idx() == 0) // first cell
1299 cur.pit() = cur.lastpit();
1300 cur.pos() = cur.lastpos();
1305 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1307 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1310 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1311 string const tmp = tabularFeature[i].feature;
1313 if (tmp == what.substr(0, tmp.length())) {
1314 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1315 //tabularFeatures[i].feature.length())) {
1316 action = tabularFeature[i].action;
1320 if (action == LyXTabular::LAST_ACTION)
1324 ltrim(what.substr(tabularFeature[i].feature.length()));
1325 tabularFeatures(cur, action, val);
1332 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1333 string const & special, bool & flag)
1335 if (special == "dl_above") {
1338 } else if (special == "dl_below") {
1339 ltt.bottomDL = flag;
1341 } else if (special == "empty") {
1353 void InsetTabular::tabularFeatures(LCursor & cur,
1354 LyXTabular::Feature feature, string const & value)
1356 BufferView & bv = cur.bv();
1357 col_type sel_col_start;
1358 col_type sel_col_end;
1359 row_type sel_row_start;
1360 row_type sel_row_end;
1361 bool setLines = false;
1362 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1363 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1367 case LyXTabular::M_ALIGN_LEFT:
1368 case LyXTabular::ALIGN_LEFT:
1369 setAlign = LYX_ALIGN_LEFT;
1372 case LyXTabular::M_ALIGN_RIGHT:
1373 case LyXTabular::ALIGN_RIGHT:
1374 setAlign = LYX_ALIGN_RIGHT;
1377 case LyXTabular::M_ALIGN_CENTER:
1378 case LyXTabular::ALIGN_CENTER:
1379 setAlign = LYX_ALIGN_CENTER;
1382 case LyXTabular::ALIGN_BLOCK:
1383 setAlign = LYX_ALIGN_BLOCK;
1386 case LyXTabular::M_VALIGN_TOP:
1387 case LyXTabular::VALIGN_TOP:
1388 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1391 case LyXTabular::M_VALIGN_BOTTOM:
1392 case LyXTabular::VALIGN_BOTTOM:
1393 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1396 case LyXTabular::M_VALIGN_MIDDLE:
1397 case LyXTabular::VALIGN_MIDDLE:
1398 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1405 recordUndoInset(cur, Undo::ATOMIC);
1407 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1408 row_type const row = tabular.row_of_cell(cur.idx());
1409 col_type const column = tabular.column_of_cell(cur.idx());
1411 LyXTabular::ltType ltt;
1415 case LyXTabular::SET_PWIDTH: {
1416 LyXLength const len(value);
1417 tabular.setColumnPWidth(cur, cur.idx(), len);
1419 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1420 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1424 case LyXTabular::SET_MPWIDTH:
1425 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1428 case LyXTabular::SET_SPECIAL_COLUMN:
1429 case LyXTabular::SET_SPECIAL_MULTI:
1430 tabular.setAlignSpecial(cur.idx(),value,feature);
1433 case LyXTabular::APPEND_ROW:
1434 // append the row into the tabular
1435 tabular.appendRow(bv.buffer()->params(), cur.idx());
1438 case LyXTabular::APPEND_COLUMN:
1439 // append the column into the tabular
1440 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1441 cur.idx() = tabular.getCellNumber(row, column);
1444 case LyXTabular::DELETE_ROW:
1445 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1446 tabular.deleteRow(sel_row_start);
1447 if (sel_row_start >= tabular.rows())
1449 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1452 cur.selection() = false;
1455 case LyXTabular::DELETE_COLUMN:
1456 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1457 tabular.deleteColumn(sel_col_start);
1458 if (sel_col_start >= tabular.columns())
1460 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1463 cur.selection() = false;
1466 case LyXTabular::M_TOGGLE_LINE_TOP:
1468 case LyXTabular::TOGGLE_LINE_TOP: {
1469 bool lineSet = !tabular.topLine(cur.idx(), flag);
1470 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1471 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1473 tabular.getCellNumber(i, j),
1478 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1480 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1481 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1482 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1483 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1484 tabular.setBottomLine(
1485 tabular.getCellNumber(i, j),
1491 case LyXTabular::M_TOGGLE_LINE_LEFT:
1493 case LyXTabular::TOGGLE_LINE_LEFT: {
1494 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1495 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1496 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1497 tabular.setLeftLine(
1498 tabular.getCellNumber(i,j),
1504 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1506 case LyXTabular::TOGGLE_LINE_RIGHT: {
1507 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1508 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1509 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1510 tabular.setRightLine(
1511 tabular.getCellNumber(i,j),
1517 case LyXTabular::M_ALIGN_LEFT:
1518 case LyXTabular::M_ALIGN_RIGHT:
1519 case LyXTabular::M_ALIGN_CENTER:
1521 case LyXTabular::ALIGN_LEFT:
1522 case LyXTabular::ALIGN_RIGHT:
1523 case LyXTabular::ALIGN_CENTER:
1524 case LyXTabular::ALIGN_BLOCK:
1525 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1526 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1527 tabular.setAlignment(
1528 tabular.getCellNumber(i, j),
1533 case LyXTabular::M_VALIGN_TOP:
1534 case LyXTabular::M_VALIGN_BOTTOM:
1535 case LyXTabular::M_VALIGN_MIDDLE:
1537 case LyXTabular::VALIGN_TOP:
1538 case LyXTabular::VALIGN_BOTTOM:
1539 case LyXTabular::VALIGN_MIDDLE:
1540 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1541 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1542 tabular.setVAlignment(
1543 tabular.getCellNumber(i, j),
1547 case LyXTabular::MULTICOLUMN: {
1548 if (sel_row_start != sel_row_end) {
1549 #ifdef WITH_WARNINGS
1550 #warning Need I say it ? This is horrible.
1552 Alert::error(_("Error setting multicolumn"),
1553 _("You cannot set multicolumn vertically."));
1556 if (!cur.selection()) {
1557 // just multicol for one single cell
1558 // check whether we are completely in a multicol
1559 if (tabular.isMultiColumn(cur.idx()))
1560 tabular.unsetMultiColumn(cur.idx());
1562 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1565 // we have a selection so this means we just add all this
1566 // cells to form a multicolumn cell
1567 idx_type const s_start = cur.selBegin().idx();
1568 idx_type const s_end = cur.selEnd().idx();
1569 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1570 cur.idx() = s_start;
1573 cur.selection() = false;
1577 case LyXTabular::SET_ALL_LINES:
1579 case LyXTabular::UNSET_ALL_LINES:
1580 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1581 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1582 tabular.setAllLines(
1583 tabular.getCellNumber(i,j), setLines);
1586 case LyXTabular::SET_LONGTABULAR:
1587 tabular.setLongTabular(true);
1590 case LyXTabular::UNSET_LONGTABULAR:
1591 tabular.setLongTabular(false);
1594 case LyXTabular::SET_ROTATE_TABULAR:
1595 tabular.setRotateTabular(true);
1598 case LyXTabular::UNSET_ROTATE_TABULAR:
1599 tabular.setRotateTabular(false);
1602 case LyXTabular::SET_ROTATE_CELL:
1603 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1604 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1605 tabular.setRotateCell(
1606 tabular.getCellNumber(i, j), true);
1609 case LyXTabular::UNSET_ROTATE_CELL:
1610 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1611 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1612 tabular.setRotateCell(
1613 tabular.getCellNumber(i, j), false);
1616 case LyXTabular::SET_USEBOX: {
1617 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1618 if (val == tabular.getUsebox(cur.idx()))
1619 val = LyXTabular::BOX_NONE;
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.setUsebox(tabular.getCellNumber(i, j), val);
1626 case LyXTabular::UNSET_LTFIRSTHEAD:
1628 case LyXTabular::SET_LTFIRSTHEAD:
1629 tabular.getRowOfLTFirstHead(row, ltt);
1630 checkLongtableSpecial(ltt, value, flag);
1631 tabular.setLTHead(row, flag, ltt, true);
1634 case LyXTabular::UNSET_LTHEAD:
1636 case LyXTabular::SET_LTHEAD:
1637 tabular.getRowOfLTHead(row, ltt);
1638 checkLongtableSpecial(ltt, value, flag);
1639 tabular.setLTHead(row, flag, ltt, false);
1642 case LyXTabular::UNSET_LTFOOT:
1644 case LyXTabular::SET_LTFOOT:
1645 tabular.getRowOfLTFoot(row, ltt);
1646 checkLongtableSpecial(ltt, value, flag);
1647 tabular.setLTFoot(row, flag, ltt, false);
1650 case LyXTabular::UNSET_LTLASTFOOT:
1652 case LyXTabular::SET_LTLASTFOOT:
1653 tabular.getRowOfLTLastFoot(row, ltt);
1654 checkLongtableSpecial(ltt, value, flag);
1655 tabular.setLTFoot(row, flag, ltt, true);
1658 case LyXTabular::SET_LTNEWPAGE:
1659 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1662 // dummy stuff just to avoid warnings
1663 case LyXTabular::LAST_ACTION:
1667 InsetTabularMailer(*this).updateDialog(&bv);
1671 bool InsetTabular::showInsetDialog(BufferView * bv) const
1673 InsetTabularMailer(*this).showDialog(bv);
1678 void InsetTabular::openLayoutDialog(BufferView * bv) const
1680 InsetTabularMailer(*this).showDialog(bv);
1684 bool InsetTabular::copySelection(LCursor & cur)
1686 if (!cur.selection())
1691 getSelection(cur, rs, re, cs, ce);
1693 paste_tabular.reset(new LyXTabular(tabular));
1695 for (row_type i = 0; i < rs; ++i)
1696 paste_tabular->deleteRow(0);
1698 row_type const rows = re - rs + 1;
1699 while (paste_tabular->rows() > rows)
1700 paste_tabular->deleteRow(rows);
1702 paste_tabular->setTopLine(0, true, true);
1703 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1706 for (col_type i = 0; i < cs; ++i)
1707 paste_tabular->deleteColumn(0);
1709 col_type const columns = ce - cs + 1;
1710 while (paste_tabular->columns() > columns)
1711 paste_tabular->deleteColumn(columns);
1713 paste_tabular->setLeftLine(0, true, true);
1714 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1718 OutputParams const runparams;
1719 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1720 cur.bv().stuffClipboard(os.str());
1721 // mark tabular stack dirty
1722 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1723 // when we (hopefully) have a one-for-all paste mechanism.
1724 lyx::cap::dirtyTabularStack(true);
1730 bool InsetTabular::pasteSelection(LCursor & cur)
1734 col_type const actcol = tabular.column_of_cell(cur.idx());
1735 row_type const actrow = tabular.row_of_cell(cur.idx());
1736 for (row_type r1 = 0, r2 = actrow;
1737 r1 < paste_tabular->rows() && r2 < tabular.rows();
1739 for (col_type c1 = 0, c2 = actcol;
1740 c1 < paste_tabular->columns() && c2 < tabular.columns();
1742 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1743 tabular.isPartOfMultiColumn(r2, c2))
1745 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1749 if (tabular.isPartOfMultiColumn(r2, c2)) {
1753 shared_ptr<InsetText> inset(
1754 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1755 tabular.setCellInset(r2, c2, inset);
1764 void InsetTabular::cutSelection(LCursor & cur)
1766 if (!cur.selection())
1771 getSelection(cur, rs, re, cs, ce);
1772 for (row_type i = rs; i <= re; ++i) {
1773 for (col_type j = cs; j <= ce; ++j) {
1774 shared_ptr<InsetText> t
1775 = cell(tabular.getCellNumber(i, j));
1776 if (cur.buffer().params().tracking_changes)
1777 t->markErased(true);
1783 // cursor position might be invalid now
1784 cur.pos() = cur.lastpos();
1785 cur.clearSelection();
1789 bool InsetTabular::isRightToLeft(LCursor & cur) const
1791 BOOST_ASSERT(cur.depth() > 1);
1792 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1793 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1794 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1795 parentpos).language()->rightToLeft();
1799 void InsetTabular::getSelection(LCursor & cur,
1800 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1802 CursorSlice const & beg = cur.selBegin();
1803 CursorSlice const & end = cur.selEnd();
1804 cs = tabular.column_of_cell(beg.idx());
1805 ce = tabular.column_of_cell(end.idx());
1808 cs = tabular.column_of_cell(end.idx());
1810 ce = tabular.right_column_of_cell(end.idx());
1813 rs = tabular.row_of_cell(beg.idx());
1814 re = tabular.row_of_cell(end.idx());
1820 LyXText * InsetTabular::getText(int idx) const
1822 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1826 void InsetTabular::markErased(bool erased)
1828 for (idx_type idx = 0; idx < nargs(); ++idx)
1829 cell(idx)->markErased(erased);
1833 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1835 return tabular.getPWidth(cell).zero();
1839 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1842 if (buf.length() <= 0)
1847 col_type maxCols = 1;
1848 string::size_type const len = buf.length();
1849 string::size_type p = 0;
1851 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1859 maxCols = max(cols, maxCols);
1865 maxCols = max(cols, maxCols);
1866 LyXTabular * loctab;
1871 paste_tabular.reset(
1872 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1873 loctab = paste_tabular.get();
1875 dirtyTabularStack(true);
1878 cell = bv.cursor().idx();
1879 ocol = tabular.column_of_cell(cell);
1880 row = tabular.row_of_cell(cell);
1883 string::size_type op = 0;
1884 idx_type const cells = loctab->getNumberOfCells();
1887 rows = loctab->rows();
1888 col_type const columns = loctab->columns();
1890 while (cell < cells && p < len && row < rows &&
1891 (p = buf.find_first_of("\t\n", p)) != string::npos)
1897 // we can only set this if we are not too far right
1898 if (cols < columns) {
1899 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1900 inset->setViewCache(&bv);
1901 Paragraph & par = inset->text_.getPar(0);
1902 LyXFont const font = inset->text_.getFont(par, 0);
1903 inset->setText(buf.substr(op, p - op), font);
1909 // we can only set this if we are not too far right
1910 if (cols < columns) {
1911 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1912 inset->setViewCache(&bv);
1913 Paragraph & par = inset->text_.getPar(0);
1914 LyXFont const font = inset->text_.getFont(par, 0);
1915 inset->setText(buf.substr(op, p - op), font);
1920 cell = loctab->getCellNumber(row, cols);
1926 // check for the last cell if there is no trailing '\n'
1927 if (cell < cells && op < len) {
1928 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1929 inset->setViewCache(&bv);
1930 Paragraph & par = inset->text_.getPar(0);
1931 LyXFont const font = inset->text_.getFont(par, 0);
1932 inset->setText(buf.substr(op, len - op), font);
1938 void InsetTabular::addPreview(PreviewLoader & loader) const
1940 row_type const rows = tabular.rows();
1941 col_type const columns = tabular.columns();
1942 for (row_type i = 0; i < rows; ++i) {
1943 for (col_type j = 0; j < columns; ++j)
1944 tabular.getCellInset(i, j)->addPreview(loader);
1949 bool InsetTabular::tablemode(LCursor & cur) const
1951 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1958 string const InsetTabularMailer::name_("tabular");
1960 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1961 : inset_(const_cast<InsetTabular &>(inset))
1965 string const InsetTabularMailer::inset2string(Buffer const &) const
1967 return params2string(inset_);
1971 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1973 istringstream data(in);
1975 lex.setStream(data);
1982 if (!lex || token != name_)
1983 return print_mailer_error("InsetTabularMailer", in, 1,
1986 // This is part of the inset proper that is usually swallowed
1987 // by Buffer::readInset
1989 if (!lex || token != "Tabular")
1990 return print_mailer_error("InsetTabularMailer", in, 2,
1993 Buffer const & buffer = inset.buffer();
1994 inset.read(buffer, lex);
1998 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2001 data << name_ << ' ';
2002 inset.write(inset.buffer(), data);
2003 data << "\\end_inset\n";