3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
37 #include "support/convert.h"
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
49 using lyx::cap::dirtyTabularStack;
50 using lyx::cap::tabularStackDirty;
52 using lyx::graphics::PreviewLoader;
54 using lyx::support::ltrim;
56 using lyx::frontend::Painter;
58 using boost::shared_ptr;
64 using std::istringstream;
66 using std::ostringstream;
73 int const ADD_TO_HEIGHT = 2;
74 int const ADD_TO_TABULAR_WIDTH = 2;
77 boost::scoped_ptr<LyXTabular> paste_tabular;
80 struct TabularFeature {
81 LyXTabular::Feature action;
86 TabularFeature tabularFeature[] =
88 { LyXTabular::APPEND_ROW, "append-row" },
89 { LyXTabular::APPEND_COLUMN, "append-column" },
90 { LyXTabular::DELETE_ROW, "delete-row" },
91 { LyXTabular::DELETE_COLUMN, "delete-column" },
92 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
93 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
94 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
95 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
96 { LyXTabular::ALIGN_LEFT, "align-left" },
97 { LyXTabular::ALIGN_RIGHT, "align-right" },
98 { LyXTabular::ALIGN_CENTER, "align-center" },
99 { LyXTabular::ALIGN_BLOCK, "align-block" },
100 { LyXTabular::VALIGN_TOP, "valign-top" },
101 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
102 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
103 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
104 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
105 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
106 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
107 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
108 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
109 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
110 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
111 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
112 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
113 { LyXTabular::MULTICOLUMN, "multicolumn" },
114 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
115 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
116 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
117 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
118 { LyXTabular::SET_PWIDTH, "set-pwidth" },
119 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
120 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
121 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
122 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
123 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
124 { LyXTabular::SET_USEBOX, "set-usebox" },
125 { LyXTabular::SET_LTHEAD, "set-lthead" },
126 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
127 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
128 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
129 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
130 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
131 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
132 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
133 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
134 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
135 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
136 { LyXTabular::LAST_ACTION, "" }
140 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
142 FeatureEqual(LyXTabular::Feature feature)
143 : feature_(feature) {}
144 bool operator()(TabularFeature const & tf) const {
145 return tf.action == feature_;
148 LyXTabular::Feature feature_;
154 string const featureAsString(LyXTabular::Feature feature)
156 TabularFeature * end = tabularFeature +
157 sizeof(tabularFeature) / sizeof(TabularFeature);
158 TabularFeature * it = std::find_if(tabularFeature, end,
159 FeatureEqual(feature));
160 return (it == end) ? string() : it->feature;
164 bool InsetTabular::hasPasteBuffer() const
166 return (paste_tabular.get() != 0);
170 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
172 : tabular(buf.params(), max(rows, row_type(1)),
173 max(columns, col_type(1))), buffer_(&buf), scx_(0)
177 InsetTabular::InsetTabular(InsetTabular const & tab)
178 : InsetOld(tab), tabular(tab.tabular),
179 buffer_(tab.buffer_), scx_(0)
183 InsetTabular::~InsetTabular()
185 InsetTabularMailer(*this).hideDialog();
189 auto_ptr<InsetBase> InsetTabular::doClone() const
191 return auto_ptr<InsetBase>(new InsetTabular(*this));
195 Buffer const & InsetTabular::buffer() const
201 void InsetTabular::buffer(Buffer const * b)
207 void InsetTabular::write(Buffer const & buf, ostream & os) const
209 os << "Tabular" << endl;
210 tabular.write(buf, os);
214 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
216 bool const old_format = (lex.getString() == "\\LyXTable");
218 tabular.read(buf, lex);
224 string token = lex.getString();
225 while (lex.isOK() && (token != "\\end_inset")) {
227 token = lex.getString();
229 if (token != "\\end_inset") {
230 lex.printError("Missing \\end_inset at this point. "
236 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
238 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
239 // mi.base.textwidth << "\n";
241 lyxerr << "InsetTabular::metrics: need bv" << endl;
246 for (idx_type cell = 0; i < tabular.rows(); ++i) {
249 for (col_type j = 0; j < tabular.columns(); ++j) {
250 if (tabular.isPartOfMultiColumn(i, j))
251 // Multicolumn cell, but not first one
256 if (tabular.cell_info[i][j].multicolumn ==
257 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
258 p_width = tabular.cellinfo_of_cell(cell).p_width;
260 p_width = tabular.column_info[j].p_width;
262 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
263 tabular.getCellInset(cell)->metrics(m, dim);
265 dim.wid = m.base.textwidth;
266 maxAsc = max(maxAsc, dim.asc);
267 maxDesc = max(maxDesc, dim.des);
268 tabular.setWidthOfCell(cell, dim.wid);
271 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
272 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
275 dim.asc = tabular.getAscentOfRow(0);
276 dim.des = tabular.getHeightOfTabular() - dim.asc;
277 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
282 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
284 setPosCache(pi, x, y);
286 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
287 BufferView * bv = pi.base.bv;
289 static lyx::frontend::NullPainter nop;
290 static PainterInfo nullpi(bv, nop);
292 //resetPos(bv->cursor());
295 x += ADD_TO_TABULAR_WIDTH;
298 first_visible_cell = LyXTabular::npos;
299 for (row_type i = 0; i < tabular.rows(); ++i) {
301 int const a = tabular.getAscentOfRow(i);
302 int const d = tabular.getDescentOfRow(i);
303 idx = tabular.getCellNumber(i, 0);
304 for (col_type j = 0; j < tabular.columns(); ++j) {
305 if (tabular.isPartOfMultiColumn(i, j))
307 if (first_visible_cell == LyXTabular::npos)
308 first_visible_cell = idx;
310 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
311 if (nx + tabular.getWidthOfColumn(idx) < 0
312 || nx > bv->workWidth()
314 || y - a > bv->workHeight()) {
315 cell(idx)->draw(nullpi, cx, y);
316 drawCellLines(nop, nx, y, i, idx, pi.erased_);
318 cell(idx)->draw(pi, cx, y);
319 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
321 nx += tabular.getWidthOfColumn(idx);
325 if (i + 1 < tabular.rows())
326 y += d + tabular.getAscentOfRow(i + 1) +
327 tabular.getAdditionalHeight(i + 1);
332 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
334 setPosCache(pi, x, y);
336 LCursor & cur = pi.base.bv->cursor();
338 x += scx_ + ADD_TO_TABULAR_WIDTH;
340 // Paint background of current tabular
341 int const w = tabular.getWidthOfTabular();
342 int const h = tabular.getHeightOfTabular();
343 int yy = y - tabular.getAscentOfRow(0);
344 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
346 if (!cur.selection())
348 if (!ptr_cmp(&cur.inset(), this))
354 if (tablemode(cur)) {
357 getSelection(cur, rs, re, cs, ce);
358 y -= tabular.getAscentOfRow(0);
359 for (row_type j = 0; j < tabular.rows(); ++j) {
360 int const a = tabular.getAscentOfRow(j);
361 int const h = a + tabular.getDescentOfRow(j);
363 y += tabular.getAdditionalHeight(j);
364 for (col_type i = 0; i < tabular.columns(); ++i) {
365 if (tabular.isPartOfMultiColumn(j, i))
367 idx_type const cell =
368 tabular.getCellNumber(j, i);
369 int const w = tabular.getWidthOfColumn(cell);
370 if (i >= cs && i <= ce && j >= rs && j <= re)
371 pi.pain.fillRectangle(xx, y, w, h,
380 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
385 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
386 row_type row, idx_type cell, bool erased) const
388 int x2 = x + tabular.getWidthOfColumn(cell);
390 LColor::color col = LColor::tabularline;
391 LColor::color onoffcol = LColor::tabularonoffline;
394 col = LColor::strikeout;
395 onoffcol = LColor::strikeout;
398 if (!tabular.topAlreadyDrawn(cell)) {
399 on_off = !tabular.topLine(cell);
400 pain.line(x, y - tabular.getAscentOfRow(row),
401 x2, y - tabular.getAscentOfRow(row),
402 on_off ? onoffcol : col,
403 on_off ? Painter::line_onoffdash : Painter::line_solid);
405 on_off = !tabular.bottomLine(cell);
406 pain.line(x, y + tabular.getDescentOfRow(row),
407 x2, y + tabular.getDescentOfRow(row),
408 on_off ? onoffcol : col,
409 on_off ? Painter::line_onoffdash : Painter::line_solid);
410 if (!tabular.leftAlreadyDrawn(cell)) {
411 on_off = !tabular.leftLine(cell);
412 pain.line(x, y - tabular.getAscentOfRow(row),
413 x, y + tabular.getDescentOfRow(row),
414 on_off ? onoffcol : col,
415 on_off ? Painter::line_onoffdash : Painter::line_solid);
417 on_off = !tabular.rightLine(cell);
418 pain.line(x2 - tabular.getAdditionalWidth(cell),
419 y - tabular.getAscentOfRow(row),
420 x2 - tabular.getAdditionalWidth(cell),
421 y + tabular.getDescentOfRow(row),
422 on_off ? onoffcol : col,
423 on_off ? Painter::line_onoffdash : Painter::line_solid);
427 string const InsetTabular::editMessage() const
429 return _("Opened table");
433 void InsetTabular::edit(LCursor & cur, bool left)
435 //lyxerr << "InsetTabular::edit: " << this << endl;
437 cur.selection() = false;
440 if (isRightToLeft(cur))
441 cur.idx() = tabular.getLastCellInRow(0);
447 if (isRightToLeft(cur))
448 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
450 cur.idx() = tabular.getNumberOfCells() - 1;
452 cur.pos() = cur.lastpos(); // FIXME crude guess
454 // this accesses the position cache before it is initialized
456 //cur.bv().fitCursor();
460 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
462 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
463 << "\n cur:" << cur << endl;
464 CursorSlice sl = cur.top();
465 LCursor & bvcur = cur.bv().cursor();
467 switch (cmd.action) {
469 case LFUN_MOUSE_PRESS:
470 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
472 if (cmd.button() == mouse_button::button1) {
473 cur.selection() = false;
474 setCursorFromCoordinates(cur, cmd.x, cmd.y);
480 if (cmd.button() == mouse_button::button2) {
481 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
482 doDispatch(cur, cmd);
486 if (cmd.button() == mouse_button::button3)
487 bvcur.setCursor(cur);
491 case LFUN_MOUSE_MOTION:
492 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
493 if (cmd.button() == mouse_button::button1) {
494 // only accept motions to places not deeper nested than the real anchor
495 if (bvcur.anchor_.hasPart(cur)) {
496 setCursorFromCoordinates(cur, cmd.x, cmd.y);
497 bvcur.setCursor(cur);
498 bvcur.selection() = true;
504 case LFUN_MOUSE_RELEASE:
505 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
506 if (cmd.button() == mouse_button::button3)
507 InsetTabularMailer(*this).showDialog(&cur.bv());
510 case LFUN_CELL_BACKWARD:
512 cur.selection() = false;
515 case LFUN_CELL_FORWARD:
517 cur.selection() = false;
520 case LFUN_CHAR_FORWARD_SELECT:
521 case LFUN_CHAR_FORWARD:
522 cell(cur.idx())->dispatch(cur, cmd);
523 if (!cur.result().dispatched()) {
524 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
526 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
532 case LFUN_CHAR_BACKWARD_SELECT:
533 case LFUN_CHAR_BACKWARD:
534 cell(cur.idx())->dispatch(cur, cmd);
535 if (!cur.result().dispatched()) {
536 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
538 cmd = FuncRequest(LFUN_FINISHED_LEFT);
544 case LFUN_DOWN_SELECT:
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 below
551 // setting also the right targetX.
552 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
553 cur.idx() = tabular.getCellBelow(cur.idx());
555 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
556 cur.pit(), 0, cur.targetX());
558 if (sl == cur.top()) {
559 // we trick it to go to the RIGHT after leaving the
561 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
568 cell(cur.idx())->dispatch(cur, cmd);
569 cur.dispatched(); // override the cell's decision
571 // if our LyXText didn't do anything to the cursor
572 // then we try to put the cursor into the cell above
573 // setting also the right targetX.
574 if (tabular.row_of_cell(cur.idx()) != 0) {
575 cur.idx() = tabular.getCellAbove(cur.idx());
576 cur.pit() = cur.lastpit();
577 LyXText const * text = cell(cur.idx())->getText(0);
578 cur.pos() = text->x2pos(
580 text->paragraphs().back().rows().size()-1,
583 if (sl == cur.top()) {
584 cmd = FuncRequest(LFUN_FINISHED_UP);
589 // case LFUN_SCREEN_DOWN: {
590 // //if (hasSelection())
591 // // cur.selection() = false;
592 // col_type const col = tabular.column_of_cell(cur.idx());
593 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
594 // if (t < yo() + tabular.getHeightOfTabular()) {
595 // cur.bv().scrollDocView(t);
596 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
598 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
605 // case LFUN_SCREEN_UP: {
606 // //if (hasSelection())
607 // // cur.selection() = false;
608 // col_type const col = tabular.column_of_cell(cur.idx());
609 // int const t = cur.bv().top_y() + cur.bv().painter().paperHeight();
611 // cur.bv().scrollDocView(t);
615 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
619 // cur.par() = cur.lastpar();
620 // cur.pos() = cur.lastpos();
624 case LFUN_LAYOUT_TABULAR:
625 InsetTabularMailer(*this).showDialog(&cur.bv());
628 case LFUN_INSET_DIALOG_UPDATE:
629 InsetTabularMailer(*this).updateDialog(&cur.bv());
632 case LFUN_TABULAR_FEATURE:
633 if (!tabularFeatures(cur, cmd.argument))
637 // insert file functions
638 case LFUN_FILE_INSERT_ASCII_PARA:
639 case LFUN_FILE_INSERT_ASCII: {
640 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
641 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
647 if (tablemode(cur)) {
648 if (copySelection(cur)) {
649 recordUndoInset(cur, Undo::DELETE);
654 cell(cur.idx())->dispatch(cur, cmd);
657 case LFUN_CHAR_DELETE_BACKWARD:
658 case LFUN_CHAR_DELETE_FORWARD:
659 if (tablemode(cur)) {
660 recordUndoInset(cur, Undo::DELETE);
664 cell(cur.idx())->dispatch(cur, cmd);
668 if (!cur.selection())
670 if (tablemode(cur)) {
674 cell(cur.idx())->dispatch(cur, cmd);
677 case LFUN_PRIMARY_SELECTION_PASTE: {
678 string const clip = cur.bv().getClipboard();
681 // pass to InsertAsciiString, but
682 // only if we have multi-cell content
683 if (clip.find_first_of("\t\n") != string::npos) {
684 if (insertAsciiString(cur.bv(), clip, false)) {
685 // content has been replaced,
686 // so cursor might be invalid
687 cur.pos() = cur.lastpos();
688 bvcur.setCursor(cur);
692 // so that the clipboard is used and it goes on
694 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
695 paste_tabular.reset();
696 dirtyTabularStack(false);
702 if (hasPasteBuffer() && tabularStackDirty()) {
703 recordUndoInset(cur, Undo::INSERT);
707 cell(cur.idx())->dispatch(cur, cmd);
708 // Reset pasted paragraphs:
709 if (tabular.getPWidth(cur.idx()).zero())
710 cell(cur.idx())->forceParagraphsToDefault(cur);
715 case LFUN_FONT_ROMAN:
721 case LFUN_FONT_FREE_APPLY:
722 case LFUN_FONT_FREE_UPDATE:
724 case LFUN_FONT_UNDERLINE:
726 case LFUN_WORD_CAPITALIZE:
727 case LFUN_WORD_UPCASE:
728 case LFUN_WORD_LOWCASE:
729 case LFUN_CHARS_TRANSPOSE:
730 if (tablemode(cur)) {
733 getSelection(cur, rs, re, cs, ce);
734 for (row_type i = rs; i <= re; ++i) {
735 for (col_type j = cs; j <= ce; ++j) {
736 // cursor follows cell:
737 cur.idx() = tabular.getCellNumber(i, j);
738 // select this cell only:
741 cur.pos() = cur.top().lastpos();
744 cell(cur.idx())->dispatch(cur, cmd);
747 // Restore original selection
748 cur.idx() = tabular.getCellNumber(rs, cs);
751 cur.idx() = tabular.getCellNumber(re, ce);
752 cur.pos() = cur.top().lastpos();
757 cell(cur.idx())->dispatch(cur, cmd);
761 // we try to handle this event in the insets dispatch function.
762 cell(cur.idx())->dispatch(cur, cmd);
767 InsetTabularMailer(*this).updateDialog(&cur.bv());
771 // function sets an object as defined in func_status.h:
772 // states OK, Unknown, Disabled, On, Off.
773 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
774 FuncStatus & status) const
776 switch (cmd.action) {
777 case LFUN_TABULAR_FEATURE: {
778 int action = LyXTabular::LAST_ACTION;
780 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
781 string const tmp = tabularFeature[i].feature;
782 if (tmp == cmd.argument.substr(0, tmp.length())) {
783 action = tabularFeature[i].action;
787 if (action == LyXTabular::LAST_ACTION) {
789 status.unknown(true);
793 string const argument
794 = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
796 row_type sel_row_start = 0;
797 row_type sel_row_end = 0;
799 LyXTabular::ltType dummyltt;
802 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
805 case LyXTabular::SET_PWIDTH:
806 case LyXTabular::SET_MPWIDTH:
807 case LyXTabular::SET_SPECIAL_COLUMN:
808 case LyXTabular::SET_SPECIAL_MULTI:
809 case LyXTabular::APPEND_ROW:
810 case LyXTabular::APPEND_COLUMN:
811 case LyXTabular::DELETE_ROW:
812 case LyXTabular::DELETE_COLUMN:
813 case LyXTabular::SET_ALL_LINES:
814 case LyXTabular::UNSET_ALL_LINES:
818 case LyXTabular::MULTICOLUMN:
819 status.setOnOff(tabular.isMultiColumn(cur.idx()));
822 case LyXTabular::M_TOGGLE_LINE_TOP:
824 case LyXTabular::TOGGLE_LINE_TOP:
825 status.setOnOff(tabular.topLine(cur.idx(), flag));
828 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
830 case LyXTabular::TOGGLE_LINE_BOTTOM:
831 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
834 case LyXTabular::M_TOGGLE_LINE_LEFT:
836 case LyXTabular::TOGGLE_LINE_LEFT:
837 status.setOnOff(tabular.leftLine(cur.idx(), flag));
840 case LyXTabular::M_TOGGLE_LINE_RIGHT:
842 case LyXTabular::TOGGLE_LINE_RIGHT:
843 status.setOnOff(tabular.rightLine(cur.idx(), flag));
846 case LyXTabular::M_ALIGN_LEFT:
848 case LyXTabular::ALIGN_LEFT:
849 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
852 case LyXTabular::M_ALIGN_RIGHT:
854 case LyXTabular::ALIGN_RIGHT:
855 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
858 case LyXTabular::M_ALIGN_CENTER:
860 case LyXTabular::ALIGN_CENTER:
861 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
864 case LyXTabular::ALIGN_BLOCK:
865 status.enabled(!tabular.getPWidth(cur.idx()).zero());
866 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
869 case LyXTabular::M_VALIGN_TOP:
871 case LyXTabular::VALIGN_TOP:
873 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
876 case LyXTabular::M_VALIGN_BOTTOM:
878 case LyXTabular::VALIGN_BOTTOM:
880 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
883 case LyXTabular::M_VALIGN_MIDDLE:
885 case LyXTabular::VALIGN_MIDDLE:
887 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
890 case LyXTabular::SET_LONGTABULAR:
891 status.setOnOff(tabular.isLongTabular());
894 case LyXTabular::UNSET_LONGTABULAR:
895 status.setOnOff(!tabular.isLongTabular());
898 case LyXTabular::SET_ROTATE_TABULAR:
899 status.setOnOff(tabular.getRotateTabular());
902 case LyXTabular::UNSET_ROTATE_TABULAR:
903 status.setOnOff(!tabular.getRotateTabular());
906 case LyXTabular::SET_ROTATE_CELL:
907 status.setOnOff(tabular.getRotateCell(cur.idx()));
910 case LyXTabular::UNSET_ROTATE_CELL:
911 status.setOnOff(!tabular.getRotateCell(cur.idx()));
914 case LyXTabular::SET_USEBOX:
915 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
918 case LyXTabular::SET_LTFIRSTHEAD:
919 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
922 case LyXTabular::UNSET_LTFIRSTHEAD:
923 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
926 case LyXTabular::SET_LTHEAD:
927 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
930 case LyXTabular::UNSET_LTHEAD:
931 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
934 case LyXTabular::SET_LTFOOT:
935 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
938 case LyXTabular::UNSET_LTFOOT:
939 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
942 case LyXTabular::SET_LTLASTFOOT:
943 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
946 case LyXTabular::UNSET_LTLASTFOOT:
947 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
950 case LyXTabular::SET_LTNEWPAGE:
951 status.setOnOff(tabular.getLTNewPage(sel_row_start));
956 status.enabled(false);
962 // These are only enabled inside tabular
963 case LFUN_CELL_BACKWARD:
964 case LFUN_CELL_FORWARD:
965 status.enabled(true);
968 // disable these with multiple cells selected
969 case LFUN_INSET_INSERT:
970 case LFUN_TABULAR_INSERT:
971 case LFUN_CHARSTYLE_INSERT:
972 case LFUN_FLOAT_INSERT:
973 case LFUN_FLOAT_WIDE_INSERT:
974 case LFUN_FOOTNOTE_INSERT:
975 case LFUN_MARGINALNOTE_INSERT:
976 case LFUN_MATH_INSERT:
978 case LFUN_MATH_MUTATE:
979 case LFUN_MATH_DISPLAY:
980 case LFUN_NOTE_INSERT:
981 case LFUN_OPTIONAL_INSERT:
982 case LFUN_BOX_INSERT:
983 case LFUN_BRANCH_INSERT:
984 case LFUN_WRAP_INSERT:
985 case LFUN_ERT_INSERT: {
986 if (tablemode(cur)) {
987 status.enabled(false);
990 return cell(cur.idx())->getStatus(cur, cmd, status);
993 // disable in non-fixed-width cells
994 case LFUN_BREAK_LINE:
995 case LFUN_BREAK_PARAGRAPH:
996 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
997 case LFUN_BREAK_PARAGRAPH_SKIP: {
998 if (tabular.getPWidth(cur.idx()).zero()) {
999 status.enabled(false);
1002 return cell(cur.idx())->getStatus(cur, cmd, status);
1006 if (tabularStackDirty()) {
1007 status.enabled(true);
1011 case LFUN_INSET_MODIFY:
1012 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1013 status.enabled(true);
1019 // we try to handle this event in the insets dispatch function.
1020 return cell(cur.idx())->getStatus(cur, cmd, status);
1025 int InsetTabular::latex(Buffer const & buf, ostream & os,
1026 OutputParams const & runparams) const
1028 return tabular.latex(buf, os, runparams);
1032 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1033 OutputParams const & runparams) const
1035 int const dp = runparams.linelen ? runparams.depth : 0;
1036 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1040 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1041 OutputParams const & runparams) const
1043 return tabular.linuxdoc(buf,os, runparams);
1047 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1048 OutputParams const & runparams) const
1051 InsetBase * master = 0;
1053 #ifdef WITH_WARNINGS
1054 #warning Why not pass a proper DocIterator here?
1057 // if the table is inside a float it doesn't need the informaltable
1058 // wrapper. Search for it.
1059 for (master = owner(); master; master = master->owner())
1060 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1065 os << "<informaltable>";
1068 ret += tabular.docbook(buf, os, runparams);
1070 os << "</informaltable>";
1077 void InsetTabular::validate(LaTeXFeatures & features) const
1079 tabular.validate(features);
1083 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1085 return tabular.getCellInset(idx);
1089 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1091 return tabular.getCellInset(idx);
1095 void InsetTabular::cursorPos
1096 (CursorSlice const & sl, bool boundary, int & x, int & y) const
1098 cell(sl.idx())->cursorPos(sl, boundary, x, y);
1100 // y offset correction
1101 int const row = tabular.row_of_cell(sl.idx());
1102 for (int i = 0; i <= row; ++i) {
1104 y += tabular.getAscentOfRow(i);
1105 y += tabular.getAdditionalHeight(i);
1108 y += tabular.getDescentOfRow(i);
1111 // x offset correction
1112 int const col = tabular.column_of_cell(sl.idx());
1113 int idx = tabular.getCellNumber(row, 0);
1114 for (int j = 0; j < col; ++j) {
1115 if (tabular.isPartOfMultiColumn(row, j))
1117 x += tabular.getWidthOfColumn(idx);
1120 x += tabular.getBeginningOfTextInCell(idx);
1121 x += ADD_TO_TABULAR_WIDTH;
1126 int InsetTabular::dist(idx_type const cell, int x, int y) const
1130 InsetBase const & inset = *tabular.getCellInset(cell);
1131 Point o = theCoords.getInsets().xy(&inset);
1132 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1133 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1134 int const ybeg = o.y_ - inset.ascent();
1135 row_type const row = tabular.row_of_cell(cell);
1136 int const rowheight = tabular.getAscentOfRow(row)
1137 + tabular.getDescentOfRow(row);
1138 int const yend = ybeg + rowheight;
1150 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1151 // << " ybeg=" << ybeg << " yend=" << yend
1152 // << " xx=" << xx << " yy=" << yy
1153 // << " dist=" << xx + yy << endl;
1158 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1160 //lyxerr << "InsetTabular::editXY: " << this << endl;
1161 cur.selection() = false;
1163 cur.idx() = getNearestCell(x, y);
1165 return cell(cur.idx())->text_.editXY(cur, x, y);
1169 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1171 cur.idx() = getNearestCell(x, y);
1172 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1176 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1178 idx_type idx_min = 0;
1179 int dist_min = std::numeric_limits<int>::max();
1180 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1181 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1182 int const d = dist(i, x, y);
1193 int InsetTabular::getCellXPos(idx_type const cell) const
1197 for (; !tabular.isFirstCellInRow(c); --c)
1199 int lx = tabular.getWidthOfColumn(cell);
1200 for (; c < cell; ++c)
1201 lx += tabular.getWidthOfColumn(c);
1203 return lx - tabular.getWidthOfColumn(cell);
1207 void InsetTabular::resetPos(LCursor & cur) const
1209 BufferView & bv = cur.bv();
1210 int const maxwidth = bv.workWidth();
1212 if (&cur.inset() != this) {
1216 int const X2 = maxwidth;
1217 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1218 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1219 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1222 scx_ = X1 + 20 - x1;
1224 scx_ = X2 - 20 - x2;
1231 InsetTabularMailer(*this).updateDialog(&bv);
1235 void InsetTabular::moveNextCell(LCursor & cur)
1237 if (isRightToLeft(cur)) {
1238 if (tabular.isFirstCellInRow(cur.idx())) {
1239 row_type const row = tabular.row_of_cell(cur.idx());
1240 if (row == tabular.rows() - 1)
1242 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1249 if (tabular.isLastCell(cur.idx()))
1259 void InsetTabular::movePrevCell(LCursor & cur)
1261 if (isRightToLeft(cur)) {
1262 if (tabular.isLastCellInRow(cur.idx())) {
1263 row_type const row = tabular.row_of_cell(cur.idx());
1266 cur.idx() = tabular.getFirstCellInRow(row);
1267 cur.idx() = tabular.getCellAbove(cur.idx());
1269 if (tabular.isLastCell(cur.idx()))
1274 if (cur.idx() == 0) // first cell
1278 cur.pit() = cur.lastpit();
1279 cur.pos() = cur.lastpos();
1284 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1286 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1289 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1290 string const tmp = tabularFeature[i].feature;
1292 if (tmp == what.substr(0, tmp.length())) {
1293 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1294 //tabularFeatures[i].feature.length())) {
1295 action = tabularFeature[i].action;
1299 if (action == LyXTabular::LAST_ACTION)
1303 ltrim(what.substr(tabularFeature[i].feature.length()));
1304 tabularFeatures(cur, action, val);
1311 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1312 string const & special, bool & flag)
1314 if (special == "dl_above") {
1317 } else if (special == "dl_below") {
1318 ltt.bottomDL = flag;
1320 } else if (special == "empty") {
1332 void InsetTabular::tabularFeatures(LCursor & cur,
1333 LyXTabular::Feature feature, string const & value)
1335 BufferView & bv = cur.bv();
1336 col_type sel_col_start;
1337 col_type sel_col_end;
1338 row_type sel_row_start;
1339 row_type sel_row_end;
1340 bool setLines = false;
1341 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1342 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1346 case LyXTabular::M_ALIGN_LEFT:
1347 case LyXTabular::ALIGN_LEFT:
1348 setAlign = LYX_ALIGN_LEFT;
1351 case LyXTabular::M_ALIGN_RIGHT:
1352 case LyXTabular::ALIGN_RIGHT:
1353 setAlign = LYX_ALIGN_RIGHT;
1356 case LyXTabular::M_ALIGN_CENTER:
1357 case LyXTabular::ALIGN_CENTER:
1358 setAlign = LYX_ALIGN_CENTER;
1361 case LyXTabular::ALIGN_BLOCK:
1362 setAlign = LYX_ALIGN_BLOCK;
1365 case LyXTabular::M_VALIGN_TOP:
1366 case LyXTabular::VALIGN_TOP:
1367 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1370 case LyXTabular::M_VALIGN_BOTTOM:
1371 case LyXTabular::VALIGN_BOTTOM:
1372 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1375 case LyXTabular::M_VALIGN_MIDDLE:
1376 case LyXTabular::VALIGN_MIDDLE:
1377 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1384 recordUndoInset(cur, Undo::ATOMIC);
1386 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1387 row_type const row = tabular.row_of_cell(cur.idx());
1388 col_type const column = tabular.column_of_cell(cur.idx());
1390 LyXTabular::ltType ltt;
1394 case LyXTabular::SET_PWIDTH: {
1395 LyXLength const len(value);
1396 tabular.setColumnPWidth(cur, cur.idx(), len);
1398 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1399 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1403 case LyXTabular::SET_MPWIDTH:
1404 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1407 case LyXTabular::SET_SPECIAL_COLUMN:
1408 case LyXTabular::SET_SPECIAL_MULTI:
1409 tabular.setAlignSpecial(cur.idx(),value,feature);
1412 case LyXTabular::APPEND_ROW:
1413 // append the row into the tabular
1414 tabular.appendRow(bv.buffer()->params(), cur.idx());
1417 case LyXTabular::APPEND_COLUMN:
1418 // append the column into the tabular
1419 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1420 cur.idx() = tabular.getCellNumber(row, column);
1423 case LyXTabular::DELETE_ROW:
1424 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1425 tabular.deleteRow(sel_row_start);
1426 if (sel_row_start >= tabular.rows())
1428 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1431 cur.selection() = false;
1434 case LyXTabular::DELETE_COLUMN:
1435 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1436 tabular.deleteColumn(sel_col_start);
1437 if (sel_col_start >= tabular.columns())
1439 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1442 cur.selection() = false;
1445 case LyXTabular::M_TOGGLE_LINE_TOP:
1447 case LyXTabular::TOGGLE_LINE_TOP: {
1448 bool lineSet = !tabular.topLine(cur.idx(), flag);
1449 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1450 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1452 tabular.getCellNumber(i, j),
1457 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1459 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1460 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1461 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1462 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1463 tabular.setBottomLine(
1464 tabular.getCellNumber(i, j),
1470 case LyXTabular::M_TOGGLE_LINE_LEFT:
1472 case LyXTabular::TOGGLE_LINE_LEFT: {
1473 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1474 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1475 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1476 tabular.setLeftLine(
1477 tabular.getCellNumber(i,j),
1483 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1485 case LyXTabular::TOGGLE_LINE_RIGHT: {
1486 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1487 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1488 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1489 tabular.setRightLine(
1490 tabular.getCellNumber(i,j),
1496 case LyXTabular::M_ALIGN_LEFT:
1497 case LyXTabular::M_ALIGN_RIGHT:
1498 case LyXTabular::M_ALIGN_CENTER:
1500 case LyXTabular::ALIGN_LEFT:
1501 case LyXTabular::ALIGN_RIGHT:
1502 case LyXTabular::ALIGN_CENTER:
1503 case LyXTabular::ALIGN_BLOCK:
1504 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1505 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1506 tabular.setAlignment(
1507 tabular.getCellNumber(i, j),
1512 case LyXTabular::M_VALIGN_TOP:
1513 case LyXTabular::M_VALIGN_BOTTOM:
1514 case LyXTabular::M_VALIGN_MIDDLE:
1516 case LyXTabular::VALIGN_TOP:
1517 case LyXTabular::VALIGN_BOTTOM:
1518 case LyXTabular::VALIGN_MIDDLE:
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.setVAlignment(
1522 tabular.getCellNumber(i, j),
1526 case LyXTabular::MULTICOLUMN: {
1527 if (sel_row_start != sel_row_end) {
1528 #ifdef WITH_WARNINGS
1529 #warning Need I say it ? This is horrible.
1531 Alert::error(_("Error setting multicolumn"),
1532 _("You cannot set multicolumn vertically."));
1535 if (!cur.selection()) {
1536 // just multicol for one single cell
1537 // check whether we are completely in a multicol
1538 if (tabular.isMultiColumn(cur.idx()))
1539 tabular.unsetMultiColumn(cur.idx());
1541 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1544 // we have a selection so this means we just add all this
1545 // cells to form a multicolumn cell
1546 idx_type const s_start = cur.selBegin().idx();
1547 idx_type const s_end = cur.selEnd().idx();
1548 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1549 cur.idx() = s_start;
1552 cur.selection() = false;
1556 case LyXTabular::SET_ALL_LINES:
1558 case LyXTabular::UNSET_ALL_LINES:
1559 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1560 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1561 tabular.setAllLines(
1562 tabular.getCellNumber(i,j), setLines);
1565 case LyXTabular::SET_LONGTABULAR:
1566 tabular.setLongTabular(true);
1569 case LyXTabular::UNSET_LONGTABULAR:
1570 tabular.setLongTabular(false);
1573 case LyXTabular::SET_ROTATE_TABULAR:
1574 tabular.setRotateTabular(true);
1577 case LyXTabular::UNSET_ROTATE_TABULAR:
1578 tabular.setRotateTabular(false);
1581 case LyXTabular::SET_ROTATE_CELL:
1582 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1583 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1584 tabular.setRotateCell(
1585 tabular.getCellNumber(i, j), true);
1588 case LyXTabular::UNSET_ROTATE_CELL:
1589 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1590 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1591 tabular.setRotateCell(
1592 tabular.getCellNumber(i, j), false);
1595 case LyXTabular::SET_USEBOX: {
1596 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1597 if (val == tabular.getUsebox(cur.idx()))
1598 val = LyXTabular::BOX_NONE;
1599 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1600 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1601 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1605 case LyXTabular::UNSET_LTFIRSTHEAD:
1607 case LyXTabular::SET_LTFIRSTHEAD:
1608 tabular.getRowOfLTFirstHead(row, ltt);
1609 checkLongtableSpecial(ltt, value, flag);
1610 tabular.setLTHead(row, flag, ltt, true);
1613 case LyXTabular::UNSET_LTHEAD:
1615 case LyXTabular::SET_LTHEAD:
1616 tabular.getRowOfLTHead(row, ltt);
1617 checkLongtableSpecial(ltt, value, flag);
1618 tabular.setLTHead(row, flag, ltt, false);
1621 case LyXTabular::UNSET_LTFOOT:
1623 case LyXTabular::SET_LTFOOT:
1624 tabular.getRowOfLTFoot(row, ltt);
1625 checkLongtableSpecial(ltt, value, flag);
1626 tabular.setLTFoot(row, flag, ltt, false);
1629 case LyXTabular::UNSET_LTLASTFOOT:
1631 case LyXTabular::SET_LTLASTFOOT:
1632 tabular.getRowOfLTLastFoot(row, ltt);
1633 checkLongtableSpecial(ltt, value, flag);
1634 tabular.setLTFoot(row, flag, ltt, true);
1637 case LyXTabular::SET_LTNEWPAGE:
1638 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1641 // dummy stuff just to avoid warnings
1642 case LyXTabular::LAST_ACTION:
1646 InsetTabularMailer(*this).updateDialog(&bv);
1650 bool InsetTabular::showInsetDialog(BufferView * bv) const
1652 InsetTabularMailer(*this).showDialog(bv);
1657 void InsetTabular::openLayoutDialog(BufferView * bv) const
1659 InsetTabularMailer(*this).showDialog(bv);
1663 bool InsetTabular::copySelection(LCursor & cur)
1665 if (!cur.selection())
1670 getSelection(cur, rs, re, cs, ce);
1672 paste_tabular.reset(new LyXTabular(tabular));
1674 for (row_type i = 0; i < rs; ++i)
1675 paste_tabular->deleteRow(0);
1677 row_type const rows = re - rs + 1;
1678 while (paste_tabular->rows() > rows)
1679 paste_tabular->deleteRow(rows);
1681 paste_tabular->setTopLine(0, true, true);
1682 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1685 for (col_type i = 0; i < cs; ++i)
1686 paste_tabular->deleteColumn(0);
1688 col_type const columns = ce - cs + 1;
1689 while (paste_tabular->columns() > columns)
1690 paste_tabular->deleteColumn(columns);
1692 paste_tabular->setLeftLine(0, true, true);
1693 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1697 OutputParams const runparams;
1698 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1699 cur.bv().stuffClipboard(os.str());
1700 // mark tabular stack dirty
1701 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1702 // when we (hopefully) have a one-for-all paste mechanism.
1703 lyx::cap::dirtyTabularStack(true);
1709 bool InsetTabular::pasteSelection(LCursor & cur)
1713 col_type const actcol = tabular.column_of_cell(cur.idx());
1714 row_type const actrow = tabular.row_of_cell(cur.idx());
1715 for (row_type r1 = 0, r2 = actrow;
1716 r1 < paste_tabular->rows() && r2 < tabular.rows();
1718 for (col_type c1 = 0, c2 = actcol;
1719 c1 < paste_tabular->columns() && c2 < tabular.columns();
1721 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1722 tabular.isPartOfMultiColumn(r2, c2))
1724 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1728 if (tabular.isPartOfMultiColumn(r2, c2)) {
1732 shared_ptr<InsetText> inset(
1733 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1734 tabular.setCellInset(r2, c2, inset);
1743 void InsetTabular::cutSelection(LCursor & cur)
1745 if (!cur.selection())
1750 getSelection(cur, rs, re, cs, ce);
1751 for (row_type i = rs; i <= re; ++i) {
1752 for (col_type j = cs; j <= ce; ++j) {
1753 shared_ptr<InsetText> t
1754 = cell(tabular.getCellNumber(i, j));
1755 if (cur.buffer().params().tracking_changes)
1756 t->markErased(true);
1762 // cursor position might be invalid now
1763 cur.pos() = cur.lastpos();
1764 cur.clearSelection();
1768 bool InsetTabular::isRightToLeft(LCursor & cur) const
1770 BOOST_ASSERT(cur.depth() > 1);
1771 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1772 LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1773 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1774 parentpos).language()->rightToLeft();
1778 void InsetTabular::getSelection(LCursor & cur,
1779 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1781 CursorSlice const & beg = cur.selBegin();
1782 CursorSlice const & end = cur.selEnd();
1783 cs = tabular.column_of_cell(beg.idx());
1784 ce = tabular.column_of_cell(end.idx());
1787 cs = tabular.column_of_cell(end.idx());
1789 ce = tabular.right_column_of_cell(end.idx());
1792 rs = tabular.row_of_cell(beg.idx());
1793 re = tabular.row_of_cell(end.idx());
1799 LyXText * InsetTabular::getText(int idx) const
1801 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1805 void InsetTabular::markErased(bool erased)
1807 for (idx_type idx = 0; idx < nargs(); ++idx)
1808 cell(idx)->markErased(erased);
1812 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1814 return tabular.getPWidth(cell).zero();
1818 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1821 if (buf.length() <= 0)
1826 col_type maxCols = 1;
1827 string::size_type const len = buf.length();
1828 string::size_type p = 0;
1830 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1838 maxCols = max(cols, maxCols);
1844 maxCols = max(cols, maxCols);
1845 LyXTabular * loctab;
1850 paste_tabular.reset(
1851 new LyXTabular(bv.buffer()->params(), rows, maxCols));
1852 loctab = paste_tabular.get();
1854 dirtyTabularStack(true);
1857 cell = bv.cursor().idx();
1858 ocol = tabular.column_of_cell(cell);
1859 row = tabular.row_of_cell(cell);
1862 string::size_type op = 0;
1863 idx_type const cells = loctab->getNumberOfCells();
1866 rows = loctab->rows();
1867 col_type const columns = loctab->columns();
1869 while (cell < cells && p < len && row < rows &&
1870 (p = buf.find_first_of("\t\n", p)) != string::npos)
1876 // we can only set this if we are not too far right
1877 if (cols < columns) {
1878 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1879 inset->setViewCache(&bv);
1880 Paragraph & par = inset->text_.getPar(0);
1881 LyXFont const font = inset->text_.getFont(par, 0);
1882 inset->setText(buf.substr(op, p - op), font);
1888 // we can only set this if we are not too far right
1889 if (cols < columns) {
1890 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1891 inset->setViewCache(&bv);
1892 Paragraph & par = inset->text_.getPar(0);
1893 LyXFont const font = inset->text_.getFont(par, 0);
1894 inset->setText(buf.substr(op, p - op), font);
1899 cell = loctab->getCellNumber(row, cols);
1905 // check for the last cell if there is no trailing '\n'
1906 if (cell < cells && op < len) {
1907 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1908 inset->setViewCache(&bv);
1909 Paragraph & par = inset->text_.getPar(0);
1910 LyXFont const font = inset->text_.getFont(par, 0);
1911 inset->setText(buf.substr(op, len - op), font);
1917 void InsetTabular::addPreview(PreviewLoader & loader) const
1919 row_type const rows = tabular.rows();
1920 col_type const columns = tabular.columns();
1921 for (row_type i = 0; i < rows; ++i) {
1922 for (col_type j = 0; j < columns; ++j)
1923 tabular.getCellInset(i, j)->addPreview(loader);
1928 bool InsetTabular::tablemode(LCursor & cur) const
1930 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1937 string const InsetTabularMailer::name_("tabular");
1939 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1940 : inset_(const_cast<InsetTabular &>(inset))
1944 string const InsetTabularMailer::inset2string(Buffer const &) const
1946 return params2string(inset_);
1950 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1952 istringstream data(in);
1954 lex.setStream(data);
1961 if (!lex || token != name_)
1962 return print_mailer_error("InsetTabularMailer", in, 1,
1965 // This is part of the inset proper that is usually swallowed
1966 // by Buffer::readInset
1968 if (!lex || token != "Tabular")
1969 return print_mailer_error("InsetTabularMailer", in, 2,
1972 Buffer const & buffer = inset.buffer();
1973 inset.read(buffer, lex);
1977 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1980 data << name_ << ' ';
1981 inset.write(inset.buffer(), data);
1982 data << "\\end_inset\n";