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 boost::shared_ptr;
62 using std::istringstream;
64 using std::ostringstream;
71 int const ADD_TO_HEIGHT = 2;
72 int const ADD_TO_TABULAR_WIDTH = 2;
75 boost::scoped_ptr<LyXTabular> paste_tabular;
78 struct TabularFeature {
79 LyXTabular::Feature action;
84 TabularFeature tabularFeature[] =
86 { LyXTabular::APPEND_ROW, "append-row" },
87 { LyXTabular::APPEND_COLUMN, "append-column" },
88 { LyXTabular::DELETE_ROW, "delete-row" },
89 { LyXTabular::DELETE_COLUMN, "delete-column" },
90 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
91 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
92 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
93 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
94 { LyXTabular::ALIGN_LEFT, "align-left" },
95 { LyXTabular::ALIGN_RIGHT, "align-right" },
96 { LyXTabular::ALIGN_CENTER, "align-center" },
97 { LyXTabular::ALIGN_BLOCK, "align-block" },
98 { LyXTabular::VALIGN_TOP, "valign-top" },
99 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
100 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
101 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
102 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
103 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
104 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
105 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
106 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
107 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
108 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
109 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
110 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
111 { LyXTabular::MULTICOLUMN, "multicolumn" },
112 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
113 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
114 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
115 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
116 { LyXTabular::SET_PWIDTH, "set-pwidth" },
117 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
118 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
119 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
120 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
121 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
122 { LyXTabular::SET_USEBOX, "set-usebox" },
123 { LyXTabular::SET_LTHEAD, "set-lthead" },
124 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
125 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
126 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
127 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
128 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
129 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
130 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
131 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
132 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
133 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
134 { LyXTabular::LAST_ACTION, "" }
138 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
140 FeatureEqual(LyXTabular::Feature feature)
141 : feature_(feature) {}
142 bool operator()(TabularFeature const & tf) const {
143 return tf.action == feature_;
146 LyXTabular::Feature feature_;
152 string const featureAsString(LyXTabular::Feature feature)
154 TabularFeature * end = tabularFeature +
155 sizeof(tabularFeature) / sizeof(TabularFeature);
156 TabularFeature * it = std::find_if(tabularFeature, end,
157 FeatureEqual(feature));
158 return (it == end) ? string() : it->feature;
162 bool InsetTabular::hasPasteBuffer() const
164 return (paste_tabular.get() != 0);
168 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
170 : tabular(buf.params(), max(rows, row_type(1)),
171 max(columns, col_type(1))), buffer_(&buf), scx_(0)
175 InsetTabular::InsetTabular(InsetTabular const & tab)
176 : InsetOld(tab), tabular(tab.tabular),
177 buffer_(tab.buffer_), scx_(0)
181 InsetTabular::~InsetTabular()
183 InsetTabularMailer(*this).hideDialog();
187 auto_ptr<InsetBase> InsetTabular::doClone() const
189 return auto_ptr<InsetBase>(new InsetTabular(*this));
193 Buffer const & InsetTabular::buffer() const
199 void InsetTabular::buffer(Buffer const * b)
205 void InsetTabular::write(Buffer const & buf, ostream & os) const
207 os << "Tabular" << endl;
208 tabular.write(buf, os);
212 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
214 bool const old_format = (lex.getString() == "\\LyXTable");
216 tabular.read(buf, lex);
222 string token = lex.getString();
223 while (lex.isOK() && (token != "\\end_inset")) {
225 token = lex.getString();
227 if (token != "\\end_inset") {
228 lex.printError("Missing \\end_inset at this point. "
234 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
236 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
237 // mi.base.textwidth << "\n";
239 lyxerr << "InsetTabular::metrics: need bv" << endl;
244 for (idx_type cell = 0; i < tabular.rows(); ++i) {
247 for (col_type j = 0; j < tabular.columns(); ++j) {
248 if (tabular.isPartOfMultiColumn(i, j))
249 // Multicolumn cell, but not first one
254 if (tabular.cell_info[i][j].multicolumn ==
255 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
256 p_width = tabular.cellinfo_of_cell(cell).p_width;
258 p_width = tabular.column_info[j].p_width;
260 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
261 tabular.getCellInset(cell)->metrics(m, dim);
263 dim.wid = m.base.textwidth;
264 maxAsc = max(maxAsc, dim.asc);
265 maxDesc = max(maxDesc, dim.des);
266 tabular.setWidthOfCell(cell, dim.wid);
269 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
270 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
273 dim.asc = tabular.getAscentOfRow(0);
274 dim.des = tabular.getHeightOfTabular() - dim.asc;
275 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
280 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
282 setPosCache(pi, x, y);
284 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
285 BufferView * bv = pi.base.bv;
287 static NullPainter nop;
288 static PainterInfo nullpi(bv, nop);
290 //resetPos(bv->cursor());
293 x += ADD_TO_TABULAR_WIDTH;
296 first_visible_cell = LyXTabular::npos;
297 for (row_type i = 0; i < tabular.rows(); ++i) {
299 int const a = tabular.getAscentOfRow(i);
300 int const d = tabular.getDescentOfRow(i);
301 idx = tabular.getCellNumber(i, 0);
302 for (col_type j = 0; j < tabular.columns(); ++j) {
303 if (tabular.isPartOfMultiColumn(i, j))
305 if (first_visible_cell == LyXTabular::npos)
306 first_visible_cell = idx;
308 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
309 if (nx + tabular.getWidthOfColumn(idx) < 0
310 || nx > bv->workWidth()
312 || y - a > bv->workHeight()) {
313 cell(idx)->draw(nullpi, cx, y);
314 drawCellLines(nop, nx, y, i, idx, pi.erased_);
316 cell(idx)->draw(pi, cx, y);
317 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
319 nx += tabular.getWidthOfColumn(idx);
323 if (i + 1 < tabular.rows())
324 y += d + tabular.getAscentOfRow(i + 1) +
325 tabular.getAdditionalHeight(i + 1);
330 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
332 setPosCache(pi, x, y);
334 LCursor & cur = pi.base.bv->cursor();
336 x += scx_ + ADD_TO_TABULAR_WIDTH;
338 // Here we take the cursor slice that is the tabular.
339 lyx::size_type d = 0;
341 for (; d < cur.depth(); d++) {
342 if (ptr_cmp(&cur[d].inset(), this)) {
349 CursorSlice const & sl = cur[d];
350 // Paint background of current cell
351 int const w = tabular.getWidthOfColumn(sl.idx());
352 int yy = y - tabular.getAscentOfRow(0) + ADD_TO_HEIGHT;
354 for (; tabular.getCellNumber(j, tabular.columns() - 1) < sl.idx(); ++j) {
355 int const a = tabular.getAscentOfRow(j);
356 int const h = a + tabular.getDescentOfRow(j);
358 yy += tabular.getAdditionalHeight(j + 1);
360 int const h = tabular.getAscentOfRow(j) + tabular.getDescentOfRow(j);
361 pi.pain.fillRectangle(x + getCellXPos(sl.idx()), yy, w, h,
365 if (!cur.selection())
367 if (!ptr_cmp(&cur.inset(), this))
373 if (tablemode(cur)) {
376 getSelection(cur, rs, re, cs, ce);
377 y -= tabular.getAscentOfRow(0);
378 for (row_type j = 0; j < tabular.rows(); ++j) {
379 int const a = tabular.getAscentOfRow(j);
380 int const h = a + tabular.getDescentOfRow(j);
382 y += tabular.getAdditionalHeight(j);
383 for (col_type i = 0; i < tabular.columns(); ++i) {
384 if (tabular.isPartOfMultiColumn(j, i))
386 idx_type const cell =
387 tabular.getCellNumber(j, i);
388 int const w = tabular.getWidthOfColumn(cell);
389 if (i >= cs && i <= ce && j >= rs && j <= re)
390 pi.pain.fillRectangle(xx, y, w, h,
399 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
404 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
405 row_type row, idx_type cell, bool erased) const
407 int x2 = x + tabular.getWidthOfColumn(cell);
409 LColor::color col = LColor::tabularline;
410 LColor::color onoffcol = LColor::tabularonoffline;
413 col = LColor::strikeout;
414 onoffcol = LColor::strikeout;
417 if (!tabular.topAlreadyDrawn(cell)) {
418 on_off = !tabular.topLine(cell);
419 pain.line(x, y - tabular.getAscentOfRow(row),
420 x2, y - tabular.getAscentOfRow(row),
421 on_off ? onoffcol : col,
422 on_off ? Painter::line_onoffdash : Painter::line_solid);
424 on_off = !tabular.bottomLine(cell);
425 pain.line(x, y + tabular.getDescentOfRow(row),
426 x2, y + tabular.getDescentOfRow(row),
427 on_off ? onoffcol : col,
428 on_off ? Painter::line_onoffdash : Painter::line_solid);
429 if (!tabular.leftAlreadyDrawn(cell)) {
430 on_off = !tabular.leftLine(cell);
431 pain.line(x, y - tabular.getAscentOfRow(row),
432 x, y + tabular.getDescentOfRow(row),
433 on_off ? onoffcol : col,
434 on_off ? Painter::line_onoffdash : Painter::line_solid);
436 on_off = !tabular.rightLine(cell);
437 pain.line(x2 - tabular.getAdditionalWidth(cell),
438 y - tabular.getAscentOfRow(row),
439 x2 - tabular.getAdditionalWidth(cell),
440 y + tabular.getDescentOfRow(row),
441 on_off ? onoffcol : col,
442 on_off ? Painter::line_onoffdash : Painter::line_solid);
446 string const InsetTabular::editMessage() const
448 return _("Opened table");
452 void InsetTabular::edit(LCursor & cur, bool left)
454 //lyxerr << "InsetTabular::edit: " << this << endl;
456 cur.selection() = false;
459 if (isRightToLeft(cur))
460 cur.idx() = tabular.getLastCellInRow(0);
466 if (isRightToLeft(cur))
467 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
469 cur.idx() = tabular.getNumberOfCells() - 1;
471 cur.pos() = cur.lastpos(); // FIXME crude guess
473 // this accesses the position cache before it is initialized
475 //cur.bv().fitCursor();
479 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
481 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
482 << "\n cur:" << cur << endl;
483 CursorSlice sl = cur.top();
484 LCursor & bvcur = cur.bv().cursor();
486 switch (cmd.action) {
488 case LFUN_MOUSE_PRESS:
489 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
491 if (cmd.button() == mouse_button::button1) {
492 cur.selection() = false;
493 setCursorFromCoordinates(cur, cmd.x, cmd.y);
499 if (cmd.button() == mouse_button::button2) {
500 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
501 doDispatch(cur, cmd);
505 if (cmd.button() == mouse_button::button3)
506 bvcur.setCursor(cur);
510 case LFUN_MOUSE_MOTION:
511 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
512 if (cmd.button() == mouse_button::button1) {
513 // only accept motions to places not deeper nested than the real anchor
514 if (bvcur.anchor_.hasPart(cur)) {
515 setCursorFromCoordinates(cur, cmd.x, cmd.y);
516 bvcur.setCursor(cur);
517 bvcur.selection() = true;
523 case LFUN_MOUSE_RELEASE:
524 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
525 if (cmd.button() == mouse_button::button3)
526 InsetTabularMailer(*this).showDialog(&cur.bv());
529 case LFUN_CELL_BACKWARD:
531 cur.selection() = false;
534 case LFUN_CELL_FORWARD:
536 cur.selection() = false;
539 case LFUN_CHAR_FORWARD_SELECT:
540 case LFUN_CHAR_FORWARD:
541 cell(cur.idx())->dispatch(cur, cmd);
542 if (!cur.result().dispatched()) {
543 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
545 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
551 case LFUN_CHAR_BACKWARD_SELECT:
552 case LFUN_CHAR_BACKWARD:
553 cell(cur.idx())->dispatch(cur, cmd);
554 if (!cur.result().dispatched()) {
555 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
557 cmd = FuncRequest(LFUN_FINISHED_LEFT);
563 case LFUN_DOWN_SELECT:
565 cell(cur.idx())->dispatch(cur, cmd);
566 cur.dispatched(); // override the cell's decision
568 // if our LyXText didn't do anything to the cursor
569 // then we try to put the cursor into the cell below
570 // setting also the right targetX.
571 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
572 cur.idx() = tabular.getCellBelow(cur.idx());
574 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
575 cur.pit(), 0, cur.targetX());
577 if (sl == cur.top()) {
578 // we trick it to go to the RIGHT after leaving the
580 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
587 cell(cur.idx())->dispatch(cur, cmd);
588 cur.dispatched(); // override the cell's decision
590 // if our LyXText didn't do anything to the cursor
591 // then we try to put the cursor into the cell above
592 // setting also the right targetX.
593 if (tabular.row_of_cell(cur.idx()) != 0) {
594 cur.idx() = tabular.getCellAbove(cur.idx());
595 cur.pit() = cur.lastpit();
596 LyXText const * text = cell(cur.idx())->getText(0);
597 cur.pos() = text->x2pos(
599 text->paragraphs().back().rows().size()-1,
602 if (sl == cur.top()) {
603 cmd = FuncRequest(LFUN_FINISHED_UP);
608 // case LFUN_SCREEN_DOWN: {
609 // //if (hasSelection())
610 // // cur.selection() = false;
611 // col_type const col = tabular.column_of_cell(cur.idx());
612 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
613 // if (t < yo() + tabular.getHeightOfTabular()) {
614 // cur.bv().scrollDocView(t);
615 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
617 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
624 // case LFUN_SCREEN_UP: {
625 // //if (hasSelection())
626 // // cur.selection() = false;
627 // col_type const col = tabular.column_of_cell(cur.idx());
628 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
630 // cur.bv().scrollDocView(t);
634 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
638 // cur.par() = cur.lastpar();
639 // cur.pos() = cur.lastpos();
643 case LFUN_LAYOUT_TABULAR:
644 InsetTabularMailer(*this).showDialog(&cur.bv());
647 case LFUN_INSET_DIALOG_UPDATE:
648 InsetTabularMailer(*this).updateDialog(&cur.bv());
651 case LFUN_TABULAR_FEATURE:
652 if (!tabularFeatures(cur, cmd.argument))
656 // insert file functions
657 case LFUN_FILE_INSERT_ASCII_PARA:
658 case LFUN_FILE_INSERT_ASCII: {
659 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
660 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
666 if (tablemode(cur)) {
667 if (copySelection(cur)) {
668 recordUndoInset(cur, Undo::DELETE);
673 cell(cur.idx())->dispatch(cur, cmd);
676 case LFUN_CHAR_DELETE_BACKWARD:
677 case LFUN_CHAR_DELETE_FORWARD:
678 if (tablemode(cur)) {
679 recordUndoInset(cur, Undo::DELETE);
683 cell(cur.idx())->dispatch(cur, cmd);
687 if (!cur.selection())
689 if (tablemode(cur)) {
693 cell(cur.idx())->dispatch(cur, cmd);
696 case LFUN_PRIMARY_SELECTION_PASTE: {
697 string const clip = cur.bv().getClipboard();
700 // pass to InsertAsciiString, but
701 // only if we have multi-cell content
702 if (clip.find_first_of("\t\n") != string::npos) {
703 if (insertAsciiString(cur.bv(), clip, false)) {
704 // content has been replaced,
705 // so cursor might be invalid
706 cur.pos() = cur.lastpos();
707 bvcur.setCursor(cur);
711 // so that the clipboard is used and it goes on
713 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
714 paste_tabular.reset();
715 dirtyTabularStack(false);
721 if (hasPasteBuffer() && tabularStackDirty()) {
722 recordUndoInset(cur, Undo::INSERT);
726 cell(cur.idx())->dispatch(cur, cmd);
727 // Reset pasted paragraphs:
728 if (tabular.getPWidth(cur.idx()).zero())
729 cell(cur.idx())->forceParagraphsToDefault(cur);
734 case LFUN_FONT_ROMAN:
740 case LFUN_FONT_FREE_APPLY:
741 case LFUN_FONT_FREE_UPDATE:
743 case LFUN_FONT_UNDERLINE:
745 case LFUN_WORD_CAPITALIZE:
746 case LFUN_WORD_UPCASE:
747 case LFUN_WORD_LOWCASE:
748 case LFUN_CHARS_TRANSPOSE:
749 if (tablemode(cur)) {
752 getSelection(cur, rs, re, cs, ce);
753 for (row_type i = rs; i <= re; ++i) {
754 for (col_type j = cs; j <= ce; ++j) {
755 // cursor follows cell:
756 cur.idx() = tabular.getCellNumber(i, j);
757 // select this cell only:
760 cur.pos() = cur.top().lastpos();
763 cell(cur.idx())->dispatch(cur, cmd);
766 // Restore original selection
767 cur.idx() = tabular.getCellNumber(rs, cs);
770 cur.idx() = tabular.getCellNumber(re, ce);
771 cur.pos() = cur.top().lastpos();
776 cell(cur.idx())->dispatch(cur, cmd);
780 // we try to handle this event in the insets dispatch function.
781 cell(cur.idx())->dispatch(cur, cmd);
786 InsetTabularMailer(*this).updateDialog(&cur.bv());
790 // function sets an object as defined in func_status.h:
791 // states OK, Unknown, Disabled, On, Off.
792 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
793 FuncStatus & status) const
795 switch (cmd.action) {
796 case LFUN_TABULAR_FEATURE: {
797 int action = LyXTabular::LAST_ACTION;
799 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
800 string const tmp = tabularFeature[i].feature;
801 if (tmp == cmd.argument.substr(0, tmp.length())) {
802 action = tabularFeature[i].action;
806 if (action == LyXTabular::LAST_ACTION) {
808 status.unknown(true);
812 string const argument
813 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
815 row_type sel_row_start = 0;
816 row_type sel_row_end = 0;
818 LyXTabular::ltType dummyltt;
821 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
824 case LyXTabular::SET_PWIDTH:
825 case LyXTabular::SET_MPWIDTH:
826 case LyXTabular::SET_SPECIAL_COLUMN:
827 case LyXTabular::SET_SPECIAL_MULTI:
828 case LyXTabular::APPEND_ROW:
829 case LyXTabular::APPEND_COLUMN:
830 case LyXTabular::DELETE_ROW:
831 case LyXTabular::DELETE_COLUMN:
832 case LyXTabular::SET_ALL_LINES:
833 case LyXTabular::UNSET_ALL_LINES:
837 case LyXTabular::MULTICOLUMN:
838 status.setOnOff(tabular.isMultiColumn(cur.idx()));
841 case LyXTabular::M_TOGGLE_LINE_TOP:
843 case LyXTabular::TOGGLE_LINE_TOP:
844 status.setOnOff(tabular.topLine(cur.idx(), flag));
847 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
849 case LyXTabular::TOGGLE_LINE_BOTTOM:
850 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
853 case LyXTabular::M_TOGGLE_LINE_LEFT:
855 case LyXTabular::TOGGLE_LINE_LEFT:
856 status.setOnOff(tabular.leftLine(cur.idx(), flag));
859 case LyXTabular::M_TOGGLE_LINE_RIGHT:
861 case LyXTabular::TOGGLE_LINE_RIGHT:
862 status.setOnOff(tabular.rightLine(cur.idx(), flag));
865 case LyXTabular::M_ALIGN_LEFT:
867 case LyXTabular::ALIGN_LEFT:
868 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
871 case LyXTabular::M_ALIGN_RIGHT:
873 case LyXTabular::ALIGN_RIGHT:
874 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
877 case LyXTabular::M_ALIGN_CENTER:
879 case LyXTabular::ALIGN_CENTER:
880 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
883 case LyXTabular::ALIGN_BLOCK:
884 status.enabled(!tabular.getPWidth(cur.idx()).zero());
885 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
888 case LyXTabular::M_VALIGN_TOP:
890 case LyXTabular::VALIGN_TOP:
892 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
895 case LyXTabular::M_VALIGN_BOTTOM:
897 case LyXTabular::VALIGN_BOTTOM:
899 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
902 case LyXTabular::M_VALIGN_MIDDLE:
904 case LyXTabular::VALIGN_MIDDLE:
906 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
909 case LyXTabular::SET_LONGTABULAR:
910 status.setOnOff(tabular.isLongTabular());
913 case LyXTabular::UNSET_LONGTABULAR:
914 status.setOnOff(!tabular.isLongTabular());
917 case LyXTabular::SET_ROTATE_TABULAR:
918 status.setOnOff(tabular.getRotateTabular());
921 case LyXTabular::UNSET_ROTATE_TABULAR:
922 status.setOnOff(!tabular.getRotateTabular());
925 case LyXTabular::SET_ROTATE_CELL:
926 status.setOnOff(tabular.getRotateCell(cur.idx()));
929 case LyXTabular::UNSET_ROTATE_CELL:
930 status.setOnOff(!tabular.getRotateCell(cur.idx()));
933 case LyXTabular::SET_USEBOX:
934 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
937 case LyXTabular::SET_LTFIRSTHEAD:
938 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
941 case LyXTabular::UNSET_LTFIRSTHEAD:
942 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
945 case LyXTabular::SET_LTHEAD:
946 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
949 case LyXTabular::UNSET_LTHEAD:
950 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
953 case LyXTabular::SET_LTFOOT:
954 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
957 case LyXTabular::UNSET_LTFOOT:
958 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
961 case LyXTabular::SET_LTLASTFOOT:
962 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
965 case LyXTabular::UNSET_LTLASTFOOT:
966 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
969 case LyXTabular::SET_LTNEWPAGE:
970 status.setOnOff(tabular.getLTNewPage(sel_row_start));
975 status.enabled(false);
981 // These are only enabled inside tabular
982 case LFUN_CELL_BACKWARD:
983 case LFUN_CELL_FORWARD:
984 status.enabled(true);
987 // disable these with multiple cells selected
988 case LFUN_INSET_INSERT:
989 case LFUN_TABULAR_INSERT:
990 case LFUN_CHARSTYLE_INSERT:
991 case LFUN_FLOAT_INSERT:
992 case LFUN_FLOAT_WIDE_INSERT:
993 case LFUN_FOOTNOTE_INSERT:
994 case LFUN_MARGINALNOTE_INSERT:
995 case LFUN_MATH_INSERT:
997 case LFUN_MATH_MUTATE:
998 case LFUN_MATH_DISPLAY:
999 case LFUN_NOTE_INSERT:
1000 case LFUN_OPTIONAL_INSERT:
1001 case LFUN_BOX_INSERT:
1002 case LFUN_BRANCH_INSERT:
1003 case LFUN_WRAP_INSERT:
1004 case LFUN_ERT_INSERT: {
1005 if (tablemode(cur)) {
1006 status.enabled(false);
1009 return cell(cur.idx())->getStatus(cur, cmd, status);
1012 // disable in non-fixed-width cells
1013 case LFUN_BREAK_LINE:
1014 case LFUN_BREAK_PARAGRAPH:
1015 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1016 case LFUN_BREAK_PARAGRAPH_SKIP: {
1017 if (tabular.getPWidth(cur.idx()).zero()) {
1018 status.enabled(false);
1021 return cell(cur.idx())->getStatus(cur, cmd, status);
1025 if (tabularStackDirty()) {
1026 status.enabled(true);
1030 case LFUN_INSET_MODIFY:
1031 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1032 status.enabled(true);
1038 // we try to handle this event in the insets dispatch function.
1039 return cell(cur.idx())->getStatus(cur, cmd, status);
1044 int InsetTabular::latex(Buffer const & buf, ostream & os,
1045 OutputParams const & runparams) const
1047 return tabular.latex(buf, os, runparams);
1051 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1052 OutputParams const & runparams) const
1054 int const dp = runparams.linelen ? runparams.depth : 0;
1055 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1059 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1060 OutputParams const & runparams) const
1062 return tabular.linuxdoc(buf,os, runparams);
1066 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1067 OutputParams const & runparams) const
1070 InsetBase * master = 0;
1072 #ifdef WITH_WARNINGS
1073 #warning Why not pass a proper DocIterator here?
1076 // if the table is inside a float it doesn't need the informaltable
1077 // wrapper. Search for it.
1078 for (master = owner(); master; master = master->owner())
1079 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1084 os << "<informaltable>";
1087 ret += tabular.docbook(buf, os, runparams);
1089 os << "</informaltable>";
1096 void InsetTabular::validate(LaTeXFeatures & features) const
1098 tabular.validate(features);
1102 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1104 return tabular.getCellInset(idx);
1108 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1110 return tabular.getCellInset(idx);
1114 void InsetTabular::cursorPos
1115 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1117 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1119 // y offset correction
1120 int const row = tabular.row_of_cell(sl.idx());
1121 for (int i = 0; i <= row; ++i) {
1123 y += tabular.getAscentOfRow(i);
1124 y += tabular.getAdditionalHeight(i);
1127 y += tabular.getDescentOfRow(i);
1130 // x offset correction
1131 int const col = tabular.column_of_cell(sl.idx());
1132 int idx = tabular.getCellNumber(row, 0);
1133 for (int j = 0; j < col; ++j) {
1134 if (tabular.isPartOfMultiColumn(row, j))
1136 x += tabular.getWidthOfColumn(idx);
1139 x += tabular.getBeginningOfTextInCell(idx);
1140 x += ADD_TO_TABULAR_WIDTH;
1145 int InsetTabular::dist(idx_type const cell, int x, int y) const
1149 InsetBase const & inset = *tabular.getCellInset(cell);
1150 Point o = theCoords.getInsets().xy(&inset);
1151 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1152 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1153 int const ybeg = o.y_ - inset.ascent();
1154 row_type const row = tabular.row_of_cell(cell);
1155 int const rowheight = tabular.getAscentOfRow(row)
1156 + tabular.getDescentOfRow(row);
1157 int const yend = ybeg + rowheight;
1169 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1170 // << " ybeg=" << ybeg << " yend=" << yend
1171 // << " xx=" << xx << " yy=" << yy
1172 // << " dist=" << xx + yy << endl;
1177 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1179 //lyxerr << "InsetTabular::editXY: " << this << endl;
1180 cur.selection() = false;
1182 cur.idx() = getNearestCell(x, y);
1184 return cell(cur.idx())->text_.editXY(cur, x, y);
1188 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1190 cur.idx() = getNearestCell(x, y);
1191 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1195 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1197 idx_type idx_min = 0;
1198 int dist_min = std::numeric_limits<int>::max();
1199 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1200 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1201 int const d = dist(i, x, y);
1212 int InsetTabular::getCellXPos(idx_type const cell) const
1216 for (; !tabular.isFirstCellInRow(c); --c)
1218 int lx = tabular.getWidthOfColumn(cell);
1219 for (; c < cell; ++c)
1220 lx += tabular.getWidthOfColumn(c);
1222 return lx - tabular.getWidthOfColumn(cell);
1226 void InsetTabular::resetPos(LCursor & cur) const
1228 BufferView & bv = cur.bv();
1229 int const maxwidth = bv.workWidth();
1231 if (&cur.inset() != this) {
1235 int const X2 = maxwidth;
1236 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1237 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1238 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1241 scx_ = X1 + 20 - x1;
1243 scx_ = X2 - 20 - x2;
1250 InsetTabularMailer(*this).updateDialog(&bv);
1254 void InsetTabular::moveNextCell(LCursor & cur)
1256 if (isRightToLeft(cur)) {
1257 if (tabular.isFirstCellInRow(cur.idx())) {
1258 row_type const row = tabular.row_of_cell(cur.idx());
1259 if (row == tabular.rows() - 1)
1261 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1268 if (tabular.isLastCell(cur.idx()))
1278 void InsetTabular::movePrevCell(LCursor & cur)
1280 if (isRightToLeft(cur)) {
1281 if (tabular.isLastCellInRow(cur.idx())) {
1282 row_type const row = tabular.row_of_cell(cur.idx());
1285 cur.idx() = tabular.getFirstCellInRow(row);
1286 cur.idx() = tabular.getCellAbove(cur.idx());
1288 if (tabular.isLastCell(cur.idx()))
1293 if (cur.idx() == 0) // first cell
1297 cur.pit() = cur.lastpit();
1298 cur.pos() = cur.lastpos();
1303 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1305 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1308 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1309 string const tmp = tabularFeature[i].feature;
1311 if (tmp == what.substr(0, tmp.length())) {
1312 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1313 //tabularFeatures[i].feature.length())) {
1314 action = tabularFeature[i].action;
1318 if (action == LyXTabular::LAST_ACTION)
1322 ltrim(what.substr(tabularFeature[i].feature.length()));
1323 tabularFeatures(cur, action, val);
1330 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1331 string const & special, bool & flag)
1333 if (special == "dl_above") {
1336 } else if (special == "dl_below") {
1337 ltt.bottomDL = flag;
1339 } else if (special == "empty") {
1351 void InsetTabular::tabularFeatures(LCursor & cur,
1352 LyXTabular::Feature feature, string const & value)
1354 BufferView & bv = cur.bv();
1355 col_type sel_col_start;
1356 col_type sel_col_end;
1357 row_type sel_row_start;
1358 row_type sel_row_end;
1359 bool setLines = false;
1360 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1361 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1365 case LyXTabular::M_ALIGN_LEFT:
1366 case LyXTabular::ALIGN_LEFT:
1367 setAlign = LYX_ALIGN_LEFT;
1370 case LyXTabular::M_ALIGN_RIGHT:
1371 case LyXTabular::ALIGN_RIGHT:
1372 setAlign = LYX_ALIGN_RIGHT;
1375 case LyXTabular::M_ALIGN_CENTER:
1376 case LyXTabular::ALIGN_CENTER:
1377 setAlign = LYX_ALIGN_CENTER;
1380 case LyXTabular::ALIGN_BLOCK:
1381 setAlign = LYX_ALIGN_BLOCK;
1384 case LyXTabular::M_VALIGN_TOP:
1385 case LyXTabular::VALIGN_TOP:
1386 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1389 case LyXTabular::M_VALIGN_BOTTOM:
1390 case LyXTabular::VALIGN_BOTTOM:
1391 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1394 case LyXTabular::M_VALIGN_MIDDLE:
1395 case LyXTabular::VALIGN_MIDDLE:
1396 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1403 recordUndoInset(cur, Undo::ATOMIC);
1405 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1406 row_type const row = tabular.row_of_cell(cur.idx());
1407 col_type const column = tabular.column_of_cell(cur.idx());
1409 LyXTabular::ltType ltt;
1413 case LyXTabular::SET_PWIDTH: {
1414 LyXLength const len(value);
1415 tabular.setColumnPWidth(cur, cur.idx(), len);
1417 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1418 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1422 case LyXTabular::SET_MPWIDTH:
1423 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1426 case LyXTabular::SET_SPECIAL_COLUMN:
1427 case LyXTabular::SET_SPECIAL_MULTI:
1428 tabular.setAlignSpecial(cur.idx(),value,feature);
1431 case LyXTabular::APPEND_ROW:
1432 // append the row into the tabular
1433 tabular.appendRow(bv.buffer()->params(), cur.idx());
1436 case LyXTabular::APPEND_COLUMN:
1437 // append the column into the tabular
1438 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1439 cur.idx() = tabular.getCellNumber(row, column);
1442 case LyXTabular::DELETE_ROW:
1443 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1444 tabular.deleteRow(sel_row_start);
1445 if (sel_row_start >= tabular.rows())
1447 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1450 cur.selection() = false;
1453 case LyXTabular::DELETE_COLUMN:
1454 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1455 tabular.deleteColumn(sel_col_start);
1456 if (sel_col_start >= tabular.columns())
1458 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1461 cur.selection() = false;
1464 case LyXTabular::M_TOGGLE_LINE_TOP:
1466 case LyXTabular::TOGGLE_LINE_TOP: {
1467 bool lineSet = !tabular.topLine(cur.idx(), flag);
1468 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1469 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1471 tabular.getCellNumber(i, j),
1476 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1478 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1479 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1480 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1481 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1482 tabular.setBottomLine(
1483 tabular.getCellNumber(i, j),
1489 case LyXTabular::M_TOGGLE_LINE_LEFT:
1491 case LyXTabular::TOGGLE_LINE_LEFT: {
1492 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1493 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1494 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1495 tabular.setLeftLine(
1496 tabular.getCellNumber(i,j),
1502 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1504 case LyXTabular::TOGGLE_LINE_RIGHT: {
1505 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1506 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1507 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508 tabular.setRightLine(
1509 tabular.getCellNumber(i,j),
1515 case LyXTabular::M_ALIGN_LEFT:
1516 case LyXTabular::M_ALIGN_RIGHT:
1517 case LyXTabular::M_ALIGN_CENTER:
1519 case LyXTabular::ALIGN_LEFT:
1520 case LyXTabular::ALIGN_RIGHT:
1521 case LyXTabular::ALIGN_CENTER:
1522 case LyXTabular::ALIGN_BLOCK:
1523 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1524 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1525 tabular.setAlignment(
1526 tabular.getCellNumber(i, j),
1531 case LyXTabular::M_VALIGN_TOP:
1532 case LyXTabular::M_VALIGN_BOTTOM:
1533 case LyXTabular::M_VALIGN_MIDDLE:
1535 case LyXTabular::VALIGN_TOP:
1536 case LyXTabular::VALIGN_BOTTOM:
1537 case LyXTabular::VALIGN_MIDDLE:
1538 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1539 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1540 tabular.setVAlignment(
1541 tabular.getCellNumber(i, j),
1545 case LyXTabular::MULTICOLUMN: {
1546 if (sel_row_start != sel_row_end) {
1547 #ifdef WITH_WARNINGS
1548 #warning Need I say it ? This is horrible.
1550 Alert::error(_("Error setting multicolumn"),
1551 _("You cannot set multicolumn vertically."));
1554 if (!cur.selection()) {
1555 // just multicol for one single cell
1556 // check whether we are completely in a multicol
1557 if (tabular.isMultiColumn(cur.idx()))
1558 tabular.unsetMultiColumn(cur.idx());
1560 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1563 // we have a selection so this means we just add all this
1564 // cells to form a multicolumn cell
1565 idx_type const s_start = cur.selBegin().idx();
1566 idx_type const s_end = cur.selEnd().idx();
1567 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1568 cur.idx() = s_start;
1571 cur.selection() = false;
1575 case LyXTabular::SET_ALL_LINES:
1577 case LyXTabular::UNSET_ALL_LINES:
1578 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1579 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1580 tabular.setAllLines(
1581 tabular.getCellNumber(i,j), setLines);
1584 case LyXTabular::SET_LONGTABULAR:
1585 tabular.setLongTabular(true);
1588 case LyXTabular::UNSET_LONGTABULAR:
1589 tabular.setLongTabular(false);
1592 case LyXTabular::SET_ROTATE_TABULAR:
1593 tabular.setRotateTabular(true);
1596 case LyXTabular::UNSET_ROTATE_TABULAR:
1597 tabular.setRotateTabular(false);
1600 case LyXTabular::SET_ROTATE_CELL:
1601 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1602 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1603 tabular.setRotateCell(
1604 tabular.getCellNumber(i, j), true);
1607 case LyXTabular::UNSET_ROTATE_CELL:
1608 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1609 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1610 tabular.setRotateCell(
1611 tabular.getCellNumber(i, j), false);
1614 case LyXTabular::SET_USEBOX: {
1615 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1616 if (val == tabular.getUsebox(cur.idx()))
1617 val = LyXTabular::BOX_NONE;
1618 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1619 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1620 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1624 case LyXTabular::UNSET_LTFIRSTHEAD:
1626 case LyXTabular::SET_LTFIRSTHEAD:
1627 tabular.getRowOfLTFirstHead(row, ltt);
1628 checkLongtableSpecial(ltt, value, flag);
1629 tabular.setLTHead(row, flag, ltt, true);
1632 case LyXTabular::UNSET_LTHEAD:
1634 case LyXTabular::SET_LTHEAD:
1635 tabular.getRowOfLTHead(row, ltt);
1636 checkLongtableSpecial(ltt, value, flag);
1637 tabular.setLTHead(row, flag, ltt, false);
1640 case LyXTabular::UNSET_LTFOOT:
1642 case LyXTabular::SET_LTFOOT:
1643 tabular.getRowOfLTFoot(row, ltt);
1644 checkLongtableSpecial(ltt, value, flag);
1645 tabular.setLTFoot(row, flag, ltt, false);
1648 case LyXTabular::UNSET_LTLASTFOOT:
1650 case LyXTabular::SET_LTLASTFOOT:
1651 tabular.getRowOfLTLastFoot(row, ltt);
1652 checkLongtableSpecial(ltt, value, flag);
1653 tabular.setLTFoot(row, flag, ltt, true);
1656 case LyXTabular::SET_LTNEWPAGE:
1657 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1660 // dummy stuff just to avoid warnings
1661 case LyXTabular::LAST_ACTION:
1665 InsetTabularMailer(*this).updateDialog(&bv);
1669 bool InsetTabular::showInsetDialog(BufferView * bv) const
1671 InsetTabularMailer(*this).showDialog(bv);
1676 void InsetTabular::openLayoutDialog(BufferView * bv) const
1678 InsetTabularMailer(*this).showDialog(bv);
1682 bool InsetTabular::copySelection(LCursor & cur)
1684 if (!cur.selection())
1689 getSelection(cur, rs, re, cs, ce);
1691 paste_tabular.reset(new LyXTabular(tabular));
1693 for (row_type i = 0; i < rs; ++i)
1694 paste_tabular->deleteRow(0);
1696 row_type const rows = re - rs + 1;
1697 while (paste_tabular->rows() > rows)
1698 paste_tabular->deleteRow(rows);
1700 paste_tabular->setTopLine(0, true, true);
1701 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1704 for (col_type i = 0; i < cs; ++i)
1705 paste_tabular->deleteColumn(0);
1707 col_type const columns = ce - cs + 1;
1708 while (paste_tabular->columns() > columns)
1709 paste_tabular->deleteColumn(columns);
1711 paste_tabular->setLeftLine(0, true, true);
1712 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1716 OutputParams const runparams;
1717 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1718 cur.bv().stuffClipboard(os.str());
1719 // mark tabular stack dirty
1720 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1721 // when we (hopefully) have a one-for-all paste mechanism.
1722 lyx::cap::dirtyTabularStack(true);
1728 bool InsetTabular::pasteSelection(LCursor & cur)
1732 col_type const actcol = tabular.column_of_cell(cur.idx());
1733 row_type const actrow = tabular.row_of_cell(cur.idx());
1734 for (row_type r1 = 0, r2 = actrow;
1735 r1 < paste_tabular->rows() && r2 < tabular.rows();
1737 for (col_type c1 = 0, c2 = actcol;
1738 c1 < paste_tabular->columns() && c2 < tabular.columns();
1740 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1741 tabular.isPartOfMultiColumn(r2, c2))
1743 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1747 if (tabular.isPartOfMultiColumn(r2, c2)) {
1751 shared_ptr<InsetText> inset(
1752 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1753 tabular.setCellInset(r2, c2, inset);
1762 void InsetTabular::cutSelection(LCursor & cur)
1764 if (!cur.selection())
1769 getSelection(cur, rs, re, cs, ce);
1770 for (row_type i = rs; i <= re; ++i) {
1771 for (col_type j = cs; j <= ce; ++j) {
1772 shared_ptr<InsetText> t
1773 = cell(tabular.getCellNumber(i, j));
1774 if (cur.buffer().params().tracking_changes)
1775 t->markErased(true);
1781 // cursor position might be invalid now
1782 cur.pos() = cur.lastpos();
1783 cur.clearSelection();
1787 bool InsetTabular::isRightToLeft(LCursor & cur) const
1789 BOOST_ASSERT(cur.depth() > 1);
1790 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1791 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1792 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1793 parentpos).language()->rightToLeft();
1797 void InsetTabular::getSelection(LCursor & cur,
1798 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1800 CursorSlice const & beg = cur.selBegin();
1801 CursorSlice const & end = cur.selEnd();
1802 cs = tabular.column_of_cell(beg.idx());
1803 ce = tabular.column_of_cell(end.idx());
1806 cs = tabular.column_of_cell(end.idx());
1808 ce = tabular.right_column_of_cell(end.idx());
1811 rs = tabular.row_of_cell(beg.idx());
1812 re = tabular.row_of_cell(end.idx());
1818 LyXText * InsetTabular::getText(int idx) const
1820 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1824 void InsetTabular::markErased(bool erased)
1826 for (idx_type idx = 0; idx < nargs(); ++idx)
1827 cell(idx)->markErased(erased);
1831 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1833 return tabular.getPWidth(cell).zero();
1837 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1840 if (buf.length() <= 0)
1845 col_type maxCols = 1;
1846 string::size_type const len = buf.length();
1847 string::size_type p = 0;
1849 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1857 maxCols = max(cols, maxCols);
1863 maxCols = max(cols, maxCols);
1864 LyXTabular * loctab;
1869 paste_tabular.reset(
1870 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1871 loctab = paste_tabular.get();
1873 dirtyTabularStack(true);
1876 cell = bv.cursor().idx();
1877 ocol = tabular.column_of_cell(cell);
1878 row = tabular.row_of_cell(cell);
1881 string::size_type op = 0;
1882 idx_type const cells = loctab->getNumberOfCells();
1885 rows = loctab->rows();
1886 col_type const columns = loctab->columns();
1888 while (cell < cells && p < len && row < rows &&
1889 (p = buf.find_first_of("\t\n", p)) != string::npos)
1895 // we can only set this if we are not too far right
1896 if (cols < columns) {
1897 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1898 inset->setViewCache(&bv);
1899 Paragraph & par = inset->text_.getPar(0);
1900 LyXFont const font = inset->text_.getFont(par, 0);
1901 inset->setText(buf.substr(op, p - op), font);
1907 // we can only set this if we are not too far right
1908 if (cols < columns) {
1909 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1910 inset->setViewCache(&bv);
1911 Paragraph & par = inset->text_.getPar(0);
1912 LyXFont const font = inset->text_.getFont(par, 0);
1913 inset->setText(buf.substr(op, p - op), font);
1918 cell = loctab->getCellNumber(row, cols);
1924 // check for the last cell if there is no trailing '\n'
1925 if (cell < cells && op < len) {
1926 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1927 inset->setViewCache(&bv);
1928 Paragraph & par = inset->text_.getPar(0);
1929 LyXFont const font = inset->text_.getFont(par, 0);
1930 inset->setText(buf.substr(op, len - op), font);
1936 void InsetTabular::addPreview(PreviewLoader & loader) const
1938 row_type const rows = tabular.rows();
1939 col_type const columns = tabular.columns();
1940 for (row_type i = 0; i < rows; ++i) {
1941 for (col_type j = 0; j < columns; ++j)
1942 tabular.getCellInset(i, j)->addPreview(loader);
1947 bool InsetTabular::tablemode(LCursor & cur) const
1949 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1956 string const InsetTabularMailer::name_("tabular");
1958 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1959 : inset_(const_cast<InsetTabular &>(inset))
1963 string const InsetTabularMailer::inset2string(Buffer const &) const
1965 return params2string(inset_);
1969 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1971 istringstream data(in);
1973 lex.setStream(data);
1980 if (!lex || token != name_)
1981 return print_mailer_error("InsetTabularMailer", in, 1,
1984 // This is part of the inset proper that is usually swallowed
1985 // by Buffer::readInset
1987 if (!lex || token != "Tabular")
1988 return print_mailer_error("InsetTabularMailer", in, 2,
1991 Buffer const & buffer = inset.buffer();
1992 inset.read(buffer, lex);
1996 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1999 data << name_ << ' ';
2000 inset.write(inset.buffer(), data);
2001 data << "\\end_inset\n";