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 "coordcache.h"
21 #include "dispatchresult.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
29 #include "metricsinfo.h"
30 #include "outputparams.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
36 #include "support/convert.h"
38 #include "frontends/Alert.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
42 #include "frontends/nullpainter.h"
48 using lyx::graphics::PreviewLoader;
50 using lyx::support::ltrim;
52 using boost::shared_ptr;
58 using std::istringstream;
60 using std::ostringstream;
67 int const ADD_TO_HEIGHT = 2;
68 int const ADD_TO_TABULAR_WIDTH = 2;
71 boost::scoped_ptr<LyXTabular> paste_tabular;
74 struct TabularFeature {
75 LyXTabular::Feature action;
80 TabularFeature tabularFeature[] =
82 { LyXTabular::APPEND_ROW, "append-row" },
83 { LyXTabular::APPEND_COLUMN, "append-column" },
84 { LyXTabular::DELETE_ROW, "delete-row" },
85 { LyXTabular::DELETE_COLUMN, "delete-column" },
86 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
87 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
88 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
89 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
90 { LyXTabular::ALIGN_LEFT, "align-left" },
91 { LyXTabular::ALIGN_RIGHT, "align-right" },
92 { LyXTabular::ALIGN_CENTER, "align-center" },
93 { LyXTabular::ALIGN_BLOCK, "align-block" },
94 { LyXTabular::VALIGN_TOP, "valign-top" },
95 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
96 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
97 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
98 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
99 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
100 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
101 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
102 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
103 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
104 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
105 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
106 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
107 { LyXTabular::MULTICOLUMN, "multicolumn" },
108 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
109 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
110 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
111 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
112 { LyXTabular::SET_PWIDTH, "set-pwidth" },
113 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
114 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
115 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
116 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
117 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
118 { LyXTabular::SET_USEBOX, "set-usebox" },
119 { LyXTabular::SET_LTHEAD, "set-lthead" },
120 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
121 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
122 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
123 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
124 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
125 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
126 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
127 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
128 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
129 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
130 { LyXTabular::LAST_ACTION, "" }
134 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
136 FeatureEqual(LyXTabular::Feature feature)
137 : feature_(feature) {}
138 bool operator()(TabularFeature const & tf) const {
139 return tf.action == feature_;
142 LyXTabular::Feature feature_;
148 string const featureAsString(LyXTabular::Feature feature)
150 TabularFeature * end = tabularFeature +
151 sizeof(tabularFeature) / sizeof(TabularFeature);
152 TabularFeature * it = std::find_if(tabularFeature, end,
153 FeatureEqual(feature));
154 return (it == end) ? string() : it->feature;
158 bool InsetTabular::hasPasteBuffer() const
160 return (paste_tabular.get() != 0);
164 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
166 : tabular(buf.params(), max(rows, row_type(1)),
167 max(columns, col_type(1))), buffer_(&buf), cursorx_(0)
171 InsetTabular::InsetTabular(InsetTabular const & tab)
172 : UpdatableInset(tab), tabular(tab.tabular),
173 buffer_(tab.buffer_), cursorx_(0)
177 InsetTabular::~InsetTabular()
179 InsetTabularMailer(*this).hideDialog();
183 auto_ptr<InsetBase> InsetTabular::doClone() const
185 return auto_ptr<InsetBase>(new InsetTabular(*this));
189 Buffer const & InsetTabular::buffer() const
195 void InsetTabular::buffer(Buffer const * b)
201 void InsetTabular::write(Buffer const & buf, ostream & os) const
203 os << "Tabular" << endl;
204 tabular.write(buf, os);
208 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
210 bool const old_format = (lex.getString() == "\\LyXTable");
212 tabular.read(buf, lex);
218 string token = lex.getString();
219 while (lex.isOK() && (token != "\\end_inset")) {
221 token = lex.getString();
223 if (token != "\\end_inset") {
224 lex.printError("Missing \\end_inset at this point. "
230 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
232 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
233 // mi.base.textwidth << "\n";
235 lyxerr << "InsetTabular::metrics: need bv" << endl;
240 for (idx_type cell = 0; i < tabular.rows(); ++i) {
243 for (col_type j = 0; j < tabular.columns(); ++j) {
244 if (tabular.isPartOfMultiColumn(i, j))
248 LyXLength p_width = tabular.column_info[j].p_width;
250 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
251 tabular.getCellInset(cell)->metrics(m, dim);
253 dim.wid = m.base.textwidth;
254 maxAsc = max(maxAsc, dim.asc);
255 maxDesc = max(maxDesc, dim.des);
256 tabular.setWidthOfCell(cell, dim.wid);
259 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
260 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
263 dim.asc = tabular.getAscentOfRow(0);
264 dim.des = tabular.getHeightOfTabular() - dim.asc;
265 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
270 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
272 setPosCache(pi, x, y);
274 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
275 BufferView * bv = pi.base.bv;
277 static NullPainter nop;
278 static PainterInfo nullpi(bv, nop);
280 resetPos(bv->cursor());
283 x += ADD_TO_TABULAR_WIDTH;
286 first_visible_cell = LyXTabular::npos;
287 for (row_type i = 0; i < tabular.rows(); ++i) {
289 int const a = tabular.getAscentOfRow(i);
290 int const d = tabular.getDescentOfRow(i);
291 idx = tabular.getCellNumber(i, 0);
292 for (col_type j = 0; j < tabular.columns(); ++j) {
293 if (tabular.isPartOfMultiColumn(i, j))
295 if (first_visible_cell == LyXTabular::npos)
296 first_visible_cell = idx;
298 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
299 if (nx + tabular.getWidthOfColumn(idx) < 0
300 || nx > bv->workWidth()
302 || y - a > bv->workHeight()) {
303 cell(idx)->draw(nullpi, cx, y);
304 drawCellLines(nop, nx, y, i, idx);
306 cell(idx)->draw(pi, cx, y);
307 drawCellLines(pi.pain, nx, y, i, idx);
309 nx += tabular.getWidthOfColumn(idx);
313 if (i + 1 < tabular.rows())
314 y += d + tabular.getAscentOfRow(i + 1) +
315 tabular.getAdditionalHeight(i + 1);
320 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
322 setPosCache(pi, x, y);
324 LCursor & cur = pi.base.bv->cursor();
325 if (!cur.selection())
327 if (!ptr_cmp(&cur.inset(), this))
333 x += ADD_TO_TABULAR_WIDTH;
335 if (tablemode(cur)) {
338 getSelection(cur, rs, re, cs, ce);
339 y -= tabular.getAscentOfRow(0);
340 for (row_type j = 0; j < tabular.rows(); ++j) {
341 int const a = tabular.getAscentOfRow(j);
342 int const h = a + tabular.getDescentOfRow(j);
344 y += tabular.getAdditionalHeight(j);
345 for (col_type i = 0; i < tabular.columns(); ++i) {
346 if (tabular.isPartOfMultiColumn(j, i))
348 idx_type const cell =
349 tabular.getCellNumber(j, i);
350 int const w = tabular.getWidthOfColumn(cell);
351 if (i >= cs && i <= ce && j >= rs && j <= re)
352 pi.pain.fillRectangle(xx, y, w, h,
361 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
366 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
367 row_type row, idx_type cell) const
369 int x2 = x + tabular.getWidthOfColumn(cell);
372 if (!tabular.topAlreadyDrawn(cell)) {
373 on_off = !tabular.topLine(cell);
374 pain.line(x, y - tabular.getAscentOfRow(row),
375 x2, y - tabular.getAscentOfRow(row),
376 on_off ? LColor::tabularonoffline : LColor::tabularline,
377 on_off ? Painter::line_onoffdash : Painter::line_solid);
379 on_off = !tabular.bottomLine(cell);
380 pain.line(x, y + tabular.getDescentOfRow(row),
381 x2, y + tabular.getDescentOfRow(row),
382 on_off ? LColor::tabularonoffline : LColor::tabularline,
383 on_off ? Painter::line_onoffdash : Painter::line_solid);
384 if (!tabular.leftAlreadyDrawn(cell)) {
385 on_off = !tabular.leftLine(cell);
386 pain.line(x, y - tabular.getAscentOfRow(row),
387 x, y + tabular.getDescentOfRow(row),
388 on_off ? LColor::tabularonoffline : LColor::tabularline,
389 on_off ? Painter::line_onoffdash : Painter::line_solid);
391 on_off = !tabular.rightLine(cell);
392 pain.line(x2 - tabular.getAdditionalWidth(cell),
393 y - tabular.getAscentOfRow(row),
394 x2 - tabular.getAdditionalWidth(cell),
395 y + tabular.getDescentOfRow(row),
396 on_off ? LColor::tabularonoffline : LColor::tabularline,
397 on_off ? Painter::line_onoffdash : Painter::line_solid);
401 string const InsetTabular::editMessage() const
403 return _("Opened table");
407 void InsetTabular::edit(LCursor & cur, bool left)
409 lyxerr << "InsetTabular::edit: " << this << endl;
411 cur.selection() = false;
414 if (isRightToLeft(cur))
415 cur.idx() = tabular.getLastCellInRow(0);
421 if (isRightToLeft(cur))
422 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
424 cur.idx() = tabular.getNumberOfCells() - 1;
426 cur.pos() = cur.lastpos(); // FIXME crude guess
428 // this accesses the position cache before it is initialized
430 //cur.bv().fitCursor();
434 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
436 //lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
437 //lyxerr << " cur:\n" << cur << endl;
438 CursorSlice sl = cur.top();
439 LCursor & bvcur = cur.bv().cursor();
441 switch (cmd.action) {
443 case LFUN_MOUSE_PRESS:
444 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
446 if (cmd.button() == mouse_button::button1) {
447 cur.selection() = false;
448 setCursorFromCoordinates(cur, cmd.x, cmd.y);
454 if (cmd.button() == mouse_button::button2) {
455 // FIXME: pasting multiple cells (with insettabular's own
456 // LFUN_PASTESELECTION still does not work! (jspitzm)
457 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
458 cell(cur.idx())->dispatch(cur, cmd);
462 // we'll pop up the table dialog on release
463 if (cmd.button() == mouse_button::button3)
467 case LFUN_MOUSE_MOTION:
468 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
469 if (cmd.button() == mouse_button::button1) {
470 // only accept motions to places not deeper nested than the real anchor
471 if (bvcur.anchor_.hasPart(cur)) {
472 setCursorFromCoordinates(cur, cmd.x, cmd.y);
473 bvcur.setCursor(cur);
474 bvcur.selection() = true;
480 case LFUN_MOUSE_RELEASE:
481 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
482 if (cmd.button() == mouse_button::button3)
483 InsetTabularMailer(*this).showDialog(&cur.bv());
486 case LFUN_CELL_BACKWARD:
488 cur.selection() = false;
491 case LFUN_CELL_FORWARD:
493 cur.selection() = false;
498 cell(cur.idx())->dispatch(cur, cmd);
499 cur.dispatched(); // override the cell's decision
501 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
502 if (sl == cur.top()) {
503 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
511 cell(cur.idx())->dispatch(cur, cmd);
512 cur.dispatched(); // override the cell's decision
514 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
515 if (sl == cur.top()) {
516 cmd = FuncRequest(LFUN_FINISHED_LEFT);
524 cell(cur.idx())->dispatch(cur, cmd);
525 cur.dispatched(); // override the cell's decision
527 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
528 cur.idx() = tabular.getCellBelow(cur.idx());
533 if (sl == cur.top()) {
534 cmd = FuncRequest(LFUN_FINISHED_DOWN);
542 cell(cur.idx())->dispatch(cur, cmd);
543 cur.dispatched(); // override the cell's decision
545 if (tabular.row_of_cell(cur.idx()) != 0) {
546 cur.idx() = tabular.getCellAbove(cur.idx());
547 cur.pit() = cur.lastpit();
548 cur.pos() = cur.lastpos();
551 if (sl == cur.top()) {
552 cmd = FuncRequest(LFUN_FINISHED_UP);
559 // //if (hasSelection())
560 // // cur.selection() = false;
561 // col_type const col = tabular.column_of_cell(cur.idx());
562 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
563 // if (t < yo() + tabular.getHeightOfTabular()) {
564 // cur.bv().scrollDocView(t);
565 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
567 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
575 // case LFUN_PRIOR: {
576 // //if (hasSelection())
577 // // cur.selection() = false;
578 // col_type const col = tabular.column_of_cell(cur.idx());
579 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
581 // cur.bv().scrollDocView(t);
585 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
589 // cur.par() = cur.lastpar();
590 // cur.pos() = cur.lastpos();
595 case LFUN_LAYOUT_TABULAR:
596 InsetTabularMailer(*this).showDialog(&cur.bv());
599 case LFUN_INSET_DIALOG_UPDATE:
600 InsetTabularMailer(*this).updateDialog(&cur.bv());
603 case LFUN_TABULAR_FEATURE:
604 if (!tabularFeatures(cur, cmd.argument))
608 // insert file functions
609 case LFUN_FILE_INSERT_ASCII_PARA:
610 case LFUN_FILE_INSERT_ASCII: {
611 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
612 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
618 if (tablemode(cur)) {
619 if (copySelection(cur)) {
620 recordUndo(cur, Undo::DELETE);
625 cell(cur.idx())->dispatch(cur, cmd);
630 recordUndo(cur, Undo::DELETE);
634 cell(cur.idx())->dispatch(cur, cmd);
638 if (!cur.selection())
640 if (tablemode(cur)) {
644 cell(cur.idx())->dispatch(cur, cmd);
647 case LFUN_PASTESELECTION: {
648 string const clip = cur.bv().getClipboard();
651 if (clip.find('\t') != string::npos) {
654 col_type maxCols = 1;
655 size_t len = clip.length();
656 for (size_t p = 0; p < len; ++p) {
657 p = clip.find_first_of("\t\n", p);
658 if (p == string::npos)
667 maxCols = max(cols, maxCols);
672 maxCols = max(cols, maxCols);
675 new LyXTabular(cur.buffer().params(), rows, maxCols));
677 string::size_type op = 0;
679 idx_type const cells =
680 paste_tabular->getNumberOfCells();
683 for (size_t p = 0; cell < cells && p < len; ++p) {
684 p = clip.find_first_of("\t\n", p);
685 if (p == string::npos || p >= len)
689 paste_tabular->getCellInset(cell)->
690 setText(clip.substr(op, p - op), font);
695 paste_tabular->getCellInset(cell)->
696 setText(clip.substr(op, p - op), font);
697 while (cols++ < maxCols)
704 // check for the last cell if there is no trailing '\n'
705 if (cell < cells && op < len)
706 paste_tabular->getCellInset(cell)->
707 setText(clip.substr(op, len - op), font);
708 } else if (!insertAsciiString(cur.bv(), clip, true)) {
709 // so that the clipboard is used and it goes on
711 // and executes LFUN_PASTESELECTION in insettext!
712 paste_tabular.reset();
718 if (hasPasteBuffer()) {
719 recordUndo(cur, Undo::INSERT);
723 cell(cur.idx())->dispatch(cur, cmd);
734 case LFUN_FREEFONT_APPLY:
735 case LFUN_FREEFONT_UPDATE:
739 case LFUN_CAPITALIZE_WORD:
740 case LFUN_UPCASE_WORD:
741 case LFUN_LOWCASE_WORD:
742 case LFUN_TRANSPOSE_CHARS:
743 if (tablemode(cur)) {
746 getSelection(cur, rs, re, cs, ce);
747 for (row_type i = rs; i <= re; ++i)
748 for (col_type j = cs; j <= ce; ++j) {
749 // cursor follows cell:
750 cur.idx() = tabular.getCellNumber(i, j);
751 // select this cell only:
754 cur.pos() = cur.top().lastpos();
757 cell(cur.idx())->dispatch(cur, cmd);
759 // Restore original selection
760 cur.idx() = tabular.getCellNumber(rs, cs);
763 cur.idx() = tabular.getCellNumber(re, ce);
764 cur.pos() = cur.top().lastpos();
769 cell(cur.idx())->dispatch(cur, cmd);
773 // we try to handle this event in the insets dispatch function.
774 cell(cur.idx())->dispatch(cur, cmd);
778 InsetTabularMailer(*this).updateDialog(&cur.bv());
782 // function sets an object as defined in func_status.h:
783 // states OK, Unknown, Disabled, On, Off.
784 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
785 FuncStatus & status) const
787 switch (cmd.action) {
788 case LFUN_TABULAR_FEATURE: {
789 int action = LyXTabular::LAST_ACTION;
791 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
792 string const tmp = tabularFeature[i].feature;
793 if (tmp == cmd.argument.substr(0, tmp.length())) {
794 action = tabularFeature[i].action;
798 if (action == LyXTabular::LAST_ACTION) {
800 status.unknown(true);
804 string const argument
805 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
807 row_type sel_row_start = 0;
808 row_type sel_row_end = 0;
810 LyXTabular::ltType dummyltt;
813 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
816 case LyXTabular::SET_PWIDTH:
817 case LyXTabular::SET_MPWIDTH:
818 case LyXTabular::SET_SPECIAL_COLUMN:
819 case LyXTabular::SET_SPECIAL_MULTI:
820 case LyXTabular::APPEND_ROW:
821 case LyXTabular::APPEND_COLUMN:
822 case LyXTabular::DELETE_ROW:
823 case LyXTabular::DELETE_COLUMN:
824 case LyXTabular::SET_ALL_LINES:
825 case LyXTabular::UNSET_ALL_LINES:
829 case LyXTabular::MULTICOLUMN:
830 status.setOnOff(tabular.isMultiColumn(cur.idx()));
833 case LyXTabular::M_TOGGLE_LINE_TOP:
835 case LyXTabular::TOGGLE_LINE_TOP:
836 status.setOnOff(tabular.topLine(cur.idx(), flag));
839 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
841 case LyXTabular::TOGGLE_LINE_BOTTOM:
842 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
845 case LyXTabular::M_TOGGLE_LINE_LEFT:
847 case LyXTabular::TOGGLE_LINE_LEFT:
848 status.setOnOff(tabular.leftLine(cur.idx(), flag));
851 case LyXTabular::M_TOGGLE_LINE_RIGHT:
853 case LyXTabular::TOGGLE_LINE_RIGHT:
854 status.setOnOff(tabular.rightLine(cur.idx(), flag));
857 case LyXTabular::M_ALIGN_LEFT:
859 case LyXTabular::ALIGN_LEFT:
860 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
863 case LyXTabular::M_ALIGN_RIGHT:
865 case LyXTabular::ALIGN_RIGHT:
866 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
869 case LyXTabular::M_ALIGN_CENTER:
871 case LyXTabular::ALIGN_CENTER:
872 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
875 case LyXTabular::ALIGN_BLOCK:
876 status.enabled(!tabular.getPWidth(cur.idx()).zero());
877 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
880 case LyXTabular::M_VALIGN_TOP:
882 case LyXTabular::VALIGN_TOP:
884 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
887 case LyXTabular::M_VALIGN_BOTTOM:
889 case LyXTabular::VALIGN_BOTTOM:
891 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
894 case LyXTabular::M_VALIGN_MIDDLE:
896 case LyXTabular::VALIGN_MIDDLE:
898 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
901 case LyXTabular::SET_LONGTABULAR:
902 status.setOnOff(tabular.isLongTabular());
905 case LyXTabular::UNSET_LONGTABULAR:
906 status.setOnOff(!tabular.isLongTabular());
909 case LyXTabular::SET_ROTATE_TABULAR:
910 status.setOnOff(tabular.getRotateTabular());
913 case LyXTabular::UNSET_ROTATE_TABULAR:
914 status.setOnOff(!tabular.getRotateTabular());
917 case LyXTabular::SET_ROTATE_CELL:
918 status.setOnOff(tabular.getRotateCell(cur.idx()));
921 case LyXTabular::UNSET_ROTATE_CELL:
922 status.setOnOff(!tabular.getRotateCell(cur.idx()));
925 case LyXTabular::SET_USEBOX:
926 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
929 case LyXTabular::SET_LTFIRSTHEAD:
930 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
933 case LyXTabular::UNSET_LTFIRSTHEAD:
934 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
937 case LyXTabular::SET_LTHEAD:
938 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
941 case LyXTabular::UNSET_LTHEAD:
942 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
945 case LyXTabular::SET_LTFOOT:
946 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
949 case LyXTabular::UNSET_LTFOOT:
950 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
953 case LyXTabular::SET_LTLASTFOOT:
954 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
957 case LyXTabular::UNSET_LTLASTFOOT:
958 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
961 case LyXTabular::SET_LTNEWPAGE:
962 status.setOnOff(tabular.getLTNewPage(sel_row_start));
967 status.enabled(false);
973 // These are only enabled inside tabular
974 case LFUN_CELL_BACKWARD:
975 case LFUN_CELL_FORWARD:
976 status.enabled(true);
979 // disable these with multiple cells selected
980 case LFUN_INSET_INSERT:
981 case LFUN_TABULAR_INSERT:
982 case LFUN_INSERT_CHARSTYLE:
983 case LFUN_INSET_FLOAT:
984 case LFUN_INSET_WIDE_FLOAT:
985 case LFUN_INSET_FOOTNOTE:
986 case LFUN_INSET_MARGINAL:
987 case LFUN_INSERT_MATH:
989 case LFUN_MATH_MUTATE:
990 case LFUN_MATH_DISPLAY:
991 case LFUN_INSERT_NOTE:
992 case LFUN_INSET_OPTARG:
993 case LFUN_INSERT_BOX:
994 case LFUN_INSERT_BRANCH:
995 case LFUN_INSET_WRAP:
996 case LFUN_INSET_ERT: {
997 if (tablemode(cur)) {
998 status.enabled(false);
1003 case LFUN_INSET_MODIFY:
1004 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1005 status.enabled(true);
1011 // we try to handle this event in the insets dispatch function.
1012 return cell(cur.idx())->getStatus(cur, cmd, status);
1017 int InsetTabular::latex(Buffer const & buf, ostream & os,
1018 OutputParams const & runparams) const
1020 return tabular.latex(buf, os, runparams);
1024 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1025 OutputParams const & runparams) const
1027 int const dp = runparams.linelen ? runparams.depth : 0;
1028 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1032 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1033 OutputParams const & runparams) const
1035 return tabular.linuxdoc(buf,os, runparams);
1039 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1040 OutputParams const & runparams) const
1043 InsetBase * master = 0;
1045 #ifdef WITH_WARNINGS
1046 #warning Why not pass a proper DocIterator here?
1049 // if the table is inside a float it doesn't need the informaltable
1050 // wrapper. Search for it.
1051 for (master = owner(); master; master = master->owner())
1052 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1057 os << "<informaltable>";
1060 ret += tabular.docbook(buf, os, runparams);
1062 os << "</informaltable>";
1069 void InsetTabular::validate(LaTeXFeatures & features) const
1071 tabular.validate(features);
1075 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1077 return tabular.getCellInset(idx);
1081 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1083 return tabular.getCellInset(idx);
1087 void InsetTabular::cursorPos
1088 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1090 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1092 // y offset correction
1093 int const row = tabular.row_of_cell(sl.idx());
1094 for (int i = 0; i <= row; ++i) {
1096 y += tabular.getAscentOfRow(i);
1097 y += tabular.getAdditionalHeight(i);
1100 y += tabular.getDescentOfRow(i);
1103 // x offset correction
1104 int const col = tabular.column_of_cell(sl.idx());
1105 int idx = tabular.getCellNumber(row, 0);
1106 for (int j = 0; j < col; ++j) {
1107 if (tabular.isPartOfMultiColumn(row, j))
1109 x += tabular.getWidthOfColumn(idx);
1112 x += tabular.getBeginningOfTextInCell(idx);
1113 x += ADD_TO_TABULAR_WIDTH;
1118 int InsetTabular::dist(idx_type const cell, int x, int y) const
1122 InsetBase const & inset = *tabular.getCellInset(cell);
1123 Point o = theCoords.getInsets().xy(&inset);
1124 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1125 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1126 int const ybeg = o.y_ - inset.ascent();
1127 row_type const row = tabular.row_of_cell(cell);
1128 int const rowheight = tabular.getAscentOfRow(row)
1129 + tabular.getDescentOfRow(row)
1130 + tabular.getAdditionalHeight(row);
1131 int const yend = ybeg + rowheight;
1143 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1144 // << " ybeg=" << ybeg << " yend=" << yend
1145 // << " xx=" << xx << " yy=" << yy
1146 // << " dist=" << xx + yy << endl;
1151 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1153 //lyxerr << "InsetTabular::editXY: " << this << endl;
1154 cur.selection() = false;
1156 cur.idx() = getNearestCell(x, y);
1158 return cell(cur.idx())->text_.editXY(cur, x, y);
1159 //int xx = cursorx_ - xo() + tabular.getBeginningOfTextInCell(cur.idx());
1163 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1165 //lyxerr << "# InsetTabular::setCursorFromCoordinates()\n" << cur << endl;
1166 cur.idx() = getNearestCell(x, y);
1168 return cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1172 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1174 //lyxerr << "# InsetTabular::getNearestCell() x=" << x << " y=" << y << endl;
1175 idx_type idx_min = 0;
1176 int dist_min = std::numeric_limits<int>::max();
1177 for (idx_type i = 0; i < nargs(); ++i) {
1178 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1179 int d = dist(i, x, y);
1190 int InsetTabular::getCellXPos(idx_type const cell) const
1194 for (; !tabular.isFirstCellInRow(c); --c)
1196 int lx = tabular.getWidthOfColumn(cell);
1197 for (; c < cell; ++c)
1198 lx += tabular.getWidthOfColumn(c);
1200 return lx - tabular.getWidthOfColumn(cell);
1204 void InsetTabular::resetPos(LCursor & cur) const
1206 BufferView & bv = cur.bv();
1207 int const maxwidth = bv.workWidth();
1208 // col_type const actcol = tabular.column_of_cell(cur.idx());
1209 // int const offset = ADD_TO_TABULAR_WIDTH + 2;
1210 // int const new_x = getCellXPos(cur.idx()) + offset;
1211 // int const old_x = cursorx_;
1212 // int const col_width = tabular.getWidthOfColumn(cur.idx());
1213 // cursorx_ = new_x;
1214 // cursor.x(getCellXPos(cur.idx()) + offset);
1215 // if (actcol < tabular.columns() - 1 && scroll(false) &&
1216 // tabular.getWidthOfTabular() < maxwidth - 20)
1219 // } else if (cursorx_ - offset > 20 &&
1220 // cursorx_ - offset + col_width > maxwidth - 20) {
1221 // setScroll(maxwidth, - col_width - 20);
1222 // } else if (cursorx_ - offset < 20) {
1223 // setScroll(maxwidth, 20 - cursorx_ + offset);
1224 // } else if (scroll() && xo() > 20 &&
1225 // xo() + tabular.getWidthOfTabular() > maxwidth - 20) {
1226 // setScroll(maxwidth, old_x - cursorx_);
1229 if (&cur.inset() != this) {
1233 int const X2 = maxwidth;
1234 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1235 int const x1 = xo() + scroll() + getCellXPos(cur.idx()) + offset;
1236 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1239 setScroll(maxwidth, X1 + 20 - x1);
1241 setScroll(maxwidth, X2 - 20 - x2);
1246 InsetTabularMailer(*this).updateDialog(&bv);
1250 void InsetTabular::moveNextCell(LCursor & cur)
1252 if (isRightToLeft(cur)) {
1253 if (tabular.isFirstCellInRow(cur.idx())) {
1254 row_type const row = tabular.row_of_cell(cur.idx());
1255 if (row == tabular.rows() - 1)
1257 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1264 if (tabular.isLastCell(cur.idx()))
1274 void InsetTabular::movePrevCell(LCursor & cur)
1276 if (isRightToLeft(cur)) {
1277 if (tabular.isLastCellInRow(cur.idx())) {
1278 row_type const row = tabular.row_of_cell(cur.idx());
1281 cur.idx() = tabular.getFirstCellInRow(row);
1282 cur.idx() = tabular.getCellAbove(cur.idx());
1284 if (tabular.isLastCell(cur.idx()))
1289 if (cur.idx() == 0) // first cell
1293 cur.pit() = cur.lastpit();
1294 cur.pos() = cur.lastpos();
1299 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1301 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1304 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1305 string const tmp = tabularFeature[i].feature;
1307 if (tmp == what.substr(0, tmp.length())) {
1308 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1309 //tabularFeatures[i].feature.length())) {
1310 action = tabularFeature[i].action;
1314 if (action == LyXTabular::LAST_ACTION)
1318 ltrim(what.substr(tabularFeature[i].feature.length()));
1319 tabularFeatures(cur, action, val);
1326 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1327 string const & special, bool & flag)
1329 if (special == "dl_above") {
1332 } else if (special == "dl_below") {
1333 ltt.bottomDL = flag;
1335 } else if (special == "empty") {
1347 void InsetTabular::tabularFeatures(LCursor & cur,
1348 LyXTabular::Feature feature, string const & value)
1350 BufferView & bv = cur.bv();
1351 col_type sel_col_start;
1352 col_type sel_col_end;
1353 row_type sel_row_start;
1354 row_type sel_row_end;
1355 bool setLines = false;
1356 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1357 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1361 case LyXTabular::M_ALIGN_LEFT:
1362 case LyXTabular::ALIGN_LEFT:
1363 setAlign = LYX_ALIGN_LEFT;
1366 case LyXTabular::M_ALIGN_RIGHT:
1367 case LyXTabular::ALIGN_RIGHT:
1368 setAlign = LYX_ALIGN_RIGHT;
1371 case LyXTabular::M_ALIGN_CENTER:
1372 case LyXTabular::ALIGN_CENTER:
1373 setAlign = LYX_ALIGN_CENTER;
1376 case LyXTabular::ALIGN_BLOCK:
1377 setAlign = LYX_ALIGN_BLOCK;
1380 case LyXTabular::M_VALIGN_TOP:
1381 case LyXTabular::VALIGN_TOP:
1382 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1385 case LyXTabular::M_VALIGN_BOTTOM:
1386 case LyXTabular::VALIGN_BOTTOM:
1387 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1390 case LyXTabular::M_VALIGN_MIDDLE:
1391 case LyXTabular::VALIGN_MIDDLE:
1392 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1399 recordUndo(cur, Undo::ATOMIC);
1401 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1402 row_type const row = tabular.row_of_cell(cur.idx());
1403 col_type const column = tabular.column_of_cell(cur.idx());
1405 LyXTabular::ltType ltt;
1409 case LyXTabular::SET_PWIDTH: {
1410 LyXLength const len(value);
1411 tabular.setColumnPWidth(cur.idx(), len);
1413 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1414 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1415 else if (!len.zero()
1416 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1417 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1421 case LyXTabular::SET_MPWIDTH:
1422 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1425 case LyXTabular::SET_SPECIAL_COLUMN:
1426 case LyXTabular::SET_SPECIAL_MULTI:
1427 tabular.setAlignSpecial(cur.idx(),value,feature);
1430 case LyXTabular::APPEND_ROW:
1431 // append the row into the tabular
1432 tabular.appendRow(bv.buffer()->params(), cur.idx());
1435 case LyXTabular::APPEND_COLUMN:
1436 // append the column into the tabular
1437 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1438 cur.idx() = tabular.getCellNumber(row, column);
1441 case LyXTabular::DELETE_ROW:
1442 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1443 tabular.deleteRow(sel_row_start);
1444 if (sel_row_start >= tabular.rows())
1446 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1449 cur.selection() = false;
1452 case LyXTabular::DELETE_COLUMN:
1453 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1454 tabular.deleteColumn(sel_col_start);
1455 if (sel_col_start >= tabular.columns())
1457 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1460 cur.selection() = false;
1463 case LyXTabular::M_TOGGLE_LINE_TOP:
1465 case LyXTabular::TOGGLE_LINE_TOP: {
1466 bool lineSet = !tabular.topLine(cur.idx(), flag);
1467 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1468 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1470 tabular.getCellNumber(i, j),
1475 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1477 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1478 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1479 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1480 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1481 tabular.setBottomLine(
1482 tabular.getCellNumber(i, j),
1488 case LyXTabular::M_TOGGLE_LINE_LEFT:
1490 case LyXTabular::TOGGLE_LINE_LEFT: {
1491 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1492 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1493 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1494 tabular.setLeftLine(
1495 tabular.getCellNumber(i,j),
1501 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1503 case LyXTabular::TOGGLE_LINE_RIGHT: {
1504 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1505 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1506 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1507 tabular.setRightLine(
1508 tabular.getCellNumber(i,j),
1514 case LyXTabular::M_ALIGN_LEFT:
1515 case LyXTabular::M_ALIGN_RIGHT:
1516 case LyXTabular::M_ALIGN_CENTER:
1518 case LyXTabular::ALIGN_LEFT:
1519 case LyXTabular::ALIGN_RIGHT:
1520 case LyXTabular::ALIGN_CENTER:
1521 case LyXTabular::ALIGN_BLOCK:
1522 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1523 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1524 tabular.setAlignment(
1525 tabular.getCellNumber(i, j),
1530 case LyXTabular::M_VALIGN_TOP:
1531 case LyXTabular::M_VALIGN_BOTTOM:
1532 case LyXTabular::M_VALIGN_MIDDLE:
1534 case LyXTabular::VALIGN_TOP:
1535 case LyXTabular::VALIGN_BOTTOM:
1536 case LyXTabular::VALIGN_MIDDLE:
1537 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1538 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1539 tabular.setVAlignment(
1540 tabular.getCellNumber(i, j),
1544 case LyXTabular::MULTICOLUMN: {
1545 if (sel_row_start != sel_row_end) {
1546 #ifdef WITH_WARNINGS
1547 #warning Need I say it ? This is horrible.
1549 Alert::error(_("Error setting multicolumn"),
1550 _("You cannot set multicolumn vertically."));
1553 if (!cur.selection()) {
1554 // just multicol for one single cell
1555 // check whether we are completely in a multicol
1556 if (tabular.isMultiColumn(cur.idx()))
1557 tabular.unsetMultiColumn(cur.idx());
1559 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1562 // we have a selection so this means we just add all this
1563 // cells to form a multicolumn cell
1564 idx_type const s_start = cur.selBegin().idx();
1565 idx_type const s_end = cur.selEnd().idx();
1566 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1567 cur.idx() = s_start;
1570 cur.selection() = false;
1574 case LyXTabular::SET_ALL_LINES:
1576 case LyXTabular::UNSET_ALL_LINES:
1577 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1578 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1579 tabular.setAllLines(
1580 tabular.getCellNumber(i,j), setLines);
1583 case LyXTabular::SET_LONGTABULAR:
1584 tabular.setLongTabular(true);
1587 case LyXTabular::UNSET_LONGTABULAR:
1588 tabular.setLongTabular(false);
1591 case LyXTabular::SET_ROTATE_TABULAR:
1592 tabular.setRotateTabular(true);
1595 case LyXTabular::UNSET_ROTATE_TABULAR:
1596 tabular.setRotateTabular(false);
1599 case LyXTabular::SET_ROTATE_CELL:
1600 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1601 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1602 tabular.setRotateCell(
1603 tabular.getCellNumber(i, j), true);
1606 case LyXTabular::UNSET_ROTATE_CELL:
1607 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1608 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1609 tabular.setRotateCell(
1610 tabular.getCellNumber(i, j), false);
1613 case LyXTabular::SET_USEBOX: {
1614 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1615 if (val == tabular.getUsebox(cur.idx()))
1616 val = LyXTabular::BOX_NONE;
1617 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1618 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1619 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1623 case LyXTabular::UNSET_LTFIRSTHEAD:
1625 case LyXTabular::SET_LTFIRSTHEAD:
1626 tabular.getRowOfLTFirstHead(row, ltt);
1627 checkLongtableSpecial(ltt, value, flag);
1628 tabular.setLTHead(row, flag, ltt, true);
1631 case LyXTabular::UNSET_LTHEAD:
1633 case LyXTabular::SET_LTHEAD:
1634 tabular.getRowOfLTHead(row, ltt);
1635 checkLongtableSpecial(ltt, value, flag);
1636 tabular.setLTHead(row, flag, ltt, false);
1639 case LyXTabular::UNSET_LTFOOT:
1641 case LyXTabular::SET_LTFOOT:
1642 tabular.getRowOfLTFoot(row, ltt);
1643 checkLongtableSpecial(ltt, value, flag);
1644 tabular.setLTFoot(row, flag, ltt, false);
1647 case LyXTabular::UNSET_LTLASTFOOT:
1649 case LyXTabular::SET_LTLASTFOOT:
1650 tabular.getRowOfLTLastFoot(row, ltt);
1651 checkLongtableSpecial(ltt, value, flag);
1652 tabular.setLTFoot(row, flag, ltt, true);
1655 case LyXTabular::SET_LTNEWPAGE:
1656 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1659 // dummy stuff just to avoid warnings
1660 case LyXTabular::LAST_ACTION:
1664 InsetTabularMailer(*this).updateDialog(&bv);
1668 bool InsetTabular::showInsetDialog(BufferView * bv) const
1670 InsetTabularMailer(*this).showDialog(bv);
1675 void InsetTabular::openLayoutDialog(BufferView * bv) const
1677 InsetTabularMailer(*this).showDialog(bv);
1681 void InsetTabular::getLabelList(Buffer const & buffer,
1682 vector<string> & list) const
1684 tabular.getLabelList(buffer, list);
1688 bool InsetTabular::copySelection(LCursor & cur)
1690 if (!cur.selection())
1695 getSelection(cur, rs, re, cs, ce);
1697 paste_tabular.reset(new LyXTabular(tabular));
1699 for (row_type i = 0; i < rs; ++i)
1700 paste_tabular->deleteRow(0);
1702 row_type const rows = re - rs + 1;
1703 while (paste_tabular->rows() > rows)
1704 paste_tabular->deleteRow(rows);
1706 paste_tabular->setTopLine(0, true, true);
1707 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1710 for (col_type i = 0; i < cs; ++i)
1711 paste_tabular->deleteColumn(0);
1713 col_type const columns = ce - cs + 1;
1714 while (paste_tabular->columns() > columns)
1715 paste_tabular->deleteColumn(columns);
1717 paste_tabular->setLeftLine(0, true, true);
1718 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1722 OutputParams const runparams;
1723 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1724 cur.bv().stuffClipboard(os.str());
1729 bool InsetTabular::pasteSelection(LCursor & cur)
1733 col_type const actcol = tabular.column_of_cell(cur.idx());
1734 row_type const actrow = tabular.row_of_cell(cur.idx());
1735 for (row_type r1 = 0, r2 = actrow;
1736 r1 < paste_tabular->rows() && r2 < tabular.rows();
1738 for (col_type c1 = 0, c2 = actcol;
1739 c1 < paste_tabular->columns() && c2 < tabular.columns();
1741 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1742 tabular.isPartOfMultiColumn(r2, c2))
1744 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1748 if (tabular.isPartOfMultiColumn(r2, c2)) {
1752 shared_ptr<InsetText> inset(
1753 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1754 tabular.setCellInset(r2, c2, inset);
1763 void InsetTabular::cutSelection(LCursor & cur)
1765 if (!cur.selection())
1768 bool const track = cur.buffer().params().tracking_changes;
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 cell(tabular.getCellNumber(i, j))->clear(track);
1776 // cursor position might be invalid now
1777 cur.pos() = cur.lastpos();
1778 cur.clearSelection();
1782 bool InsetTabular::isRightToLeft(LCursor & cur) const
1784 BOOST_ASSERT(cur.depth() > 1);
1785 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1786 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1787 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1788 parentpos).language()->RightToLeft();
1792 void InsetTabular::getSelection(LCursor & cur,
1793 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1795 CursorSlice const & beg = cur.selBegin();
1796 CursorSlice const & end = cur.selEnd();
1797 cs = tabular.column_of_cell(beg.idx());
1798 ce = tabular.column_of_cell(end.idx());
1801 cs = tabular.column_of_cell(end.idx());
1803 ce = tabular.right_column_of_cell(end.idx());
1806 rs = tabular.row_of_cell(beg.idx());
1807 re = tabular.row_of_cell(end.idx());
1813 size_t InsetTabular::nargs() const
1815 return tabular.getNumberOfCells();
1819 LyXText * InsetTabular::getText(int idx) const
1821 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1825 void InsetTabular::markErased()
1827 for (idx_type idx = 0; idx < nargs(); ++idx)
1828 cell(idx)->markErased();
1832 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1835 idx_type const cell = tabular.getCellFromInset(in);
1836 // FIXME: getCellFromInset() returns now always a valid cell, so
1837 // the stuff below can be deleted, and instead we have:
1838 return tabular.getPWidth(cell).zero();
1841 return tabular.getPWidth(cell).zero();
1843 // this is a workaround for a crash (New, Insert->Tabular,
1844 // Insert->FootNote)
1848 // well we didn't obviously find it so maybe our owner knows more
1849 BOOST_ASSERT(owner());
1850 return owner()->forceDefaultParagraphs(in);
1856 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1859 if (buf.length() <= 0)
1864 col_type maxCols = 1;
1865 string::size_type const len = buf.length();
1866 string::size_type p = 0;
1868 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1876 maxCols = max(cols, maxCols);
1882 maxCols = max(cols, maxCols);
1883 LyXTabular * loctab;
1888 paste_tabular.reset(
1889 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1890 loctab = paste_tabular.get();
1894 cell = bv.cursor().idx();
1895 ocol = tabular.column_of_cell(cell);
1896 row = tabular.row_of_cell(cell);
1899 string::size_type op = 0;
1900 idx_type const cells = loctab->getNumberOfCells();
1903 rows = loctab->rows();
1904 col_type const columns = loctab->columns();
1906 while (cell < cells && p < len && row < rows &&
1907 (p = buf.find_first_of("\t\n", p)) != string::npos)
1913 // we can only set this if we are not too far right
1914 if (cols < columns) {
1915 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1916 Paragraph & par = inset->text_.getPar(0);
1917 LyXFont const font = inset->text_.getFont(par, 0);
1918 inset->setText(buf.substr(op, p - op), font);
1924 // we can only set this if we are not too far right
1925 if (cols < columns) {
1926 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1927 Paragraph & par = inset->text_.getPar(0);
1928 LyXFont const font = inset->text_.getFont(par, 0);
1929 inset->setText(buf.substr(op, p - op), font);
1934 cell = loctab->getCellNumber(row, cols);
1940 // check for the last cell if there is no trailing '\n'
1941 if (cell < cells && op < len) {
1942 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1943 Paragraph & par = inset->text_.getPar(0);
1944 LyXFont const font = inset->text_.getFont(par, 0);
1945 inset->setText(buf.substr(op, len - op), font);
1951 void InsetTabular::addPreview(PreviewLoader & loader) const
1953 row_type const rows = tabular.rows();
1954 col_type const columns = tabular.columns();
1955 for (row_type i = 0; i < rows; ++i) {
1956 for (col_type j = 0; j < columns; ++j)
1957 tabular.getCellInset(i, j)->addPreview(loader);
1962 bool InsetTabular::tablemode(LCursor & cur) const
1964 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1971 string const InsetTabularMailer::name_("tabular");
1973 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1974 : inset_(const_cast<InsetTabular &>(inset))
1978 string const InsetTabularMailer::inset2string(Buffer const &) const
1980 return params2string(inset_);
1984 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1986 istringstream data(in);
1988 lex.setStream(data);
1995 if (!lex || token != name_)
1996 return print_mailer_error("InsetTabularMailer", in, 1,
1999 // This is part of the inset proper that is usually swallowed
2000 // by Buffer::readInset
2002 if (!lex || token != "Tabular")
2003 return print_mailer_error("InsetTabularMailer", in, 2,
2006 Buffer const & buffer = inset.buffer();
2007 inset.read(buffer, lex);
2011 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2014 data << name_ << ' ';
2015 inset.write(inset.buffer(), data);
2016 data << "\\end_inset\n";