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::tabularStackDirty;
51 using lyx::graphics::PreviewLoader;
53 using lyx::support::ltrim;
55 using boost::shared_ptr;
61 using std::istringstream;
63 using std::ostringstream;
70 int const ADD_TO_HEIGHT = 2;
71 int const ADD_TO_TABULAR_WIDTH = 2;
74 boost::scoped_ptr<LyXTabular> paste_tabular;
77 struct TabularFeature {
78 LyXTabular::Feature action;
83 TabularFeature tabularFeature[] =
85 { LyXTabular::APPEND_ROW, "append-row" },
86 { LyXTabular::APPEND_COLUMN, "append-column" },
87 { LyXTabular::DELETE_ROW, "delete-row" },
88 { LyXTabular::DELETE_COLUMN, "delete-column" },
89 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
90 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
91 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
92 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
93 { LyXTabular::ALIGN_LEFT, "align-left" },
94 { LyXTabular::ALIGN_RIGHT, "align-right" },
95 { LyXTabular::ALIGN_CENTER, "align-center" },
96 { LyXTabular::ALIGN_BLOCK, "align-block" },
97 { LyXTabular::VALIGN_TOP, "valign-top" },
98 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
99 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
100 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
101 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
102 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
103 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
104 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
105 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
106 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
107 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
108 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
109 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
110 { LyXTabular::MULTICOLUMN, "multicolumn" },
111 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
112 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
113 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
114 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
115 { LyXTabular::SET_PWIDTH, "set-pwidth" },
116 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
117 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
118 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
119 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
120 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
121 { LyXTabular::SET_USEBOX, "set-usebox" },
122 { LyXTabular::SET_LTHEAD, "set-lthead" },
123 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
124 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
125 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
126 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
127 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
128 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
129 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
130 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
131 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
132 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
133 { LyXTabular::LAST_ACTION, "" }
137 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
139 FeatureEqual(LyXTabular::Feature feature)
140 : feature_(feature) {}
141 bool operator()(TabularFeature const & tf) const {
142 return tf.action == feature_;
145 LyXTabular::Feature feature_;
151 string const featureAsString(LyXTabular::Feature feature)
153 TabularFeature * end = tabularFeature +
154 sizeof(tabularFeature) / sizeof(TabularFeature);
155 TabularFeature * it = std::find_if(tabularFeature, end,
156 FeatureEqual(feature));
157 return (it == end) ? string() : it->feature;
161 bool InsetTabular::hasPasteBuffer() const
163 return (paste_tabular.get() != 0);
167 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
169 : tabular(buf.params(), max(rows, row_type(1)),
170 max(columns, col_type(1))), buffer_(&buf), scx_(0)
174 InsetTabular::InsetTabular(InsetTabular const & tab)
175 : InsetOld(tab), tabular(tab.tabular),
176 buffer_(tab.buffer_), scx_(0)
180 InsetTabular::~InsetTabular()
182 InsetTabularMailer(*this).hideDialog();
186 auto_ptr<InsetBase> InsetTabular::doClone() const
188 return auto_ptr<InsetBase>(new InsetTabular(*this));
192 Buffer const & InsetTabular::buffer() const
198 void InsetTabular::buffer(Buffer const * b)
204 void InsetTabular::write(Buffer const & buf, ostream & os) const
206 os << "Tabular" << endl;
207 tabular.write(buf, os);
211 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
213 bool const old_format = (lex.getString() == "\\LyXTable");
215 tabular.read(buf, lex);
221 string token = lex.getString();
222 while (lex.isOK() && (token != "\\end_inset")) {
224 token = lex.getString();
226 if (token != "\\end_inset") {
227 lex.printError("Missing \\end_inset at this point. "
233 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
235 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
236 // mi.base.textwidth << "\n";
238 lyxerr << "InsetTabular::metrics: need bv" << endl;
243 for (idx_type cell = 0; i < tabular.rows(); ++i) {
246 for (col_type j = 0; j < tabular.columns(); ++j) {
247 if (tabular.isPartOfMultiColumn(i, j))
251 LyXLength p_width = tabular.column_info[j].p_width;
253 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
254 tabular.getCellInset(cell)->metrics(m, dim);
256 dim.wid = m.base.textwidth;
257 maxAsc = max(maxAsc, dim.asc);
258 maxDesc = max(maxDesc, dim.des);
259 tabular.setWidthOfCell(cell, dim.wid);
262 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
263 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
266 dim.asc = tabular.getAscentOfRow(0);
267 dim.des = tabular.getHeightOfTabular() - dim.asc;
268 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
273 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
275 setPosCache(pi, x, y);
277 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
278 BufferView * bv = pi.base.bv;
280 static NullPainter nop;
281 static PainterInfo nullpi(bv, nop);
283 //resetPos(bv->cursor());
286 x += ADD_TO_TABULAR_WIDTH;
289 first_visible_cell = LyXTabular::npos;
290 for (row_type i = 0; i < tabular.rows(); ++i) {
292 int const a = tabular.getAscentOfRow(i);
293 int const d = tabular.getDescentOfRow(i);
294 idx = tabular.getCellNumber(i, 0);
295 for (col_type j = 0; j < tabular.columns(); ++j) {
296 if (tabular.isPartOfMultiColumn(i, j))
298 if (first_visible_cell == LyXTabular::npos)
299 first_visible_cell = idx;
301 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
302 if (nx + tabular.getWidthOfColumn(idx) < 0
303 || nx > bv->workWidth()
305 || y - a > bv->workHeight()) {
306 cell(idx)->draw(nullpi, cx, y);
307 drawCellLines(nop, nx, y, i, idx);
309 cell(idx)->draw(pi, cx, y);
310 drawCellLines(pi.pain, nx, y, i, idx);
312 nx += tabular.getWidthOfColumn(idx);
316 if (i + 1 < tabular.rows())
317 y += d + tabular.getAscentOfRow(i + 1) +
318 tabular.getAdditionalHeight(i + 1);
323 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
325 setPosCache(pi, x, y);
327 LCursor & cur = pi.base.bv->cursor();
328 if (!cur.selection())
330 if (!ptr_cmp(&cur.inset(), this))
335 x += scx_ + ADD_TO_TABULAR_WIDTH;
337 if (tablemode(cur)) {
340 getSelection(cur, rs, re, cs, ce);
341 y -= tabular.getAscentOfRow(0);
342 for (row_type j = 0; j < tabular.rows(); ++j) {
343 int const a = tabular.getAscentOfRow(j);
344 int const h = a + tabular.getDescentOfRow(j);
346 y += tabular.getAdditionalHeight(j);
347 for (col_type i = 0; i < tabular.columns(); ++i) {
348 if (tabular.isPartOfMultiColumn(j, i))
350 idx_type const cell =
351 tabular.getCellNumber(j, i);
352 int const w = tabular.getWidthOfColumn(cell);
353 if (i >= cs && i <= ce && j >= rs && j <= re)
354 pi.pain.fillRectangle(xx, y, w, h,
363 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
368 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
369 row_type row, idx_type cell) const
371 int x2 = x + tabular.getWidthOfColumn(cell);
374 if (!tabular.topAlreadyDrawn(cell)) {
375 on_off = !tabular.topLine(cell);
376 pain.line(x, y - tabular.getAscentOfRow(row),
377 x2, y - tabular.getAscentOfRow(row),
378 on_off ? LColor::tabularonoffline : LColor::tabularline,
379 on_off ? Painter::line_onoffdash : Painter::line_solid);
381 on_off = !tabular.bottomLine(cell);
382 pain.line(x, y + tabular.getDescentOfRow(row),
383 x2, y + tabular.getDescentOfRow(row),
384 on_off ? LColor::tabularonoffline : LColor::tabularline,
385 on_off ? Painter::line_onoffdash : Painter::line_solid);
386 if (!tabular.leftAlreadyDrawn(cell)) {
387 on_off = !tabular.leftLine(cell);
388 pain.line(x, y - tabular.getAscentOfRow(row),
389 x, y + tabular.getDescentOfRow(row),
390 on_off ? LColor::tabularonoffline : LColor::tabularline,
391 on_off ? Painter::line_onoffdash : Painter::line_solid);
393 on_off = !tabular.rightLine(cell);
394 pain.line(x2 - tabular.getAdditionalWidth(cell),
395 y - tabular.getAscentOfRow(row),
396 x2 - tabular.getAdditionalWidth(cell),
397 y + tabular.getDescentOfRow(row),
398 on_off ? LColor::tabularonoffline : LColor::tabularline,
399 on_off ? Painter::line_onoffdash : Painter::line_solid);
403 string const InsetTabular::editMessage() const
405 return _("Opened table");
409 void InsetTabular::edit(LCursor & cur, bool left)
411 lyxerr << "InsetTabular::edit: " << this << endl;
413 cur.selection() = false;
416 if (isRightToLeft(cur))
417 cur.idx() = tabular.getLastCellInRow(0);
423 if (isRightToLeft(cur))
424 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
426 cur.idx() = tabular.getNumberOfCells() - 1;
428 cur.pos() = cur.lastpos(); // FIXME crude guess
430 // this accesses the position cache before it is initialized
432 //cur.bv().fitCursor();
436 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
438 lyxerr << "# InsetTabular::dispatch: cmd: " << cmd << endl;
439 lyxerr << " cur:\n" << cur << endl;
440 CursorSlice sl = cur.top();
441 LCursor & bvcur = cur.bv().cursor();
443 switch (cmd.action) {
445 case LFUN_MOUSE_PRESS:
446 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
448 if (cmd.button() == mouse_button::button1) {
449 cur.selection() = false;
450 setCursorFromCoordinates(cur, cmd.x, cmd.y);
456 if (cmd.button() == mouse_button::button2) {
457 // FIXME: pasting multiple cells (with insettabular's own
458 // LFUN_PASTESELECTION still does not work! (jspitzm)
459 cmd = FuncRequest(LFUN_PASTESELECTION, "paragraph");
460 cell(cur.idx())->dispatch(cur, cmd);
464 // we'll pop up the table dialog on release
465 if (cmd.button() == mouse_button::button3)
469 case LFUN_MOUSE_MOTION:
470 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
471 if (cmd.button() == mouse_button::button1) {
472 // only accept motions to places not deeper nested than the real anchor
473 if (bvcur.anchor_.hasPart(cur)) {
474 setCursorFromCoordinates(cur, cmd.x, cmd.y);
475 bvcur.setCursor(cur);
476 bvcur.selection() = true;
482 case LFUN_MOUSE_RELEASE:
483 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
484 if (cmd.button() == mouse_button::button3)
485 InsetTabularMailer(*this).showDialog(&cur.bv());
488 case LFUN_CELL_BACKWARD:
490 cur.selection() = false;
493 case LFUN_CELL_FORWARD:
495 cur.selection() = false;
500 cell(cur.idx())->dispatch(cur, cmd);
501 cur.dispatched(); // override the cell's decision
503 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
504 if (sl == cur.top()) {
505 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
512 cell(cur.idx())->dispatch(cur, cmd);
513 cur.dispatched(); // override the cell's decision
515 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
516 if (sl == cur.top()) {
517 cmd = FuncRequest(LFUN_FINISHED_LEFT);
524 cell(cur.idx())->dispatch(cur, cmd);
525 cur.dispatched(); // override the cell's decision
527 // if our LyXText didn't do anything to the cursor
528 // then we try to put the cursor into the cell below
529 // setting also the right targetX.
530 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
531 cur.idx() = tabular.getCellBelow(cur.idx());
533 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
534 cur.pit(), 0, cur.targetX());
536 if (sl == cur.top()) {
537 // we trick it to go to the RIGHT after leaving the
539 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
546 cell(cur.idx())->dispatch(cur, cmd);
547 cur.dispatched(); // override the cell's decision
549 // if our LyXText didn't do anything to the cursor
550 // then we try to put the cursor into the cell above
551 // setting also the right targetX.
552 if (tabular.row_of_cell(cur.idx()) != 0) {
553 cur.idx() = tabular.getCellAbove(cur.idx());
554 cur.pit() = cur.lastpit();
555 LyXText const * text = cell(cur.idx())->getText(0);
556 cur.pos() = text->x2pos(
558 text->paragraphs().back().rows().size()-1,
561 if (sl == cur.top()) {
562 cmd = FuncRequest(LFUN_FINISHED_UP);
568 // //if (hasSelection())
569 // // cur.selection() = false;
570 // col_type const col = tabular.column_of_cell(cur.idx());
571 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
572 // if (t < yo() + tabular.getHeightOfTabular()) {
573 // cur.bv().scrollDocView(t);
574 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
576 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
583 // case LFUN_PRIOR: {
584 // //if (hasSelection())
585 // // cur.selection() = false;
586 // col_type const col = tabular.column_of_cell(cur.idx());
587 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
589 // cur.bv().scrollDocView(t);
593 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
597 // cur.par() = cur.lastpar();
598 // cur.pos() = cur.lastpos();
602 case LFUN_LAYOUT_TABULAR:
603 InsetTabularMailer(*this).showDialog(&cur.bv());
606 case LFUN_INSET_DIALOG_UPDATE:
607 InsetTabularMailer(*this).updateDialog(&cur.bv());
610 case LFUN_TABULAR_FEATURE:
611 if (!tabularFeatures(cur, cmd.argument))
615 // insert file functions
616 case LFUN_FILE_INSERT_ASCII_PARA:
617 case LFUN_FILE_INSERT_ASCII: {
618 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
619 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
625 if (tablemode(cur)) {
626 if (copySelection(cur)) {
627 recordUndo(cur, Undo::DELETE);
632 cell(cur.idx())->dispatch(cur, cmd);
637 recordUndo(cur, Undo::DELETE);
641 cell(cur.idx())->dispatch(cur, cmd);
645 if (!cur.selection())
647 if (tablemode(cur)) {
651 cell(cur.idx())->dispatch(cur, cmd);
654 case LFUN_PASTESELECTION: {
655 string const clip = cur.bv().getClipboard();
658 if (clip.find('\t') != string::npos) {
661 col_type maxCols = 1;
662 size_t len = clip.length();
663 for (size_t p = 0; p < len; ++p) {
664 p = clip.find_first_of("\t\n", p);
665 if (p == string::npos)
674 maxCols = max(cols, maxCols);
679 maxCols = max(cols, maxCols);
682 new LyXTabular(cur.buffer().params(), rows, maxCols));
684 string::size_type op = 0;
686 idx_type const cells =
687 paste_tabular->getNumberOfCells();
690 for (size_t p = 0; cell < cells && p < len; ++p) {
691 p = clip.find_first_of("\t\n", p);
692 if (p == string::npos || p >= len)
696 paste_tabular->getCellInset(cell)->
697 setText(clip.substr(op, p - op), font);
702 paste_tabular->getCellInset(cell)->
703 setText(clip.substr(op, p - op), font);
704 while (cols++ < maxCols)
711 // check for the last cell if there is no trailing '\n'
712 if (cell < cells && op < len)
713 paste_tabular->getCellInset(cell)->
714 setText(clip.substr(op, len - op), font);
715 } else if (!insertAsciiString(cur.bv(), clip, true)) {
716 // so that the clipboard is used and it goes on
718 // and executes LFUN_PASTESELECTION in insettext!
719 paste_tabular.reset();
725 if (hasPasteBuffer() && tabularStackDirty()) {
726 recordUndo(cur, Undo::INSERT);
730 cell(cur.idx())->dispatch(cur, cmd);
741 case LFUN_FREEFONT_APPLY:
742 case LFUN_FREEFONT_UPDATE:
746 case LFUN_CAPITALIZE_WORD:
747 case LFUN_UPCASE_WORD:
748 case LFUN_LOWCASE_WORD:
749 case LFUN_TRANSPOSE_CHARS:
750 if (tablemode(cur)) {
753 getSelection(cur, rs, re, cs, ce);
754 for (row_type i = rs; i <= re; ++i) {
755 for (col_type j = cs; j <= ce; ++j) {
756 // cursor follows cell:
757 cur.idx() = tabular.getCellNumber(i, j);
758 // select this cell only:
761 cur.pos() = cur.top().lastpos();
764 cell(cur.idx())->dispatch(cur, cmd);
767 // Restore original selection
768 cur.idx() = tabular.getCellNumber(rs, cs);
771 cur.idx() = tabular.getCellNumber(re, ce);
772 cur.pos() = cur.top().lastpos();
777 cell(cur.idx())->dispatch(cur, cmd);
781 // we try to handle this event in the insets dispatch function.
782 cell(cur.idx())->dispatch(cur, cmd);
787 InsetTabularMailer(*this).updateDialog(&cur.bv());
791 // function sets an object as defined in func_status.h:
792 // states OK, Unknown, Disabled, On, Off.
793 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
794 FuncStatus & status) const
796 switch (cmd.action) {
797 case LFUN_TABULAR_FEATURE: {
798 int action = LyXTabular::LAST_ACTION;
800 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
801 string const tmp = tabularFeature[i].feature;
802 if (tmp == cmd.argument.substr(0, tmp.length())) {
803 action = tabularFeature[i].action;
807 if (action == LyXTabular::LAST_ACTION) {
809 status.unknown(true);
813 string const argument
814 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
816 row_type sel_row_start = 0;
817 row_type sel_row_end = 0;
819 LyXTabular::ltType dummyltt;
822 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
825 case LyXTabular::SET_PWIDTH:
826 case LyXTabular::SET_MPWIDTH:
827 case LyXTabular::SET_SPECIAL_COLUMN:
828 case LyXTabular::SET_SPECIAL_MULTI:
829 case LyXTabular::APPEND_ROW:
830 case LyXTabular::APPEND_COLUMN:
831 case LyXTabular::DELETE_ROW:
832 case LyXTabular::DELETE_COLUMN:
833 case LyXTabular::SET_ALL_LINES:
834 case LyXTabular::UNSET_ALL_LINES:
838 case LyXTabular::MULTICOLUMN:
839 status.setOnOff(tabular.isMultiColumn(cur.idx()));
842 case LyXTabular::M_TOGGLE_LINE_TOP:
844 case LyXTabular::TOGGLE_LINE_TOP:
845 status.setOnOff(tabular.topLine(cur.idx(), flag));
848 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
850 case LyXTabular::TOGGLE_LINE_BOTTOM:
851 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
854 case LyXTabular::M_TOGGLE_LINE_LEFT:
856 case LyXTabular::TOGGLE_LINE_LEFT:
857 status.setOnOff(tabular.leftLine(cur.idx(), flag));
860 case LyXTabular::M_TOGGLE_LINE_RIGHT:
862 case LyXTabular::TOGGLE_LINE_RIGHT:
863 status.setOnOff(tabular.rightLine(cur.idx(), flag));
866 case LyXTabular::M_ALIGN_LEFT:
868 case LyXTabular::ALIGN_LEFT:
869 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
872 case LyXTabular::M_ALIGN_RIGHT:
874 case LyXTabular::ALIGN_RIGHT:
875 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
878 case LyXTabular::M_ALIGN_CENTER:
880 case LyXTabular::ALIGN_CENTER:
881 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
884 case LyXTabular::ALIGN_BLOCK:
885 status.enabled(!tabular.getPWidth(cur.idx()).zero());
886 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
889 case LyXTabular::M_VALIGN_TOP:
891 case LyXTabular::VALIGN_TOP:
893 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
896 case LyXTabular::M_VALIGN_BOTTOM:
898 case LyXTabular::VALIGN_BOTTOM:
900 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
903 case LyXTabular::M_VALIGN_MIDDLE:
905 case LyXTabular::VALIGN_MIDDLE:
907 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
910 case LyXTabular::SET_LONGTABULAR:
911 status.setOnOff(tabular.isLongTabular());
914 case LyXTabular::UNSET_LONGTABULAR:
915 status.setOnOff(!tabular.isLongTabular());
918 case LyXTabular::SET_ROTATE_TABULAR:
919 status.setOnOff(tabular.getRotateTabular());
922 case LyXTabular::UNSET_ROTATE_TABULAR:
923 status.setOnOff(!tabular.getRotateTabular());
926 case LyXTabular::SET_ROTATE_CELL:
927 status.setOnOff(tabular.getRotateCell(cur.idx()));
930 case LyXTabular::UNSET_ROTATE_CELL:
931 status.setOnOff(!tabular.getRotateCell(cur.idx()));
934 case LyXTabular::SET_USEBOX:
935 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
938 case LyXTabular::SET_LTFIRSTHEAD:
939 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
942 case LyXTabular::UNSET_LTFIRSTHEAD:
943 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
946 case LyXTabular::SET_LTHEAD:
947 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
950 case LyXTabular::UNSET_LTHEAD:
951 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
954 case LyXTabular::SET_LTFOOT:
955 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
958 case LyXTabular::UNSET_LTFOOT:
959 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
962 case LyXTabular::SET_LTLASTFOOT:
963 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
966 case LyXTabular::UNSET_LTLASTFOOT:
967 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
970 case LyXTabular::SET_LTNEWPAGE:
971 status.setOnOff(tabular.getLTNewPage(sel_row_start));
976 status.enabled(false);
982 // These are only enabled inside tabular
983 case LFUN_CELL_BACKWARD:
984 case LFUN_CELL_FORWARD:
985 status.enabled(true);
988 // disable these with multiple cells selected
989 case LFUN_INSET_INSERT:
990 case LFUN_TABULAR_INSERT:
991 case LFUN_INSERT_CHARSTYLE:
992 case LFUN_INSET_FLOAT:
993 case LFUN_INSET_WIDE_FLOAT:
994 case LFUN_INSET_FOOTNOTE:
995 case LFUN_INSET_MARGINAL:
996 case LFUN_INSERT_MATH:
998 case LFUN_MATH_MUTATE:
999 case LFUN_MATH_DISPLAY:
1000 case LFUN_INSERT_NOTE:
1001 case LFUN_INSET_OPTARG:
1002 case LFUN_INSERT_BOX:
1003 case LFUN_INSERT_BRANCH:
1004 case LFUN_INSET_WRAP:
1005 case LFUN_INSET_ERT: {
1006 if (tablemode(cur)) {
1007 status.enabled(false);
1010 return cell(cur.idx())->getStatus(cur, cmd, status);
1013 // disable in non-fixed-width cells
1014 case LFUN_BREAKLINE:
1015 case LFUN_BREAKPARAGRAPH:
1016 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1017 case LFUN_BREAKPARAGRAPH_SKIP: {
1018 if (tabular.getPWidth(cur.idx()).zero()) {
1019 status.enabled(false);
1022 return cell(cur.idx())->getStatus(cur, cmd, status);
1025 case LFUN_INSET_MODIFY:
1026 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1027 status.enabled(true);
1033 // we try to handle this event in the insets dispatch function.
1034 return cell(cur.idx())->getStatus(cur, cmd, status);
1039 int InsetTabular::latex(Buffer const & buf, ostream & os,
1040 OutputParams const & runparams) const
1042 return tabular.latex(buf, os, runparams);
1046 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1047 OutputParams const & runparams) const
1049 int const dp = runparams.linelen ? runparams.depth : 0;
1050 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1054 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1055 OutputParams const & runparams) const
1057 return tabular.linuxdoc(buf,os, runparams);
1061 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1062 OutputParams const & runparams) const
1065 InsetBase * master = 0;
1067 #ifdef WITH_WARNINGS
1068 #warning Why not pass a proper DocIterator here?
1071 // if the table is inside a float it doesn't need the informaltable
1072 // wrapper. Search for it.
1073 for (master = owner(); master; master = master->owner())
1074 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1079 os << "<informaltable>";
1082 ret += tabular.docbook(buf, os, runparams);
1084 os << "</informaltable>";
1091 void InsetTabular::validate(LaTeXFeatures & features) const
1093 tabular.validate(features);
1097 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1099 return tabular.getCellInset(idx);
1103 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1105 return tabular.getCellInset(idx);
1109 void InsetTabular::cursorPos
1110 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1112 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1114 // y offset correction
1115 int const row = tabular.row_of_cell(sl.idx());
1116 for (int i = 0; i <= row; ++i) {
1118 y += tabular.getAscentOfRow(i);
1119 y += tabular.getAdditionalHeight(i);
1122 y += tabular.getDescentOfRow(i);
1125 // x offset correction
1126 int const col = tabular.column_of_cell(sl.idx());
1127 int idx = tabular.getCellNumber(row, 0);
1128 for (int j = 0; j < col; ++j) {
1129 if (tabular.isPartOfMultiColumn(row, j))
1131 x += tabular.getWidthOfColumn(idx);
1134 x += tabular.getBeginningOfTextInCell(idx);
1135 x += ADD_TO_TABULAR_WIDTH;
1140 int InsetTabular::dist(idx_type const cell, int x, int y) const
1144 InsetBase const & inset = *tabular.getCellInset(cell);
1145 Point o = theCoords.getInsets().xy(&inset);
1146 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1147 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1148 int const ybeg = o.y_ - inset.ascent();
1149 row_type const row = tabular.row_of_cell(cell);
1150 int const rowheight = tabular.getAscentOfRow(row)
1151 + tabular.getDescentOfRow(row)
1152 + tabular.getAdditionalHeight(row);
1153 int const yend = ybeg + rowheight;
1165 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1166 // << " ybeg=" << ybeg << " yend=" << yend
1167 // << " xx=" << xx << " yy=" << yy
1168 // << " dist=" << xx + yy << endl;
1173 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1175 //lyxerr << "InsetTabular::editXY: " << this << endl;
1176 cur.selection() = false;
1178 cur.idx() = getNearestCell(x, y);
1180 return cell(cur.idx())->text_.editXY(cur, x, y);
1184 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1186 cur.idx() = getNearestCell(x, y);
1187 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1191 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1193 idx_type idx_min = 0;
1194 int dist_min = std::numeric_limits<int>::max();
1195 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1196 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1197 int const d = dist(i, x, y);
1208 int InsetTabular::getCellXPos(idx_type const cell) const
1212 for (; !tabular.isFirstCellInRow(c); --c)
1214 int lx = tabular.getWidthOfColumn(cell);
1215 for (; c < cell; ++c)
1216 lx += tabular.getWidthOfColumn(c);
1218 return lx - tabular.getWidthOfColumn(cell);
1222 void InsetTabular::resetPos(LCursor & cur) const
1224 BufferView & bv = cur.bv();
1225 int const maxwidth = bv.workWidth();
1227 if (&cur.inset() != this) {
1231 int const X2 = maxwidth;
1232 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1233 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1234 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1237 scx_ = X1 + 20 - x1;
1239 scx_ = 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());
1725 // mark tabular stack dirty
1726 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1727 // when we (hopefully) have a one-for-all paste mechanism.
1728 lyx::cap::dirtyTabularStack(true);
1734 bool InsetTabular::pasteSelection(LCursor & cur)
1738 col_type const actcol = tabular.column_of_cell(cur.idx());
1739 row_type const actrow = tabular.row_of_cell(cur.idx());
1740 for (row_type r1 = 0, r2 = actrow;
1741 r1 < paste_tabular->rows() && r2 < tabular.rows();
1743 for (col_type c1 = 0, c2 = actcol;
1744 c1 < paste_tabular->columns() && c2 < tabular.columns();
1746 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1747 tabular.isPartOfMultiColumn(r2, c2))
1749 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1753 if (tabular.isPartOfMultiColumn(r2, c2)) {
1757 shared_ptr<InsetText> inset(
1758 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1759 tabular.setCellInset(r2, c2, inset);
1768 void InsetTabular::cutSelection(LCursor & cur)
1770 if (!cur.selection())
1773 bool const track = cur.buffer().params().tracking_changes;
1776 getSelection(cur, rs, re, cs, ce);
1777 for (row_type i = rs; i <= re; ++i)
1778 for (col_type j = cs; j <= ce; ++j)
1779 cell(tabular.getCellNumber(i, j))->clear(track);
1781 // cursor position might be invalid now
1782 cur.pos() = cur.lastpos();
1783 cur.clearSelection();
1787 bool InsetTabular::isRightToLeft(LCursor & cur) const
1789 BOOST_ASSERT(cur.depth() > 1);
1790 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1791 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1792 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1793 parentpos).language()->RightToLeft();
1797 void InsetTabular::getSelection(LCursor & cur,
1798 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1800 CursorSlice const & beg = cur.selBegin();
1801 CursorSlice const & end = cur.selEnd();
1802 cs = tabular.column_of_cell(beg.idx());
1803 ce = tabular.column_of_cell(end.idx());
1806 cs = tabular.column_of_cell(end.idx());
1808 ce = tabular.right_column_of_cell(end.idx());
1811 rs = tabular.row_of_cell(beg.idx());
1812 re = tabular.row_of_cell(end.idx());
1818 LyXText * InsetTabular::getText(int idx) const
1820 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1824 void InsetTabular::markErased()
1826 for (idx_type idx = 0; idx < nargs(); ++idx)
1827 cell(idx)->markErased();
1831 bool InsetTabular::forceDefaultParagraphs(InsetBase const *) const
1834 idx_type const cell = tabular.getCellFromInset(in);
1835 // FIXME: getCellFromInset() returns now always a valid cell, so
1836 // the stuff below can be deleted, and instead we have:
1837 return tabular.getPWidth(cell).zero();
1840 return tabular.getPWidth(cell).zero();
1842 // this is a workaround for a crash (New, Insert->Tabular,
1843 // Insert->FootNote)
1847 // well we didn't obviously find it so maybe our owner knows more
1848 BOOST_ASSERT(owner());
1849 return owner()->forceDefaultParagraphs(in);
1855 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1858 if (buf.length() <= 0)
1863 col_type maxCols = 1;
1864 string::size_type const len = buf.length();
1865 string::size_type p = 0;
1867 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1875 maxCols = max(cols, maxCols);
1881 maxCols = max(cols, maxCols);
1882 LyXTabular * loctab;
1887 paste_tabular.reset(
1888 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1889 loctab = paste_tabular.get();
1893 cell = bv.cursor().idx();
1894 ocol = tabular.column_of_cell(cell);
1895 row = tabular.row_of_cell(cell);
1898 string::size_type op = 0;
1899 idx_type const cells = loctab->getNumberOfCells();
1902 rows = loctab->rows();
1903 col_type const columns = loctab->columns();
1905 while (cell < cells && p < len && row < rows &&
1906 (p = buf.find_first_of("\t\n", p)) != string::npos)
1912 // we can only set this if we are not too far right
1913 if (cols < columns) {
1914 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1915 Paragraph & par = inset->text_.getPar(0);
1916 LyXFont const font = inset->text_.getFont(par, 0);
1917 inset->setText(buf.substr(op, p - op), font);
1923 // we can only set this if we are not too far right
1924 if (cols < columns) {
1925 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1926 Paragraph & par = inset->text_.getPar(0);
1927 LyXFont const font = inset->text_.getFont(par, 0);
1928 inset->setText(buf.substr(op, p - op), font);
1933 cell = loctab->getCellNumber(row, cols);
1939 // check for the last cell if there is no trailing '\n'
1940 if (cell < cells && op < len) {
1941 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1942 Paragraph & par = inset->text_.getPar(0);
1943 LyXFont const font = inset->text_.getFont(par, 0);
1944 inset->setText(buf.substr(op, len - op), font);
1950 void InsetTabular::addPreview(PreviewLoader & loader) const
1952 row_type const rows = tabular.rows();
1953 col_type const columns = tabular.columns();
1954 for (row_type i = 0; i < rows; ++i) {
1955 for (col_type j = 0; j < columns; ++j)
1956 tabular.getCellInset(i, j)->addPreview(loader);
1961 bool InsetTabular::tablemode(LCursor & cur) const
1963 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1970 string const InsetTabularMailer::name_("tabular");
1972 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1973 : inset_(const_cast<InsetTabular &>(inset))
1977 string const InsetTabularMailer::inset2string(Buffer const &) const
1979 return params2string(inset_);
1983 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1985 istringstream data(in);
1987 lex.setStream(data);
1994 if (!lex || token != name_)
1995 return print_mailer_error("InsetTabularMailer", in, 1,
1998 // This is part of the inset proper that is usually swallowed
1999 // by Buffer::readInset
2001 if (!lex || token != "Tabular")
2002 return print_mailer_error("InsetTabularMailer", in, 2,
2005 Buffer const & buffer = inset.buffer();
2006 inset.read(buffer, lex);
2010 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2013 data << name_ << ' ';
2014 inset.write(inset.buffer(), data);
2015 data << "\\end_inset\n";