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"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
35 #include "frontends/Alert.h"
36 #include "frontends/font_metrics.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
40 #include "support/std_sstream.h"
44 using lyx::graphics::PreviewLoader;
46 using lyx::support::ltrim;
47 using lyx::support::strToInt;
48 using lyx::support::strToDbl;
55 using std::istringstream;
57 using std::ostringstream;
62 int const ADD_TO_HEIGHT = 2;
63 int const ADD_TO_TABULAR_WIDTH = 2;
66 boost::scoped_ptr<LyXTabular> paste_tabular;
69 struct TabularFeature {
70 LyXTabular::Feature action;
75 TabularFeature tabularFeature[] =
77 { LyXTabular::APPEND_ROW, "append-row" },
78 { LyXTabular::APPEND_COLUMN, "append-column" },
79 { LyXTabular::DELETE_ROW, "delete-row" },
80 { LyXTabular::DELETE_COLUMN, "delete-column" },
81 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
82 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
83 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
84 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
85 { LyXTabular::ALIGN_LEFT, "align-left" },
86 { LyXTabular::ALIGN_RIGHT, "align-right" },
87 { LyXTabular::ALIGN_CENTER, "align-center" },
88 { LyXTabular::ALIGN_BLOCK, "align-block" },
89 { LyXTabular::VALIGN_TOP, "valign-top" },
90 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
91 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
92 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
93 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
94 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
95 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
96 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
97 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
98 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
99 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
100 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
101 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
102 { LyXTabular::MULTICOLUMN, "multicolumn" },
103 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
104 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
105 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
106 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
107 { LyXTabular::SET_PWIDTH, "set-pwidth" },
108 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
109 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
110 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
111 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
112 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
113 { LyXTabular::SET_USEBOX, "set-usebox" },
114 { LyXTabular::SET_LTHEAD, "set-lthead" },
115 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
116 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
117 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
118 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
119 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
120 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
121 { LyXTabular::LAST_ACTION, "" }
125 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
127 FeatureEqual(LyXTabular::Feature feature)
128 : feature_(feature) {}
129 bool operator()(TabularFeature const & tf) const {
130 return tf.action == feature_;
133 LyXTabular::Feature feature_;
139 string const featureAsString(LyXTabular::Feature feature)
141 TabularFeature * end = tabularFeature +
142 sizeof(tabularFeature) / sizeof(TabularFeature);
143 TabularFeature * it = std::find_if(tabularFeature, end,
144 FeatureEqual(feature));
145 return (it == end) ? string() : it->feature;
149 bool InsetTabular::hasPasteBuffer() const
151 return (paste_tabular.get() != 0);
155 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
156 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
157 buffer_(&buf), cursorx_(0), cursory_(0), tablemode(false)
159 tabular.setOwner(this);
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166 : UpdatableInset(tab), tabular(tab.tabular),
167 buffer_(tab.buffer_), cursorx_(0), cursory_(0), tablemode(false)
169 tabular.setOwner(this);
175 InsetTabular::~InsetTabular()
177 InsetTabularMailer(*this).hideDialog();
181 auto_ptr<InsetBase> InsetTabular::clone() const
183 return auto_ptr<InsetBase>(new InsetTabular(*this));
187 Buffer const & InsetTabular::buffer() const
193 void InsetTabular::buffer(Buffer * b)
199 void InsetTabular::write(Buffer const & buf, ostream & os) const
201 os << "Tabular" << endl;
202 tabular.write(buf, os);
206 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
208 bool const old_format = (lex.getString() == "\\LyXTable");
210 tabular.read(buf, lex);
216 string token = lex.getString();
217 while (lex.isOK() && (token != "\\end_inset")) {
219 token = lex.getString();
221 if (token != "\\end_inset") {
222 lex.printError("Missing \\end_inset at this point. "
228 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
230 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
231 // mi.base.textwidth << "\n";
233 lyxerr << "InsetTabular::metrics: need bv" << endl;
237 calculate_dimensions_of_cells(mi);
239 dim.asc = tabular.getAscentOfRow(0);
240 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
241 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
246 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
248 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
250 BufferView * bv = pi.base.bv;
257 x += ADD_TO_TABULAR_WIDTH;
260 first_visible_cell = -1;
261 for (int i = 0; i < tabular.rows(); ++i) {
263 cell = tabular.getCellNumber(i, 0);
264 if (y + tabular.getDescentOfRow(i) <= 0 &&
265 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
267 y += tabular.getDescentOfRow(i) +
268 tabular.getAscentOfRow(i + 1) +
269 tabular.getAdditionalHeight(i + 1);
272 for (int j = 0; j < tabular.columns(); ++j) {
273 if (nx > bv->workWidth())
275 if (tabular.isPartOfMultiColumn(i, j))
277 if (first_visible_cell < 0)
278 first_visible_cell = cell;
280 drawCellSelection(pi.pain, nx, y, i, j, cell);
282 int const cx = nx + tabular.getBeginningOfTextInCell(cell);
283 tabular.getCellInset(cell).draw(pi, cx, y);
284 drawCellLines(pi.pain, nx, y, i, cell);
285 nx += tabular.getWidthOfColumn(cell);
289 // Would be nice, but for some completely unfathomable reason,
290 // on a col resize to a new fixed width, even though the insettexts
291 // are resized, the cell isn't, but drawing all cells in a tall table
292 // has the desired effect somehow. Complete dark magic.
294 // avoiding drawing the rest of a long table is
295 // a pretty big speedup
296 if (y > bv->workHeight())
300 y += tabular.getDescentOfRow(i) +
301 tabular.getAscentOfRow(i + 1) +
302 tabular.getAdditionalHeight(i + 1);
307 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
308 int row, int cell) const
310 int x2 = x + tabular.getWidthOfColumn(cell);
313 if (!tabular.topAlreadyDrawn(cell)) {
314 on_off = !tabular.topLine(cell);
315 pain.line(x, y - tabular.getAscentOfRow(row),
316 x2, y - tabular.getAscentOfRow(row),
317 on_off ? LColor::tabularonoffline : LColor::tabularline,
318 on_off ? Painter::line_onoffdash : Painter::line_solid);
320 on_off = !tabular.bottomLine(cell);
321 pain.line(x, y + tabular.getDescentOfRow(row),
322 x2, y + tabular.getDescentOfRow(row),
323 on_off ? LColor::tabularonoffline : LColor::tabularline,
324 on_off ? Painter::line_onoffdash : Painter::line_solid);
325 if (!tabular.leftAlreadyDrawn(cell)) {
326 on_off = !tabular.leftLine(cell);
327 pain.line(x, y - tabular.getAscentOfRow(row),
328 x, y + tabular.getDescentOfRow(row),
329 on_off ? LColor::tabularonoffline : LColor::tabularline,
330 on_off ? Painter::line_onoffdash : Painter::line_solid);
332 on_off = !tabular.rightLine(cell);
333 pain.line(x2 - tabular.getAdditionalWidth(cell),
334 y - tabular.getAscentOfRow(row),
335 x2 - tabular.getAdditionalWidth(cell),
336 y + tabular.getDescentOfRow(row),
337 on_off ? LColor::tabularonoffline : LColor::tabularline,
338 on_off ? Painter::line_onoffdash : Painter::line_solid);
342 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
343 int row, int column, int cell) const
348 BOOST_ASSERT(hasSelection());
349 int cs = tabular.column_of_cell(sel_cell_start);
350 int ce = tabular.column_of_cell(sel_cell_end);
353 cs = tabular.column_of_cell(sel_cell_end);
355 ce = tabular.right_column_of_cell(sel_cell_end);
358 int rs = tabular.row_of_cell(sel_cell_start);
359 int re = tabular.row_of_cell(sel_cell_end);
363 if (column >= cs && column <= ce && row >= rs && row <= re) {
364 int w = tabular.getWidthOfColumn(cell);
365 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
366 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
367 w, h, LColor::selection);
372 string const InsetTabular::editMessage() const
374 return _("Opened table");
378 void InsetTabular::updateLocal(BufferView * bv) const
384 extern LCursor theTempCursor;
388 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
390 if (hasSelection() && cmd.button() == mouse_button::button3)
393 BufferView * bv = cmd.view();
395 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
398 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
401 bv->fullCursor(theTempCursor);
402 bv->fullCursor().push(this);
403 bv->fullCursor().cell(cell);
406 setPos(bv, cmd.x, cmd.y);
407 bv->fullCursor(theTempCursor);
408 bv->fullCursor().cell(cell);
410 lyxerr << bv->cursor() << endl;
412 if (cmd.button() == mouse_button::button2)
413 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
417 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
419 BufferView * bv = cmd.view();
420 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
421 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
423 setPos(bv, cmd.x, cmd.y);
424 if (!hasSelection()) {
425 setSelection(actcell, actcell);
428 bv->cursor().cell(actcell);
429 setSelection(sel_cell_start, actcell);
430 tablemode = (sel_cell_start != actcell);
435 void InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
437 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
438 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
439 if (cmd.button() == mouse_button::button3)
440 InsetTabularMailer(*this).showDialog(cmd.view());
444 void InsetTabular::edit(BufferView * bv, bool left)
446 lyxerr << "InsetTabular::edit: " << this << endl;
451 if (isRightToLeft(bv))
452 cell = tabular.getLastCellInRow(0);
456 if (isRightToLeft(bv))
457 cell = tabular.getFirstCellInRow(tabular.rows()-1);
459 cell = tabular.getNumberOfCells() - 1;
464 bv->fullCursor().push(this);
465 bv->fullCursor().cell(cell);
466 lyxerr << bv->cursor() << endl;
470 void InsetTabular::edit(BufferView * bv, int x, int y)
472 lyxerr << "InsetTabular::edit: " << this << " first cell "
473 << &tabular.cell_info[0][0].inset << endl;
479 //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
480 bv->fullCursor().push(this);
482 // activateCellInset(bv, cell, x - xx, y - cursory_);
487 InsetTabular::priv_dispatch(FuncRequest const & cmd,
488 idx_type & idx, pos_type & pos)
490 lyxerr << "# InsetTabular::dispatch: " << cmd
491 << " tablemode: " << tablemode << endl;
493 DispatchResult result(true, true);
494 BufferView * bv = cmd.view();
496 switch (cmd.action) {
498 case LFUN_MOUSE_PRESS:
500 return DispatchResult(true, true);
502 case LFUN_MOUSE_MOTION:
503 lfunMouseMotion(cmd);
504 return DispatchResult(true, true);
506 case LFUN_MOUSE_RELEASE:
507 lfunMouseRelease(cmd);
508 return DispatchResult(true, true);
514 CursorSlice & cur = bv->cursor();
519 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
520 result = tabular.getCellInset(cell).dispatch(cmd, idx, pos);
522 switch (result.val()) {
524 if (movePrevCell(bv, cur))
525 result = DispatchResult(true, true);
527 result = DispatchResult(false, FINISHED);
531 if (moveNextCell(bv, cur))
532 result = DispatchResult(true, true);
534 result = DispatchResult(false, FINISHED_RIGHT);
538 if (moveUpLock(bv, cur))
539 result = DispatchResult(true, true);
541 result = DispatchResult(false, FINISHED_UP);
545 if (moveDownLock(bv, cur))
546 result = DispatchResult(true, true);
548 result = DispatchResult(false, FINISHED_UP);
552 lyxerr << "# don't handle dispatch" << endl;
556 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
559 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
560 switch (cmd.action) {
562 case LFUN_CELL_BACKWARD:
563 case LFUN_CELL_FORWARD:
564 if (cmd.action == LFUN_CELL_FORWARD)
565 moveNextCell(bv, cur);
567 movePrevCell(bv, cur);
571 case LFUN_SCROLL_INSET:
572 if (!cmd.argument.empty()) {
573 if (cmd.argument.find('.') != cmd.argument.npos)
574 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
576 scroll(cmd.view(), strToInt(cmd.argument));
577 cmd.view()->update();
578 return DispatchResult(true, true);
581 case LFUN_RIGHTSEL: {
582 int const start = hasSelection() ? sel_cell_start : cur.idx_;
583 if (tabular.isLastCellInRow(cur.idx_)) {
584 setSelection(start, cur.idx_);
589 // if we are starting a selection, only select
590 // the current cell at the beginning
591 if (hasSelection()) {
595 setSelection(start, end);
600 if (!moveRightLock(bv, cur))
601 result = DispatchResult(false, FINISHED_RIGHT);
606 int const start = hasSelection() ? sel_cell_start : cur.idx_;
607 if (tabular.isFirstCellInRow(cur.idx_)) {
608 setSelection(start, cur.idx_);
613 // if we are starting a selection, only select
614 // the current cell at the beginning
615 if (hasSelection()) {
619 setSelection(start, end);
624 if (!moveLeftLock(bv, cur))
625 result = DispatchResult(false, FINISHED);
630 int const start = hasSelection() ? sel_cell_start : cur.idx_;
631 int const ocell = cur.idx_;
632 // if we are starting a selection, only select
633 // the current cell at the beginning
634 if (hasSelection()) {
636 if (ocell == sel_cell_end ||
637 tabular.column_of_cell(ocell) > tabular.column_of_cell(cur.idx_))
638 setSelection(start, tabular.getCellBelow(sel_cell_end));
640 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
642 setSelection(start, start);
648 if (!moveDown(bv, cur))
649 result = DispatchResult(false, FINISHED_DOWN);
654 int const start = hasSelection() ? sel_cell_start : cur.idx_;
655 int const ocell = cur.idx_;
656 // if we are starting a selection, only select
657 // the current cell at the beginning
658 if (hasSelection()) {
660 if (ocell == sel_cell_end ||
661 tabular.column_of_cell(ocell) > tabular.column_of_cell(cur.idx_))
662 setSelection(start, tabular.getCellAbove(sel_cell_end));
664 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
666 setSelection(start, start);
672 if (!moveUp(bv, cur))
673 result = DispatchResult(false, FINISHED_DOWN);
680 int actcell = bv->cursor().idx();
681 int actcol = tabular.column_of_cell(actcell);
683 if (bv->top_y() + bv->painter().paperHeight()
684 < yo_ + tabular.getHeightOfTabular())
686 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
687 cur.idx_ = tabular.getCellBelow(first_visible_cell) + column;
689 cur.idx_ = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
698 int actcell = bv->cursor().idx();
699 int actcol = tabular.column_of_cell(actcell);
702 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
706 cur.idx_ = tabular.getCellBelow(first_visible_cell) + column;
714 // none of these make sense for insettabular,
715 // but we must catch them to prevent any
716 // selection from being confused
720 case LFUN_WORDLEFTSEL:
722 case LFUN_WORDRIGHTSEL:
724 case LFUN_DOWN_PARAGRAPH:
725 case LFUN_DOWN_PARAGRAPHSEL:
726 case LFUN_UP_PARAGRAPH:
727 case LFUN_UP_PARAGRAPHSEL:
733 case LFUN_BEGINNINGBUF:
734 case LFUN_BEGINNINGBUFSEL:
739 case LFUN_LAYOUT_TABULAR:
740 InsetTabularMailer(*this).showDialog(bv);
743 case LFUN_INSET_DIALOG_UPDATE:
744 InsetTabularMailer(*this).updateDialog(bv);
747 case LFUN_TABULAR_FEATURE:
748 if (!tabularFeatures(bv, cmd.argument))
749 result = DispatchResult(false);
752 // insert file functions
753 case LFUN_FILE_INSERT_ASCII_PARA:
754 case LFUN_FILE_INSERT_ASCII: {
755 string tmpstr = getContentsOfAsciiFile(bv, cmd.argument, false);
756 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
757 result = DispatchResult(false);
771 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
775 if (copySelection(bv)) {
776 recordUndo(bv, Undo::DELETE);
777 cutSelection(bv->buffer()->params());
782 recordUndo(bv, Undo::DELETE);
783 cutSelection(bv->buffer()->params());
793 case LFUN_PASTESELECTION: {
794 string const clip = bv->getClipboard();
797 if (clip.find('\t') != string::npos) {
801 string::size_type len = clip.length();
802 string::size_type p = 0;
805 (p = clip.find_first_of("\t\n", p)) != string::npos) {
813 maxCols = max(cols, maxCols);
819 maxCols = max(cols, maxCols);
822 new LyXTabular(bv->buffer()->params(), rows, maxCols));
824 string::size_type op = 0;
826 int cells = paste_tabular->getNumberOfCells();
830 while (cell < cells && p < len &&
831 (p = clip.find_first_of("\t\n", p)) != string::npos) {
836 paste_tabular->getCellInset(cell).
837 setText(clip.substr(op, p-op), font);
842 paste_tabular->getCellInset(cell).
843 setText(clip.substr(op, p-op), font);
844 while (cols++ < maxCols)
852 // check for the last cell if there is no trailing '\n'
853 if (cell < cells && op < len)
854 paste_tabular->getCellInset(cell).
855 setText(clip.substr(op, len-op), font);
856 } else if (!insertAsciiString(bv, clip, true))
858 // so that the clipboard is used and it goes on
860 // and executes LFUN_PASTESELECTION in insettext!
861 paste_tabular.reset();
867 if (hasPasteBuffer()) {
868 recordUndo(bv, Undo::INSERT);
874 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
877 // handle font changing stuff on selection before we lock the inset
878 // in the default part!
879 result = DispatchResult(false);
880 // we try to activate the actual inset and put this event down to
881 // the insets dispatch function.
886 InsetTabularMailer(*this).updateDialog(bv);
889 if (cmd.action == LFUN_INSET_TOGGLE) {
890 tablemode = !tablemode;
891 result = DispatchResult(true, true);
898 int InsetTabular::latex(Buffer const & buf, ostream & os,
899 OutputParams const & runparams) const
901 return tabular.latex(buf, os, runparams);
905 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
906 OutputParams const & runparams) const
908 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
909 return tabular.plaintext(buf, os, runparams, dp, false, 0);
913 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
914 OutputParams const & runparams) const
916 return tabular.linuxdoc(buf,os, runparams);
920 int InsetTabular::docbook(Buffer const & buf, ostream & os,
921 OutputParams const & runparams) const
926 // if the table is inside a float it doesn't need the informaltable
927 // wrapper. Search for it.
928 for (master = owner(); master; master = master->owner())
929 if (master->lyxCode() == InsetOld::FLOAT_CODE)
933 os << "<informaltable>";
934 if (runparams.mixed_content)
938 ret += tabular.docbook(buf, os, runparams);
940 os << "</informaltable>";
941 if (runparams.mixed_content)
949 void InsetTabular::validate(LaTeXFeatures & features) const
951 tabular.validate(features);
955 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
957 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
960 for (int j = 0; j < tabular.columns(); ++j) {
961 if (tabular.isPartOfMultiColumn(i, j))
967 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
968 tabular.getCellInset(cell).metrics(m, dim);
969 maxAsc = max(maxAsc, dim.asc);
970 maxDesc = max(maxDesc, dim.des);
971 tabular.setWidthOfCell(cell, dim.wid);
973 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
974 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
979 void InsetTabular::getCursorPos(int cell, int & x, int & y) const
981 InsetText const & inset = tabular.getCellInset(cell);
982 inset.getCursorPos(cell, x, y);
983 x += inset.x() - xo_;
984 y += inset.y() - yo_;
988 void InsetTabular::setPos(BufferView * bv, int x, int y) const
990 int const cell = getCell(x + xo_, y + yo_);
991 lyxerr << "# InsetTabular::setPos() cell: " << cell << endl;
992 InsetText const & inset = tabular.getCellInset(cell);
993 inset.text_.setCursorFromCoordinates(x, y);
999 int ly = tabular.getDescentOfRow(actrow);
1001 // first search the right row
1002 while (ly < y && actrow + 1 < tabular.rows()) {
1003 cursory_ += tabular.getDescentOfRow(actrow) +
1004 tabular.getAscentOfRow(actrow + 1) +
1005 tabular.getAdditionalHeight(actrow + 1);
1007 ly = cursory_ + tabular.getDescentOfRow(actrow);
1009 actcell = tabular.getCellNumber(actrow, actcol);
1011 // now search the right column
1012 int lx = tabular.getWidthOfColumn(actcell) -
1013 tabular.getAdditionalWidth(actcell);
1015 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1016 lx += tabular.getWidthOfColumn(actcell + 1)
1017 + tabular.getAdditionalWidth(actcell);
1019 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1025 int InsetTabular::getCellXPos(int cell) const
1029 for (; !tabular.isFirstCellInRow(c); --c)
1031 int lx = tabular.getWidthOfColumn(cell);
1032 for (; c < cell; ++c)
1033 lx += tabular.getWidthOfColumn(c);
1035 return lx - tabular.getWidthOfColumn(cell) + xo_;
1039 void InsetTabular::resetPos(BufferView *) const
1042 #ifdef WITH_WARNINGS
1043 #warning This should be fixed in the right manner (20011128 Jug)
1045 // fast hack to fix infinite repaintings!
1046 if (in_reset_pos > 0)
1050 int actcell = bv->cursor().cell();
1051 int actcol = tabular.column_of_cell(actcell);
1054 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1055 if (tabular.isLastCellInRow(cell)) {
1056 cursory_ += tabular.getDescentOfRow(actrow) +
1057 tabular.getAscentOfRow(actrow + 1) +
1058 tabular.getAdditionalHeight(actrow + 1);
1063 // we need this only from here on!!!
1065 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1066 int new_x = getCellXPos(actcell) + offset;
1067 int old_x = cursorx_;
1069 // cursor.x(getCellXPos(actcell) + offset);
1070 if (actcol < tabular.columns() - 1 && scroll(false) &&
1071 tabular.getWidthOfTabular() < bv->workWidth()-20)
1075 } else if (cursorx_ - offset > 20 &&
1076 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1077 > bv->workWidth() - 20) {
1078 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1080 } else if (cursorx_ - offset < 20) {
1081 scroll(bv, 20 - cursorx_ + offset);
1083 } else if (scroll() && xo_ > 20 &&
1084 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1085 scroll(bv, old_x - cursorx_);
1088 InsetTabularMailer(*this).updateDialog(bv);
1094 bool InsetTabular::moveRight(BufferView * bv, CursorSlice & cur)
1096 bool moved = isRightToLeft(bv)
1097 ? movePrevCell(bv, cur) : moveNextCell(bv, cur);
1105 bool InsetTabular::moveRightLock(BufferView * bv, CursorSlice & cur)
1107 bool moved = isRightToLeft(bv)
1108 ? movePrevCell(bv, cur) : moveNextCell(bv, cur);
1111 activateCellInset(bv, cur.idx_, false);
1116 bool InsetTabular::moveLeft(BufferView * bv, CursorSlice & cur)
1118 bool moved = isRightToLeft(bv)
1119 ? moveNextCell(bv, cur) : movePrevCell(bv, cur);
1127 bool InsetTabular::moveLeftLock(BufferView * bv, CursorSlice & cur)
1129 bool moved = isRightToLeft(bv)
1130 ? moveNextCell(bv, cur) : movePrevCell(bv, cur);
1133 activateCellInset(bv, cur.idx_, true);
1138 bool InsetTabular::moveUp(BufferView * bv, CursorSlice & cur)
1140 if (tabular.row_of_cell(cur.idx_) == 0)
1142 cur.idx_ = tabular.getCellAbove(cur.idx_);
1148 bool InsetTabular::moveUpLock(BufferView * bv, CursorSlice & cur)
1150 if (tabular.row_of_cell(cur.idx_) == 0)
1152 cur.idx_ = tabular.getCellAbove(cur.idx_);
1154 activateCellInset(bv, cur.idx_, bv->x_target(), 0);
1159 bool InsetTabular::moveDown(BufferView * bv, CursorSlice & cur)
1161 if (tabular.row_of_cell(cur.idx_) == tabular.rows() - 1)
1163 cur.idx_ = tabular.getCellBelow(cur.idx_);
1169 bool InsetTabular::moveDownLock(BufferView * bv, CursorSlice & cur)
1171 if (tabular.row_of_cell(cur.idx_) == tabular.rows() - 1)
1173 cur.idx_ = tabular.getCellBelow(cur.idx_);
1175 activateCellInset(bv, cur.idx_, bv->x_target());
1180 bool InsetTabular::moveNextCell(BufferView * bv, CursorSlice & cur)
1182 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1183 if (isRightToLeft(bv)) {
1184 if (tabular.isFirstCellInRow(cur.idx_)) {
1185 int row = tabular.row_of_cell(cur.idx_);
1186 if (row == tabular.rows() - 1)
1188 cur.idx_ = tabular.getLastCellInRow(row);
1189 cur.idx_ = tabular.getCellBelow(cur.idx_);
1196 if (tabular.isLastCell(cur.idx_))
1200 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1206 bool InsetTabular::movePrevCell(BufferView * bv, CursorSlice & cur)
1208 if (isRightToLeft(bv)) {
1209 if (tabular.isLastCellInRow(cur.idx_)) {
1210 int row = tabular.row_of_cell(cur.idx_);
1213 cur.idx_ = tabular.getFirstCellInRow(row);
1214 cur.idx_ = tabular.getCellAbove(cur.idx_);
1216 if (tabular.isLastCell(cur.idx_))
1221 if (cur.idx_ == 0) // first cell
1230 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1232 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1235 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1236 string const tmp = tabularFeature[i].feature;
1238 if (tmp == what.substr(0, tmp.length())) {
1239 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1240 //tabularFeatures[i].feature.length())) {
1241 action = tabularFeature[i].action;
1245 if (action == LyXTabular::LAST_ACTION)
1249 ltrim(what.substr(tabularFeature[i].feature.length()));
1250 tabularFeatures(bv, action, val);
1257 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1258 string const & special, bool & flag)
1260 if (special == "dl_above") {
1263 } else if (special == "dl_below") {
1264 ltt.bottomDL = flag;
1266 } else if (special == "empty") {
1278 void InsetTabular::tabularFeatures(BufferView * bv,
1279 LyXTabular::Feature feature, string const & value)
1281 int actcell = bv->cursor().idx();
1286 bool setLines = false;
1287 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1288 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1292 case LyXTabular::M_ALIGN_LEFT:
1293 case LyXTabular::ALIGN_LEFT:
1294 setAlign = LYX_ALIGN_LEFT;
1297 case LyXTabular::M_ALIGN_RIGHT:
1298 case LyXTabular::ALIGN_RIGHT:
1299 setAlign = LYX_ALIGN_RIGHT;
1302 case LyXTabular::M_ALIGN_CENTER:
1303 case LyXTabular::ALIGN_CENTER:
1304 setAlign = LYX_ALIGN_CENTER;
1307 case LyXTabular::ALIGN_BLOCK:
1308 setAlign = LYX_ALIGN_BLOCK;
1311 case LyXTabular::M_VALIGN_TOP:
1312 case LyXTabular::VALIGN_TOP:
1313 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1316 case LyXTabular::M_VALIGN_BOTTOM:
1317 case LyXTabular::VALIGN_BOTTOM:
1318 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1321 case LyXTabular::M_VALIGN_MIDDLE:
1322 case LyXTabular::VALIGN_MIDDLE:
1323 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1330 if (hasSelection()) {
1331 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1333 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1334 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1336 recordUndo(bv, Undo::ATOMIC);
1338 int row = tabular.row_of_cell(actcell);
1339 int column = tabular.column_of_cell(actcell);
1341 LyXTabular::ltType ltt;
1345 case LyXTabular::SET_PWIDTH: {
1346 LyXLength const len(value);
1347 tabular.setColumnPWidth(actcell, len);
1349 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1350 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1351 else if (!len.zero()
1352 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1353 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1357 case LyXTabular::SET_MPWIDTH:
1358 tabular.setMColumnPWidth(actcell, LyXLength(value));
1361 case LyXTabular::SET_SPECIAL_COLUMN:
1362 case LyXTabular::SET_SPECIAL_MULTI:
1363 tabular.setAlignSpecial(actcell,value,feature);
1366 case LyXTabular::APPEND_ROW:
1367 // append the row into the tabular
1368 tabular.appendRow(bv->buffer()->params(), actcell);
1369 tabular.setOwner(this);
1372 case LyXTabular::APPEND_COLUMN:
1373 // append the column into the tabular
1374 tabular.appendColumn(bv->buffer()->params(), actcell);
1375 tabular.setOwner(this);
1376 actcell = tabular.getCellNumber(row, column);
1379 case LyXTabular::DELETE_ROW:
1380 for (int i = sel_row_start; i <= sel_row_end; ++i)
1381 tabular.deleteRow(sel_row_start);
1382 if (sel_row_start >= tabular.rows())
1384 actcell = tabular.getCellNumber(sel_row_start, column);
1388 case LyXTabular::DELETE_COLUMN:
1389 for (int i = sel_col_start; i <= sel_col_end; ++i)
1390 tabular.deleteColumn(sel_col_start);
1391 if (sel_col_start >= tabular.columns())
1393 actcell = tabular.getCellNumber(row, sel_col_start);
1397 case LyXTabular::M_TOGGLE_LINE_TOP:
1399 case LyXTabular::TOGGLE_LINE_TOP: {
1400 bool lineSet = !tabular.topLine(actcell, flag);
1401 for (int i = sel_row_start; i <= sel_row_end; ++i)
1402 for (int j = sel_col_start; j <= sel_col_end; ++j)
1404 tabular.getCellNumber(i, j),
1409 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1411 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1412 bool lineSet = !tabular.bottomLine(actcell, flag);
1413 for (int i = sel_row_start; i <= sel_row_end; ++i)
1414 for (int j = sel_col_start; j <= sel_col_end; ++j)
1415 tabular.setBottomLine(
1416 tabular.getCellNumber(i, j),
1422 case LyXTabular::M_TOGGLE_LINE_LEFT:
1424 case LyXTabular::TOGGLE_LINE_LEFT: {
1425 bool lineSet = !tabular.leftLine(actcell, flag);
1426 for (int i = sel_row_start; i <= sel_row_end; ++i)
1427 for (int j = sel_col_start; j <= sel_col_end; ++j)
1428 tabular.setLeftLine(
1429 tabular.getCellNumber(i,j),
1435 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1437 case LyXTabular::TOGGLE_LINE_RIGHT: {
1438 bool lineSet = !tabular.rightLine(actcell, flag);
1439 for (int i = sel_row_start; i <= sel_row_end; ++i)
1440 for (int j = sel_col_start; j <= sel_col_end; ++j)
1441 tabular.setRightLine(
1442 tabular.getCellNumber(i,j),
1448 case LyXTabular::M_ALIGN_LEFT:
1449 case LyXTabular::M_ALIGN_RIGHT:
1450 case LyXTabular::M_ALIGN_CENTER:
1452 case LyXTabular::ALIGN_LEFT:
1453 case LyXTabular::ALIGN_RIGHT:
1454 case LyXTabular::ALIGN_CENTER:
1455 case LyXTabular::ALIGN_BLOCK:
1456 for (int i = sel_row_start; i <= sel_row_end; ++i)
1457 for (int j = sel_col_start; j <= sel_col_end; ++j)
1458 tabular.setAlignment(
1459 tabular.getCellNumber(i, j),
1464 case LyXTabular::M_VALIGN_TOP:
1465 case LyXTabular::M_VALIGN_BOTTOM:
1466 case LyXTabular::M_VALIGN_MIDDLE:
1468 case LyXTabular::VALIGN_TOP:
1469 case LyXTabular::VALIGN_BOTTOM:
1470 case LyXTabular::VALIGN_MIDDLE:
1471 for (int i = sel_row_start; i <= sel_row_end; ++i)
1472 for (int j = sel_col_start; j <= sel_col_end; ++j)
1473 tabular.setVAlignment(
1474 tabular.getCellNumber(i, j),
1478 case LyXTabular::MULTICOLUMN: {
1479 if (sel_row_start != sel_row_end) {
1480 #ifdef WITH_WARNINGS
1481 #warning Need I say it ? This is horrible.
1483 Alert::error(_("Error setting multicolumn"),
1484 _("You cannot set multicolumn vertically."));
1487 // just multicol for one Single Cell
1488 if (!hasSelection()) {
1489 // check wether we are completly in a multicol
1490 if (tabular.isMultiColumn(actcell))
1491 tabular.unsetMultiColumn(actcell);
1493 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1496 // we have a selection so this means we just add all this
1497 // cells to form a multicolumn cell
1501 if (sel_cell_start > sel_cell_end) {
1502 s_start = sel_cell_end;
1503 s_end = sel_cell_start;
1505 s_start = sel_cell_start;
1506 s_end = sel_cell_end;
1508 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1514 case LyXTabular::SET_ALL_LINES:
1516 case LyXTabular::UNSET_ALL_LINES:
1517 for (int i = sel_row_start; i <= sel_row_end; ++i)
1518 for (int j = sel_col_start; j <= sel_col_end; ++j)
1519 tabular.setAllLines(
1520 tabular.getCellNumber(i,j), setLines);
1523 case LyXTabular::SET_LONGTABULAR:
1524 tabular.setLongTabular(true);
1527 case LyXTabular::UNSET_LONGTABULAR:
1528 tabular.setLongTabular(false);
1531 case LyXTabular::SET_ROTATE_TABULAR:
1532 tabular.setRotateTabular(true);
1535 case LyXTabular::UNSET_ROTATE_TABULAR:
1536 tabular.setRotateTabular(false);
1539 case LyXTabular::SET_ROTATE_CELL:
1540 for (int i = sel_row_start; i <= sel_row_end; ++i)
1541 for (int j = sel_col_start; j <= sel_col_end; ++j)
1542 tabular.setRotateCell(
1543 tabular.getCellNumber(i, j), true);
1546 case LyXTabular::UNSET_ROTATE_CELL:
1547 for (int i = sel_row_start; i <= sel_row_end; ++i)
1548 for (int j = sel_col_start; j <= sel_col_end; ++j)
1549 tabular.setRotateCell(
1550 tabular.getCellNumber(i, j), false);
1553 case LyXTabular::SET_USEBOX: {
1554 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1555 if (val == tabular.getUsebox(actcell))
1556 val = LyXTabular::BOX_NONE;
1557 for (int i = sel_row_start; i <= sel_row_end; ++i)
1558 for (int j = sel_col_start; j <= sel_col_end; ++j)
1559 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1563 case LyXTabular::UNSET_LTFIRSTHEAD:
1565 case LyXTabular::SET_LTFIRSTHEAD:
1566 tabular.getRowOfLTFirstHead(row, ltt);
1567 checkLongtableSpecial(ltt, value, flag);
1568 tabular.setLTHead(row, flag, ltt, true);
1571 case LyXTabular::UNSET_LTHEAD:
1573 case LyXTabular::SET_LTHEAD:
1574 tabular.getRowOfLTHead(row, ltt);
1575 checkLongtableSpecial(ltt, value, flag);
1576 tabular.setLTHead(row, flag, ltt, false);
1579 case LyXTabular::UNSET_LTFOOT:
1581 case LyXTabular::SET_LTFOOT:
1582 tabular.getRowOfLTFoot(row, ltt);
1583 checkLongtableSpecial(ltt, value, flag);
1584 tabular.setLTFoot(row, flag, ltt, false);
1587 case LyXTabular::UNSET_LTLASTFOOT:
1589 case LyXTabular::SET_LTLASTFOOT:
1590 tabular.getRowOfLTLastFoot(row, ltt);
1591 checkLongtableSpecial(ltt, value, flag);
1592 tabular.setLTFoot(row, flag, ltt, true);
1595 case LyXTabular::SET_LTNEWPAGE:
1596 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1599 // dummy stuff just to avoid warnings
1600 case LyXTabular::LAST_ACTION:
1605 InsetTabularMailer(*this).updateDialog(bv);
1609 void InsetTabular::activateCellInset(BufferView * bv, int cell, int x, int y)
1611 tabular.getCellInset(cell).edit(bv, x, y);
1612 bv->cursor().cell(cell);
1617 void InsetTabular::activateCellInset(BufferView * bv, int cell, bool behind)
1619 tabular.getCellInset(cell).edit(bv, behind);
1620 bv->cursor().cell(cell);
1625 bool InsetTabular::showInsetDialog(BufferView * bv) const
1627 InsetTabularMailer(*this).showDialog(bv);
1632 void InsetTabular::openLayoutDialog(BufferView * bv) const
1634 InsetTabularMailer(*this).showDialog(bv);
1639 // function returns an object as defined in func_status.h:
1640 // states OK, Unknown, Disabled, On, Off.
1642 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1645 int action = LyXTabular::LAST_ACTION;
1648 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1649 string const tmp = tabularFeature[i].feature;
1650 if (tmp == what.substr(0, tmp.length())) {
1651 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1652 // tabularFeatures[i].feature.length()))
1653 action = tabularFeature[i].action;
1657 if (action == LyXTabular::LAST_ACTION) {
1659 status.unknown(true);
1663 string const argument
1664 = ltrim(what.substr(tabularFeature[i].feature.length()));
1669 LyXTabular::ltType dummyltt;
1673 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1675 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1678 case LyXTabular::SET_PWIDTH:
1679 case LyXTabular::SET_MPWIDTH:
1680 case LyXTabular::SET_SPECIAL_COLUMN:
1681 case LyXTabular::SET_SPECIAL_MULTI:
1682 case LyXTabular::APPEND_ROW:
1683 case LyXTabular::APPEND_COLUMN:
1684 case LyXTabular::DELETE_ROW:
1685 case LyXTabular::DELETE_COLUMN:
1686 case LyXTabular::SET_ALL_LINES:
1687 case LyXTabular::UNSET_ALL_LINES:
1691 case LyXTabular::MULTICOLUMN:
1692 status.setOnOff(tabular.isMultiColumn(actcell));
1695 case LyXTabular::M_TOGGLE_LINE_TOP:
1697 case LyXTabular::TOGGLE_LINE_TOP:
1698 status.setOnOff(tabular.topLine(actcell, flag));
1701 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1703 case LyXTabular::TOGGLE_LINE_BOTTOM:
1704 status.setOnOff(tabular.bottomLine(actcell, flag));
1707 case LyXTabular::M_TOGGLE_LINE_LEFT:
1709 case LyXTabular::TOGGLE_LINE_LEFT:
1710 status.setOnOff(tabular.leftLine(actcell, flag));
1713 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1715 case LyXTabular::TOGGLE_LINE_RIGHT:
1716 status.setOnOff(tabular.rightLine(actcell, flag));
1719 case LyXTabular::M_ALIGN_LEFT:
1721 case LyXTabular::ALIGN_LEFT:
1722 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1725 case LyXTabular::M_ALIGN_RIGHT:
1727 case LyXTabular::ALIGN_RIGHT:
1728 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1731 case LyXTabular::M_ALIGN_CENTER:
1733 case LyXTabular::ALIGN_CENTER:
1734 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1737 case LyXTabular::ALIGN_BLOCK:
1738 status.disabled(tabular.getPWidth(actcell).zero());
1739 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1742 case LyXTabular::M_VALIGN_TOP:
1744 case LyXTabular::VALIGN_TOP:
1746 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1749 case LyXTabular::M_VALIGN_BOTTOM:
1751 case LyXTabular::VALIGN_BOTTOM:
1753 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1756 case LyXTabular::M_VALIGN_MIDDLE:
1758 case LyXTabular::VALIGN_MIDDLE:
1760 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1763 case LyXTabular::SET_LONGTABULAR:
1764 status.setOnOff(tabular.isLongTabular());
1767 case LyXTabular::UNSET_LONGTABULAR:
1768 status.setOnOff(!tabular.isLongTabular());
1771 case LyXTabular::SET_ROTATE_TABULAR:
1772 status.setOnOff(tabular.getRotateTabular());
1775 case LyXTabular::UNSET_ROTATE_TABULAR:
1776 status.setOnOff(!tabular.getRotateTabular());
1779 case LyXTabular::SET_ROTATE_CELL:
1780 status.setOnOff(tabular.getRotateCell(actcell));
1783 case LyXTabular::UNSET_ROTATE_CELL:
1784 status.setOnOff(!tabular.getRotateCell(actcell));
1787 case LyXTabular::SET_USEBOX:
1788 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1791 case LyXTabular::SET_LTFIRSTHEAD:
1792 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1795 case LyXTabular::SET_LTHEAD:
1796 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1799 case LyXTabular::SET_LTFOOT:
1800 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1803 case LyXTabular::SET_LTLASTFOOT:
1804 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1807 case LyXTabular::SET_LTNEWPAGE:
1808 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1813 status.disabled(true);
1820 void InsetTabular::getLabelList(Buffer const & buffer,
1821 std::vector<string> & list) const
1823 tabular.getLabelList(buffer, list);
1827 bool InsetTabular::copySelection(BufferView * bv)
1829 if (!hasSelection())
1832 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1833 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1834 if (sel_col_start > sel_col_end) {
1835 sel_col_start = sel_col_end;
1836 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1838 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1841 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1842 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1843 if (sel_row_start > sel_row_end)
1844 swap(sel_row_start, sel_row_end);
1846 paste_tabular.reset(new LyXTabular(tabular));
1847 paste_tabular->setOwner(this);
1849 for (int i = 0; i < sel_row_start; ++i)
1850 paste_tabular->deleteRow(0);
1852 int const rows = sel_row_end - sel_row_start + 1;
1853 while (paste_tabular->rows() > rows)
1854 paste_tabular->deleteRow(rows);
1856 paste_tabular->setTopLine(0, true, true);
1857 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1860 for (int i = 0; i < sel_col_start; ++i)
1861 paste_tabular->deleteColumn(0);
1863 int const columns = sel_col_end - sel_col_start + 1;
1864 while (paste_tabular->columns() > columns)
1865 paste_tabular->deleteColumn(columns);
1867 paste_tabular->setLeftLine(0, true, true);
1868 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1872 OutputParams const runparams;
1873 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1874 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1875 bv->stuffClipboard(os.str());
1880 bool InsetTabular::pasteSelection(BufferView * bv)
1884 int actcell = bv->cursor().idx();
1885 int actcol = tabular.column_of_cell(actcell);
1886 int actrow = tabular.row_of_cell(actcell);
1887 for (int r1 = 0, r2 = actrow;
1888 r1 < paste_tabular->rows() && r2 < tabular.rows();
1890 for (int c1 = 0, c2 = actcol;
1891 c1 < paste_tabular->columns() && c2 < tabular.columns();
1893 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1894 tabular.isPartOfMultiColumn(r2, c2))
1896 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1900 if (tabular.isPartOfMultiColumn(r2, c2)) {
1904 InsetText & inset = tabular.getCellInset(r2, c2);
1905 inset = paste_tabular->getCellInset(r1, c1);
1906 inset.setOwner(this);
1914 bool InsetTabular::cutSelection(BufferParams const & bp)
1916 if (!hasSelection())
1919 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1920 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1921 if (sel_col_start > sel_col_end) {
1922 sel_col_start = sel_col_end;
1923 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1925 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1928 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1929 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1931 if (sel_row_start > sel_row_end)
1932 swap(sel_row_start, sel_row_end);
1934 if (sel_cell_start > sel_cell_end)
1935 swap(sel_cell_start, sel_cell_end);
1937 for (int i = sel_row_start; i <= sel_row_end; ++i)
1938 for (int j = sel_col_start; j <= sel_col_end; ++j)
1939 tabular.getCellInset(tabular.getCellNumber(i, j))
1940 .clear(bp.tracking_changes);
1945 bool InsetTabular::isRightToLeft(BufferView * bv)
1947 return bv->getParentLanguage(this)->RightToLeft();
1951 void InsetTabular::getSelection(int actcell,
1952 int & srow, int & erow, int & scol, int & ecol) const
1954 int const start = hasSelection() ? sel_cell_start : actcell;
1955 int const end = hasSelection() ? sel_cell_end : actcell;
1957 srow = tabular.row_of_cell(start);
1958 erow = tabular.row_of_cell(end);
1962 scol = tabular.column_of_cell(start);
1963 ecol = tabular.column_of_cell(end);
1967 ecol = tabular.right_column_of_cell(end);
1971 int InsetTabular::numParagraphs() const
1973 return tabular.getNumberOfCells();
1977 LyXText * InsetTabular::getText(int i) const
1979 return i < tabular.getNumberOfCells()
1980 ? tabular.getCellInset(i).getText(0)
1985 void InsetTabular::markErased()
1987 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1988 tabular.getCellInset(cell).markErased();
1992 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1994 const int cell = tabular.getCellFromInset(in);
1997 return tabular.getPWidth(cell).zero();
1999 // this is a workaround for a crash (New, Insert->Tabular,
2000 // Insert->FootNote)
2004 // well we didn't obviously find it so maybe our owner knows more
2005 BOOST_ASSERT(owner());
2006 return owner()->forceDefaultParagraphs(in);
2010 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2013 if (buf.length() <= 0)
2019 string::size_type len = buf.length();
2020 string::size_type p = 0;
2022 int actcell = bv->cursor().idx();
2023 int actcol = tabular.column_of_cell(actcell);
2024 int actrow = tabular.row_of_cell(actcell);
2026 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2034 maxCols = max(cols, maxCols);
2040 maxCols = max(cols, maxCols);
2041 LyXTabular * loctab;
2046 paste_tabular.reset(
2047 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2050 paste_tabular->setOwner(this);
2051 loctab = paste_tabular.get();
2060 string::size_type op = 0;
2061 int cells = loctab->getNumberOfCells();
2064 rows = loctab->rows();
2065 int const columns = loctab->columns();
2067 while (cell < cells && p < len && row < rows &&
2068 (p = buf.find_first_of("\t\n", p)) != string::npos)
2074 // we can only set this if we are not too far right
2075 if (cols < columns) {
2076 InsetText & inset = loctab->getCellInset(cell);
2077 LyXFont const font = inset.text_.
2078 getFont(inset.paragraphs().begin(), 0);
2079 inset.setText(buf.substr(op, p - op), font);
2085 // we can only set this if we are not too far right
2086 if (cols < columns) {
2087 InsetText & inset = tabular.getCellInset(cell);
2088 LyXFont const font = inset.text_.
2089 getFont(inset.paragraphs().begin(), 0);
2090 inset.setText(buf.substr(op, p - op), font);
2095 cell = loctab->getCellNumber(row, cols);
2101 // check for the last cell if there is no trailing '\n'
2102 if (cell < cells && op < len) {
2103 InsetText & inset = loctab->getCellInset(cell);
2104 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2105 inset.setText(buf.substr(op, len - op), font);
2111 void InsetTabular::addPreview(PreviewLoader & loader) const
2113 int const rows = tabular.rows();
2114 int const columns = tabular.columns();
2115 for (int i = 0; i < rows; ++i) {
2116 for (int j = 0; j < columns; ++j)
2117 tabular.getCellInset(i, j).addPreview(loader);
2123 void InsetTabular::clearSelection() const
2127 has_selection = false;
2131 void InsetTabular::setSelection(int start, int end) const
2133 sel_cell_start = start;
2135 has_selection = true;
2139 string const InsetTabularMailer::name_("tabular");
2141 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2142 : inset_(const_cast<InsetTabular &>(inset))
2146 string const InsetTabularMailer::inset2string(Buffer const &) const
2148 return params2string(inset_);
2152 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2154 istringstream data(in);
2156 lex.setStream(data);
2158 #warning CHECK verify that this is a sane value to return.
2164 string const token = lex.getString();
2172 string const token = lex.getString();
2173 if (token != "\\active_cell")
2176 cell = lex.getInteger();
2179 // This is part of the inset proper that is usually swallowed
2180 // by Buffer::readInset
2183 string const token = lex.getString();
2184 if (token != "Tabular")
2191 Buffer const & buffer = inset.buffer();
2192 inset.read(buffer, lex);
2194 // We can't set the active cell, but we can tell the frontend
2200 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2204 //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2205 data << name_ << " \\active_cell " << 0 << '\n';
2206 inset.write(inset.buffer(), data);
2207 data << "\\end_inset\n";