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 // Paint background of current tabular
341 int const w = tabular.getWidthOfTabular();
342 int const h = tabular.getHeightOfTabular();
343 int yy = y - tabular.getAscentOfRow(0);
344 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
346 if (!cur.selection())
348 if (!ptr_cmp(&cur.inset(), this))
354 if (tablemode(cur)) {
357 getSelection(cur, rs, re, cs, ce);
358 y -= tabular.getAscentOfRow(0);
359 for (row_type j = 0; j < tabular.rows(); ++j) {
360 int const a = tabular.getAscentOfRow(j);
361 int const h = a + tabular.getDescentOfRow(j);
363 y += tabular.getAdditionalHeight(j);
364 for (col_type i = 0; i < tabular.columns(); ++i) {
365 if (tabular.isPartOfMultiColumn(j, i))
367 idx_type const cell =
368 tabular.getCellNumber(j, i);
369 int const w = tabular.getWidthOfColumn(cell);
370 if (i >= cs && i <= ce && j >= rs && j <= re)
371 pi.pain.fillRectangle(xx, y, w, h,
380 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
385 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
386 row_type row, idx_type cell, bool erased) const
388 int x2 = x + tabular.getWidthOfColumn(cell);
390 LColor::color col = LColor::tabularline;
391 LColor::color onoffcol = LColor::tabularonoffline;
394 col = LColor::strikeout;
395 onoffcol = LColor::strikeout;
398 if (!tabular.topAlreadyDrawn(cell)) {
399 on_off = !tabular.topLine(cell);
400 pain.line(x, y - tabular.getAscentOfRow(row),
401 x2, y - tabular.getAscentOfRow(row),
402 on_off ? onoffcol : col,
403 on_off ? Painter::line_onoffdash : Painter::line_solid);
405 on_off = !tabular.bottomLine(cell);
406 pain.line(x, y + tabular.getDescentOfRow(row),
407 x2, y + tabular.getDescentOfRow(row),
408 on_off ? onoffcol : col,
409 on_off ? Painter::line_onoffdash : Painter::line_solid);
410 if (!tabular.leftAlreadyDrawn(cell)) {
411 on_off = !tabular.leftLine(cell);
412 pain.line(x, y - tabular.getAscentOfRow(row),
413 x, y + tabular.getDescentOfRow(row),
414 on_off ? onoffcol : col,
415 on_off ? Painter::line_onoffdash : Painter::line_solid);
417 on_off = !tabular.rightLine(cell);
418 pain.line(x2 - tabular.getAdditionalWidth(cell),
419 y - tabular.getAscentOfRow(row),
420 x2 - tabular.getAdditionalWidth(cell),
421 y + tabular.getDescentOfRow(row),
422 on_off ? onoffcol : col,
423 on_off ? Painter::line_onoffdash : Painter::line_solid);
427 string const InsetTabular::editMessage() const
429 return _("Opened table");
433 void InsetTabular::edit(LCursor & cur, bool left)
435 //lyxerr << "InsetTabular::edit: " << this << endl;
437 cur.selection() = false;
440 if (isRightToLeft(cur))
441 cur.idx() = tabular.getLastCellInRow(0);
447 if (isRightToLeft(cur))
448 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
450 cur.idx() = tabular.getNumberOfCells() - 1;
452 cur.pos() = cur.lastpos(); // FIXME crude guess
454 // this accesses the position cache before it is initialized
456 //cur.bv().fitCursor();
460 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
462 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
463 << "\n cur:" << cur << endl;
464 CursorSlice sl = cur.top();
465 LCursor & bvcur = cur.bv().cursor();
467 switch (cmd.action) {
469 case LFUN_MOUSE_PRESS:
470 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
472 if (cmd.button() == mouse_button::button1
473 || cmd.button() == mouse_button::button3) {
474 cur.selection() = false;
475 setCursorFromCoordinates(cur, cmd.x, cmd.y);
481 if (cmd.button() == mouse_button::button2) {
482 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
483 doDispatch(cur, cmd);
487 case LFUN_MOUSE_MOTION:
488 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
489 if (cmd.button() == mouse_button::button1) {
490 // only accept motions to places not deeper nested than the real anchor
491 if (bvcur.anchor_.hasPart(cur)) {
492 setCursorFromCoordinates(cur, cmd.x, cmd.y);
493 bvcur.setCursor(cur);
494 bvcur.selection() = true;
500 case LFUN_MOUSE_RELEASE:
501 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
502 if (cmd.button() == mouse_button::button3)
503 InsetTabularMailer(*this).showDialog(&cur.bv());
506 case LFUN_CELL_BACKWARD:
508 cur.selection() = false;
511 case LFUN_CELL_FORWARD:
513 cur.selection() = false;
516 case LFUN_CHAR_FORWARD_SELECT:
517 case LFUN_CHAR_FORWARD:
518 cell(cur.idx())->dispatch(cur, cmd);
519 if (!cur.result().dispatched()) {
520 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
522 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
528 case LFUN_CHAR_BACKWARD_SELECT:
529 case LFUN_CHAR_BACKWARD:
530 cell(cur.idx())->dispatch(cur, cmd);
531 if (!cur.result().dispatched()) {
532 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
534 cmd = FuncRequest(LFUN_FINISHED_LEFT);
540 case LFUN_DOWN_SELECT:
542 cell(cur.idx())->dispatch(cur, cmd);
543 cur.dispatched(); // override the cell's decision
545 // if our LyXText didn't do anything to the cursor
546 // then we try to put the cursor into the cell below
547 // setting also the right targetX.
548 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
549 cur.idx() = tabular.getCellBelow(cur.idx());
551 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
552 cur.pit(), 0, cur.targetX());
554 if (sl == cur.top()) {
555 // we trick it to go to the RIGHT after leaving the
557 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
564 cell(cur.idx())->dispatch(cur, cmd);
565 cur.dispatched(); // override the cell's decision
567 // if our LyXText didn't do anything to the cursor
568 // then we try to put the cursor into the cell above
569 // setting also the right targetX.
570 if (tabular.row_of_cell(cur.idx()) != 0) {
571 cur.idx() = tabular.getCellAbove(cur.idx());
572 cur.pit() = cur.lastpit();
573 LyXText const * text = cell(cur.idx())->getText(0);
574 cur.pos() = text->x2pos(
576 text->paragraphs().back().rows().size()-1,
579 if (sl == cur.top()) {
580 cmd = FuncRequest(LFUN_FINISHED_UP);
585 // case LFUN_SCREEN_DOWN: {
586 // //if (hasSelection())
587 // // cur.selection() = false;
588 // col_type const col = tabular.column_of_cell(cur.idx());
589 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
590 // if (t < yo() + tabular.getHeightOfTabular()) {
591 // cur.bv().scrollDocView(t);
592 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
594 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
601 // case LFUN_SCREEN_UP: {
602 // //if (hasSelection())
603 // // cur.selection() = false;
604 // col_type const col = tabular.column_of_cell(cur.idx());
605 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
607 // cur.bv().scrollDocView(t);
611 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
615 // cur.par() = cur.lastpar();
616 // cur.pos() = cur.lastpos();
620 case LFUN_LAYOUT_TABULAR:
621 InsetTabularMailer(*this).showDialog(&cur.bv());
624 case LFUN_INSET_DIALOG_UPDATE:
625 InsetTabularMailer(*this).updateDialog(&cur.bv());
628 case LFUN_TABULAR_FEATURE:
629 if (!tabularFeatures(cur, cmd.argument))
633 // insert file functions
634 case LFUN_FILE_INSERT_ASCII_PARA:
635 case LFUN_FILE_INSERT_ASCII: {
636 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
637 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
643 if (tablemode(cur)) {
644 if (copySelection(cur)) {
645 recordUndoInset(cur, Undo::DELETE);
650 cell(cur.idx())->dispatch(cur, cmd);
653 case LFUN_CHAR_DELETE_BACKWARD:
654 case LFUN_CHAR_DELETE_FORWARD:
655 if (tablemode(cur)) {
656 recordUndoInset(cur, Undo::DELETE);
660 cell(cur.idx())->dispatch(cur, cmd);
664 if (!cur.selection())
666 if (tablemode(cur)) {
670 cell(cur.idx())->dispatch(cur, cmd);
673 case LFUN_PRIMARY_SELECTION_PASTE: {
674 string const clip = cur.bv().getClipboard();
677 // pass to InsertAsciiString, but
678 // only if we have multi-cell content
679 if (clip.find_first_of("\t\n") != string::npos) {
680 if (insertAsciiString(cur.bv(), clip, false)) {
681 // content has been replaced,
682 // so cursor might be invalid
683 cur.pos() = cur.lastpos();
684 bvcur.setCursor(cur);
688 // so that the clipboard is used and it goes on
690 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
691 paste_tabular.reset();
692 dirtyTabularStack(false);
698 if (hasPasteBuffer() && tabularStackDirty()) {
699 recordUndoInset(cur, Undo::INSERT);
703 cell(cur.idx())->dispatch(cur, cmd);
704 // Reset pasted paragraphs:
705 if (tabular.getPWidth(cur.idx()).zero())
706 cell(cur.idx())->forceParagraphsToDefault(cur);
711 case LFUN_FONT_ROMAN:
717 case LFUN_FONT_FREE_APPLY:
718 case LFUN_FONT_FREE_UPDATE:
720 case LFUN_FONT_UNDERLINE:
722 case LFUN_WORD_CAPITALIZE:
723 case LFUN_WORD_UPCASE:
724 case LFUN_WORD_LOWCASE:
725 case LFUN_CHARS_TRANSPOSE:
726 if (tablemode(cur)) {
729 getSelection(cur, rs, re, cs, ce);
730 for (row_type i = rs; i <= re; ++i) {
731 for (col_type j = cs; j <= ce; ++j) {
732 // cursor follows cell:
733 cur.idx() = tabular.getCellNumber(i, j);
734 // select this cell only:
737 cur.pos() = cur.top().lastpos();
740 cell(cur.idx())->dispatch(cur, cmd);
743 // Restore original selection
744 cur.idx() = tabular.getCellNumber(rs, cs);
747 cur.idx() = tabular.getCellNumber(re, ce);
748 cur.pos() = cur.top().lastpos();
753 cell(cur.idx())->dispatch(cur, cmd);
757 // we try to handle this event in the insets dispatch function.
758 cell(cur.idx())->dispatch(cur, cmd);
763 InsetTabularMailer(*this).updateDialog(&cur.bv());
767 // function sets an object as defined in func_status.h:
768 // states OK, Unknown, Disabled, On, Off.
769 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
770 FuncStatus & status) const
772 switch (cmd.action) {
773 case LFUN_TABULAR_FEATURE: {
774 int action = LyXTabular::LAST_ACTION;
776 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
777 string const tmp = tabularFeature[i].feature;
778 if (tmp == cmd.argument.substr(0, tmp.length())) {
779 action = tabularFeature[i].action;
783 if (action == LyXTabular::LAST_ACTION) {
785 status.unknown(true);
789 string const argument
790 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
792 row_type sel_row_start = 0;
793 row_type sel_row_end = 0;
795 LyXTabular::ltType dummyltt;
798 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
801 case LyXTabular::SET_PWIDTH:
802 case LyXTabular::SET_MPWIDTH:
803 case LyXTabular::SET_SPECIAL_COLUMN:
804 case LyXTabular::SET_SPECIAL_MULTI:
805 case LyXTabular::APPEND_ROW:
806 case LyXTabular::APPEND_COLUMN:
807 case LyXTabular::DELETE_ROW:
808 case LyXTabular::DELETE_COLUMN:
809 case LyXTabular::SET_ALL_LINES:
810 case LyXTabular::UNSET_ALL_LINES:
814 case LyXTabular::MULTICOLUMN:
815 status.setOnOff(tabular.isMultiColumn(cur.idx()));
818 case LyXTabular::M_TOGGLE_LINE_TOP:
820 case LyXTabular::TOGGLE_LINE_TOP:
821 status.setOnOff(tabular.topLine(cur.idx(), flag));
824 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
826 case LyXTabular::TOGGLE_LINE_BOTTOM:
827 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
830 case LyXTabular::M_TOGGLE_LINE_LEFT:
832 case LyXTabular::TOGGLE_LINE_LEFT:
833 status.setOnOff(tabular.leftLine(cur.idx(), flag));
836 case LyXTabular::M_TOGGLE_LINE_RIGHT:
838 case LyXTabular::TOGGLE_LINE_RIGHT:
839 status.setOnOff(tabular.rightLine(cur.idx(), flag));
842 case LyXTabular::M_ALIGN_LEFT:
844 case LyXTabular::ALIGN_LEFT:
845 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
848 case LyXTabular::M_ALIGN_RIGHT:
850 case LyXTabular::ALIGN_RIGHT:
851 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
854 case LyXTabular::M_ALIGN_CENTER:
856 case LyXTabular::ALIGN_CENTER:
857 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
860 case LyXTabular::ALIGN_BLOCK:
861 status.enabled(!tabular.getPWidth(cur.idx()).zero());
862 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
865 case LyXTabular::M_VALIGN_TOP:
867 case LyXTabular::VALIGN_TOP:
869 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
872 case LyXTabular::M_VALIGN_BOTTOM:
874 case LyXTabular::VALIGN_BOTTOM:
876 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
879 case LyXTabular::M_VALIGN_MIDDLE:
881 case LyXTabular::VALIGN_MIDDLE:
883 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
886 case LyXTabular::SET_LONGTABULAR:
887 status.setOnOff(tabular.isLongTabular());
890 case LyXTabular::UNSET_LONGTABULAR:
891 status.setOnOff(!tabular.isLongTabular());
894 case LyXTabular::SET_ROTATE_TABULAR:
895 status.setOnOff(tabular.getRotateTabular());
898 case LyXTabular::UNSET_ROTATE_TABULAR:
899 status.setOnOff(!tabular.getRotateTabular());
902 case LyXTabular::SET_ROTATE_CELL:
903 status.setOnOff(tabular.getRotateCell(cur.idx()));
906 case LyXTabular::UNSET_ROTATE_CELL:
907 status.setOnOff(!tabular.getRotateCell(cur.idx()));
910 case LyXTabular::SET_USEBOX:
911 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
914 case LyXTabular::SET_LTFIRSTHEAD:
915 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
918 case LyXTabular::UNSET_LTFIRSTHEAD:
919 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
922 case LyXTabular::SET_LTHEAD:
923 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
926 case LyXTabular::UNSET_LTHEAD:
927 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
930 case LyXTabular::SET_LTFOOT:
931 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
934 case LyXTabular::UNSET_LTFOOT:
935 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
938 case LyXTabular::SET_LTLASTFOOT:
939 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
942 case LyXTabular::UNSET_LTLASTFOOT:
943 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
946 case LyXTabular::SET_LTNEWPAGE:
947 status.setOnOff(tabular.getLTNewPage(sel_row_start));
952 status.enabled(false);
958 // These are only enabled inside tabular
959 case LFUN_CELL_BACKWARD:
960 case LFUN_CELL_FORWARD:
961 status.enabled(true);
964 // disable these with multiple cells selected
965 case LFUN_INSET_INSERT:
966 case LFUN_TABULAR_INSERT:
967 case LFUN_CHARSTYLE_INSERT:
968 case LFUN_FLOAT_INSERT:
969 case LFUN_FLOAT_WIDE_INSERT:
970 case LFUN_FOOTNOTE_INSERT:
971 case LFUN_MARGINALNOTE_INSERT:
972 case LFUN_MATH_INSERT:
974 case LFUN_MATH_MUTATE:
975 case LFUN_MATH_DISPLAY:
976 case LFUN_NOTE_INSERT:
977 case LFUN_OPTIONAL_INSERT:
978 case LFUN_BOX_INSERT:
979 case LFUN_BRANCH_INSERT:
980 case LFUN_WRAP_INSERT:
981 case LFUN_ERT_INSERT: {
982 if (tablemode(cur)) {
983 status.enabled(false);
986 return cell(cur.idx())->getStatus(cur, cmd, status);
989 // disable in non-fixed-width cells
990 case LFUN_BREAK_LINE:
991 case LFUN_BREAK_PARAGRAPH:
992 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
993 case LFUN_BREAK_PARAGRAPH_SKIP: {
994 if (tabular.getPWidth(cur.idx()).zero()) {
995 status.enabled(false);
998 return cell(cur.idx())->getStatus(cur, cmd, status);
1002 if (tabularStackDirty()) {
1003 status.enabled(true);
1007 case LFUN_INSET_MODIFY:
1008 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1009 status.enabled(true);
1015 // we try to handle this event in the insets dispatch function.
1016 return cell(cur.idx())->getStatus(cur, cmd, status);
1021 int InsetTabular::latex(Buffer const & buf, ostream & os,
1022 OutputParams const & runparams) const
1024 return tabular.latex(buf, os, runparams);
1028 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1029 OutputParams const & runparams) const
1031 int const dp = runparams.linelen ? runparams.depth : 0;
1032 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1036 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1037 OutputParams const & runparams) const
1039 return tabular.linuxdoc(buf,os, runparams);
1043 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1044 OutputParams const & runparams) const
1047 InsetBase * master = 0;
1049 #ifdef WITH_WARNINGS
1050 #warning Why not pass a proper DocIterator here?
1053 // if the table is inside a float it doesn't need the informaltable
1054 // wrapper. Search for it.
1055 for (master = owner(); master; master = master->owner())
1056 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1061 os << "<informaltable>";
1064 ret += tabular.docbook(buf, os, runparams);
1066 os << "</informaltable>";
1073 void InsetTabular::validate(LaTeXFeatures & features) const
1075 tabular.validate(features);
1079 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1081 return tabular.getCellInset(idx);
1085 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1087 return tabular.getCellInset(idx);
1091 void InsetTabular::cursorPos
1092 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1094 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1096 // y offset correction
1097 int const row = tabular.row_of_cell(sl.idx());
1098 for (int i = 0; i <= row; ++i) {
1100 y += tabular.getAscentOfRow(i);
1101 y += tabular.getAdditionalHeight(i);
1104 y += tabular.getDescentOfRow(i);
1107 // x offset correction
1108 int const col = tabular.column_of_cell(sl.idx());
1109 int idx = tabular.getCellNumber(row, 0);
1110 for (int j = 0; j < col; ++j) {
1111 if (tabular.isPartOfMultiColumn(row, j))
1113 x += tabular.getWidthOfColumn(idx);
1116 x += tabular.getBeginningOfTextInCell(idx);
1117 x += ADD_TO_TABULAR_WIDTH;
1122 int InsetTabular::dist(idx_type const cell, int x, int y) const
1126 InsetBase const & inset = *tabular.getCellInset(cell);
1127 Point o = theCoords.getInsets().xy(&inset);
1128 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1129 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1130 int const ybeg = o.y_ - inset.ascent();
1131 row_type const row = tabular.row_of_cell(cell);
1132 int const rowheight = tabular.getAscentOfRow(row)
1133 + tabular.getDescentOfRow(row);
1134 int const yend = ybeg + rowheight;
1146 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1147 // << " ybeg=" << ybeg << " yend=" << yend
1148 // << " xx=" << xx << " yy=" << yy
1149 // << " dist=" << xx + yy << endl;
1154 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1156 //lyxerr << "InsetTabular::editXY: " << this << endl;
1157 cur.selection() = false;
1159 cur.idx() = getNearestCell(x, y);
1161 return cell(cur.idx())->text_.editXY(cur, x, y);
1165 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1167 cur.idx() = getNearestCell(x, y);
1168 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1172 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1174 idx_type idx_min = 0;
1175 int dist_min = std::numeric_limits<int>::max();
1176 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1177 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1178 int const d = dist(i, x, y);
1189 int InsetTabular::getCellXPos(idx_type const cell) const
1193 for (; !tabular.isFirstCellInRow(c); --c)
1195 int lx = tabular.getWidthOfColumn(cell);
1196 for (; c < cell; ++c)
1197 lx += tabular.getWidthOfColumn(c);
1199 return lx - tabular.getWidthOfColumn(cell);
1203 void InsetTabular::resetPos(LCursor & cur) const
1205 BufferView & bv = cur.bv();
1206 int const maxwidth = bv.workWidth();
1208 if (&cur.inset() != this) {
1212 int const X2 = maxwidth;
1213 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1214 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1215 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1218 scx_ = X1 + 20 - x1;
1220 scx_ = X2 - 20 - x2;
1227 InsetTabularMailer(*this).updateDialog(&bv);
1231 void InsetTabular::moveNextCell(LCursor & cur)
1233 if (isRightToLeft(cur)) {
1234 if (tabular.isFirstCellInRow(cur.idx())) {
1235 row_type const row = tabular.row_of_cell(cur.idx());
1236 if (row == tabular.rows() - 1)
1238 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1245 if (tabular.isLastCell(cur.idx()))
1255 void InsetTabular::movePrevCell(LCursor & cur)
1257 if (isRightToLeft(cur)) {
1258 if (tabular.isLastCellInRow(cur.idx())) {
1259 row_type const row = tabular.row_of_cell(cur.idx());
1262 cur.idx() = tabular.getFirstCellInRow(row);
1263 cur.idx() = tabular.getCellAbove(cur.idx());
1265 if (tabular.isLastCell(cur.idx()))
1270 if (cur.idx() == 0) // first cell
1274 cur.pit() = cur.lastpit();
1275 cur.pos() = cur.lastpos();
1280 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1282 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1285 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1286 string const tmp = tabularFeature[i].feature;
1288 if (tmp == what.substr(0, tmp.length())) {
1289 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1290 //tabularFeatures[i].feature.length())) {
1291 action = tabularFeature[i].action;
1295 if (action == LyXTabular::LAST_ACTION)
1299 ltrim(what.substr(tabularFeature[i].feature.length()));
1300 tabularFeatures(cur, action, val);
1307 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1308 string const & special, bool & flag)
1310 if (special == "dl_above") {
1313 } else if (special == "dl_below") {
1314 ltt.bottomDL = flag;
1316 } else if (special == "empty") {
1328 void InsetTabular::tabularFeatures(LCursor & cur,
1329 LyXTabular::Feature feature, string const & value)
1331 BufferView & bv = cur.bv();
1332 col_type sel_col_start;
1333 col_type sel_col_end;
1334 row_type sel_row_start;
1335 row_type sel_row_end;
1336 bool setLines = false;
1337 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1338 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1342 case LyXTabular::M_ALIGN_LEFT:
1343 case LyXTabular::ALIGN_LEFT:
1344 setAlign = LYX_ALIGN_LEFT;
1347 case LyXTabular::M_ALIGN_RIGHT:
1348 case LyXTabular::ALIGN_RIGHT:
1349 setAlign = LYX_ALIGN_RIGHT;
1352 case LyXTabular::M_ALIGN_CENTER:
1353 case LyXTabular::ALIGN_CENTER:
1354 setAlign = LYX_ALIGN_CENTER;
1357 case LyXTabular::ALIGN_BLOCK:
1358 setAlign = LYX_ALIGN_BLOCK;
1361 case LyXTabular::M_VALIGN_TOP:
1362 case LyXTabular::VALIGN_TOP:
1363 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1366 case LyXTabular::M_VALIGN_BOTTOM:
1367 case LyXTabular::VALIGN_BOTTOM:
1368 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1371 case LyXTabular::M_VALIGN_MIDDLE:
1372 case LyXTabular::VALIGN_MIDDLE:
1373 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1380 recordUndoInset(cur, Undo::ATOMIC);
1382 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1383 row_type const row = tabular.row_of_cell(cur.idx());
1384 col_type const column = tabular.column_of_cell(cur.idx());
1386 LyXTabular::ltType ltt;
1390 case LyXTabular::SET_PWIDTH: {
1391 LyXLength const len(value);
1392 tabular.setColumnPWidth(cur, cur.idx(), len);
1394 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1395 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1399 case LyXTabular::SET_MPWIDTH:
1400 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1403 case LyXTabular::SET_SPECIAL_COLUMN:
1404 case LyXTabular::SET_SPECIAL_MULTI:
1405 tabular.setAlignSpecial(cur.idx(),value,feature);
1408 case LyXTabular::APPEND_ROW:
1409 // append the row into the tabular
1410 tabular.appendRow(bv.buffer()->params(), cur.idx());
1413 case LyXTabular::APPEND_COLUMN:
1414 // append the column into the tabular
1415 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1416 cur.idx() = tabular.getCellNumber(row, column);
1419 case LyXTabular::DELETE_ROW:
1420 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1421 tabular.deleteRow(sel_row_start);
1422 if (sel_row_start >= tabular.rows())
1424 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1427 cur.selection() = false;
1430 case LyXTabular::DELETE_COLUMN:
1431 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1432 tabular.deleteColumn(sel_col_start);
1433 if (sel_col_start >= tabular.columns())
1435 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1438 cur.selection() = false;
1441 case LyXTabular::M_TOGGLE_LINE_TOP:
1443 case LyXTabular::TOGGLE_LINE_TOP: {
1444 bool lineSet = !tabular.topLine(cur.idx(), flag);
1445 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1446 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1448 tabular.getCellNumber(i, j),
1453 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1455 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1456 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1457 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1458 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1459 tabular.setBottomLine(
1460 tabular.getCellNumber(i, j),
1466 case LyXTabular::M_TOGGLE_LINE_LEFT:
1468 case LyXTabular::TOGGLE_LINE_LEFT: {
1469 bool lineSet = !tabular.leftLine(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)
1472 tabular.setLeftLine(
1473 tabular.getCellNumber(i,j),
1479 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1481 case LyXTabular::TOGGLE_LINE_RIGHT: {
1482 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1483 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1484 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1485 tabular.setRightLine(
1486 tabular.getCellNumber(i,j),
1492 case LyXTabular::M_ALIGN_LEFT:
1493 case LyXTabular::M_ALIGN_RIGHT:
1494 case LyXTabular::M_ALIGN_CENTER:
1496 case LyXTabular::ALIGN_LEFT:
1497 case LyXTabular::ALIGN_RIGHT:
1498 case LyXTabular::ALIGN_CENTER:
1499 case LyXTabular::ALIGN_BLOCK:
1500 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1501 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1502 tabular.setAlignment(
1503 tabular.getCellNumber(i, j),
1508 case LyXTabular::M_VALIGN_TOP:
1509 case LyXTabular::M_VALIGN_BOTTOM:
1510 case LyXTabular::M_VALIGN_MIDDLE:
1512 case LyXTabular::VALIGN_TOP:
1513 case LyXTabular::VALIGN_BOTTOM:
1514 case LyXTabular::VALIGN_MIDDLE:
1515 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1516 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1517 tabular.setVAlignment(
1518 tabular.getCellNumber(i, j),
1522 case LyXTabular::MULTICOLUMN: {
1523 if (sel_row_start != sel_row_end) {
1524 #ifdef WITH_WARNINGS
1525 #warning Need I say it ? This is horrible.
1527 Alert::error(_("Error setting multicolumn"),
1528 _("You cannot set multicolumn vertically."));
1531 if (!cur.selection()) {
1532 // just multicol for one single cell
1533 // check whether we are completely in a multicol
1534 if (tabular.isMultiColumn(cur.idx()))
1535 tabular.unsetMultiColumn(cur.idx());
1537 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1540 // we have a selection so this means we just add all this
1541 // cells to form a multicolumn cell
1542 idx_type const s_start = cur.selBegin().idx();
1543 idx_type const s_end = cur.selEnd().idx();
1544 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1545 cur.idx() = s_start;
1548 cur.selection() = false;
1552 case LyXTabular::SET_ALL_LINES:
1554 case LyXTabular::UNSET_ALL_LINES:
1555 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1556 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1557 tabular.setAllLines(
1558 tabular.getCellNumber(i,j), setLines);
1561 case LyXTabular::SET_LONGTABULAR:
1562 tabular.setLongTabular(true);
1565 case LyXTabular::UNSET_LONGTABULAR:
1566 tabular.setLongTabular(false);
1569 case LyXTabular::SET_ROTATE_TABULAR:
1570 tabular.setRotateTabular(true);
1573 case LyXTabular::UNSET_ROTATE_TABULAR:
1574 tabular.setRotateTabular(false);
1577 case LyXTabular::SET_ROTATE_CELL:
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.setRotateCell(
1581 tabular.getCellNumber(i, j), true);
1584 case LyXTabular::UNSET_ROTATE_CELL:
1585 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1586 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1587 tabular.setRotateCell(
1588 tabular.getCellNumber(i, j), false);
1591 case LyXTabular::SET_USEBOX: {
1592 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1593 if (val == tabular.getUsebox(cur.idx()))
1594 val = LyXTabular::BOX_NONE;
1595 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1596 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1597 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1601 case LyXTabular::UNSET_LTFIRSTHEAD:
1603 case LyXTabular::SET_LTFIRSTHEAD:
1604 tabular.getRowOfLTFirstHead(row, ltt);
1605 checkLongtableSpecial(ltt, value, flag);
1606 tabular.setLTHead(row, flag, ltt, true);
1609 case LyXTabular::UNSET_LTHEAD:
1611 case LyXTabular::SET_LTHEAD:
1612 tabular.getRowOfLTHead(row, ltt);
1613 checkLongtableSpecial(ltt, value, flag);
1614 tabular.setLTHead(row, flag, ltt, false);
1617 case LyXTabular::UNSET_LTFOOT:
1619 case LyXTabular::SET_LTFOOT:
1620 tabular.getRowOfLTFoot(row, ltt);
1621 checkLongtableSpecial(ltt, value, flag);
1622 tabular.setLTFoot(row, flag, ltt, false);
1625 case LyXTabular::UNSET_LTLASTFOOT:
1627 case LyXTabular::SET_LTLASTFOOT:
1628 tabular.getRowOfLTLastFoot(row, ltt);
1629 checkLongtableSpecial(ltt, value, flag);
1630 tabular.setLTFoot(row, flag, ltt, true);
1633 case LyXTabular::SET_LTNEWPAGE:
1634 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1637 // dummy stuff just to avoid warnings
1638 case LyXTabular::LAST_ACTION:
1642 InsetTabularMailer(*this).updateDialog(&bv);
1646 bool InsetTabular::showInsetDialog(BufferView * bv) const
1648 InsetTabularMailer(*this).showDialog(bv);
1653 void InsetTabular::openLayoutDialog(BufferView * bv) const
1655 InsetTabularMailer(*this).showDialog(bv);
1659 bool InsetTabular::copySelection(LCursor & cur)
1661 if (!cur.selection())
1666 getSelection(cur, rs, re, cs, ce);
1668 paste_tabular.reset(new LyXTabular(tabular));
1670 for (row_type i = 0; i < rs; ++i)
1671 paste_tabular->deleteRow(0);
1673 row_type const rows = re - rs + 1;
1674 while (paste_tabular->rows() > rows)
1675 paste_tabular->deleteRow(rows);
1677 paste_tabular->setTopLine(0, true, true);
1678 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1681 for (col_type i = 0; i < cs; ++i)
1682 paste_tabular->deleteColumn(0);
1684 col_type const columns = ce - cs + 1;
1685 while (paste_tabular->columns() > columns)
1686 paste_tabular->deleteColumn(columns);
1688 paste_tabular->setLeftLine(0, true, true);
1689 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1693 OutputParams const runparams;
1694 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1695 cur.bv().stuffClipboard(os.str());
1696 // mark tabular stack dirty
1697 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1698 // when we (hopefully) have a one-for-all paste mechanism.
1699 lyx::cap::dirtyTabularStack(true);
1705 bool InsetTabular::pasteSelection(LCursor & cur)
1709 col_type const actcol = tabular.column_of_cell(cur.idx());
1710 row_type const actrow = tabular.row_of_cell(cur.idx());
1711 for (row_type r1 = 0, r2 = actrow;
1712 r1 < paste_tabular->rows() && r2 < tabular.rows();
1714 for (col_type c1 = 0, c2 = actcol;
1715 c1 < paste_tabular->columns() && c2 < tabular.columns();
1717 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1718 tabular.isPartOfMultiColumn(r2, c2))
1720 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1724 if (tabular.isPartOfMultiColumn(r2, c2)) {
1728 shared_ptr<InsetText> inset(
1729 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1730 tabular.setCellInset(r2, c2, inset);
1739 void InsetTabular::cutSelection(LCursor & cur)
1741 if (!cur.selection())
1746 getSelection(cur, rs, re, cs, ce);
1747 for (row_type i = rs; i <= re; ++i) {
1748 for (col_type j = cs; j <= ce; ++j) {
1749 shared_ptr<InsetText> t
1750 = cell(tabular.getCellNumber(i, j));
1751 if (cur.buffer().params().tracking_changes)
1752 t->markErased(true);
1758 // cursor position might be invalid now
1759 cur.pos() = cur.lastpos();
1760 cur.clearSelection();
1764 bool InsetTabular::isRightToLeft(LCursor & cur) const
1766 BOOST_ASSERT(cur.depth() > 1);
1767 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1768 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1769 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1770 parentpos).language()->rightToLeft();
1774 void InsetTabular::getSelection(LCursor & cur,
1775 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1777 CursorSlice const & beg = cur.selBegin();
1778 CursorSlice const & end = cur.selEnd();
1779 cs = tabular.column_of_cell(beg.idx());
1780 ce = tabular.column_of_cell(end.idx());
1783 cs = tabular.column_of_cell(end.idx());
1785 ce = tabular.right_column_of_cell(end.idx());
1788 rs = tabular.row_of_cell(beg.idx());
1789 re = tabular.row_of_cell(end.idx());
1795 LyXText * InsetTabular::getText(int idx) const
1797 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1801 void InsetTabular::markErased(bool erased)
1803 for (idx_type idx = 0; idx < nargs(); ++idx)
1804 cell(idx)->markErased(erased);
1808 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1810 return tabular.getPWidth(cell).zero();
1814 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1817 if (buf.length() <= 0)
1822 col_type maxCols = 1;
1823 string::size_type const len = buf.length();
1824 string::size_type p = 0;
1826 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1834 maxCols = max(cols, maxCols);
1840 maxCols = max(cols, maxCols);
1841 LyXTabular * loctab;
1846 paste_tabular.reset(
1847 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1848 loctab = paste_tabular.get();
1850 dirtyTabularStack(true);
1853 cell = bv.cursor().idx();
1854 ocol = tabular.column_of_cell(cell);
1855 row = tabular.row_of_cell(cell);
1858 string::size_type op = 0;
1859 idx_type const cells = loctab->getNumberOfCells();
1862 rows = loctab->rows();
1863 col_type const columns = loctab->columns();
1865 while (cell < cells && p < len && row < rows &&
1866 (p = buf.find_first_of("\t\n", p)) != string::npos)
1872 // we can only set this if we are not too far right
1873 if (cols < columns) {
1874 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1875 inset->setViewCache(&bv);
1876 Paragraph & par = inset->text_.getPar(0);
1877 LyXFont const font = inset->text_.getFont(par, 0);
1878 inset->setText(buf.substr(op, p - op), font);
1884 // we can only set this if we are not too far right
1885 if (cols < columns) {
1886 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1887 inset->setViewCache(&bv);
1888 Paragraph & par = inset->text_.getPar(0);
1889 LyXFont const font = inset->text_.getFont(par, 0);
1890 inset->setText(buf.substr(op, p - op), font);
1895 cell = loctab->getCellNumber(row, cols);
1901 // check for the last cell if there is no trailing '\n'
1902 if (cell < cells && op < len) {
1903 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1904 inset->setViewCache(&bv);
1905 Paragraph & par = inset->text_.getPar(0);
1906 LyXFont const font = inset->text_.getFont(par, 0);
1907 inset->setText(buf.substr(op, len - op), font);
1913 void InsetTabular::addPreview(PreviewLoader & loader) const
1915 row_type const rows = tabular.rows();
1916 col_type const columns = tabular.columns();
1917 for (row_type i = 0; i < rows; ++i) {
1918 for (col_type j = 0; j < columns; ++j)
1919 tabular.getCellInset(i, j)->addPreview(loader);
1924 bool InsetTabular::tablemode(LCursor & cur) const
1926 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1933 string const InsetTabularMailer::name_("tabular");
1935 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1936 : inset_(const_cast<InsetTabular &>(inset))
1940 string const InsetTabularMailer::inset2string(Buffer const &) const
1942 return params2string(inset_);
1946 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1948 istringstream data(in);
1950 lex.setStream(data);
1957 if (!lex || token != name_)
1958 return print_mailer_error("InsetTabularMailer", in, 1,
1961 // This is part of the inset proper that is usually swallowed
1962 // by Buffer::readInset
1964 if (!lex || token != "Tabular")
1965 return print_mailer_error("InsetTabularMailer", in, 2,
1968 Buffer const & buffer = inset.buffer();
1969 inset.read(buffer, lex);
1973 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1976 data << name_ << ' ';
1977 inset.write(inset.buffer(), data);
1978 data << "\\end_inset\n";