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/Clipboard.h"
41 #include "frontends/Painter.h"
42 #include "frontends/Selection.h"
51 using cap::dirtyTabularStack;
52 using cap::tabularStackDirty;
54 using graphics::PreviewLoader;
58 using frontend::Painter;
59 using frontend::Clipboard;
61 using boost::shared_ptr;
67 using std::istringstream;
69 using std::ostringstream;
73 namespace Alert = frontend::Alert;
78 int const ADD_TO_HEIGHT = 2;
79 int const ADD_TO_TABULAR_WIDTH = 2;
80 int const default_line_space = 10;
83 boost::scoped_ptr<LyXTabular> paste_tabular;
86 struct TabularFeature {
87 LyXTabular::Feature action;
92 TabularFeature tabularFeature[] =
94 { LyXTabular::APPEND_ROW, "append-row" },
95 { LyXTabular::APPEND_COLUMN, "append-column" },
96 { LyXTabular::DELETE_ROW, "delete-row" },
97 { LyXTabular::DELETE_COLUMN, "delete-column" },
98 { LyXTabular::COPY_ROW, "copy-row" },
99 { LyXTabular::COPY_COLUMN, "copy-column" },
100 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
101 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
102 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
103 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
104 { LyXTabular::ALIGN_LEFT, "align-left" },
105 { LyXTabular::ALIGN_RIGHT, "align-right" },
106 { LyXTabular::ALIGN_CENTER, "align-center" },
107 { LyXTabular::ALIGN_BLOCK, "align-block" },
108 { LyXTabular::VALIGN_TOP, "valign-top" },
109 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
110 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
111 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
112 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
113 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
114 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
115 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
116 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
117 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
118 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
119 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
120 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
121 { LyXTabular::MULTICOLUMN, "multicolumn" },
122 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
123 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
124 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
125 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
126 { LyXTabular::SET_PWIDTH, "set-pwidth" },
127 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
128 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
129 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
130 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
131 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
132 { LyXTabular::SET_USEBOX, "set-usebox" },
133 { LyXTabular::SET_LTHEAD, "set-lthead" },
134 { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
135 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
136 { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
137 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
138 { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
139 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
140 { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
141 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
142 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
143 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
144 { LyXTabular::SET_BOOKTABS, "set-booktabs" },
145 { LyXTabular::UNSET_BOOKTABS, "unset-booktabs" },
146 { LyXTabular::SET_TOP_SPACE, "set-top-space" },
147 { LyXTabular::SET_BOTTOM_SPACE, "set-bottom-space" },
148 { LyXTabular::SET_INTERLINE_SPACE, "set-interline-space" },
149 { LyXTabular::LAST_ACTION, "" }
153 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
155 FeatureEqual(LyXTabular::Feature feature)
156 : feature_(feature) {}
157 bool operator()(TabularFeature const & tf) const {
158 return tf.action == feature_;
161 LyXTabular::Feature feature_;
167 string const featureAsString(LyXTabular::Feature feature)
169 TabularFeature * end = tabularFeature +
170 sizeof(tabularFeature) / sizeof(TabularFeature);
171 TabularFeature * it = std::find_if(tabularFeature, end,
172 FeatureEqual(feature));
173 return (it == end) ? string() : it->feature;
177 bool InsetTabular::hasPasteBuffer() const
179 return (paste_tabular.get() != 0);
183 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
185 : tabular(buf.params(), max(rows, row_type(1)),
186 max(columns, col_type(1))), buffer_(&buf), scx_(0)
190 InsetTabular::InsetTabular(InsetTabular const & tab)
191 : InsetOld(tab), tabular(tab.tabular),
192 buffer_(tab.buffer_), scx_(0)
196 InsetTabular::~InsetTabular()
198 InsetTabularMailer(*this).hideDialog();
202 auto_ptr<InsetBase> InsetTabular::doClone() const
204 return auto_ptr<InsetBase>(new InsetTabular(*this));
208 Buffer const & InsetTabular::buffer() const
214 void InsetTabular::buffer(Buffer const * b)
220 void InsetTabular::write(Buffer const & buf, ostream & os) const
222 os << "Tabular" << endl;
223 tabular.write(buf, os);
227 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
229 bool const old_format = (lex.getString() == "\\LyXTable");
231 tabular.read(buf, lex);
237 string token = lex.getString();
238 while (lex.isOK() && (token != "\\end_inset")) {
240 token = lex.getString();
242 if (token != "\\end_inset") {
243 lex.printError("Missing \\end_inset at this point. "
249 bool InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
251 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
252 // mi.base.textwidth << "\n";
254 lyxerr << "InsetTabular::metrics: need bv" << endl;
259 for (idx_type cell = 0; i < tabular.rows(); ++i) {
262 for (col_type j = 0; j < tabular.columns(); ++j) {
263 if (tabular.isPartOfMultiColumn(i, j))
264 // Multicolumn cell, but not first one
269 if (tabular.cell_info[i][j].multicolumn ==
270 LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
271 p_width = tabular.cellinfo_of_cell(cell).p_width;
273 p_width = tabular.column_info[j].p_width;
275 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
276 tabular.getCellInset(cell)->metrics(m, dim);
278 dim.wid = m.base.textwidth;
279 tabular.setWidthOfCell(cell, dim.wid);
280 if (p_width.zero()) {
281 m.base.textwidth = dim.wid + 2 * ADD_TO_TABULAR_WIDTH;
282 // FIXME there must be a way to get rid of
283 // the second metrics call
284 tabular.getCellInset(cell)->metrics(m, dim);
286 maxAsc = max(maxAsc, dim.asc);
287 maxDesc = max(maxDesc, dim.des);
290 int const top_space = tabular.row_info[i].top_space_default ?
292 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
293 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT + top_space);
294 int const bottom_space = tabular.row_info[i].bottom_space_default ?
296 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
297 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
300 dim.asc = tabular.getAscentOfRow(0);
301 dim.des = tabular.getHeightOfTabular() - dim.asc;
302 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
303 bool const changed = dim_ != dim;
309 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
311 setPosCache(pi, x, y);
313 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
314 BufferView * bv = pi.base.bv;
316 resetPos(bv->cursor());
319 x += ADD_TO_TABULAR_WIDTH;
322 first_visible_cell = LyXTabular::npos;
323 for (row_type i = 0; i < tabular.rows(); ++i) {
325 int const a = tabular.getAscentOfRow(i);
326 int const d = tabular.getDescentOfRow(i);
327 idx = tabular.getCellNumber(i, 0);
328 for (col_type j = 0; j < tabular.columns(); ++j) {
329 if (tabular.isPartOfMultiColumn(i, j))
331 if (first_visible_cell == LyXTabular::npos)
332 first_visible_cell = idx;
334 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
335 if (nx + tabular.getWidthOfColumn(idx) < 0
336 || nx > bv->workWidth()
338 || y - a > bv->workHeight()) {
339 pi.pain.setDrawingEnabled(false);
340 cell(idx)->draw(pi, cx, y);
341 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
342 pi.pain.setDrawingEnabled(true);
344 cell(idx)->draw(pi, cx, y);
345 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
347 nx += tabular.getWidthOfColumn(idx);
351 if (i + 1 < tabular.rows())
352 y += d + tabular.getAscentOfRow(i + 1) +
353 tabular.getAdditionalHeight(i + 1);
358 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
360 setPosCache(pi, x, y);
362 LCursor & cur = pi.base.bv->cursor();
364 x += scx_ + ADD_TO_TABULAR_WIDTH;
366 // Paint background of current tabular
367 int const w = tabular.getWidthOfTabular();
368 int const h = tabular.getHeightOfTabular();
369 int yy = y - tabular.getAscentOfRow(0);
370 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
372 if (!cur.selection())
374 if (!ptr_cmp(&cur.inset(), this))
380 if (tablemode(cur)) {
383 getSelection(cur, rs, re, cs, ce);
384 y -= tabular.getAscentOfRow(0);
385 for (row_type j = 0; j < tabular.rows(); ++j) {
386 int const a = tabular.getAscentOfRow(j);
387 int const h = a + tabular.getDescentOfRow(j);
389 y += tabular.getAdditionalHeight(j);
390 for (col_type i = 0; i < tabular.columns(); ++i) {
391 if (tabular.isPartOfMultiColumn(j, i))
393 idx_type const cell =
394 tabular.getCellNumber(j, i);
395 int const w = tabular.getWidthOfColumn(cell);
396 if (i >= cs && i <= ce && j >= rs && j <= re)
397 pi.pain.fillRectangle(xx, y, w, h,
406 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
411 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
412 row_type row, idx_type cell, bool erased) const
414 int x2 = x + tabular.getWidthOfColumn(cell);
416 LColor::color col = LColor::tabularline;
417 LColor::color onoffcol = LColor::tabularonoffline;
420 col = LColor::strikeout;
421 onoffcol = LColor::strikeout;
424 if (!tabular.topAlreadyDrawn(cell)) {
425 on_off = !tabular.topLine(cell);
426 pain.line(x, y - tabular.getAscentOfRow(row),
427 x2, y - tabular.getAscentOfRow(row),
428 on_off ? onoffcol : col,
429 on_off ? Painter::line_onoffdash : Painter::line_solid);
431 on_off = !tabular.bottomLine(cell);
432 pain.line(x, y + tabular.getDescentOfRow(row),
433 x2, y + tabular.getDescentOfRow(row),
434 on_off ? onoffcol : col,
435 on_off ? Painter::line_onoffdash : Painter::line_solid);
436 if (!tabular.leftAlreadyDrawn(cell)) {
437 on_off = !tabular.leftLine(cell);
438 pain.line(x, y - tabular.getAscentOfRow(row),
439 x, y + tabular.getDescentOfRow(row),
440 on_off ? onoffcol : col,
441 on_off ? Painter::line_onoffdash : Painter::line_solid);
443 on_off = !tabular.rightLine(cell);
444 pain.line(x2 - tabular.getAdditionalWidth(cell),
445 y - tabular.getAscentOfRow(row),
446 x2 - tabular.getAdditionalWidth(cell),
447 y + tabular.getDescentOfRow(row),
448 on_off ? onoffcol : col,
449 on_off ? Painter::line_onoffdash : Painter::line_solid);
453 docstring const InsetTabular::editMessage() const
455 return _("Opened table");
459 void InsetTabular::edit(LCursor & cur, bool left)
461 //lyxerr << "InsetTabular::edit: " << this << endl;
463 cur.selection() = false;
466 if (isRightToLeft(cur))
467 cur.idx() = tabular.getLastCellInRow(0);
473 if (isRightToLeft(cur))
474 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
476 cur.idx() = tabular.getNumberOfCells() - 1;
478 cur.pos() = cur.lastpos(); // FIXME crude guess
480 // FIXME: this accesses the position cache before it is initialized
482 //cur.bv().fitCursor();
486 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
488 lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
489 << "\n cur:" << cur << endl;
490 CursorSlice sl = cur.top();
491 LCursor & bvcur = cur.bv().cursor();
493 switch (cmd.action) {
495 case LFUN_MOUSE_PRESS:
496 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
498 if (cmd.button() == mouse_button::button1
499 || (cmd.button() == mouse_button::button3
500 && (&bvcur.selBegin().inset() != this || !tablemode(bvcur)))) {
501 if (!bvcur.selection() && !cur.bv().mouseSetCursor(cur))
503 cur.selection() = false;
504 setCursorFromCoordinates(cur, cmd.x, cmd.y);
505 cur.bv().mouseSetCursor(cur);
509 if (cmd.button() == mouse_button::button2) {
510 cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
511 doDispatch(cur, cmd);
515 case LFUN_MOUSE_MOTION:
516 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
517 if (cmd.button() == mouse_button::button1) {
518 // only accept motions to places not deeper nested than the real anchor
519 if (bvcur.anchor_.hasPart(cur)) {
520 // only update if selection changes
521 if (bvcur.idx() == cur.idx() &&
522 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
524 setCursorFromCoordinates(cur, cmd.x, cmd.y);
525 bvcur.setCursor(cur);
526 bvcur.selection() = true;
532 case LFUN_MOUSE_RELEASE:
533 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
534 if (cmd.button() == mouse_button::button3)
535 InsetTabularMailer(*this).showDialog(&cur.bv());
538 case LFUN_CELL_BACKWARD:
540 cur.selection() = false;
543 case LFUN_CELL_FORWARD:
545 cur.selection() = false;
548 case LFUN_CHAR_FORWARD_SELECT:
549 case LFUN_CHAR_FORWARD:
550 cell(cur.idx())->dispatch(cur, cmd);
551 if (!cur.result().dispatched()) {
552 isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
554 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
560 case LFUN_CHAR_BACKWARD_SELECT:
561 case LFUN_CHAR_BACKWARD:
562 cell(cur.idx())->dispatch(cur, cmd);
563 if (!cur.result().dispatched()) {
564 isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
566 cmd = FuncRequest(LFUN_FINISHED_LEFT);
572 case LFUN_DOWN_SELECT:
574 cell(cur.idx())->dispatch(cur, cmd);
575 cur.dispatched(); // override the cell's decision
577 // if our LyXText didn't do anything to the cursor
578 // then we try to put the cursor into the cell below
579 // setting also the right targetX.
580 if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
581 cur.idx() = tabular.getCellBelow(cur.idx());
583 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
584 cur.bv(), cur.pit(), 0, cur.targetX());
586 if (sl == cur.top()) {
587 // we trick it to go to the RIGHT after leaving the
589 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
596 cell(cur.idx())->dispatch(cur, cmd);
597 cur.dispatched(); // override the cell's decision
599 // if our LyXText didn't do anything to the cursor
600 // then we try to put the cursor into the cell above
601 // setting also the right targetX.
602 if (tabular.row_of_cell(cur.idx()) != 0) {
603 cur.idx() = tabular.getCellAbove(cur.idx());
604 cur.pit() = cur.lastpit();
605 LyXText const * text = cell(cur.idx())->getText(0);
606 ParagraphMetrics const & pm =
607 cur.bv().parMetrics(text, cur.lastpit());
608 cur.pos() = text->x2pos(
614 if (sl == cur.top()) {
615 cmd = FuncRequest(LFUN_FINISHED_UP);
620 // case LFUN_SCREEN_DOWN: {
621 // //if (hasSelection())
622 // // cur.selection() = false;
623 // col_type const col = tabular.column_of_cell(cur.idx());
624 // int const t = cur.bv().top_y() + cur.bv().height();
625 // if (t < yo() + tabular.getHeightOfTabular()) {
626 // cur.bv().scrollDocView(t);
627 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
629 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
636 // case LFUN_SCREEN_UP: {
637 // //if (hasSelection())
638 // // cur.selection() = false;
639 // col_type const col = tabular.column_of_cell(cur.idx());
640 // int const t = cur.bv().top_y() + cur.bv().height();
642 // cur.bv().scrollDocView(t);
646 // cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
650 // cur.par() = cur.lastpar();
651 // cur.pos() = cur.lastpos();
655 case LFUN_LAYOUT_TABULAR:
656 InsetTabularMailer(*this).showDialog(&cur.bv());
659 case LFUN_INSET_DIALOG_UPDATE:
660 InsetTabularMailer(*this).updateDialog(&cur.bv());
663 case LFUN_TABULAR_FEATURE:
664 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
668 // insert file functions
669 case LFUN_FILE_INSERT_ASCII_PARA:
670 case LFUN_FILE_INSERT_ASCII: {
672 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), to_utf8(cmd.argument()), false);
673 // FIXME: We don't know the encoding of the file
674 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), from_utf8(tmpstr), false))
680 if (tablemode(cur)) {
681 if (copySelection(cur)) {
682 recordUndoInset(cur, Undo::DELETE);
687 cell(cur.idx())->dispatch(cur, cmd);
690 case LFUN_CHAR_DELETE_BACKWARD:
691 case LFUN_CHAR_DELETE_FORWARD:
692 if (tablemode(cur)) {
693 recordUndoInset(cur, Undo::DELETE);
697 cell(cur.idx())->dispatch(cur, cmd);
701 if (!cur.selection())
703 if (tablemode(cur)) {
707 cell(cur.idx())->dispatch(cur, cmd);
710 case LFUN_CLIPBOARD_PASTE:
711 case LFUN_PRIMARY_SELECTION_PASTE: {
712 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
713 theClipboard().get() :
714 theSelection().get();
717 // pass to InsertAsciiString, but
718 // only if we have multi-cell content
719 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
720 if (insertAsciiString(cur.bv(), clip, false)) {
721 // content has been replaced,
722 // so cursor might be invalid
723 cur.pos() = cur.lastpos();
724 bvcur.setCursor(cur);
728 // so that the clipboard is used and it goes on
730 // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
731 paste_tabular.reset();
732 dirtyTabularStack(false);
738 if (hasPasteBuffer() && tabularStackDirty()) {
739 recordUndoInset(cur, Undo::INSERT);
743 cell(cur.idx())->dispatch(cur, cmd);
748 case LFUN_FONT_ROMAN:
754 case LFUN_FONT_FREE_APPLY:
755 case LFUN_FONT_FREE_UPDATE:
757 case LFUN_FONT_UNDERLINE:
759 case LFUN_WORD_CAPITALIZE:
760 case LFUN_WORD_UPCASE:
761 case LFUN_WORD_LOWCASE:
762 case LFUN_CHARS_TRANSPOSE:
763 if (tablemode(cur)) {
766 getSelection(cur, rs, re, cs, ce);
767 LCursor tmpcur = cur;
768 for (row_type i = rs; i <= re; ++i) {
769 for (col_type j = cs; j <= ce; ++j) {
770 // cursor follows cell:
771 tmpcur.idx() = tabular.getCellNumber(i, j);
772 // select this cell only:
775 tmpcur.resetAnchor();
776 tmpcur.pit() = tmpcur.lastpit();
777 tmpcur.pos() = tmpcur.top().lastpos();
778 tmpcur.setCursor(tmpcur);
779 tmpcur.setSelection();
780 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
785 cell(cur.idx())->dispatch(cur, cmd);
789 // we try to handle this event in the insets dispatch function.
790 cell(cur.idx())->dispatch(cur, cmd);
794 // FIXME: this accesses the position cache before it is initialized
796 InsetTabularMailer(*this).updateDialog(&cur.bv());
800 // function sets an object as defined in func_status.h:
801 // states OK, Unknown, Disabled, On, Off.
802 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
803 FuncStatus & status) const
805 switch (cmd.action) {
806 case LFUN_TABULAR_FEATURE: {
807 int action = LyXTabular::LAST_ACTION;
809 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
810 string const tmp = tabularFeature[i].feature;
811 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
812 action = tabularFeature[i].action;
816 if (action == LyXTabular::LAST_ACTION) {
818 status.unknown(true);
822 string const argument
823 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
825 row_type sel_row_start = 0;
826 row_type sel_row_end = 0;
828 LyXTabular::ltType dummyltt;
831 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
834 case LyXTabular::SET_PWIDTH:
835 case LyXTabular::SET_MPWIDTH:
836 case LyXTabular::SET_SPECIAL_COLUMN:
837 case LyXTabular::SET_SPECIAL_MULTI:
838 case LyXTabular::APPEND_ROW:
839 case LyXTabular::APPEND_COLUMN:
840 case LyXTabular::DELETE_ROW:
841 case LyXTabular::DELETE_COLUMN:
842 case LyXTabular::COPY_ROW:
843 case LyXTabular::COPY_COLUMN:
844 case LyXTabular::SET_ALL_LINES:
845 case LyXTabular::UNSET_ALL_LINES:
846 case LyXTabular::SET_TOP_SPACE:
847 case LyXTabular::SET_BOTTOM_SPACE:
848 case LyXTabular::SET_INTERLINE_SPACE:
852 case LyXTabular::MULTICOLUMN:
853 status.setOnOff(tabular.isMultiColumn(cur.idx()));
856 case LyXTabular::M_TOGGLE_LINE_TOP:
858 case LyXTabular::TOGGLE_LINE_TOP:
859 status.setOnOff(tabular.topLine(cur.idx(), flag));
862 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
864 case LyXTabular::TOGGLE_LINE_BOTTOM:
865 status.setOnOff(tabular.bottomLine(cur.idx(), flag));
868 case LyXTabular::M_TOGGLE_LINE_LEFT:
870 case LyXTabular::TOGGLE_LINE_LEFT:
871 status.setOnOff(tabular.leftLine(cur.idx(), flag));
874 case LyXTabular::M_TOGGLE_LINE_RIGHT:
876 case LyXTabular::TOGGLE_LINE_RIGHT:
877 status.setOnOff(tabular.rightLine(cur.idx(), flag));
880 case LyXTabular::M_ALIGN_LEFT:
882 case LyXTabular::ALIGN_LEFT:
883 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
886 case LyXTabular::M_ALIGN_RIGHT:
888 case LyXTabular::ALIGN_RIGHT:
889 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
892 case LyXTabular::M_ALIGN_CENTER:
894 case LyXTabular::ALIGN_CENTER:
895 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
898 case LyXTabular::ALIGN_BLOCK:
899 status.enabled(!tabular.getPWidth(cur.idx()).zero());
900 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
903 case LyXTabular::M_VALIGN_TOP:
905 case LyXTabular::VALIGN_TOP:
907 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
910 case LyXTabular::M_VALIGN_BOTTOM:
912 case LyXTabular::VALIGN_BOTTOM:
914 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
917 case LyXTabular::M_VALIGN_MIDDLE:
919 case LyXTabular::VALIGN_MIDDLE:
921 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
924 case LyXTabular::SET_LONGTABULAR:
925 status.setOnOff(tabular.isLongTabular());
928 case LyXTabular::UNSET_LONGTABULAR:
929 status.setOnOff(!tabular.isLongTabular());
932 case LyXTabular::SET_ROTATE_TABULAR:
933 status.setOnOff(tabular.getRotateTabular());
936 case LyXTabular::UNSET_ROTATE_TABULAR:
937 status.setOnOff(!tabular.getRotateTabular());
940 case LyXTabular::SET_ROTATE_CELL:
941 status.setOnOff(tabular.getRotateCell(cur.idx()));
944 case LyXTabular::UNSET_ROTATE_CELL:
945 status.setOnOff(!tabular.getRotateCell(cur.idx()));
948 case LyXTabular::SET_USEBOX:
949 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
952 case LyXTabular::SET_LTFIRSTHEAD:
953 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
956 case LyXTabular::UNSET_LTFIRSTHEAD:
957 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
960 case LyXTabular::SET_LTHEAD:
961 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
964 case LyXTabular::UNSET_LTHEAD:
965 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
968 case LyXTabular::SET_LTFOOT:
969 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
972 case LyXTabular::UNSET_LTFOOT:
973 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
976 case LyXTabular::SET_LTLASTFOOT:
977 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
980 case LyXTabular::UNSET_LTLASTFOOT:
981 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
984 case LyXTabular::SET_LTNEWPAGE:
985 status.setOnOff(tabular.getLTNewPage(sel_row_start));
988 case LyXTabular::SET_BOOKTABS:
989 status.setOnOff(tabular.useBookTabs());
992 case LyXTabular::UNSET_BOOKTABS:
993 status.setOnOff(!tabular.useBookTabs());
998 status.enabled(false);
1004 // These are only enabled inside tabular
1005 case LFUN_CELL_BACKWARD:
1006 case LFUN_CELL_FORWARD:
1007 status.enabled(true);
1010 // disable these with multiple cells selected
1011 case LFUN_INSET_INSERT:
1012 case LFUN_TABULAR_INSERT:
1013 case LFUN_CHARSTYLE_INSERT:
1014 case LFUN_FLOAT_INSERT:
1015 case LFUN_FLOAT_WIDE_INSERT:
1016 case LFUN_FOOTNOTE_INSERT:
1017 case LFUN_MARGINALNOTE_INSERT:
1018 case LFUN_MATH_INSERT:
1019 case LFUN_MATH_MODE:
1020 case LFUN_MATH_MUTATE:
1021 case LFUN_MATH_DISPLAY:
1022 case LFUN_NOTE_INSERT:
1023 case LFUN_OPTIONAL_INSERT:
1024 case LFUN_BOX_INSERT:
1025 case LFUN_BRANCH_INSERT:
1026 case LFUN_WRAP_INSERT:
1027 case LFUN_ERT_INSERT: {
1028 if (tablemode(cur)) {
1029 status.enabled(false);
1032 return cell(cur.idx())->getStatus(cur, cmd, status);
1035 // disable in non-fixed-width cells
1036 case LFUN_BREAK_LINE:
1037 case LFUN_BREAK_PARAGRAPH:
1038 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
1039 case LFUN_BREAK_PARAGRAPH_SKIP: {
1040 if (tabular.getPWidth(cur.idx()).zero()) {
1041 status.enabled(false);
1044 return cell(cur.idx())->getStatus(cur, cmd, status);
1048 if (tabularStackDirty()) {
1049 status.enabled(true);
1053 case LFUN_INSET_MODIFY:
1054 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
1055 status.enabled(true);
1061 // we try to handle this event in the insets dispatch function.
1062 return cell(cur.idx())->getStatus(cur, cmd, status);
1067 int InsetTabular::latex(Buffer const & buf, odocstream & os,
1068 OutputParams const & runparams) const
1070 return tabular.latex(buf, os, runparams);
1074 int InsetTabular::plaintext(Buffer const & buf, odocstream & os,
1075 OutputParams const & runparams) const
1077 int const dp = runparams.linelen ? runparams.depth : 0;
1078 return tabular.plaintext(buf, os, runparams, dp, false, 0);
1082 int InsetTabular::docbook(Buffer const & buf, odocstream & os,
1083 OutputParams const & runparams) const
1086 InsetBase * master = 0;
1088 #ifdef WITH_WARNINGS
1089 #warning Why not pass a proper DocIterator here?
1092 // if the table is inside a float it doesn't need the informaltable
1093 // wrapper. Search for it.
1094 for (master = owner(); master; master = master->owner())
1095 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1100 os << "<informaltable>";
1103 ret += tabular.docbook(buf, os, runparams);
1105 os << "</informaltable>";
1112 void InsetTabular::validate(LaTeXFeatures & features) const
1114 tabular.validate(features);
1118 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1120 return tabular.getCellInset(idx);
1124 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1126 return tabular.getCellInset(idx);
1130 void InsetTabular::cursorPos(BufferView const & bv,
1131 CursorSlice const & sl, bool boundary, int & x, int & y) const
1133 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
1135 // y offset correction
1136 int const row = tabular.row_of_cell(sl.idx());
1137 for (int i = 0; i <= row; ++i) {
1139 y += tabular.getAscentOfRow(i);
1140 y += tabular.getAdditionalHeight(i);
1143 y += tabular.getDescentOfRow(i);
1146 // x offset correction
1147 int const col = tabular.column_of_cell(sl.idx());
1148 int idx = tabular.getCellNumber(row, 0);
1149 for (int j = 0; j < col; ++j) {
1150 if (tabular.isPartOfMultiColumn(row, j))
1152 x += tabular.getWidthOfColumn(idx);
1155 x += tabular.getBeginningOfTextInCell(idx);
1156 x += ADD_TO_TABULAR_WIDTH;
1161 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
1165 InsetBase const & inset = *tabular.getCellInset(cell);
1166 Point o = bv.coordCache().getInsets().xy(&inset);
1167 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1168 int const xend = xbeg + tabular.getWidthOfColumn(cell);
1169 row_type const row = tabular.row_of_cell(cell);
1170 int const ybeg = o.y_ - tabular.getAscentOfRow(row) -
1171 tabular.getAdditionalHeight(row);
1172 int const yend = o.y_ + tabular.getDescentOfRow(row);
1184 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
1185 // << " ybeg=" << ybeg << " yend=" << yend
1186 // << " xx=" << xx << " yy=" << yy
1187 // << " dist=" << xx + yy << endl;
1192 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1194 //lyxerr << "InsetTabular::editXY: " << this << endl;
1195 cur.selection() = false;
1197 cur.idx() = getNearestCell(cur.bv(), x, y);
1199 return cell(cur.idx())->text_.editXY(cur, x, y);
1203 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1205 cur.idx() = getNearestCell(cur.bv(), x, y);
1206 cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1210 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
1212 idx_type idx_min = 0;
1213 int dist_min = std::numeric_limits<int>::max();
1214 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1215 if (bv.coordCache().getInsets().has(tabular.getCellInset(i).get())) {
1216 int const d = dist(bv, i, x, y);
1227 int InsetTabular::getCellXPos(idx_type const cell) const
1231 for (; !tabular.isFirstCellInRow(c); --c)
1233 int lx = tabular.getWidthOfColumn(cell);
1234 for (; c < cell; ++c)
1235 lx += tabular.getWidthOfColumn(c);
1237 return lx - tabular.getWidthOfColumn(cell);
1241 void InsetTabular::resetPos(LCursor & cur) const
1243 BufferView & bv = cur.bv();
1244 int const maxwidth = bv.workWidth();
1246 if (&cur.inset() != this) {
1250 int const X2 = maxwidth;
1251 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1252 int const x1 = xo(cur.bv()) + getCellXPos(cur.idx()) + offset;
1253 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1256 scx_ = X1 + 20 - x1;
1258 scx_ = X2 - 20 - x2;
1263 cur.updateFlags(Update::Force | Update::FitCursor);
1265 InsetTabularMailer(*this).updateDialog(&bv);
1269 void InsetTabular::moveNextCell(LCursor & cur)
1271 if (isRightToLeft(cur)) {
1272 if (tabular.isFirstCellInRow(cur.idx())) {
1273 row_type const row = tabular.row_of_cell(cur.idx());
1274 if (row == tabular.rows() - 1)
1276 cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1283 if (tabular.isLastCell(cur.idx()))
1293 void InsetTabular::movePrevCell(LCursor & cur)
1295 if (isRightToLeft(cur)) {
1296 if (tabular.isLastCellInRow(cur.idx())) {
1297 row_type const row = tabular.row_of_cell(cur.idx());
1300 cur.idx() = tabular.getFirstCellInRow(row);
1301 cur.idx() = tabular.getCellAbove(cur.idx());
1303 if (tabular.isLastCell(cur.idx()))
1308 if (cur.idx() == 0) // first cell
1312 cur.pit() = cur.lastpit();
1313 cur.pos() = cur.lastpos();
1315 // FIXME: this accesses the position cache before it is initialized
1320 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1322 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1325 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1326 string const tmp = tabularFeature[i].feature;
1328 if (tmp == what.substr(0, tmp.length())) {
1329 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1330 //tabularFeatures[i].feature.length())) {
1331 action = tabularFeature[i].action;
1335 if (action == LyXTabular::LAST_ACTION)
1339 ltrim(what.substr(tabularFeature[i].feature.length()));
1340 tabularFeatures(cur, action, val);
1347 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1348 string const & special, bool & flag)
1350 if (special == "dl_above") {
1353 } else if (special == "dl_below") {
1354 ltt.bottomDL = flag;
1356 } else if (special == "empty") {
1368 void InsetTabular::tabularFeatures(LCursor & cur,
1369 LyXTabular::Feature feature, string const & value)
1371 BufferView & bv = cur.bv();
1372 col_type sel_col_start;
1373 col_type sel_col_end;
1374 row_type sel_row_start;
1375 row_type sel_row_end;
1376 bool setLines = false;
1377 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1378 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1382 case LyXTabular::M_ALIGN_LEFT:
1383 case LyXTabular::ALIGN_LEFT:
1384 setAlign = LYX_ALIGN_LEFT;
1387 case LyXTabular::M_ALIGN_RIGHT:
1388 case LyXTabular::ALIGN_RIGHT:
1389 setAlign = LYX_ALIGN_RIGHT;
1392 case LyXTabular::M_ALIGN_CENTER:
1393 case LyXTabular::ALIGN_CENTER:
1394 setAlign = LYX_ALIGN_CENTER;
1397 case LyXTabular::ALIGN_BLOCK:
1398 setAlign = LYX_ALIGN_BLOCK;
1401 case LyXTabular::M_VALIGN_TOP:
1402 case LyXTabular::VALIGN_TOP:
1403 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1406 case LyXTabular::M_VALIGN_BOTTOM:
1407 case LyXTabular::VALIGN_BOTTOM:
1408 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1411 case LyXTabular::M_VALIGN_MIDDLE:
1412 case LyXTabular::VALIGN_MIDDLE:
1413 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1420 recordUndoInset(cur, Undo::ATOMIC);
1422 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1423 row_type const row = tabular.row_of_cell(cur.idx());
1424 col_type const column = tabular.column_of_cell(cur.idx());
1426 LyXTabular::ltType ltt;
1430 case LyXTabular::SET_PWIDTH: {
1431 LyXLength const len(value);
1432 tabular.setColumnPWidth(cur, cur.idx(), len);
1434 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1435 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1439 case LyXTabular::SET_MPWIDTH:
1440 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1443 case LyXTabular::SET_SPECIAL_COLUMN:
1444 case LyXTabular::SET_SPECIAL_MULTI:
1445 tabular.setAlignSpecial(cur.idx(),value,feature);
1448 case LyXTabular::APPEND_ROW:
1449 // append the row into the tabular
1450 tabular.appendRow(bv.buffer()->params(), cur.idx());
1453 case LyXTabular::APPEND_COLUMN:
1454 // append the column into the tabular
1455 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1456 cur.idx() = tabular.getCellNumber(row, column);
1459 case LyXTabular::DELETE_ROW:
1460 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1461 tabular.deleteRow(sel_row_start);
1462 if (sel_row_start >= tabular.rows())
1464 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1467 cur.selection() = false;
1470 case LyXTabular::DELETE_COLUMN:
1471 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1472 tabular.deleteColumn(sel_col_start);
1473 if (sel_col_start >= tabular.columns())
1475 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1478 cur.selection() = false;
1481 case LyXTabular::COPY_ROW:
1482 tabular.copyRow(bv.buffer()->params(), row);
1485 case LyXTabular::COPY_COLUMN:
1486 tabular.copyColumn(bv.buffer()->params(), column);
1487 cur.idx() = tabular.getCellNumber(row, column);
1490 case LyXTabular::M_TOGGLE_LINE_TOP:
1492 case LyXTabular::TOGGLE_LINE_TOP: {
1493 bool lineSet = !tabular.topLine(cur.idx(), flag);
1494 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1495 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1497 tabular.getCellNumber(i, j),
1502 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1504 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1505 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1506 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1507 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1508 tabular.setBottomLine(
1509 tabular.getCellNumber(i, j),
1515 case LyXTabular::M_TOGGLE_LINE_LEFT:
1517 case LyXTabular::TOGGLE_LINE_LEFT: {
1518 bool lineSet = !tabular.leftLine(cur.idx(), flag);
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.setLeftLine(
1522 tabular.getCellNumber(i,j),
1528 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1530 case LyXTabular::TOGGLE_LINE_RIGHT: {
1531 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1532 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1533 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1534 tabular.setRightLine(
1535 tabular.getCellNumber(i,j),
1541 case LyXTabular::M_ALIGN_LEFT:
1542 case LyXTabular::M_ALIGN_RIGHT:
1543 case LyXTabular::M_ALIGN_CENTER:
1545 case LyXTabular::ALIGN_LEFT:
1546 case LyXTabular::ALIGN_RIGHT:
1547 case LyXTabular::ALIGN_CENTER:
1548 case LyXTabular::ALIGN_BLOCK:
1549 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1550 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1551 tabular.setAlignment(
1552 tabular.getCellNumber(i, j),
1557 case LyXTabular::M_VALIGN_TOP:
1558 case LyXTabular::M_VALIGN_BOTTOM:
1559 case LyXTabular::M_VALIGN_MIDDLE:
1561 case LyXTabular::VALIGN_TOP:
1562 case LyXTabular::VALIGN_BOTTOM:
1563 case LyXTabular::VALIGN_MIDDLE:
1564 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1565 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1566 tabular.setVAlignment(
1567 tabular.getCellNumber(i, j),
1571 case LyXTabular::MULTICOLUMN: {
1572 if (sel_row_start != sel_row_end) {
1573 #ifdef WITH_WARNINGS
1574 #warning Need I say it ? This is horrible.
1577 Alert::error(_("Error setting multicolumn"),
1578 _("You cannot set multicolumn vertically."));
1581 if (!cur.selection()) {
1582 // just multicol for one single cell
1583 // check whether we are completely in a multicol
1584 if (tabular.isMultiColumn(cur.idx()))
1585 tabular.unsetMultiColumn(cur.idx());
1587 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1590 // we have a selection so this means we just add all this
1591 // cells to form a multicolumn cell
1592 idx_type const s_start = cur.selBegin().idx();
1593 idx_type const s_end = cur.selEnd().idx();
1594 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1595 cur.idx() = s_start;
1598 cur.selection() = false;
1602 case LyXTabular::SET_ALL_LINES:
1604 case LyXTabular::UNSET_ALL_LINES:
1605 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1606 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1607 tabular.setAllLines(
1608 tabular.getCellNumber(i,j), setLines);
1611 case LyXTabular::SET_LONGTABULAR:
1612 tabular.setLongTabular(true);
1615 case LyXTabular::UNSET_LONGTABULAR:
1616 tabular.setLongTabular(false);
1619 case LyXTabular::SET_ROTATE_TABULAR:
1620 tabular.setRotateTabular(true);
1623 case LyXTabular::UNSET_ROTATE_TABULAR:
1624 tabular.setRotateTabular(false);
1627 case LyXTabular::SET_ROTATE_CELL:
1628 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1629 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1630 tabular.setRotateCell(
1631 tabular.getCellNumber(i, j), true);
1634 case LyXTabular::UNSET_ROTATE_CELL:
1635 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1636 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1637 tabular.setRotateCell(
1638 tabular.getCellNumber(i, j), false);
1641 case LyXTabular::SET_USEBOX: {
1642 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1643 if (val == tabular.getUsebox(cur.idx()))
1644 val = LyXTabular::BOX_NONE;
1645 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1646 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1647 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1651 case LyXTabular::UNSET_LTFIRSTHEAD:
1653 case LyXTabular::SET_LTFIRSTHEAD:
1654 tabular.getRowOfLTFirstHead(row, ltt);
1655 checkLongtableSpecial(ltt, value, flag);
1656 tabular.setLTHead(row, flag, ltt, true);
1659 case LyXTabular::UNSET_LTHEAD:
1661 case LyXTabular::SET_LTHEAD:
1662 tabular.getRowOfLTHead(row, ltt);
1663 checkLongtableSpecial(ltt, value, flag);
1664 tabular.setLTHead(row, flag, ltt, false);
1667 case LyXTabular::UNSET_LTFOOT:
1669 case LyXTabular::SET_LTFOOT:
1670 tabular.getRowOfLTFoot(row, ltt);
1671 checkLongtableSpecial(ltt, value, flag);
1672 tabular.setLTFoot(row, flag, ltt, false);
1675 case LyXTabular::UNSET_LTLASTFOOT:
1677 case LyXTabular::SET_LTLASTFOOT:
1678 tabular.getRowOfLTLastFoot(row, ltt);
1679 checkLongtableSpecial(ltt, value, flag);
1680 tabular.setLTFoot(row, flag, ltt, true);
1683 case LyXTabular::SET_LTNEWPAGE:
1684 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1687 case LyXTabular::SET_BOOKTABS:
1688 tabular.setBookTabs(true);
1691 case LyXTabular::UNSET_BOOKTABS:
1692 tabular.setBookTabs(false);
1695 case LyXTabular::SET_TOP_SPACE: {
1697 if (value == "default")
1698 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1699 tabular.row_info[i].top_space_default = true;
1700 else if (isValidLength(value, &len))
1701 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1702 tabular.row_info[i].top_space_default = false;
1703 tabular.row_info[i].top_space = len;
1706 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1707 tabular.row_info[i].top_space_default = false;
1708 tabular.row_info[i].top_space = len;
1713 case LyXTabular::SET_BOTTOM_SPACE: {
1715 if (value == "default")
1716 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1717 tabular.row_info[i].bottom_space_default = true;
1718 else if (isValidLength(value, &len))
1719 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1720 tabular.row_info[i].bottom_space_default = false;
1721 tabular.row_info[i].bottom_space = len;
1724 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1725 tabular.row_info[i].bottom_space_default = false;
1726 tabular.row_info[i].bottom_space = len;
1731 case LyXTabular::SET_INTERLINE_SPACE: {
1733 if (value == "default")
1734 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1735 tabular.row_info[i].interline_space_default = true;
1736 else if (isValidLength(value, &len))
1737 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1738 tabular.row_info[i].interline_space_default = false;
1739 tabular.row_info[i].interline_space = len;
1742 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
1743 tabular.row_info[i].interline_space_default = false;
1744 tabular.row_info[i].interline_space = len;
1749 // dummy stuff just to avoid warnings
1750 case LyXTabular::LAST_ACTION:
1754 InsetTabularMailer(*this).updateDialog(&bv);
1758 bool InsetTabular::showInsetDialog(BufferView * bv) const
1760 InsetTabularMailer(*this).showDialog(bv);
1765 void InsetTabular::openLayoutDialog(BufferView * bv) const
1767 InsetTabularMailer(*this).showDialog(bv);
1771 bool InsetTabular::copySelection(LCursor & cur)
1773 if (!cur.selection())
1778 getSelection(cur, rs, re, cs, ce);
1780 paste_tabular.reset(new LyXTabular(tabular));
1782 for (row_type i = 0; i < rs; ++i)
1783 paste_tabular->deleteRow(0);
1785 row_type const rows = re - rs + 1;
1786 while (paste_tabular->rows() > rows)
1787 paste_tabular->deleteRow(rows);
1789 paste_tabular->setTopLine(0, true, true);
1790 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1793 for (col_type i = 0; i < cs; ++i)
1794 paste_tabular->deleteColumn(0);
1796 col_type const columns = ce - cs + 1;
1797 while (paste_tabular->columns() > columns)
1798 paste_tabular->deleteColumn(columns);
1800 paste_tabular->setLeftLine(0, true, true);
1801 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1804 odocstringstream os;
1805 OutputParams const runparams;
1806 paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1807 theClipboard().put(os.str());
1808 // mark tabular stack dirty
1809 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1810 // when we (hopefully) have a one-for-all paste mechanism.
1811 cap::dirtyTabularStack(true);
1817 bool InsetTabular::pasteSelection(LCursor & cur)
1821 col_type const actcol = tabular.column_of_cell(cur.idx());
1822 row_type const actrow = tabular.row_of_cell(cur.idx());
1823 for (row_type r1 = 0, r2 = actrow;
1824 r1 < paste_tabular->rows() && r2 < tabular.rows();
1826 for (col_type c1 = 0, c2 = actcol;
1827 c1 < paste_tabular->columns() && c2 < tabular.columns();
1829 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1830 tabular.isPartOfMultiColumn(r2, c2))
1832 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1836 if (tabular.isPartOfMultiColumn(r2, c2)) {
1840 shared_ptr<InsetText> inset(
1841 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1842 tabular.setCellInset(r2, c2, inset);
1843 // FIXME: change tracking (MG)
1844 inset->setChange(Change(cur.buffer().params().trackChanges ?
1845 Change::INSERTED : Change::UNCHANGED));
1853 void InsetTabular::cutSelection(LCursor & cur)
1855 if (!cur.selection())
1860 getSelection(cur, rs, re, cs, ce);
1861 for (row_type i = rs; i <= re; ++i) {
1862 for (col_type j = cs; j <= ce; ++j) {
1863 shared_ptr<InsetText> t
1864 = cell(tabular.getCellNumber(i, j));
1865 if (cur.buffer().params().trackChanges)
1866 // FIXME: Change tracking (MG)
1867 t->setChange(Change(Change::DELETED));
1873 // cursor position might be invalid now
1874 if (cur.pit() > cur.lastpit())
1875 cur.pit() = cur.lastpit();
1876 if (cur.pos() > cur.lastpos())
1877 cur.pos() = cur.lastpos();
1878 cur.clearSelection();
1882 bool InsetTabular::isRightToLeft(LCursor & cur) const
1884 BOOST_ASSERT(cur.depth() > 1);
1885 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1886 pos_type const parentpos = cur[cur.depth() - 2].pos();
1887 return parentpar.getFontSettings(cur.bv().buffer()->params(),
1888 parentpos).language()->rightToLeft();
1892 void InsetTabular::getSelection(LCursor & cur,
1893 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1895 CursorSlice const & beg = cur.selBegin();
1896 CursorSlice const & end = cur.selEnd();
1897 cs = tabular.column_of_cell(beg.idx());
1898 ce = tabular.column_of_cell(end.idx());
1901 cs = tabular.column_of_cell(end.idx());
1903 ce = tabular.right_column_of_cell(end.idx());
1906 rs = tabular.row_of_cell(beg.idx());
1907 re = tabular.row_of_cell(end.idx());
1913 LyXText * InsetTabular::getText(int idx) const
1915 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1919 void InsetTabular::setChange(Change const & change)
1921 for (idx_type idx = 0; idx < nargs(); ++idx)
1922 cell(idx)->setChange(change);
1926 void InsetTabular::acceptChanges()
1928 for (idx_type idx = 0; idx < nargs(); ++idx)
1929 cell(idx)->acceptChanges();
1933 void InsetTabular::rejectChanges()
1935 for (idx_type idx = 0; idx < nargs(); ++idx)
1936 cell(idx)->rejectChanges();
1940 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1942 return tabular.getPWidth(cell).zero();
1946 bool InsetTabular::insertAsciiString(BufferView & bv, docstring const & buf,
1949 if (buf.length() <= 0)
1952 Buffer const & buffer = *bv.buffer();
1956 col_type maxCols = 1;
1957 docstring::size_type const len = buf.length();
1958 docstring::size_type p = 0;
1961 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
1969 maxCols = max(cols, maxCols);
1975 maxCols = max(cols, maxCols);
1976 LyXTabular * loctab;
1981 paste_tabular.reset(
1982 new LyXTabular(buffer.params(), rows, maxCols));
1983 loctab = paste_tabular.get();
1985 dirtyTabularStack(true);
1988 cell = bv.cursor().idx();
1989 ocol = tabular.column_of_cell(cell);
1990 row = tabular.row_of_cell(cell);
1993 docstring::size_type op = 0;
1994 idx_type const cells = loctab->getNumberOfCells();
1997 rows = loctab->rows();
1998 col_type const columns = loctab->columns();
2000 while (cell < cells && p < len && row < rows &&
2001 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
2007 // we can only set this if we are not too far right
2008 if (cols < columns) {
2009 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2010 Paragraph & par = inset->text_.getPar(0);
2011 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2012 inset->setText(buf.substr(op, p - op), font,
2013 buffer.params().trackChanges);
2019 // we can only set this if we are not too far right
2020 if (cols < columns) {
2021 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
2022 Paragraph & par = inset->text_.getPar(0);
2023 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2024 inset->setText(buf.substr(op, p - op), font,
2025 buffer.params().trackChanges);
2030 cell = loctab->getCellNumber(row, cols);
2036 // check for the last cell if there is no trailing '\n'
2037 if (cell < cells && op < len) {
2038 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
2039 Paragraph & par = inset->text_.getPar(0);
2040 LyXFont const font = inset->text_.getFont(buffer, par, 0);
2041 inset->setText(buf.substr(op, len - op), font,
2042 buffer.params().trackChanges);
2048 void InsetTabular::addPreview(PreviewLoader & loader) const
2050 row_type const rows = tabular.rows();
2051 col_type const columns = tabular.columns();
2052 for (row_type i = 0; i < rows; ++i) {
2053 for (col_type j = 0; j < columns; ++j)
2054 tabular.getCellInset(i, j)->addPreview(loader);
2059 bool InsetTabular::tablemode(LCursor & cur) const
2061 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
2068 string const InsetTabularMailer::name_("tabular");
2070 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2071 : inset_(const_cast<InsetTabular &>(inset))
2075 string const InsetTabularMailer::inset2string(Buffer const &) const
2077 return params2string(inset_);
2081 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2083 istringstream data(in);
2085 lex.setStream(data);
2092 if (!lex || token != name_)
2093 return print_mailer_error("InsetTabularMailer", in, 1,
2096 // This is part of the inset proper that is usually swallowed
2097 // by Buffer::readInset
2099 if (!lex || token != "Tabular")
2100 return print_mailer_error("InsetTabularMailer", in, 2,
2103 Buffer const & buffer = inset.buffer();
2104 inset.read(buffer, lex);
2108 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2111 data << name_ << ' ';
2112 inset.write(inset.buffer(), data);
2113 data << "\\end_inset\n";