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), scx_(0)
171 InsetTabular::InsetTabular(InsetTabular const & tab)
172 : InsetOld(tab), tabular(tab.tabular),
173 buffer_(tab.buffer_), scx_(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))
332 x += scx_ + ADD_TO_TABULAR_WIDTH;
334 if (tablemode(cur)) {
337 getSelection(cur, rs, re, cs, ce);
338 y -= tabular.getAscentOfRow(0);
339 for (row_type j = 0; j < tabular.rows(); ++j) {
340 int const a = tabular.getAscentOfRow(j);
341 int const h = a + tabular.getDescentOfRow(j);
343 y += tabular.getAdditionalHeight(j);
344 for (col_type i = 0; i < tabular.columns(); ++i) {
345 if (tabular.isPartOfMultiColumn(j, i))
347 idx_type const cell =
348 tabular.getCellNumber(j, i);
349 int const w = tabular.getWidthOfColumn(cell);
350 if (i >= cs && i <= ce && j >= rs && j <= re)
351 pi.pain.fillRectangle(xx, y, w, h,
360 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
365 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
366 row_type row, idx_type cell) const
368 int x2 = x + tabular.getWidthOfColumn(cell);
371 if (!tabular.topAlreadyDrawn(cell)) {
372 on_off = !tabular.topLine(cell);
373 pain.line(x, y - tabular.getAscentOfRow(row),
374 x2, y - tabular.getAscentOfRow(row),
375 on_off ? LColor::tabularonoffline : LColor::tabularline,
376 on_off ? Painter::line_onoffdash : Painter::line_solid);
378 on_off = !tabular.bottomLine(cell);
379 pain.line(x, y + tabular.getDescentOfRow(row),
380 x2, y + tabular.getDescentOfRow(row),
381 on_off ? LColor::tabularonoffline : LColor::tabularline,
382 on_off ? Painter::line_onoffdash : Painter::line_solid);
383 if (!tabular.leftAlreadyDrawn(cell)) {
384 on_off = !tabular.leftLine(cell);
385 pain.line(x, y - tabular.getAscentOfRow(row),
386 x, y + tabular.getDescentOfRow(row),
387 on_off ? LColor::tabularonoffline : LColor::tabularline,
388 on_off ? Painter::line_onoffdash : Painter::line_solid);
390 on_off = !tabular.rightLine(cell);
391 pain.line(x2 - tabular.getAdditionalWidth(cell),
392 y - tabular.getAscentOfRow(row),
393 x2 - tabular.getAdditionalWidth(cell),
394 y + tabular.getDescentOfRow(row),
395 on_off ? LColor::tabularonoffline : LColor::tabularline,
396 on_off ? Painter::line_onoffdash : Painter::line_solid);
400 string const InsetTabular::editMessage() const
402 return _("Opened table");
406 void InsetTabular::edit(LCursor & cur, bool left)
408 lyxerr << "InsetTabular::edit: " << this << endl;
410 cur.selection() = false;
413 if (isRightToLeft(cur))
414 cur.idx() = tabular.getLastCellInRow(0);
420 if (isRightToLeft(cur))
421 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
423 cur.idx() = tabular.getNumberOfCells() - 1;
425 cur.pos() = cur.lastpos(); // FIXME crude guess
427 // this accesses the position cache before it is initialized
429 //cur.bv().fitCursor();
433 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
435 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
436 lyxerr << " cur:\n" << cur << endl;
437 CursorSlice sl = cur.top();
438 LCursor & bvcur = cur.bv().cursor();
440 switch (cmd.action) {
442 case LFUN_MOUSE_PRESS:
443 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
445 if (cmd.button() == mouse_button::button1) {
446 cur.selection() = false;
447 setCursorFromCoordinates(cur, cmd.x, cmd.y);
453 if (cmd.button() == mouse_button::button2) {
454 // FIXME: pasting multiple cells (with insettabular's own
455 // LFUN_PASTESELECTION still does not work! (jspitzm)
456 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
457 cell(cur.idx())->dispatch(cur, cmd);
461 // we'll pop up the table dialog on release
462 if (cmd.button() == mouse_button::button3)
466 case LFUN_MOUSE_MOTION:
467 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
468 if (cmd.button() == mouse_button::button1) {
469 // only accept motions to places not deeper nested than the real anchor
470 if (bvcur.anchor_.hasPart(cur)) {
471 setCursorFromCoordinates(cur, cmd.x, cmd.y);
472 bvcur.setCursor(cur);
473 bvcur.selection() = true;
479 case LFUN_MOUSE_RELEASE:
480 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
481 if (cmd.button() == mouse_button::button3)
482 InsetTabularMailer(*this).showDialog(&cur.bv());
485 case LFUN_CELL_BACKWARD:
487 cur.selection() = false;
490 case LFUN_CELL_FORWARD:
492 cur.selection() = false;
497 cell(cur.idx())->dispatch(cur, cmd);
498 cur.dispatched(); // override the cell's decision
500 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
501 if (sl == cur.top()) {
502 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
509 cell(cur.idx())->dispatch(cur, cmd);
510 cur.dispatched(); // override the cell's decision
512 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
513 if (sl == cur.top()) {
514 cmd = FuncRequest(LFUN_FINISHED_LEFT);
521 cell(cur.idx())->dispatch(cur, cmd);
522 cur.dispatched(); // override the cell's decision
524 // if our LyXText didn't do anything to the cursor
525 // then we try to put the cursor into the cell below
526 // setting also the right targetX.
527 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
528 cur.idx() = tabular.getCellBelow(cur.idx());
530 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
531 cur.pit(), 0, cur.targetX());
533 if (sl == cur.top()) {
534 // we trick it to go to the RIGHT after leaving the
536 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
543 cell(cur.idx())->dispatch(cur, cmd);
544 cur.dispatched(); // override the cell's decision
546 // if our LyXText didn't do anything to the cursor
547 // then we try to put the cursor into the cell above
548 // setting also the right targetX.
549 if (tabular.row_of_cell(cur.idx()) != 0) {
550 cur.idx() = tabular.getCellAbove(cur.idx());
551 cur.pit() = cur.lastpit();
552 LyXText const * text = cell(cur.idx())->getText(0);
553 cur.pos() = text->x2pos(
555 text->paragraphs().back().rows().size()-1,
558 if (sl == cur.top()) {
559 cmd = FuncRequest(LFUN_FINISHED_UP);
565 // //if (hasSelection())
566 // // cur.selection() = false;
567 // col_type const col = tabular.column_of_cell(cur.idx());
568 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
569 // if (t < yo() + tabular.getHeightOfTabular()) {
570 // cur.bv().scrollDocView(t);
571 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
573 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
580 // case LFUN_PRIOR: {
581 // //if (hasSelection())
582 // // cur.selection() = false;
583 // col_type const col = tabular.column_of_cell(cur.idx());
584 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
586 // cur.bv().scrollDocView(t);
590 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
594 // cur.par() = cur.lastpar();
595 // cur.pos() = cur.lastpos();
599 case LFUN_LAYOUT_TABULAR:
600 InsetTabularMailer(*this).showDialog(&cur.bv());
603 case LFUN_INSET_DIALOG_UPDATE:
604 InsetTabularMailer(*this).updateDialog(&cur.bv());
607 case LFUN_TABULAR_FEATURE:
608 if (!tabularFeatures(cur, cmd.argument))
612 // insert file functions
613 case LFUN_FILE_INSERT_ASCII_PARA:
614 case LFUN_FILE_INSERT_ASCII: {
615 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
616 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
622 if (tablemode(cur)) {
623 if (copySelection(cur)) {
624 recordUndo(cur, Undo::DELETE);
629 cell(cur.idx())->dispatch(cur, cmd);
634 recordUndo(cur, Undo::DELETE);
638 cell(cur.idx())->dispatch(cur, cmd);
642 if (!cur.selection())
644 if (tablemode(cur)) {
648 cell(cur.idx())->dispatch(cur, cmd);
651 case LFUN_PASTESELECTION: {
652 string const clip = cur.bv().getClipboard();
655 if (clip.find('\t') != string::npos) {
658 col_type maxCols = 1;
659 size_t len = clip.length();
660 for (size_t p = 0; p < len; ++p) {
661 p = clip.find_first_of("\t\n", p);
662 if (p == string::npos)
671 maxCols = max(cols, maxCols);
676 maxCols = max(cols, maxCols);
679 new LyXTabular(cur.buffer().params(), rows, maxCols));
681 string::size_type op = 0;
683 idx_type const cells =
684 paste_tabular->getNumberOfCells();
687 for (size_t p = 0; cell < cells && p < len; ++p) {
688 p = clip.find_first_of("\t\n", p);
689 if (p == string::npos || p >= len)
693 paste_tabular->getCellInset(cell)->
694 setText(clip.substr(op, p - op), font);
699 paste_tabular->getCellInset(cell)->
700 setText(clip.substr(op, p - op), font);
701 while (cols++ < maxCols)
708 // check for the last cell if there is no trailing '\n'
709 if (cell < cells && op < len)
710 paste_tabular->getCellInset(cell)->
711 setText(clip.substr(op, len - op), font);
712 } else if (!insertAsciiString(cur.bv(), clip, true)) {
713 // so that the clipboard is used and it goes on
715 // and executes LFUN_PASTESELECTION in insettext!
716 paste_tabular.reset();
722 if (hasPasteBuffer()) {
723 recordUndo(cur, Undo::INSERT);
727 cell(cur.idx())->dispatch(cur, cmd);
738 case LFUN_FREEFONT_APPLY:
739 case LFUN_FREEFONT_UPDATE:
743 case LFUN_CAPITALIZE_WORD:
744 case LFUN_UPCASE_WORD:
745 case LFUN_LOWCASE_WORD:
746 case LFUN_TRANSPOSE_CHARS:
747 if (tablemode(cur)) {
750 getSelection(cur, rs, re, cs, ce);
751 for (row_type i = rs; i <= re; ++i) {
752 for (col_type j = cs; j <= ce; ++j) {
753 // cursor follows cell:
754 cur.idx() = tabular.getCellNumber(i, j);
755 // select this cell only:
758 cur.pos() = cur.top().lastpos();
761 cell(cur.idx())->dispatch(cur, cmd);
764 // Restore original selection
765 cur.idx() = tabular.getCellNumber(rs, cs);
768 cur.idx() = tabular.getCellNumber(re, ce);
769 cur.pos() = cur.top().lastpos();
774 cell(cur.idx())->dispatch(cur, cmd);
778 // we try to handle this event in the insets dispatch function.
779 cell(cur.idx())->dispatch(cur, cmd);
784 InsetTabularMailer(*this).updateDialog(&cur.bv());
788 // function sets an object as defined in func_status.h:
789 // states OK, Unknown, Disabled, On, Off.
790 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
791 FuncStatus & status) const
793 switch (cmd.action) {
794 case LFUN_TABULAR_FEATURE: {
795 int action = LyXTabular::LAST_ACTION;
797 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
798 string const tmp = tabularFeature[i].feature;
799 if (tmp == cmd.argument.substr(0, tmp.length())) {
800 action = tabularFeature[i].action;
804 if (action == LyXTabular::LAST_ACTION) {
806 status.unknown(true);
810 string const argument
811 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
813 row_type sel_row_start = 0;
814 row_type sel_row_end = 0;
816 LyXTabular::ltType dummyltt;
819 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
822 case LyXTabular::SET_PWIDTH:
823 case LyXTabular::SET_MPWIDTH:
824 case LyXTabular::SET_SPECIAL_COLUMN:
825 case LyXTabular::SET_SPECIAL_MULTI:
826 case LyXTabular::APPEND_ROW:
827 case LyXTabular::APPEND_COLUMN:
828 case LyXTabular::DELETE_ROW:
829 case LyXTabular::DELETE_COLUMN:
830 case LyXTabular::SET_ALL_LINES:
831 case LyXTabular::UNSET_ALL_LINES:
835 case LyXTabular::MULTICOLUMN:
836 status.setOnOff(tabular.isMultiColumn(cur.idx()));
839 case LyXTabular::M_TOGGLE_LINE_TOP:
841 case LyXTabular::TOGGLE_LINE_TOP:
842 status.setOnOff(tabular.topLine(cur.idx(), flag));
845 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
847 case LyXTabular::TOGGLE_LINE_BOTTOM:
848 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
851 case LyXTabular::M_TOGGLE_LINE_LEFT:
853 case LyXTabular::TOGGLE_LINE_LEFT:
854 status.setOnOff(tabular.leftLine(cur.idx(), flag));
857 case LyXTabular::M_TOGGLE_LINE_RIGHT:
859 case LyXTabular::TOGGLE_LINE_RIGHT:
860 status.setOnOff(tabular.rightLine(cur.idx(), flag));
863 case LyXTabular::M_ALIGN_LEFT:
865 case LyXTabular::ALIGN_LEFT:
866 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
869 case LyXTabular::M_ALIGN_RIGHT:
871 case LyXTabular::ALIGN_RIGHT:
872 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
875 case LyXTabular::M_ALIGN_CENTER:
877 case LyXTabular::ALIGN_CENTER:
878 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
881 case LyXTabular::ALIGN_BLOCK:
882 status.enabled(!tabular.getPWidth(cur.idx()).zero());
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
886 case LyXTabular::M_VALIGN_TOP:
888 case LyXTabular::VALIGN_TOP:
890 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
893 case LyXTabular::M_VALIGN_BOTTOM:
895 case LyXTabular::VALIGN_BOTTOM:
897 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
900 case LyXTabular::M_VALIGN_MIDDLE:
902 case LyXTabular::VALIGN_MIDDLE:
904 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
907 case LyXTabular::SET_LONGTABULAR:
908 status.setOnOff(tabular.isLongTabular());
911 case LyXTabular::UNSET_LONGTABULAR:
912 status.setOnOff(!tabular.isLongTabular());
915 case LyXTabular::SET_ROTATE_TABULAR:
916 status.setOnOff(tabular.getRotateTabular());
919 case LyXTabular::UNSET_ROTATE_TABULAR:
920 status.setOnOff(!tabular.getRotateTabular());
923 case LyXTabular::SET_ROTATE_CELL:
924 status.setOnOff(tabular.getRotateCell(cur.idx()));
927 case LyXTabular::UNSET_ROTATE_CELL:
928 status.setOnOff(!tabular.getRotateCell(cur.idx()));
931 case LyXTabular::SET_USEBOX:
932 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
935 case LyXTabular::SET_LTFIRSTHEAD:
936 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
939 case LyXTabular::UNSET_LTFIRSTHEAD:
940 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
943 case LyXTabular::SET_LTHEAD:
944 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
947 case LyXTabular::UNSET_LTHEAD:
948 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
951 case LyXTabular::SET_LTFOOT:
952 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
955 case LyXTabular::UNSET_LTFOOT:
956 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
959 case LyXTabular::SET_LTLASTFOOT:
960 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
963 case LyXTabular::UNSET_LTLASTFOOT:
964 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
967 case LyXTabular::SET_LTNEWPAGE:
968 status.setOnOff(tabular.getLTNewPage(sel_row_start));
973 status.enabled(false);
979 // These are only enabled inside tabular
980 case LFUN_CELL_BACKWARD:
981 case LFUN_CELL_FORWARD:
982 status.enabled(true);
985 // disable these with multiple cells selected
986 case LFUN_INSET_INSERT:
987 case LFUN_TABULAR_INSERT:
988 case LFUN_INSERT_CHARSTYLE:
989 case LFUN_INSET_FLOAT:
990 case LFUN_INSET_WIDE_FLOAT:
991 case LFUN_INSET_FOOTNOTE:
992 case LFUN_INSET_MARGINAL:
993 case LFUN_INSERT_MATH:
995 case LFUN_MATH_MUTATE:
996 case LFUN_MATH_DISPLAY:
997 case LFUN_INSERT_NOTE:
998 case LFUN_INSET_OPTARG:
999 case LFUN_INSERT_BOX:
1000 case LFUN_INSERT_BRANCH:
1001 case LFUN_INSET_WRAP:
1002 case LFUN_INSET_ERT: {
1003 if (tablemode(cur)) {
1004 status.enabled(false);
1007 return cell(cur.idx())->getStatus(cur, cmd, status);
1010 // disable in non-fixed-width cells
1011 case LFUN_BREAKLINE:
1012 case LFUN_BREAKPARAGRAPH:
1013 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1014 case LFUN_BREAKPARAGRAPH_SKIP: {
1015 if (tabular.getPWidth(cur.idx()).zero()) {
1016 status.enabled(false);
1019 return cell(cur.idx())->getStatus(cur, cmd, status);
1022 case LFUN_INSET_MODIFY:
1023 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1024 status.enabled(true);
1030 // we try to handle this event in the insets dispatch function.
1031 return cell(cur.idx())->getStatus(cur, cmd, status);
1036 int InsetTabular::latex(Buffer const & buf, ostream & os,
1037 OutputParams const & runparams) const
1039 return tabular.latex(buf, os, runparams);
1043 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1044 OutputParams const & runparams) const
1046 int const dp = runparams.linelen ? runparams.depth : 0;
1047 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1051 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1052 OutputParams const & runparams) const
1054 return tabular.linuxdoc(buf,os, runparams);
1058 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1059 OutputParams const & runparams) const
1062 InsetBase * master = 0;
1064 #ifdef WITH_WARNINGS
1065 #warning Why not pass a proper DocIterator here?
1068 // if the table is inside a float it doesn't need the informaltable
1069 // wrapper. Search for it.
1070 for (master = owner(); master; master = master->owner())
1071 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1076 os << "<informaltable>";
1079 ret += tabular.docbook(buf, os, runparams);
1081 os << "</informaltable>";
1088 void InsetTabular::validate(LaTeXFeatures & features) const
1090 tabular.validate(features);
1094 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1096 return tabular.getCellInset(idx);
1100 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1102 return tabular.getCellInset(idx);
1106 void InsetTabular::cursorPos
1107 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1109 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1111 // y offset correction
1112 int const row = tabular.row_of_cell(sl.idx());
1113 for (int i = 0; i <= row; ++i) {
1115 y += tabular.getAscentOfRow(i);
1116 y += tabular.getAdditionalHeight(i);
1119 y += tabular.getDescentOfRow(i);
1122 // x offset correction
1123 int const col = tabular.column_of_cell(sl.idx());
1124 int idx = tabular.getCellNumber(row, 0);
1125 for (int j = 0; j < col; ++j) {
1126 if (tabular.isPartOfMultiColumn(row, j))
1128 x += tabular.getWidthOfColumn(idx);
1131 x += tabular.getBeginningOfTextInCell(idx);
1132 x += ADD_TO_TABULAR_WIDTH;
1137 int InsetTabular::dist(idx_type const cell, int x, int y) const
1141 InsetBase const & inset = *tabular.getCellInset(cell);
1142 Point o = theCoords.getInsets().xy(&inset);
1143 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1144 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1145 int const ybeg = o.y_ - inset.ascent();
1146 row_type const row = tabular.row_of_cell(cell);
1147 int const rowheight = tabular.getAscentOfRow(row)
1148 + tabular.getDescentOfRow(row)
1149 + tabular.getAdditionalHeight(row);
1150 int const yend = ybeg + rowheight;
1162 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1163 // << " ybeg=" << ybeg << " yend=" << yend
1164 // << " xx=" << xx << " yy=" << yy
1165 // << " dist=" << xx + yy << endl;
1170 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1172 //lyxerr << "InsetTabular::editXY: " << this << endl;
1173 cur.selection() = false;
1175 cur.idx() = getNearestCell(x, y);
1177 return cell(cur.idx())->text_.editXY(cur, x, y);
1181 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1183 cur.idx() = getNearestCell(x, y);
1184 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1188 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1190 idx_type idx_min = 0;
1191 int dist_min = std::numeric_limits<int>::max();
1192 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1193 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1194 int const d = dist(i, x, y);
1205 int InsetTabular::getCellXPos(idx_type const cell) const
1209 for (; !tabular.isFirstCellInRow(c); --c)
1211 int lx = tabular.getWidthOfColumn(cell);
1212 for (; c < cell; ++c)
1213 lx += tabular.getWidthOfColumn(c);
1215 return lx - tabular.getWidthOfColumn(cell);
1219 void InsetTabular::resetPos(LCursor & cur) const
1221 BufferView & bv = cur.bv();
1222 int const maxwidth = bv.workWidth();
1224 if (&cur.inset() != this) {
1228 int const X2 = maxwidth;
1229 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1230 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1231 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1234 scx_ = X1 + 20 - x1;
1236 scx_ = X2 - 20 - x2;
1243 InsetTabularMailer(*this).updateDialog(&bv);
1247 void InsetTabular::moveNextCell(LCursor & cur)
1249 if (isRightToLeft(cur)) {
1250 if (tabular.isFirstCellInRow(cur.idx())) {
1251 row_type const row = tabular.row_of_cell(cur.idx());
1252 if (row == tabular.rows() - 1)
1254 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1261 if (tabular.isLastCell(cur.idx()))
1271 void InsetTabular::movePrevCell(LCursor & cur)
1273 if (isRightToLeft(cur)) {
1274 if (tabular.isLastCellInRow(cur.idx())) {
1275 row_type const row = tabular.row_of_cell(cur.idx());
1278 cur.idx() = tabular.getFirstCellInRow(row);
1279 cur.idx() = tabular.getCellAbove(cur.idx());
1281 if (tabular.isLastCell(cur.idx()))
1286 if (cur.idx() == 0) // first cell
1290 cur.pit() = cur.lastpit();
1291 cur.pos() = cur.lastpos();
1296 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1298 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1301 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1302 string const tmp = tabularFeature[i].feature;
1304 if (tmp == what.substr(0, tmp.length())) {
1305 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1306 //tabularFeatures[i].feature.length())) {
1307 action = tabularFeature[i].action;
1311 if (action == LyXTabular::LAST_ACTION)
1315 ltrim(what.substr(tabularFeature[i].feature.length()));
1316 tabularFeatures(cur, action, val);
1323 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1324 string const & special, bool & flag)
1326 if (special == "dl_above") {
1329 } else if (special == "dl_below") {
1330 ltt.bottomDL = flag;
1332 } else if (special == "empty") {
1344 void InsetTabular::tabularFeatures(LCursor & cur,
1345 LyXTabular::Feature feature, string const & value)
1347 BufferView & bv = cur.bv();
1348 col_type sel_col_start;
1349 col_type sel_col_end;
1350 row_type sel_row_start;
1351 row_type sel_row_end;
1352 bool setLines = false;
1353 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1354 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1358 case LyXTabular::M_ALIGN_LEFT:
1359 case LyXTabular::ALIGN_LEFT:
1360 setAlign = LYX_ALIGN_LEFT;
1363 case LyXTabular::M_ALIGN_RIGHT:
1364 case LyXTabular::ALIGN_RIGHT:
1365 setAlign = LYX_ALIGN_RIGHT;
1368 case LyXTabular::M_ALIGN_CENTER:
1369 case LyXTabular::ALIGN_CENTER:
1370 setAlign = LYX_ALIGN_CENTER;
1373 case LyXTabular::ALIGN_BLOCK:
1374 setAlign = LYX_ALIGN_BLOCK;
1377 case LyXTabular::M_VALIGN_TOP:
1378 case LyXTabular::VALIGN_TOP:
1379 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1382 case LyXTabular::M_VALIGN_BOTTOM:
1383 case LyXTabular::VALIGN_BOTTOM:
1384 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1387 case LyXTabular::M_VALIGN_MIDDLE:
1388 case LyXTabular::VALIGN_MIDDLE:
1389 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1396 recordUndo(cur, Undo::ATOMIC);
1398 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1399 row_type const row = tabular.row_of_cell(cur.idx());
1400 col_type const column = tabular.column_of_cell(cur.idx());
1402 LyXTabular::ltType ltt;
1406 case LyXTabular::SET_PWIDTH: {
1407 LyXLength const len(value);
1408 tabular.setColumnPWidth(cur.idx(), len);
1410 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1411 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1412 else if (!len.zero()
1413 && tabular.getAlignment(cur.idx(), true) != LYX_ALIGN_BLOCK)
1414 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1418 case LyXTabular::SET_MPWIDTH:
1419 tabular.setMColumnPWidth(cur.idx(), LyXLength(value));
1422 case LyXTabular::SET_SPECIAL_COLUMN:
1423 case LyXTabular::SET_SPECIAL_MULTI:
1424 tabular.setAlignSpecial(cur.idx(),value,feature);
1427 case LyXTabular::APPEND_ROW:
1428 // append the row into the tabular
1429 tabular.appendRow(bv.buffer()->params(), cur.idx());
1432 case LyXTabular::APPEND_COLUMN:
1433 // append the column into the tabular
1434 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1435 cur.idx() = tabular.getCellNumber(row, column);
1438 case LyXTabular::DELETE_ROW:
1439 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1440 tabular.deleteRow(sel_row_start);
1441 if (sel_row_start >= tabular.rows())
1443 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1446 cur.selection() = false;
1449 case LyXTabular::DELETE_COLUMN:
1450 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1451 tabular.deleteColumn(sel_col_start);
1452 if (sel_col_start >= tabular.columns())
1454 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1457 cur.selection() = false;
1460 case LyXTabular::M_TOGGLE_LINE_TOP:
1462 case LyXTabular::TOGGLE_LINE_TOP: {
1463 bool lineSet = !tabular.topLine(cur.idx(), flag);
1464 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1465 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1467 tabular.getCellNumber(i, j),
1472 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1474 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1475 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1476 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1477 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1478 tabular.setBottomLine(
1479 tabular.getCellNumber(i, j),
1485 case LyXTabular::M_TOGGLE_LINE_LEFT:
1487 case LyXTabular::TOGGLE_LINE_LEFT: {
1488 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1489 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1490 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1491 tabular.setLeftLine(
1492 tabular.getCellNumber(i,j),
1498 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1500 case LyXTabular::TOGGLE_LINE_RIGHT: {
1501 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1502 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1503 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1504 tabular.setRightLine(
1505 tabular.getCellNumber(i,j),
1511 case LyXTabular::M_ALIGN_LEFT:
1512 case LyXTabular::M_ALIGN_RIGHT:
1513 case LyXTabular::M_ALIGN_CENTER:
1515 case LyXTabular::ALIGN_LEFT:
1516 case LyXTabular::ALIGN_RIGHT:
1517 case LyXTabular::ALIGN_CENTER:
1518 case LyXTabular::ALIGN_BLOCK:
1519 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1520 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1521 tabular.setAlignment(
1522 tabular.getCellNumber(i, j),
1527 case LyXTabular::M_VALIGN_TOP:
1528 case LyXTabular::M_VALIGN_BOTTOM:
1529 case LyXTabular::M_VALIGN_MIDDLE:
1531 case LyXTabular::VALIGN_TOP:
1532 case LyXTabular::VALIGN_BOTTOM:
1533 case LyXTabular::VALIGN_MIDDLE:
1534 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1535 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1536 tabular.setVAlignment(
1537 tabular.getCellNumber(i, j),
1541 case LyXTabular::MULTICOLUMN: {
1542 if (sel_row_start != sel_row_end) {
1543 #ifdef WITH_WARNINGS
1544 #warning Need I say it ? This is horrible.
1546 Alert::error(_("Error setting multicolumn"),
1547 _("You cannot set multicolumn vertically."));
1550 if (!cur.selection()) {
1551 // just multicol for one single cell
1552 // check whether we are completely in a multicol
1553 if (tabular.isMultiColumn(cur.idx()))
1554 tabular.unsetMultiColumn(cur.idx());
1556 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1559 // we have a selection so this means we just add all this
1560 // cells to form a multicolumn cell
1561 idx_type const s_start = cur.selBegin().idx();
1562 idx_type const s_end = cur.selEnd().idx();
1563 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1564 cur.idx() = s_start;
1567 cur.selection() = false;
1571 case LyXTabular::SET_ALL_LINES:
1573 case LyXTabular::UNSET_ALL_LINES:
1574 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1575 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1576 tabular.setAllLines(
1577 tabular.getCellNumber(i,j), setLines);
1580 case LyXTabular::SET_LONGTABULAR:
1581 tabular.setLongTabular(true);
1584 case LyXTabular::UNSET_LONGTABULAR:
1585 tabular.setLongTabular(false);
1588 case LyXTabular::SET_ROTATE_TABULAR:
1589 tabular.setRotateTabular(true);
1592 case LyXTabular::UNSET_ROTATE_TABULAR:
1593 tabular.setRotateTabular(false);
1596 case LyXTabular::SET_ROTATE_CELL:
1597 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1598 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1599 tabular.setRotateCell(
1600 tabular.getCellNumber(i, j), true);
1603 case LyXTabular::UNSET_ROTATE_CELL:
1604 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1605 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1606 tabular.setRotateCell(
1607 tabular.getCellNumber(i, j), false);
1610 case LyXTabular::SET_USEBOX: {
1611 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1612 if (val == tabular.getUsebox(cur.idx()))
1613 val = LyXTabular::BOX_NONE;
1614 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1615 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1616 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1620 case LyXTabular::UNSET_LTFIRSTHEAD:
1622 case LyXTabular::SET_LTFIRSTHEAD:
1623 tabular.getRowOfLTFirstHead(row, ltt);
1624 checkLongtableSpecial(ltt, value, flag);
1625 tabular.setLTHead(row, flag, ltt, true);
1628 case LyXTabular::UNSET_LTHEAD:
1630 case LyXTabular::SET_LTHEAD:
1631 tabular.getRowOfLTHead(row, ltt);
1632 checkLongtableSpecial(ltt, value, flag);
1633 tabular.setLTHead(row, flag, ltt, false);
1636 case LyXTabular::UNSET_LTFOOT:
1638 case LyXTabular::SET_LTFOOT:
1639 tabular.getRowOfLTFoot(row, ltt);
1640 checkLongtableSpecial(ltt, value, flag);
1641 tabular.setLTFoot(row, flag, ltt, false);
1644 case LyXTabular::UNSET_LTLASTFOOT:
1646 case LyXTabular::SET_LTLASTFOOT:
1647 tabular.getRowOfLTLastFoot(row, ltt);
1648 checkLongtableSpecial(ltt, value, flag);
1649 tabular.setLTFoot(row, flag, ltt, true);
1652 case LyXTabular::SET_LTNEWPAGE:
1653 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1656 // dummy stuff just to avoid warnings
1657 case LyXTabular::LAST_ACTION:
1661 InsetTabularMailer(*this).updateDialog(&bv);
1665 bool InsetTabular::showInsetDialog(BufferView * bv) const
1667 InsetTabularMailer(*this).showDialog(bv);
1672 void InsetTabular::openLayoutDialog(BufferView * bv) const
1674 InsetTabularMailer(*this).showDialog(bv);
1678 void InsetTabular::getLabelList(Buffer const & buffer,
1679 vector<string> & list) const
1681 tabular.getLabelList(buffer, list);
1685 bool InsetTabular::copySelection(LCursor & cur)
1687 if (!cur.selection())
1692 getSelection(cur, rs, re, cs, ce);
1694 paste_tabular.reset(new LyXTabular(tabular));
1696 for (row_type i = 0; i < rs; ++i)
1697 paste_tabular->deleteRow(0);
1699 row_type const rows = re - rs + 1;
1700 while (paste_tabular->rows() > rows)
1701 paste_tabular->deleteRow(rows);
1703 paste_tabular->setTopLine(0, true, true);
1704 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1707 for (col_type i = 0; i < cs; ++i)
1708 paste_tabular->deleteColumn(0);
1710 col_type const columns = ce - cs + 1;
1711 while (paste_tabular->columns() > columns)
1712 paste_tabular->deleteColumn(columns);
1714 paste_tabular->setLeftLine(0, true, true);
1715 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1719 OutputParams const runparams;
1720 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1721 cur.bv().stuffClipboard(os.str());
1726 bool InsetTabular::pasteSelection(LCursor & cur)
1730 col_type const actcol = tabular.column_of_cell(cur.idx());
1731 row_type const actrow = tabular.row_of_cell(cur.idx());
1732 for (row_type r1 = 0, r2 = actrow;
1733 r1 < paste_tabular->rows() && r2 < tabular.rows();
1735 for (col_type c1 = 0, c2 = actcol;
1736 c1 < paste_tabular->columns() && c2 < tabular.columns();
1738 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1739 tabular.isPartOfMultiColumn(r2, c2))
1741 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1745 if (tabular.isPartOfMultiColumn(r2, c2)) {
1749 shared_ptr<InsetText> inset(
1750 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1751 tabular.setCellInset(r2, c2, inset);
1760 void InsetTabular::cutSelection(LCursor & cur)
1762 if (!cur.selection())
1765 bool const track = cur.buffer().params().tracking_changes;
1768 getSelection(cur, rs, re, cs, ce);
1769 for (row_type i = rs; i <= re; ++i)
1770 for (col_type j = cs; j <= ce; ++j)
1771 cell(tabular.getCellNumber(i, j))->clear(track);
1773 // cursor position might be invalid now
1774 cur.pos() = cur.lastpos();
1775 cur.clearSelection();
1779 bool InsetTabular::isRightToLeft(LCursor & cur) const
1781 BOOST_ASSERT(cur.depth() > 1);
1782 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1783 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1784 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1785 parentpos).language()->RightToLeft();
1789 void InsetTabular::getSelection(LCursor & cur,
1790 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1792 CursorSlice const & beg = cur.selBegin();
1793 CursorSlice const & end = cur.selEnd();
1794 cs = tabular.column_of_cell(beg.idx());
1795 ce = tabular.column_of_cell(end.idx());
1798 cs = tabular.column_of_cell(end.idx());
1800 ce = tabular.right_column_of_cell(end.idx());
1803 rs = tabular.row_of_cell(beg.idx());
1804 re = tabular.row_of_cell(end.idx());
1810 LyXText * InsetTabular::getText(int idx) const
1812 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1816 void InsetTabular::markErased()
1818 for (idx_type idx = 0; idx < nargs(); ++idx)
1819 cell(idx)->markErased();
1823 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1826 idx_type const cell = tabular.getCellFromInset(in);
1827 // FIXME: getCellFromInset() returns now always a valid cell, so
1828 // the stuff below can be deleted, and instead we have:
1829 return tabular.getPWidth(cell).zero();
1832 return tabular.getPWidth(cell).zero();
1834 // this is a workaround for a crash (New, Insert->Tabular,
1835 // Insert->FootNote)
1839 // well we didn't obviously find it so maybe our owner knows more
1840 BOOST_ASSERT(owner());
1841 return owner()->forceDefaultParagraphs(in);
1847 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1850 if (buf.length() <= 0)
1855 col_type maxCols = 1;
1856 string::size_type const len = buf.length();
1857 string::size_type p = 0;
1859 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1867 maxCols = max(cols, maxCols);
1873 maxCols = max(cols, maxCols);
1874 LyXTabular * loctab;
1879 paste_tabular.reset(
1880 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1881 loctab = paste_tabular.get();
1885 cell = bv.cursor().idx();
1886 ocol = tabular.column_of_cell(cell);
1887 row = tabular.row_of_cell(cell);
1890 string::size_type op = 0;
1891 idx_type const cells = loctab->getNumberOfCells();
1894 rows = loctab->rows();
1895 col_type const columns = loctab->columns();
1897 while (cell < cells && p < len && row < rows &&
1898 (p = buf.find_first_of("\t\n", p)) != string::npos)
1904 // we can only set this if we are not too far right
1905 if (cols < columns) {
1906 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1907 Paragraph & par = inset->text_.getPar(0);
1908 LyXFont const font = inset->text_.getFont(par, 0);
1909 inset->setText(buf.substr(op, p - op), font);
1915 // we can only set this if we are not too far right
1916 if (cols < columns) {
1917 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1918 Paragraph & par = inset->text_.getPar(0);
1919 LyXFont const font = inset->text_.getFont(par, 0);
1920 inset->setText(buf.substr(op, p - op), font);
1925 cell = loctab->getCellNumber(row, cols);
1931 // check for the last cell if there is no trailing '\n'
1932 if (cell < cells && op < len) {
1933 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1934 Paragraph & par = inset->text_.getPar(0);
1935 LyXFont const font = inset->text_.getFont(par, 0);
1936 inset->setText(buf.substr(op, len - op), font);
1942 void InsetTabular::addPreview(PreviewLoader & loader) const
1944 row_type const rows = tabular.rows();
1945 col_type const columns = tabular.columns();
1946 for (row_type i = 0; i < rows; ++i) {
1947 for (col_type j = 0; j < columns; ++j)
1948 tabular.getCellInset(i, j)->addPreview(loader);
1953 bool InsetTabular::tablemode(LCursor & cur) const
1955 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1962 string const InsetTabularMailer::name_("tabular");
1964 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1965 : inset_(const_cast<InsetTabular &>(inset))
1969 string const InsetTabularMailer::inset2string(Buffer const &) const
1971 return params2string(inset_);
1975 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1977 istringstream data(in);
1979 lex.setStream(data);
1986 if (!lex || token != name_)
1987 return print_mailer_error("InsetTabularMailer", in, 1,
1990 // This is part of the inset proper that is usually swallowed
1991 // by Buffer::readInset
1993 if (!lex || token != "Tabular")
1994 return print_mailer_error("InsetTabularMailer", in, 2,
1997 Buffer const & buffer = inset.buffer();
1998 inset.read(buffer, lex);
2002 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2005 data << name_ << ' ';
2006 inset.write(inset.buffer(), data);
2007 data << "\\end_inset\n";