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)
159 tabular.setOwner(this);
167 InsetTabular::InsetTabular(InsetTabular const & tab)
168 : UpdatableInset(tab), tabular(tab.tabular),
169 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
171 tabular.setOwner(this);
179 InsetTabular::~InsetTabular()
181 InsetTabularMailer(*this).hideDialog();
185 auto_ptr<InsetBase> InsetTabular::clone() const
187 return auto_ptr<InsetBase>(new InsetTabular(*this));
191 Buffer const & InsetTabular::buffer() const
197 void InsetTabular::buffer(Buffer * b)
203 void InsetTabular::write(Buffer const & buf, ostream & os) const
205 os << "Tabular" << endl;
206 tabular.write(buf, os);
210 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
212 bool const old_format = (lex.getString() == "\\LyXTable");
214 tabular.read(buf, lex);
220 string token = lex.getString();
221 while (lex.isOK() && (token != "\\end_inset")) {
223 token = lex.getString();
225 if (token != "\\end_inset") {
226 lex.printError("Missing \\end_inset at this point. "
232 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
234 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
235 // mi.base.textwidth << "\n";
237 lyxerr << "InsetTabular::metrics: need bv" << endl;
241 calculate_dimensions_of_cells(mi);
243 dim.asc = tabular.getAscentOfRow(0);
244 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
245 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
250 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
252 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
254 BufferView * bv = pi.base.bv;
261 x += ADD_TO_TABULAR_WIDTH;
264 first_visible_cell = -1;
265 for (int i = 0; i < tabular.rows(); ++i) {
267 cell = tabular.getCellNumber(i, 0);
268 if (y + tabular.getDescentOfRow(i) <= 0 &&
269 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
271 y += tabular.getDescentOfRow(i) +
272 tabular.getAscentOfRow(i + 1) +
273 tabular.getAdditionalHeight(i + 1);
276 for (int j = 0; j < tabular.columns(); ++j) {
277 if (nx > bv->workWidth())
279 if (tabular.isPartOfMultiColumn(i, j))
281 if (first_visible_cell < 0)
282 first_visible_cell = cell;
284 drawCellSelection(pi.pain, nx, y, i, j, cell);
286 int const cx = nx + tabular.getBeginningOfTextInCell(cell);
287 tabular.getCellInset(cell).draw(pi, cx, y);
288 drawCellLines(pi.pain, nx, y, i, cell);
289 nx += tabular.getWidthOfColumn(cell);
293 // Would be nice, but for some completely unfathomable reason,
294 // on a col resize to a new fixed width, even though the insettexts
295 // are resized, the cell isn't, but drawing all cells in a tall table
296 // has the desired effect somehow. Complete dark magic.
298 // avoiding drawing the rest of a long table is
299 // a pretty big speedup
300 if (y > bv->workHeight())
304 y += tabular.getDescentOfRow(i) +
305 tabular.getAscentOfRow(i + 1) +
306 tabular.getAdditionalHeight(i + 1);
311 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
312 int row, int cell) const
314 int x2 = x + tabular.getWidthOfColumn(cell);
317 if (!tabular.topAlreadyDrawn(cell)) {
318 on_off = !tabular.topLine(cell);
319 pain.line(x, y - tabular.getAscentOfRow(row),
320 x2, y - tabular.getAscentOfRow(row),
321 on_off ? LColor::tabularonoffline : LColor::tabularline,
322 on_off ? Painter::line_onoffdash : Painter::line_solid);
324 on_off = !tabular.bottomLine(cell);
325 pain.line(x, y + tabular.getDescentOfRow(row),
326 x2, y + tabular.getDescentOfRow(row),
327 on_off ? LColor::tabularonoffline : LColor::tabularline,
328 on_off ? Painter::line_onoffdash : Painter::line_solid);
329 if (!tabular.leftAlreadyDrawn(cell)) {
330 on_off = !tabular.leftLine(cell);
331 pain.line(x, y - tabular.getAscentOfRow(row),
332 x, y + tabular.getDescentOfRow(row),
333 on_off ? LColor::tabularonoffline : LColor::tabularline,
334 on_off ? Painter::line_onoffdash : Painter::line_solid);
336 on_off = !tabular.rightLine(cell);
337 pain.line(x2 - tabular.getAdditionalWidth(cell),
338 y - tabular.getAscentOfRow(row),
339 x2 - tabular.getAdditionalWidth(cell),
340 y + tabular.getDescentOfRow(row),
341 on_off ? LColor::tabularonoffline : LColor::tabularline,
342 on_off ? Painter::line_onoffdash : Painter::line_solid);
346 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
347 int row, int column, int cell) const
352 BOOST_ASSERT(hasSelection());
353 int cs = tabular.column_of_cell(sel_cell_start);
354 int ce = tabular.column_of_cell(sel_cell_end);
357 cs = tabular.column_of_cell(sel_cell_end);
359 ce = tabular.right_column_of_cell(sel_cell_end);
362 int rs = tabular.row_of_cell(sel_cell_start);
363 int re = tabular.row_of_cell(sel_cell_end);
367 if (column >= cs && column <= ce && row >= rs && row <= re) {
368 int w = tabular.getWidthOfColumn(cell);
369 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
370 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
371 w, h, LColor::selection);
376 string const InsetTabular::editMessage() const
378 return _("Opened table");
382 void InsetTabular::updateLocal(BufferView * bv) const
388 extern LCursor theTempCursor;
392 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
394 if (hasSelection() && cmd.button() == mouse_button::button3)
400 BufferView * bv = cmd.view();
402 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
403 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
405 bv->cursor() = theTempCursor;
406 bv->cursor().data_.push_back(CursorSlice(this));
407 bv->cursor().data_.back().idx_ = cell;
409 setPos(bv, cmd.x, cmd.y);
411 bv->cursor() = theTempCursor;
412 bv->cursor().data_.back().idx_ = cell;
415 lyxerr << bv->cursor() << endl;
417 if (cmd.button() == mouse_button::button2)
418 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
422 void InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
424 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
425 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << cell << endl;
426 if (cmd.button() == mouse_button::button3)
427 InsetTabularMailer(*this).showDialog(cmd.view());
431 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
433 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
434 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << cell << endl;
435 BufferView * bv = cmd.view();
436 int const old_cell = actcell;
438 setPos(bv, cmd.x, cmd.y);
440 setSelection(actcell, actcell);
441 else if (old_cell != actcell)
442 setSelection(sel_cell_start, actcell);
446 void InsetTabular::edit(BufferView * bv, bool left)
448 lyxerr << "InsetTabular::edit: " << this << endl;
451 if (isRightToLeft(bv))
452 actcell = tabular.getLastCellInRow(0);
456 if (isRightToLeft(bv))
457 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
459 actcell = tabular.getNumberOfCells() - 1;
464 bv->cursor().push(this);
465 bv->cursor().data_.back().idx_ = actcell;
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->cursor().push(this);
482 activateCellInset(bv, actcell, x - xx, y - cursory_);
487 InsetTabular::priv_dispatch(FuncRequest const & cmd,
488 idx_type & idx, pos_type & pos)
490 //lyxerr << "# InsetTabular::dispatch: " << cmd << endl;
492 DispatchResult result(true, true);
493 BufferView * bv = cmd.view();
495 switch (cmd.action) {
497 case LFUN_MOUSE_PRESS:
499 return DispatchResult(true, true);
501 case LFUN_MOUSE_MOTION:
502 lfunMouseMotion(cmd);
503 return DispatchResult(true, true);
505 case LFUN_MOUSE_RELEASE:
506 lfunMouseRelease(cmd);
507 return DispatchResult(true, true);
513 int cell = bv->cursor().data_.back().idx_;
516 if (cell != actcell) {
517 lyxerr << "## ERROR ## InsetTabular::priv_dispatch: actcell: "
518 << actcell << " and cell " << cell << " should be the same "
522 result = tabular.getCellInset(cell).dispatch(cmd, idx, pos);
524 switch (result.val()) {
526 if (movePrevCell(bv))
527 result = DispatchResult(true, true);
529 result = DispatchResult(false, FINISHED);
533 if (moveNextCell(bv))
534 result = DispatchResult(true, true);
536 result = DispatchResult(false, FINISHED_RIGHT);
540 result = moveUpLock(bv);
544 result = moveDownLock(bv);
548 lyxerr << "# don't handle dispatch in act: " << actcell << endl;
555 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
556 switch (cmd.action) {
558 case LFUN_CELL_BACKWARD:
559 case LFUN_CELL_FORWARD:
560 if (cmd.action == LFUN_CELL_FORWARD)
567 case LFUN_SCROLL_INSET:
568 if (!cmd.argument.empty()) {
569 if (cmd.argument.find('.') != cmd.argument.npos)
570 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
572 scroll(cmd.view(), strToInt(cmd.argument));
573 cmd.view()->update();
574 return DispatchResult(true, true);
577 case LFUN_RIGHTSEL: {
578 int const start = hasSelection() ? sel_cell_start : actcell;
579 if (tabular.isLastCellInRow(actcell)) {
580 setSelection(start, actcell);
585 // if we are starting a selection, only select
586 // the current cell at the beginning
587 if (hasSelection()) {
591 setSelection(start, end);
596 result = moveRightLock(bv);
601 int const start = hasSelection() ? sel_cell_start : actcell;
602 if (tabular.isFirstCellInRow(actcell)) {
603 setSelection(start, actcell);
608 // if we are starting a selection, only select
609 // the current cell at the beginning
610 if (hasSelection()) {
614 setSelection(start, end);
619 result = moveLeftLock(bv);
624 int const start = hasSelection() ? sel_cell_start : actcell;
625 int const ocell = actcell;
626 // if we are starting a selection, only select
627 // the current cell at the beginning
628 if (hasSelection()) {
630 if (ocell == sel_cell_end ||
631 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
632 setSelection(start, tabular.getCellBelow(sel_cell_end));
634 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
636 setSelection(start, start);
642 result = moveDown(bv);
647 int const start = hasSelection() ? sel_cell_start : actcell;
648 int const ocell = actcell;
649 // if we are starting a selection, only select
650 // the current cell at the beginning
651 if (hasSelection()) {
653 if (ocell == sel_cell_end ||
654 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
655 setSelection(start, tabular.getCellAbove(sel_cell_end));
657 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
659 setSelection(start, start);
673 if (bv->top_y() + bv->painter().paperHeight()
674 < yo_ + tabular.getHeightOfTabular())
676 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
677 actcell = tabular.getCellBelow(first_visible_cell) + column;
679 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
690 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
694 actcell = tabular.getCellBelow(first_visible_cell) + column;
702 // none of these make sense for insettabular,
703 // but we must catch them to prevent any
704 // selection from being confused
708 case LFUN_WORDLEFTSEL:
710 case LFUN_WORDRIGHTSEL:
712 case LFUN_DOWN_PARAGRAPH:
713 case LFUN_DOWN_PARAGRAPHSEL:
714 case LFUN_UP_PARAGRAPH:
715 case LFUN_UP_PARAGRAPHSEL:
721 case LFUN_BEGINNINGBUF:
722 case LFUN_BEGINNINGBUFSEL:
727 case LFUN_LAYOUT_TABULAR:
728 InsetTabularMailer(*this).showDialog(bv);
731 case LFUN_INSET_DIALOG_UPDATE:
732 InsetTabularMailer(*this).updateDialog(bv);
735 case LFUN_TABULAR_FEATURE:
736 if (!tabularFeatures(bv, cmd.argument))
737 result = DispatchResult(false);
740 // insert file functions
741 case LFUN_FILE_INSERT_ASCII_PARA:
742 case LFUN_FILE_INSERT_ASCII: {
743 string tmpstr = getContentsOfAsciiFile(bv, cmd.argument, false);
744 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
745 result = DispatchResult(false);
759 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
763 if (copySelection(bv)) {
764 recordUndo(bv, Undo::DELETE);
765 cutSelection(bv->buffer()->params());
770 recordUndo(bv, Undo::DELETE);
771 cutSelection(bv->buffer()->params());
781 case LFUN_PASTESELECTION: {
782 string const clip = bv->getClipboard();
785 if (clip.find('\t') != string::npos) {
789 string::size_type len = clip.length();
790 string::size_type p = 0;
793 (p = clip.find_first_of("\t\n", p)) != string::npos) {
801 maxCols = max(cols, maxCols);
807 maxCols = max(cols, maxCols);
810 new LyXTabular(bv->buffer()->params(), rows, maxCols));
812 string::size_type op = 0;
814 int cells = paste_tabular->getNumberOfCells();
818 while (cell < cells && p < len &&
819 (p = clip.find_first_of("\t\n", p)) != string::npos) {
824 paste_tabular->getCellInset(cell).
825 setText(clip.substr(op, p-op), font);
830 paste_tabular->getCellInset(cell).
831 setText(clip.substr(op, p-op), font);
832 while (cols++ < maxCols)
840 // check for the last cell if there is no trailing '\n'
841 if (cell < cells && op < len)
842 paste_tabular->getCellInset(cell).
843 setText(clip.substr(op, len-op), font);
844 } else if (!insertAsciiString(bv, clip, true))
846 // so that the clipboard is used and it goes on
848 // and executes LFUN_PASTESELECTION in insettext!
849 paste_tabular.reset();
855 if (hasPasteBuffer()) {
856 recordUndo(bv, Undo::INSERT);
862 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
865 // handle font changing stuff on selection before we lock the inset
866 // in the default part!
867 result = DispatchResult(false);
868 // we try to activate the actual inset and put this event down to
869 // the insets dispatch function.
874 InsetTabularMailer(*this).updateDialog(bv);
879 int InsetTabular::latex(Buffer const & buf, ostream & os,
880 OutputParams const & runparams) const
882 return tabular.latex(buf, os, runparams);
886 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
887 OutputParams const & runparams) const
889 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
890 return tabular.plaintext(buf, os, runparams, dp, false, 0);
894 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
895 OutputParams const & runparams) const
897 return tabular.linuxdoc(buf,os, runparams);
901 int InsetTabular::docbook(Buffer const & buf, ostream & os,
902 OutputParams const & runparams) const
907 // if the table is inside a float it doesn't need the informaltable
908 // wrapper. Search for it.
909 for (master = owner();
910 master && master->lyxCode() != InsetOld::FLOAT_CODE;
911 master = master->owner())
915 os << "<informaltable>";
916 if (runparams.mixed_content)
920 ret += tabular.docbook(buf, os, runparams);
922 os << "</informaltable>";
923 if (runparams.mixed_content)
931 void InsetTabular::validate(LaTeXFeatures & features) const
933 tabular.validate(features);
937 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
940 for (int i = 0; i < tabular.rows(); ++i) {
943 for (int j = 0; j < tabular.columns(); ++j) {
944 if (tabular.isPartOfMultiColumn(i, j))
950 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
951 tabular.getCellInset(cell).metrics(m, dim);
952 maxAsc = max(maxAsc, dim.asc);
953 maxDesc = max(maxDesc, dim.des);
954 tabular.setWidthOfCell(cell, dim.wid);
956 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
957 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
962 void InsetTabular::getCursorPos(int & x, int & y) const
965 x = TEXT_TO_INSET_OFFSET + cursorx_ - xo_;
966 y = TEXT_TO_INSET_OFFSET + cursory_;
968 InsetText const & inset = tabular.getCellInset(actcell);
969 inset.getCursorPos(x, y);
970 x += inset.x() - xo_;
971 y += inset.y() - yo_;
976 void InsetTabular::setPos(BufferView * bv, int x, int y) const
982 int ly = tabular.getDescentOfRow(actrow);
984 // first search the right row
985 while (ly < y && actrow + 1 < tabular.rows()) {
986 cursory_ += tabular.getDescentOfRow(actrow) +
987 tabular.getAscentOfRow(actrow + 1) +
988 tabular.getAdditionalHeight(actrow + 1);
990 ly = cursory_ + tabular.getDescentOfRow(actrow);
992 actcell = tabular.getCellNumber(actrow, actcol);
994 // now search the right column
995 int lx = tabular.getWidthOfColumn(actcell) -
996 tabular.getAdditionalWidth(actcell);
998 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
999 lx += tabular.getWidthOfColumn(actcell + 1)
1000 + tabular.getAdditionalWidth(actcell);
1002 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1007 int InsetTabular::getCellXPos(int cell) const
1011 for (; !tabular.isFirstCellInRow(c); --c)
1013 int lx = tabular.getWidthOfColumn(cell);
1014 for (; c < cell; ++c)
1015 lx += tabular.getWidthOfColumn(c);
1017 return lx - tabular.getWidthOfColumn(cell) + xo_;
1021 void InsetTabular::resetPos(BufferView * bv) const
1023 #ifdef WITH_WARNINGS
1024 #warning This should be fixed in the right manner (20011128 Jug)
1026 // fast hack to fix infinite repaintings!
1027 if (in_reset_pos > 0)
1031 actcol = tabular.column_of_cell(actcell);
1034 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1035 if (tabular.isLastCellInRow(cell)) {
1036 cursory_ += tabular.getDescentOfRow(actrow) +
1037 tabular.getAscentOfRow(actrow + 1) +
1038 tabular.getAdditionalHeight(actrow + 1);
1043 // we need this only from here on!!!
1045 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1046 int new_x = getCellXPos(actcell) + offset;
1047 int old_x = cursorx_;
1049 // cursor.x(getCellXPos(actcell) + offset);
1050 if (actcol < tabular.columns() - 1 && scroll(false) &&
1051 tabular.getWidthOfTabular() < bv->workWidth()-20)
1055 } else if (cursorx_ - offset > 20 &&
1056 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1057 > bv->workWidth() - 20) {
1058 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1060 } else if (cursorx_ - offset < 20) {
1061 scroll(bv, 20 - cursorx_ + offset);
1063 } else if (scroll() && xo_ > 20 &&
1064 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1065 scroll(bv, old_x - cursorx_);
1068 InsetTabularMailer(*this).updateDialog(bv);
1073 DispatchResult InsetTabular::moveRight(BufferView * bv)
1075 bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1077 return DispatchResult(false, FINISHED_RIGHT);
1079 return DispatchResult(true);
1083 DispatchResult InsetTabular::moveRightLock(BufferView * bv)
1085 bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1087 return DispatchResult(false, FINISHED_RIGHT);
1088 activateCellInset(bv, actcell, false);
1089 return DispatchResult(true, true);
1093 DispatchResult InsetTabular::moveLeft(BufferView * bv)
1095 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1097 return DispatchResult(false, FINISHED);
1099 return DispatchResult(true);
1103 DispatchResult InsetTabular::moveLeftLock(BufferView * bv)
1105 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1107 return DispatchResult(false, FINISHED);
1108 activateCellInset(bv, actcell, true);
1109 return DispatchResult(true, true);
1113 DispatchResult InsetTabular::moveUp(BufferView * bv)
1115 int const ocell = actcell;
1116 actcell = tabular.getCellAbove(actcell);
1117 if (actcell == ocell) // we moved out of the inset
1118 return DispatchResult(false, FINISHED_UP);
1120 return DispatchResult(true, true);
1124 DispatchResult InsetTabular::moveUpLock(BufferView * bv)
1126 int const ocell = actcell;
1127 actcell = tabular.getCellAbove(actcell);
1128 if (actcell == ocell) // we moved out of the inset
1129 return DispatchResult(false, FINISHED_UP);
1131 activateCellInset(bv, actcell, bv->x_target(), 0);
1132 return DispatchResult(true, true);
1136 DispatchResult InsetTabular::moveDown(BufferView * bv)
1138 int const ocell = actcell;
1139 actcell = tabular.getCellBelow(actcell);
1140 if (actcell == ocell) // we moved out of the inset
1141 return DispatchResult(false, FINISHED_DOWN);
1143 return DispatchResult(true, true);
1147 DispatchResult InsetTabular::moveDownLock(BufferView * bv)
1149 int const ocell = actcell;
1150 actcell = tabular.getCellBelow(actcell);
1151 if (actcell == ocell) // we moved out of the inset
1152 return DispatchResult(false, FINISHED_DOWN);
1154 activateCellInset(bv, actcell, bv->x_target());
1155 return DispatchResult(true, true);
1159 bool InsetTabular::moveNextCell(BufferView * bv)
1161 lyxerr << "InsetTabular::moveNextCell 1 actcell: " << actcell << endl;
1162 if (isRightToLeft(bv)) {
1163 if (tabular.isFirstCellInRow(actcell)) {
1164 int row = tabular.row_of_cell(actcell);
1165 if (row == tabular.rows() - 1)
1167 actcell = tabular.getLastCellInRow(row);
1168 actcell = tabular.getCellBelow(actcell);
1175 if (tabular.isLastCell(actcell))
1179 lyxerr << "InsetTabular::moveNextCell 2 actcell: " << actcell << endl;
1185 bool InsetTabular::movePrevCell(BufferView * bv)
1187 if (isRightToLeft(bv)) {
1188 if (tabular.isLastCellInRow(actcell)) {
1189 int row = tabular.row_of_cell(actcell);
1192 actcell = tabular.getFirstCellInRow(row);
1193 actcell = tabular.getCellAbove(actcell);
1195 if (tabular.isLastCell(actcell))
1200 if (actcell == 0) // first cell
1209 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1211 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1214 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1215 string const tmp = tabularFeature[i].feature;
1217 if (tmp == what.substr(0, tmp.length())) {
1218 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1219 //tabularFeatures[i].feature.length())) {
1220 action = tabularFeature[i].action;
1224 if (action == LyXTabular::LAST_ACTION)
1228 ltrim(what.substr(tabularFeature[i].feature.length()));
1229 tabularFeatures(bv, action, val);
1236 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1237 string const & special, bool & flag)
1239 if (special == "dl_above") {
1242 } else if (special == "dl_below") {
1243 ltt.bottomDL = flag;
1245 } else if (special == "empty") {
1257 void InsetTabular::tabularFeatures(BufferView * bv,
1258 LyXTabular::Feature feature, string const & value)
1264 bool setLines = false;
1265 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1266 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1270 case LyXTabular::M_ALIGN_LEFT:
1271 case LyXTabular::ALIGN_LEFT:
1272 setAlign = LYX_ALIGN_LEFT;
1275 case LyXTabular::M_ALIGN_RIGHT:
1276 case LyXTabular::ALIGN_RIGHT:
1277 setAlign = LYX_ALIGN_RIGHT;
1280 case LyXTabular::M_ALIGN_CENTER:
1281 case LyXTabular::ALIGN_CENTER:
1282 setAlign = LYX_ALIGN_CENTER;
1285 case LyXTabular::ALIGN_BLOCK:
1286 setAlign = LYX_ALIGN_BLOCK;
1289 case LyXTabular::M_VALIGN_TOP:
1290 case LyXTabular::VALIGN_TOP:
1291 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1294 case LyXTabular::M_VALIGN_BOTTOM:
1295 case LyXTabular::VALIGN_BOTTOM:
1296 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1299 case LyXTabular::M_VALIGN_MIDDLE:
1300 case LyXTabular::VALIGN_MIDDLE:
1301 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1308 if (hasSelection()) {
1309 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1311 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1312 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1314 recordUndo(bv, Undo::ATOMIC);
1316 int row = tabular.row_of_cell(actcell);
1317 int column = tabular.column_of_cell(actcell);
1319 LyXTabular::ltType ltt;
1323 case LyXTabular::SET_PWIDTH: {
1324 LyXLength const len(value);
1325 tabular.setColumnPWidth(actcell, len);
1327 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1328 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1329 else if (!len.zero()
1330 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1331 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1335 case LyXTabular::SET_MPWIDTH:
1336 tabular.setMColumnPWidth(actcell, LyXLength(value));
1339 case LyXTabular::SET_SPECIAL_COLUMN:
1340 case LyXTabular::SET_SPECIAL_MULTI:
1341 tabular.setAlignSpecial(actcell,value,feature);
1344 case LyXTabular::APPEND_ROW:
1345 // append the row into the tabular
1346 tabular.appendRow(bv->buffer()->params(), actcell);
1347 tabular.setOwner(this);
1350 case LyXTabular::APPEND_COLUMN:
1351 // append the column into the tabular
1352 tabular.appendColumn(bv->buffer()->params(), actcell);
1353 tabular.setOwner(this);
1354 actcell = tabular.getCellNumber(row, column);
1357 case LyXTabular::DELETE_ROW:
1358 for (int i = sel_row_start; i <= sel_row_end; ++i)
1359 tabular.deleteRow(sel_row_start);
1360 if (sel_row_start >= tabular.rows())
1362 actcell = tabular.getCellNumber(sel_row_start, column);
1366 case LyXTabular::DELETE_COLUMN:
1367 for (int i = sel_col_start; i <= sel_col_end; ++i)
1368 tabular.deleteColumn(sel_col_start);
1369 if (sel_col_start >= tabular.columns())
1371 actcell = tabular.getCellNumber(row, sel_col_start);
1375 case LyXTabular::M_TOGGLE_LINE_TOP:
1377 case LyXTabular::TOGGLE_LINE_TOP: {
1378 bool lineSet = !tabular.topLine(actcell, flag);
1379 for (int i = sel_row_start; i <= sel_row_end; ++i)
1380 for (int j = sel_col_start; j <= sel_col_end; ++j)
1382 tabular.getCellNumber(i, j),
1387 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1389 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1390 bool lineSet = !tabular.bottomLine(actcell, flag);
1391 for (int i = sel_row_start; i <= sel_row_end; ++i)
1392 for (int j = sel_col_start; j <= sel_col_end; ++j)
1393 tabular.setBottomLine(
1394 tabular.getCellNumber(i, j),
1400 case LyXTabular::M_TOGGLE_LINE_LEFT:
1402 case LyXTabular::TOGGLE_LINE_LEFT: {
1403 bool lineSet = !tabular.leftLine(actcell, flag);
1404 for (int i = sel_row_start; i <= sel_row_end; ++i)
1405 for (int j = sel_col_start; j <= sel_col_end; ++j)
1406 tabular.setLeftLine(
1407 tabular.getCellNumber(i,j),
1413 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1415 case LyXTabular::TOGGLE_LINE_RIGHT: {
1416 bool lineSet = !tabular.rightLine(actcell, flag);
1417 for (int i = sel_row_start; i <= sel_row_end; ++i)
1418 for (int j = sel_col_start; j <= sel_col_end; ++j)
1419 tabular.setRightLine(
1420 tabular.getCellNumber(i,j),
1426 case LyXTabular::M_ALIGN_LEFT:
1427 case LyXTabular::M_ALIGN_RIGHT:
1428 case LyXTabular::M_ALIGN_CENTER:
1430 case LyXTabular::ALIGN_LEFT:
1431 case LyXTabular::ALIGN_RIGHT:
1432 case LyXTabular::ALIGN_CENTER:
1433 case LyXTabular::ALIGN_BLOCK:
1434 for (int i = sel_row_start; i <= sel_row_end; ++i)
1435 for (int j = sel_col_start; j <= sel_col_end; ++j)
1436 tabular.setAlignment(
1437 tabular.getCellNumber(i, j),
1442 case LyXTabular::M_VALIGN_TOP:
1443 case LyXTabular::M_VALIGN_BOTTOM:
1444 case LyXTabular::M_VALIGN_MIDDLE:
1446 case LyXTabular::VALIGN_TOP:
1447 case LyXTabular::VALIGN_BOTTOM:
1448 case LyXTabular::VALIGN_MIDDLE:
1449 for (int i = sel_row_start; i <= sel_row_end; ++i)
1450 for (int j = sel_col_start; j <= sel_col_end; ++j)
1451 tabular.setVAlignment(
1452 tabular.getCellNumber(i, j),
1456 case LyXTabular::MULTICOLUMN: {
1457 if (sel_row_start != sel_row_end) {
1458 #ifdef WITH_WARNINGS
1459 #warning Need I say it ? This is horrible.
1461 Alert::error(_("Error setting multicolumn"),
1462 _("You cannot set multicolumn vertically."));
1465 // just multicol for one Single Cell
1466 if (!hasSelection()) {
1467 // check wether we are completly in a multicol
1468 if (tabular.isMultiColumn(actcell))
1469 tabular.unsetMultiColumn(actcell);
1471 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1474 // we have a selection so this means we just add all this
1475 // cells to form a multicolumn cell
1479 if (sel_cell_start > sel_cell_end) {
1480 s_start = sel_cell_end;
1481 s_end = sel_cell_start;
1483 s_start = sel_cell_start;
1484 s_end = sel_cell_end;
1486 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1492 case LyXTabular::SET_ALL_LINES:
1494 case LyXTabular::UNSET_ALL_LINES:
1495 for (int i = sel_row_start; i <= sel_row_end; ++i)
1496 for (int j = sel_col_start; j <= sel_col_end; ++j)
1497 tabular.setAllLines(
1498 tabular.getCellNumber(i,j), setLines);
1501 case LyXTabular::SET_LONGTABULAR:
1502 tabular.setLongTabular(true);
1505 case LyXTabular::UNSET_LONGTABULAR:
1506 tabular.setLongTabular(false);
1509 case LyXTabular::SET_ROTATE_TABULAR:
1510 tabular.setRotateTabular(true);
1513 case LyXTabular::UNSET_ROTATE_TABULAR:
1514 tabular.setRotateTabular(false);
1517 case LyXTabular::SET_ROTATE_CELL:
1518 for (int i = sel_row_start; i <= sel_row_end; ++i)
1519 for (int j = sel_col_start; j <= sel_col_end; ++j)
1520 tabular.setRotateCell(
1521 tabular.getCellNumber(i, j), true);
1524 case LyXTabular::UNSET_ROTATE_CELL:
1525 for (int i = sel_row_start; i <= sel_row_end; ++i)
1526 for (int j = sel_col_start; j <= sel_col_end; ++j)
1527 tabular.setRotateCell(
1528 tabular.getCellNumber(i, j), false);
1531 case LyXTabular::SET_USEBOX: {
1532 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1533 if (val == tabular.getUsebox(actcell))
1534 val = LyXTabular::BOX_NONE;
1535 for (int i = sel_row_start; i <= sel_row_end; ++i)
1536 for (int j = sel_col_start; j <= sel_col_end; ++j)
1537 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1541 case LyXTabular::UNSET_LTFIRSTHEAD:
1543 case LyXTabular::SET_LTFIRSTHEAD:
1544 tabular.getRowOfLTFirstHead(row, ltt);
1545 checkLongtableSpecial(ltt, value, flag);
1546 tabular.setLTHead(row, flag, ltt, true);
1549 case LyXTabular::UNSET_LTHEAD:
1551 case LyXTabular::SET_LTHEAD:
1552 tabular.getRowOfLTHead(row, ltt);
1553 checkLongtableSpecial(ltt, value, flag);
1554 tabular.setLTHead(row, flag, ltt, false);
1557 case LyXTabular::UNSET_LTFOOT:
1559 case LyXTabular::SET_LTFOOT:
1560 tabular.getRowOfLTFoot(row, ltt);
1561 checkLongtableSpecial(ltt, value, flag);
1562 tabular.setLTFoot(row, flag, ltt, false);
1565 case LyXTabular::UNSET_LTLASTFOOT:
1567 case LyXTabular::SET_LTLASTFOOT:
1568 tabular.getRowOfLTLastFoot(row, ltt);
1569 checkLongtableSpecial(ltt, value, flag);
1570 tabular.setLTFoot(row, flag, ltt, true);
1573 case LyXTabular::SET_LTNEWPAGE:
1574 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1577 // dummy stuff just to avoid warnings
1578 case LyXTabular::LAST_ACTION:
1583 InsetTabularMailer(*this).updateDialog(bv);
1587 void InsetTabular::activateCellInset(BufferView * bv, int cell, int x, int y)
1590 tabular.getCellInset(actcell).edit(bv, x, y);
1591 bv->cursor().data_.back().idx_ = actcell;
1596 void InsetTabular::activateCellInset(BufferView * bv, int cell, bool behind)
1599 tabular.getCellInset(actcell).edit(bv, behind);
1600 bv->cursor().data_.back().idx_ = actcell;
1606 bool InsetTabular::showInsetDialog(BufferView * bv) const
1608 InsetTabularMailer(*this).showDialog(bv);
1613 void InsetTabular::openLayoutDialog(BufferView * bv) const
1615 InsetTabularMailer(*this).showDialog(bv);
1620 // function returns an object as defined in func_status.h:
1621 // states OK, Unknown, Disabled, On, Off.
1623 FuncStatus InsetTabular::getStatus(string const & what) const
1625 int action = LyXTabular::LAST_ACTION;
1629 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1630 string const tmp = tabularFeature[i].feature;
1631 if (tmp == what.substr(0, tmp.length())) {
1632 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1633 // tabularFeatures[i].feature.length())) {
1634 action = tabularFeature[i].action;
1638 if (action == LyXTabular::LAST_ACTION) {
1640 status.unknown(true);
1644 string const argument
1645 = ltrim(what.substr(tabularFeature[i].feature.length()));
1650 LyXTabular::ltType dummyltt;
1654 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1656 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1659 case LyXTabular::SET_PWIDTH:
1660 case LyXTabular::SET_MPWIDTH:
1661 case LyXTabular::SET_SPECIAL_COLUMN:
1662 case LyXTabular::SET_SPECIAL_MULTI:
1663 case LyXTabular::APPEND_ROW:
1664 case LyXTabular::APPEND_COLUMN:
1665 case LyXTabular::DELETE_ROW:
1666 case LyXTabular::DELETE_COLUMN:
1667 case LyXTabular::SET_ALL_LINES:
1668 case LyXTabular::UNSET_ALL_LINES:
1672 case LyXTabular::MULTICOLUMN:
1673 status.setOnOff(tabular.isMultiColumn(actcell));
1676 case LyXTabular::M_TOGGLE_LINE_TOP:
1678 case LyXTabular::TOGGLE_LINE_TOP:
1679 status.setOnOff(tabular.topLine(actcell, flag));
1682 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1684 case LyXTabular::TOGGLE_LINE_BOTTOM:
1685 status.setOnOff(tabular.bottomLine(actcell, flag));
1688 case LyXTabular::M_TOGGLE_LINE_LEFT:
1690 case LyXTabular::TOGGLE_LINE_LEFT:
1691 status.setOnOff(tabular.leftLine(actcell, flag));
1694 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1696 case LyXTabular::TOGGLE_LINE_RIGHT:
1697 status.setOnOff(tabular.rightLine(actcell, flag));
1700 case LyXTabular::M_ALIGN_LEFT:
1702 case LyXTabular::ALIGN_LEFT:
1703 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1706 case LyXTabular::M_ALIGN_RIGHT:
1708 case LyXTabular::ALIGN_RIGHT:
1709 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1712 case LyXTabular::M_ALIGN_CENTER:
1714 case LyXTabular::ALIGN_CENTER:
1715 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1718 case LyXTabular::ALIGN_BLOCK:
1719 status.disabled(tabular.getPWidth(actcell).zero());
1720 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1723 case LyXTabular::M_VALIGN_TOP:
1725 case LyXTabular::VALIGN_TOP:
1727 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1730 case LyXTabular::M_VALIGN_BOTTOM:
1732 case LyXTabular::VALIGN_BOTTOM:
1734 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1737 case LyXTabular::M_VALIGN_MIDDLE:
1739 case LyXTabular::VALIGN_MIDDLE:
1741 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1744 case LyXTabular::SET_LONGTABULAR:
1745 status.setOnOff(tabular.isLongTabular());
1748 case LyXTabular::UNSET_LONGTABULAR:
1749 status.setOnOff(!tabular.isLongTabular());
1752 case LyXTabular::SET_ROTATE_TABULAR:
1753 status.setOnOff(tabular.getRotateTabular());
1756 case LyXTabular::UNSET_ROTATE_TABULAR:
1757 status.setOnOff(!tabular.getRotateTabular());
1760 case LyXTabular::SET_ROTATE_CELL:
1761 status.setOnOff(tabular.getRotateCell(actcell));
1764 case LyXTabular::UNSET_ROTATE_CELL:
1765 status.setOnOff(!tabular.getRotateCell(actcell));
1768 case LyXTabular::SET_USEBOX:
1769 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1772 case LyXTabular::SET_LTFIRSTHEAD:
1773 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1776 case LyXTabular::SET_LTHEAD:
1777 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1780 case LyXTabular::SET_LTFOOT:
1781 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1784 case LyXTabular::SET_LTLASTFOOT:
1785 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1788 case LyXTabular::SET_LTNEWPAGE:
1789 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1794 status.disabled(true);
1801 void InsetTabular::getLabelList(Buffer const & buffer,
1802 std::vector<string> & list) const
1804 tabular.getLabelList(buffer, list);
1808 bool InsetTabular::copySelection(BufferView * bv)
1810 if (!hasSelection())
1813 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1814 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1815 if (sel_col_start > sel_col_end) {
1816 sel_col_start = sel_col_end;
1817 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1819 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1822 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1823 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1824 if (sel_row_start > sel_row_end)
1825 swap(sel_row_start, sel_row_end);
1827 paste_tabular.reset(new LyXTabular(tabular));
1828 paste_tabular->setOwner(this);
1830 for (int i = 0; i < sel_row_start; ++i)
1831 paste_tabular->deleteRow(0);
1833 int const rows = sel_row_end - sel_row_start + 1;
1834 while (paste_tabular->rows() > rows)
1835 paste_tabular->deleteRow(rows);
1837 paste_tabular->setTopLine(0, true, true);
1838 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1841 for (int i = 0; i < sel_col_start; ++i)
1842 paste_tabular->deleteColumn(0);
1844 int const columns = sel_col_end - sel_col_start + 1;
1845 while (paste_tabular->columns() > columns)
1846 paste_tabular->deleteColumn(columns);
1848 paste_tabular->setLeftLine(0, true, true);
1849 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1853 OutputParams const runparams;
1854 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1855 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1856 bv->stuffClipboard(os.str());
1861 bool InsetTabular::pasteSelection(BufferView *)
1866 for (int r1 = 0, r2 = actrow;
1867 r1 < paste_tabular->rows() && r2 < tabular.rows();
1869 for (int c1 = 0, c2 = actcol;
1870 c1 < paste_tabular->columns() && c2 < tabular.columns();
1872 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1873 tabular.isPartOfMultiColumn(r2, c2))
1875 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1879 if (tabular.isPartOfMultiColumn(r2, c2)) {
1883 InsetText & inset = tabular.getCellInset(r2, c2);
1884 inset = paste_tabular->getCellInset(r1, c1);
1885 inset.setOwner(this);
1893 bool InsetTabular::cutSelection(BufferParams const & bp)
1895 if (!hasSelection())
1898 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1899 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1900 if (sel_col_start > sel_col_end) {
1901 sel_col_start = sel_col_end;
1902 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1904 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1907 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1908 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1910 if (sel_row_start > sel_row_end)
1911 swap(sel_row_start, sel_row_end);
1913 if (sel_cell_start > sel_cell_end)
1914 swap(sel_cell_start, sel_cell_end);
1916 for (int i = sel_row_start; i <= sel_row_end; ++i)
1917 for (int j = sel_col_start; j <= sel_col_end; ++j)
1918 tabular.getCellInset(tabular.getCellNumber(i, j))
1919 .clear(bp.tracking_changes);
1924 bool InsetTabular::isRightToLeft(BufferView * bv)
1926 return bv->getParentLanguage(this)->RightToLeft();
1930 void InsetTabular::getSelection(int & srow, int & erow,
1931 int & scol, int & ecol) const
1933 int const start = hasSelection() ? sel_cell_start : actcell;
1934 int const end = hasSelection() ? sel_cell_end : actcell;
1936 srow = tabular.row_of_cell(start);
1937 erow = tabular.row_of_cell(end);
1941 scol = tabular.column_of_cell(start);
1942 ecol = tabular.column_of_cell(end);
1946 ecol = tabular.right_column_of_cell(end);
1950 int InsetTabular::numParagraphs() const
1952 return tabular.getNumberOfCells();
1956 LyXText * InsetTabular::getText(int i) const
1958 return i < tabular.getNumberOfCells()
1959 ? tabular.getCellInset(i).getText(0)
1964 void InsetTabular::markErased()
1966 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1967 tabular.getCellInset(cell).markErased();
1971 bool InsetTabular::insetAllowed(InsetOld::Code) const
1977 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1979 const int cell = tabular.getCellFromInset(in);
1982 return tabular.getPWidth(cell).zero();
1984 // this is a workaround for a crash (New, Insert->Tabular,
1985 // Insert->FootNote)
1989 // well we didn't obviously find it so maybe our owner knows more
1990 BOOST_ASSERT(owner());
1991 return owner()->forceDefaultParagraphs(in);
1995 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
1998 if (buf.length() <= 0)
2004 string::size_type len = buf.length();
2005 string::size_type p = 0;
2007 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2015 maxCols = max(cols, maxCols);
2021 maxCols = max(cols, maxCols);
2022 LyXTabular * loctab;
2027 paste_tabular.reset(
2028 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2031 paste_tabular->setOwner(this);
2032 loctab = paste_tabular.get();
2041 string::size_type op = 0;
2042 int cells = loctab->getNumberOfCells();
2045 rows = loctab->rows();
2046 int const columns = loctab->columns();
2048 while (cell < cells && p < len && row < rows &&
2049 (p = buf.find_first_of("\t\n", p)) != string::npos)
2055 // we can only set this if we are not too far right
2056 if (cols < columns) {
2057 InsetText & inset = loctab->getCellInset(cell);
2058 LyXFont const font = inset.text_.
2059 getFont(inset.paragraphs().begin(), 0);
2060 inset.setText(buf.substr(op, p - op), font);
2066 // we can only set this if we are not too far right
2067 if (cols < columns) {
2068 InsetText & inset = tabular.getCellInset(cell);
2069 LyXFont const font = inset.text_.
2070 getFont(inset.paragraphs().begin(), 0);
2071 inset.setText(buf.substr(op, p - op), font);
2076 cell = loctab->getCellNumber(row, cols);
2082 // check for the last cell if there is no trailing '\n'
2083 if (cell < cells && op < len) {
2084 InsetText & inset = loctab->getCellInset(cell);
2085 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2086 inset.setText(buf.substr(op, len - op), font);
2093 void InsetTabular::addPreview(PreviewLoader & loader) const
2095 int const rows = tabular.rows();
2096 int const columns = tabular.columns();
2097 for (int i = 0; i < rows; ++i) {
2098 for (int j = 0; j < columns; ++j)
2099 tabular.getCellInset(i, j).addPreview(loader);
2104 string const InsetTabularMailer::name_("tabular");
2106 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2107 : inset_(const_cast<InsetTabular &>(inset))
2111 string const InsetTabularMailer::inset2string(Buffer const &) const
2113 return params2string(inset_);
2117 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2119 istringstream data(in);
2121 lex.setStream(data);
2123 #warning CHECK verify that this is a sane value to return.
2129 string const token = lex.getString();
2137 string const token = lex.getString();
2138 if (token != "\\active_cell")
2141 cell = lex.getInteger();
2144 // This is part of the inset proper that is usually swallowed
2145 // by Buffer::readInset
2148 string const token = lex.getString();
2149 if (token != "Tabular")
2156 Buffer const & buffer = inset.buffer();
2157 inset.read(buffer, lex);
2159 // We can't set the active cell, but we can tell the frontend
2165 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2168 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2169 inset.write(inset.buffer(), data);
2170 data << "\\end_inset\n";