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(BufferView & bv, FuncRequest const & cmd)
390 if (hasSelection() && cmd.button() == mouse_button::button3)
393 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
396 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
399 bv.fullCursor(theTempCursor);
400 bv.fullCursor().push(this);
401 bv.fullCursor().idx() = cell;
404 setPos(bv, cmd.x, cmd.y);
405 bv.fullCursor(theTempCursor);
406 bv.fullCursor().idx() = cell;
408 lyxerr << bv.cursor() << endl;
410 if (cmd.button() == mouse_button::button2)
411 dispatch(bv, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
415 void InsetTabular::lfunMouseMotion(BufferView & bv, FuncRequest const & cmd)
417 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
418 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
420 setPos(bv, cmd.x, cmd.y);
421 if (!hasSelection()) {
422 setSelection(actcell, actcell);
425 bv.cursor().idx() = actcell;
426 setSelection(sel_cell_start, actcell);
427 tablemode = (sel_cell_start != actcell);
432 void InsetTabular::lfunMouseRelease(BufferView & bv, FuncRequest const & cmd)
434 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
435 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
436 if (cmd.button() == mouse_button::button3)
437 InsetTabularMailer(*this).showDialog(&bv);
441 void InsetTabular::edit(BufferView * view, bool left)
443 BufferView & bv = *view;
444 lyxerr << "InsetTabular::edit: " << this << endl;
449 if (isRightToLeft(bv))
450 cell = tabular.getLastCellInRow(0);
454 if (isRightToLeft(bv))
455 cell = tabular.getFirstCellInRow(tabular.rows()-1);
457 cell = tabular.getNumberOfCells() - 1;
462 bv.fullCursor().push(this);
463 bv.fullCursor().idx() = cell;
464 lyxerr << bv.cursor() << endl;
468 void InsetTabular::edit(BufferView * bv, int x, int y)
470 lyxerr << "InsetTabular::edit: " << this << " first cell "
471 << &tabular.cell_info[0][0].inset << endl;
477 //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
478 bv->fullCursor().push(this);
480 // activateCellInset(bv, cell, x - xx, y - cursory_);
485 InsetTabular::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
487 lyxerr << "# InsetTabular::dispatch: " << cmd
488 << " tablemode: " << tablemode << endl;
490 DispatchResult result(true, true);
491 switch (cmd.action) {
493 case LFUN_MOUSE_PRESS:
494 lfunMousePress(bv, cmd);
495 return DispatchResult(true, true);
497 case LFUN_MOUSE_MOTION:
498 lfunMouseMotion(bv, cmd);
499 return DispatchResult(true, true);
501 case LFUN_MOUSE_RELEASE:
502 lfunMouseRelease(bv, cmd);
503 return DispatchResult(true, true);
509 CursorSlice & cur = bv.cursor();
514 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
515 result = tabular.getCellInset(cell).dispatch(bv, cmd);
517 switch (result.val()) {
519 if (movePrevCell(bv, cur))
520 result = DispatchResult(true, true);
522 result = DispatchResult(false, FINISHED);
526 if (moveNextCell(bv, cur))
527 result = DispatchResult(true, true);
529 result = DispatchResult(false, FINISHED_RIGHT);
533 if (moveUpLock(bv, cur))
534 result = DispatchResult(true, true);
536 result = DispatchResult(false, FINISHED_UP);
540 if (moveDownLock(bv, cur))
541 result = DispatchResult(true, true);
543 result = DispatchResult(false, FINISHED_UP);
547 lyxerr << "# don't handle dispatch" << endl;
551 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
554 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
555 switch (cmd.action) {
557 case LFUN_CELL_BACKWARD:
558 case LFUN_CELL_FORWARD:
559 if (cmd.action == LFUN_CELL_FORWARD)
560 moveNextCell(bv, cur);
562 movePrevCell(bv, cur);
566 case LFUN_SCROLL_INSET:
567 if (!cmd.argument.empty()) {
568 if (cmd.argument.find('.') != cmd.argument.npos)
569 scroll(bv, static_cast<float>(strToDbl(cmd.argument)));
571 scroll(bv, strToInt(cmd.argument));
573 return DispatchResult(true, true);
576 case LFUN_RIGHTSEL: {
577 int const start = hasSelection() ? sel_cell_start : cur.idx_;
578 if (tabular.isLastCellInRow(cur.idx_)) {
579 setSelection(start, cur.idx_);
584 // if we are starting a selection, only select
585 // the current cell at the beginning
586 if (hasSelection()) {
590 setSelection(start, end);
595 if (!moveRightLock(bv, cur))
596 result = DispatchResult(false, FINISHED_RIGHT);
601 int const start = hasSelection() ? sel_cell_start : cur.idx_;
602 if (tabular.isFirstCellInRow(cur.idx_)) {
603 setSelection(start, cur.idx_);
608 // if we are starting a selection, only select
609 // the current cell at the beginning
610 if (hasSelection()) {
614 setSelection(start, end);
619 if (!moveLeftLock(bv, cur))
620 result = DispatchResult(false, FINISHED);
625 int const start = hasSelection() ? sel_cell_start : cur.idx_;
626 int const ocell = cur.idx_;
627 // if we are starting a selection, only select
628 // the current cell at the beginning
629 if (hasSelection()) {
631 if (ocell == sel_cell_end ||
632 tabular.column_of_cell(ocell) > tabular.column_of_cell(cur.idx_))
633 setSelection(start, tabular.getCellBelow(sel_cell_end));
635 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
637 setSelection(start, start);
643 if (!moveDown(bv, cur))
644 result = DispatchResult(false, FINISHED_DOWN);
649 int const start = hasSelection() ? sel_cell_start : cur.idx_;
650 int const ocell = cur.idx_;
651 // if we are starting a selection, only select
652 // the current cell at the beginning
653 if (hasSelection()) {
655 if (ocell == sel_cell_end ||
656 tabular.column_of_cell(ocell) > tabular.column_of_cell(cur.idx_))
657 setSelection(start, tabular.getCellAbove(sel_cell_end));
659 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
661 setSelection(start, start);
667 if (!moveUp(bv, cur))
668 result = DispatchResult(false, FINISHED_DOWN);
675 int actcell = bv.cursor().idx();
676 int actcol = tabular.column_of_cell(actcell);
678 if (bv.top_y() + bv.painter().paperHeight()
679 < yo_ + tabular.getHeightOfTabular())
681 bv.scrollDocView(bv.top_y() + bv.painter().paperHeight());
682 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
684 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
693 int actcell = bv.cursor().idx();
694 int actcol = tabular.column_of_cell(actcell);
697 bv.scrollDocView(bv.top_y() - bv.painter().paperHeight());
701 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
709 // none of these make sense for insettabular,
710 // but we must catch them to prevent any
711 // selection from being confused
715 case LFUN_WORDLEFTSEL:
717 case LFUN_WORDRIGHTSEL:
719 case LFUN_DOWN_PARAGRAPH:
720 case LFUN_DOWN_PARAGRAPHSEL:
721 case LFUN_UP_PARAGRAPH:
722 case LFUN_UP_PARAGRAPHSEL:
728 case LFUN_BEGINNINGBUF:
729 case LFUN_BEGINNINGBUFSEL:
734 case LFUN_LAYOUT_TABULAR:
735 InsetTabularMailer(*this).showDialog(&bv);
738 case LFUN_INSET_DIALOG_UPDATE:
739 InsetTabularMailer(*this).updateDialog(&bv);
742 case LFUN_TABULAR_FEATURE:
743 if (!tabularFeatures(bv, cmd.argument))
744 result = DispatchResult(false);
747 // insert file functions
748 case LFUN_FILE_INSERT_ASCII_PARA:
749 case LFUN_FILE_INSERT_ASCII: {
750 string tmpstr = getContentsOfAsciiFile(&bv, cmd.argument, false);
751 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
752 result = DispatchResult(false);
766 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
770 if (copySelection(bv)) {
771 recordUndo(bv, Undo::DELETE);
772 cutSelection(bv.buffer()->params());
777 recordUndo(bv, Undo::DELETE);
778 cutSelection(bv.buffer()->params());
788 case LFUN_PASTESELECTION: {
789 string const clip = bv.getClipboard();
792 if (clip.find('\t') != string::npos) {
796 string::size_type len = clip.length();
797 string::size_type p = 0;
800 (p = clip.find_first_of("\t\n", p)) != string::npos) {
808 maxCols = max(cols, maxCols);
814 maxCols = max(cols, maxCols);
817 new LyXTabular(bv.buffer()->params(), rows, maxCols));
819 string::size_type op = 0;
821 int cells = paste_tabular->getNumberOfCells();
825 while (cell < cells && p < len &&
826 (p = clip.find_first_of("\t\n", p)) != string::npos) {
831 paste_tabular->getCellInset(cell).
832 setText(clip.substr(op, p-op), font);
837 paste_tabular->getCellInset(cell).
838 setText(clip.substr(op, p-op), font);
839 while (cols++ < maxCols)
847 // check for the last cell if there is no trailing '\n'
848 if (cell < cells && op < len)
849 paste_tabular->getCellInset(cell).
850 setText(clip.substr(op, len-op), font);
851 } else if (!insertAsciiString(bv, clip, true))
853 // so that the clipboard is used and it goes on
855 // and executes LFUN_PASTESELECTION in insettext!
856 paste_tabular.reset();
862 if (hasPasteBuffer()) {
863 recordUndo(bv, Undo::INSERT);
869 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
872 // handle font changing stuff on selection before we lock the inset
873 // in the default part!
874 result = DispatchResult(false);
875 // we try to activate the actual inset and put this event down to
876 // the insets dispatch function.
881 InsetTabularMailer(*this).updateDialog(&bv);
884 if (cmd.action == LFUN_INSET_TOGGLE) {
885 tablemode = !tablemode;
886 result = DispatchResult(true, true);
893 int InsetTabular::latex(Buffer const & buf, ostream & os,
894 OutputParams const & runparams) const
896 return tabular.latex(buf, os, runparams);
900 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
901 OutputParams const & runparams) const
903 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
904 return tabular.plaintext(buf, os, runparams, dp, false, 0);
908 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
909 OutputParams const & runparams) const
911 return tabular.linuxdoc(buf,os, runparams);
915 int InsetTabular::docbook(Buffer const & buf, ostream & os,
916 OutputParams const & runparams) const
921 // if the table is inside a float it doesn't need the informaltable
922 // wrapper. Search for it.
923 for (master = owner(); master; master = master->owner())
924 if (master->lyxCode() == InsetOld::FLOAT_CODE)
928 os << "<informaltable>";
929 if (runparams.mixed_content)
933 ret += tabular.docbook(buf, os, runparams);
935 os << "</informaltable>";
936 if (runparams.mixed_content)
944 void InsetTabular::validate(LaTeXFeatures & features) const
946 tabular.validate(features);
950 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
952 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
955 for (int j = 0; j < tabular.columns(); ++j) {
956 if (tabular.isPartOfMultiColumn(i, j))
962 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
963 tabular.getCellInset(cell).metrics(m, dim);
964 maxAsc = max(maxAsc, dim.asc);
965 maxDesc = max(maxDesc, dim.des);
966 tabular.setWidthOfCell(cell, dim.wid);
968 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
969 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
974 void InsetTabular::getCursorPos(int cell, int & x, int & y) const
976 InsetText const & inset = tabular.getCellInset(cell);
977 inset.getCursorPos(cell, x, y);
978 x += inset.x() - xo_;
979 y += inset.y() - yo_;
983 void InsetTabular::setPos(BufferView & bv, int x, int y) const
985 int const cell = getCell(x + xo_, y + yo_);
986 lyxerr << "# InsetTabular::setPos() cell: " << cell << endl;
987 InsetText const & inset = tabular.getCellInset(cell);
988 inset.text_.setCursorFromCoordinates(x, y);
994 int ly = tabular.getDescentOfRow(actrow);
996 // first search the right row
997 while (ly < y && actrow + 1 < tabular.rows()) {
998 cursory_ += tabular.getDescentOfRow(actrow) +
999 tabular.getAscentOfRow(actrow + 1) +
1000 tabular.getAdditionalHeight(actrow + 1);
1002 ly = cursory_ + tabular.getDescentOfRow(actrow);
1004 actcell = tabular.getCellNumber(actrow, actcol);
1006 // now search the right column
1007 int lx = tabular.getWidthOfColumn(actcell) -
1008 tabular.getAdditionalWidth(actcell);
1010 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1011 lx += tabular.getWidthOfColumn(actcell + 1)
1012 + tabular.getAdditionalWidth(actcell);
1014 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1020 int InsetTabular::getCellXPos(int cell) const
1024 for (; !tabular.isFirstCellInRow(c); --c)
1026 int lx = tabular.getWidthOfColumn(cell);
1027 for (; c < cell; ++c)
1028 lx += tabular.getWidthOfColumn(c);
1030 return lx - tabular.getWidthOfColumn(cell) + xo_;
1034 void InsetTabular::resetPos(BufferView &) const
1037 #ifdef WITH_WARNINGS
1038 #warning This should be fixed in the right manner (20011128 Jug)
1040 // fast hack to fix infinite repaintings!
1041 if (in_reset_pos > 0)
1045 int actcell = bv->cursor().cell();
1046 int actcol = tabular.column_of_cell(actcell);
1049 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1050 if (tabular.isLastCellInRow(cell)) {
1051 cursory_ += tabular.getDescentOfRow(actrow) +
1052 tabular.getAscentOfRow(actrow + 1) +
1053 tabular.getAdditionalHeight(actrow + 1);
1058 // we need this only from here on!!!
1060 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1061 int new_x = getCellXPos(actcell) + offset;
1062 int old_x = cursorx_;
1064 // cursor.x(getCellXPos(actcell) + offset);
1065 if (actcol < tabular.columns() - 1 && scroll(false) &&
1066 tabular.getWidthOfTabular() < bv->workWidth()-20)
1070 } else if (cursorx_ - offset > 20 &&
1071 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1072 > bv.workWidth() - 20) {
1073 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1075 } else if (cursorx_ - offset < 20) {
1076 scroll(&bv, 20 - cursorx_ + offset);
1078 } else if (scroll() && xo_ > 20 &&
1079 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1080 scroll(&bv, old_x - cursorx_);
1083 InsetTabularMailer(*this).updateDialog(bv);
1089 bool InsetTabular::moveRight(BufferView & bv, CursorSlice & cur)
1091 bool moved = isRightToLeft(bv)
1092 ? movePrevCell(bv, cur) : moveNextCell(bv, cur);
1100 bool InsetTabular::moveRightLock(BufferView & bv, CursorSlice & cur)
1102 bool moved = isRightToLeft(bv)
1103 ? movePrevCell(bv, cur) : moveNextCell(bv, cur);
1106 activateCellInset(bv, cur.idx_, false);
1111 bool InsetTabular::moveLeft(BufferView & bv, CursorSlice & cur)
1113 bool moved = isRightToLeft(bv)
1114 ? moveNextCell(bv, cur) : movePrevCell(bv, cur);
1122 bool InsetTabular::moveLeftLock(BufferView & bv, CursorSlice & cur)
1124 bool moved = isRightToLeft(bv)
1125 ? moveNextCell(bv, cur) : movePrevCell(bv, cur);
1128 activateCellInset(bv, cur.idx_, true);
1133 bool InsetTabular::moveUp(BufferView & bv, CursorSlice & cur)
1135 if (tabular.row_of_cell(cur.idx_) == 0)
1137 cur.idx_ = tabular.getCellAbove(cur.idx_);
1143 bool InsetTabular::moveUpLock(BufferView & bv, CursorSlice & cur)
1145 if (tabular.row_of_cell(cur.idx_) == 0)
1147 cur.idx_ = tabular.getCellAbove(cur.idx_);
1149 activateCellInset(bv, cur.idx_, bv.x_target(), 0);
1154 bool InsetTabular::moveDown(BufferView & bv, CursorSlice & cur)
1156 if (tabular.row_of_cell(cur.idx_) == tabular.rows() - 1)
1158 cur.idx_ = tabular.getCellBelow(cur.idx_);
1164 bool InsetTabular::moveDownLock(BufferView & bv, CursorSlice & cur)
1166 if (tabular.row_of_cell(cur.idx_) == tabular.rows() - 1)
1168 cur.idx_ = tabular.getCellBelow(cur.idx_);
1170 activateCellInset(bv, cur.idx_, bv.x_target());
1175 bool InsetTabular::moveNextCell(BufferView & bv, CursorSlice & cur)
1177 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1178 if (isRightToLeft(bv)) {
1179 if (tabular.isFirstCellInRow(cur.idx_)) {
1180 int row = tabular.row_of_cell(cur.idx_);
1181 if (row == tabular.rows() - 1)
1183 cur.idx_ = tabular.getLastCellInRow(row);
1184 cur.idx_ = tabular.getCellBelow(cur.idx_);
1191 if (tabular.isLastCell(cur.idx_))
1197 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1203 bool InsetTabular::movePrevCell(BufferView & bv, CursorSlice & cur)
1205 if (isRightToLeft(bv)) {
1206 if (tabular.isLastCellInRow(cur.idx_)) {
1207 int row = tabular.row_of_cell(cur.idx_);
1210 cur.idx_ = tabular.getFirstCellInRow(row);
1211 cur.idx_ = tabular.getCellAbove(cur.idx_);
1213 if (tabular.isLastCell(cur.idx_))
1218 if (cur.idx_ == 0) // first cell
1229 bool InsetTabular::tabularFeatures(BufferView & bv, string const & what)
1231 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1234 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1235 string const tmp = tabularFeature[i].feature;
1237 if (tmp == what.substr(0, tmp.length())) {
1238 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1239 //tabularFeatures[i].feature.length())) {
1240 action = tabularFeature[i].action;
1244 if (action == LyXTabular::LAST_ACTION)
1248 ltrim(what.substr(tabularFeature[i].feature.length()));
1249 tabularFeatures(bv, action, val);
1256 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1257 string const & special, bool & flag)
1259 if (special == "dl_above") {
1262 } else if (special == "dl_below") {
1263 ltt.bottomDL = flag;
1265 } else if (special == "empty") {
1277 void InsetTabular::tabularFeatures(BufferView & bv,
1278 LyXTabular::Feature feature, string const & value)
1280 int actcell = bv.cursor().idx();
1285 bool setLines = false;
1286 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1287 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1291 case LyXTabular::M_ALIGN_LEFT:
1292 case LyXTabular::ALIGN_LEFT:
1293 setAlign = LYX_ALIGN_LEFT;
1296 case LyXTabular::M_ALIGN_RIGHT:
1297 case LyXTabular::ALIGN_RIGHT:
1298 setAlign = LYX_ALIGN_RIGHT;
1301 case LyXTabular::M_ALIGN_CENTER:
1302 case LyXTabular::ALIGN_CENTER:
1303 setAlign = LYX_ALIGN_CENTER;
1306 case LyXTabular::ALIGN_BLOCK:
1307 setAlign = LYX_ALIGN_BLOCK;
1310 case LyXTabular::M_VALIGN_TOP:
1311 case LyXTabular::VALIGN_TOP:
1312 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1315 case LyXTabular::M_VALIGN_BOTTOM:
1316 case LyXTabular::VALIGN_BOTTOM:
1317 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1320 case LyXTabular::M_VALIGN_MIDDLE:
1321 case LyXTabular::VALIGN_MIDDLE:
1322 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1329 if (hasSelection()) {
1330 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1332 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1333 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1335 recordUndo(bv, Undo::ATOMIC);
1337 int row = tabular.row_of_cell(actcell);
1338 int column = tabular.column_of_cell(actcell);
1340 LyXTabular::ltType ltt;
1344 case LyXTabular::SET_PWIDTH: {
1345 LyXLength const len(value);
1346 tabular.setColumnPWidth(actcell, len);
1348 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1349 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1350 else if (!len.zero()
1351 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1352 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1356 case LyXTabular::SET_MPWIDTH:
1357 tabular.setMColumnPWidth(actcell, LyXLength(value));
1360 case LyXTabular::SET_SPECIAL_COLUMN:
1361 case LyXTabular::SET_SPECIAL_MULTI:
1362 tabular.setAlignSpecial(actcell,value,feature);
1365 case LyXTabular::APPEND_ROW:
1366 // append the row into the tabular
1367 tabular.appendRow(bv.buffer()->params(), actcell);
1368 tabular.setOwner(this);
1371 case LyXTabular::APPEND_COLUMN:
1372 // append the column into the tabular
1373 tabular.appendColumn(bv.buffer()->params(), actcell);
1374 tabular.setOwner(this);
1375 actcell = tabular.getCellNumber(row, column);
1378 case LyXTabular::DELETE_ROW:
1379 for (int i = sel_row_start; i <= sel_row_end; ++i)
1380 tabular.deleteRow(sel_row_start);
1381 if (sel_row_start >= tabular.rows())
1383 actcell = tabular.getCellNumber(sel_row_start, column);
1387 case LyXTabular::DELETE_COLUMN:
1388 for (int i = sel_col_start; i <= sel_col_end; ++i)
1389 tabular.deleteColumn(sel_col_start);
1390 if (sel_col_start >= tabular.columns())
1392 actcell = tabular.getCellNumber(row, sel_col_start);
1396 case LyXTabular::M_TOGGLE_LINE_TOP:
1398 case LyXTabular::TOGGLE_LINE_TOP: {
1399 bool lineSet = !tabular.topLine(actcell, flag);
1400 for (int i = sel_row_start; i <= sel_row_end; ++i)
1401 for (int j = sel_col_start; j <= sel_col_end; ++j)
1403 tabular.getCellNumber(i, j),
1408 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1410 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1411 bool lineSet = !tabular.bottomLine(actcell, flag);
1412 for (int i = sel_row_start; i <= sel_row_end; ++i)
1413 for (int j = sel_col_start; j <= sel_col_end; ++j)
1414 tabular.setBottomLine(
1415 tabular.getCellNumber(i, j),
1421 case LyXTabular::M_TOGGLE_LINE_LEFT:
1423 case LyXTabular::TOGGLE_LINE_LEFT: {
1424 bool lineSet = !tabular.leftLine(actcell, flag);
1425 for (int i = sel_row_start; i <= sel_row_end; ++i)
1426 for (int j = sel_col_start; j <= sel_col_end; ++j)
1427 tabular.setLeftLine(
1428 tabular.getCellNumber(i,j),
1434 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1436 case LyXTabular::TOGGLE_LINE_RIGHT: {
1437 bool lineSet = !tabular.rightLine(actcell, flag);
1438 for (int i = sel_row_start; i <= sel_row_end; ++i)
1439 for (int j = sel_col_start; j <= sel_col_end; ++j)
1440 tabular.setRightLine(
1441 tabular.getCellNumber(i,j),
1447 case LyXTabular::M_ALIGN_LEFT:
1448 case LyXTabular::M_ALIGN_RIGHT:
1449 case LyXTabular::M_ALIGN_CENTER:
1451 case LyXTabular::ALIGN_LEFT:
1452 case LyXTabular::ALIGN_RIGHT:
1453 case LyXTabular::ALIGN_CENTER:
1454 case LyXTabular::ALIGN_BLOCK:
1455 for (int i = sel_row_start; i <= sel_row_end; ++i)
1456 for (int j = sel_col_start; j <= sel_col_end; ++j)
1457 tabular.setAlignment(
1458 tabular.getCellNumber(i, j),
1463 case LyXTabular::M_VALIGN_TOP:
1464 case LyXTabular::M_VALIGN_BOTTOM:
1465 case LyXTabular::M_VALIGN_MIDDLE:
1467 case LyXTabular::VALIGN_TOP:
1468 case LyXTabular::VALIGN_BOTTOM:
1469 case LyXTabular::VALIGN_MIDDLE:
1470 for (int i = sel_row_start; i <= sel_row_end; ++i)
1471 for (int j = sel_col_start; j <= sel_col_end; ++j)
1472 tabular.setVAlignment(
1473 tabular.getCellNumber(i, j),
1477 case LyXTabular::MULTICOLUMN: {
1478 if (sel_row_start != sel_row_end) {
1479 #ifdef WITH_WARNINGS
1480 #warning Need I say it ? This is horrible.
1482 Alert::error(_("Error setting multicolumn"),
1483 _("You cannot set multicolumn vertically."));
1486 // just multicol for one Single Cell
1487 if (!hasSelection()) {
1488 // check wether we are completly in a multicol
1489 if (tabular.isMultiColumn(actcell))
1490 tabular.unsetMultiColumn(actcell);
1492 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1495 // we have a selection so this means we just add all this
1496 // cells to form a multicolumn cell
1500 if (sel_cell_start > sel_cell_end) {
1501 s_start = sel_cell_end;
1502 s_end = sel_cell_start;
1504 s_start = sel_cell_start;
1505 s_end = sel_cell_end;
1507 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1513 case LyXTabular::SET_ALL_LINES:
1515 case LyXTabular::UNSET_ALL_LINES:
1516 for (int i = sel_row_start; i <= sel_row_end; ++i)
1517 for (int j = sel_col_start; j <= sel_col_end; ++j)
1518 tabular.setAllLines(
1519 tabular.getCellNumber(i,j), setLines);
1522 case LyXTabular::SET_LONGTABULAR:
1523 tabular.setLongTabular(true);
1526 case LyXTabular::UNSET_LONGTABULAR:
1527 tabular.setLongTabular(false);
1530 case LyXTabular::SET_ROTATE_TABULAR:
1531 tabular.setRotateTabular(true);
1534 case LyXTabular::UNSET_ROTATE_TABULAR:
1535 tabular.setRotateTabular(false);
1538 case LyXTabular::SET_ROTATE_CELL:
1539 for (int i = sel_row_start; i <= sel_row_end; ++i)
1540 for (int j = sel_col_start; j <= sel_col_end; ++j)
1541 tabular.setRotateCell(
1542 tabular.getCellNumber(i, j), true);
1545 case LyXTabular::UNSET_ROTATE_CELL:
1546 for (int i = sel_row_start; i <= sel_row_end; ++i)
1547 for (int j = sel_col_start; j <= sel_col_end; ++j)
1548 tabular.setRotateCell(
1549 tabular.getCellNumber(i, j), false);
1552 case LyXTabular::SET_USEBOX: {
1553 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1554 if (val == tabular.getUsebox(actcell))
1555 val = LyXTabular::BOX_NONE;
1556 for (int i = sel_row_start; i <= sel_row_end; ++i)
1557 for (int j = sel_col_start; j <= sel_col_end; ++j)
1558 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1562 case LyXTabular::UNSET_LTFIRSTHEAD:
1564 case LyXTabular::SET_LTFIRSTHEAD:
1565 tabular.getRowOfLTFirstHead(row, ltt);
1566 checkLongtableSpecial(ltt, value, flag);
1567 tabular.setLTHead(row, flag, ltt, true);
1570 case LyXTabular::UNSET_LTHEAD:
1572 case LyXTabular::SET_LTHEAD:
1573 tabular.getRowOfLTHead(row, ltt);
1574 checkLongtableSpecial(ltt, value, flag);
1575 tabular.setLTHead(row, flag, ltt, false);
1578 case LyXTabular::UNSET_LTFOOT:
1580 case LyXTabular::SET_LTFOOT:
1581 tabular.getRowOfLTFoot(row, ltt);
1582 checkLongtableSpecial(ltt, value, flag);
1583 tabular.setLTFoot(row, flag, ltt, false);
1586 case LyXTabular::UNSET_LTLASTFOOT:
1588 case LyXTabular::SET_LTLASTFOOT:
1589 tabular.getRowOfLTLastFoot(row, ltt);
1590 checkLongtableSpecial(ltt, value, flag);
1591 tabular.setLTFoot(row, flag, ltt, true);
1594 case LyXTabular::SET_LTNEWPAGE:
1595 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1598 // dummy stuff just to avoid warnings
1599 case LyXTabular::LAST_ACTION:
1604 InsetTabularMailer(*this).updateDialog(&bv);
1608 void InsetTabular::activateCellInset(BufferView & bv, int cell, int x, int y)
1610 tabular.getCellInset(cell).edit(&bv, x, y);
1611 bv.cursor().idx(cell);
1616 void InsetTabular::activateCellInset(BufferView & bv, int cell, bool behind)
1618 tabular.getCellInset(cell).edit(&bv, behind);
1619 bv.cursor().idx(cell);
1624 bool InsetTabular::showInsetDialog(BufferView * bv) const
1626 InsetTabularMailer(*this).showDialog(bv);
1631 void InsetTabular::openLayoutDialog(BufferView * bv) const
1633 InsetTabularMailer(*this).showDialog(bv);
1638 // function returns an object as defined in func_status.h:
1639 // states OK, Unknown, Disabled, On, Off.
1641 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1644 int action = LyXTabular::LAST_ACTION;
1647 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1648 string const tmp = tabularFeature[i].feature;
1649 if (tmp == what.substr(0, tmp.length())) {
1650 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1651 // tabularFeatures[i].feature.length()))
1652 action = tabularFeature[i].action;
1656 if (action == LyXTabular::LAST_ACTION) {
1658 status.unknown(true);
1662 string const argument
1663 = ltrim(what.substr(tabularFeature[i].feature.length()));
1668 LyXTabular::ltType dummyltt;
1672 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1674 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1677 case LyXTabular::SET_PWIDTH:
1678 case LyXTabular::SET_MPWIDTH:
1679 case LyXTabular::SET_SPECIAL_COLUMN:
1680 case LyXTabular::SET_SPECIAL_MULTI:
1681 case LyXTabular::APPEND_ROW:
1682 case LyXTabular::APPEND_COLUMN:
1683 case LyXTabular::DELETE_ROW:
1684 case LyXTabular::DELETE_COLUMN:
1685 case LyXTabular::SET_ALL_LINES:
1686 case LyXTabular::UNSET_ALL_LINES:
1690 case LyXTabular::MULTICOLUMN:
1691 status.setOnOff(tabular.isMultiColumn(actcell));
1694 case LyXTabular::M_TOGGLE_LINE_TOP:
1696 case LyXTabular::TOGGLE_LINE_TOP:
1697 status.setOnOff(tabular.topLine(actcell, flag));
1700 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1702 case LyXTabular::TOGGLE_LINE_BOTTOM:
1703 status.setOnOff(tabular.bottomLine(actcell, flag));
1706 case LyXTabular::M_TOGGLE_LINE_LEFT:
1708 case LyXTabular::TOGGLE_LINE_LEFT:
1709 status.setOnOff(tabular.leftLine(actcell, flag));
1712 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1714 case LyXTabular::TOGGLE_LINE_RIGHT:
1715 status.setOnOff(tabular.rightLine(actcell, flag));
1718 case LyXTabular::M_ALIGN_LEFT:
1720 case LyXTabular::ALIGN_LEFT:
1721 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1724 case LyXTabular::M_ALIGN_RIGHT:
1726 case LyXTabular::ALIGN_RIGHT:
1727 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1730 case LyXTabular::M_ALIGN_CENTER:
1732 case LyXTabular::ALIGN_CENTER:
1733 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1736 case LyXTabular::ALIGN_BLOCK:
1737 status.disabled(tabular.getPWidth(actcell).zero());
1738 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1741 case LyXTabular::M_VALIGN_TOP:
1743 case LyXTabular::VALIGN_TOP:
1745 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1748 case LyXTabular::M_VALIGN_BOTTOM:
1750 case LyXTabular::VALIGN_BOTTOM:
1752 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1755 case LyXTabular::M_VALIGN_MIDDLE:
1757 case LyXTabular::VALIGN_MIDDLE:
1759 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1762 case LyXTabular::SET_LONGTABULAR:
1763 status.setOnOff(tabular.isLongTabular());
1766 case LyXTabular::UNSET_LONGTABULAR:
1767 status.setOnOff(!tabular.isLongTabular());
1770 case LyXTabular::SET_ROTATE_TABULAR:
1771 status.setOnOff(tabular.getRotateTabular());
1774 case LyXTabular::UNSET_ROTATE_TABULAR:
1775 status.setOnOff(!tabular.getRotateTabular());
1778 case LyXTabular::SET_ROTATE_CELL:
1779 status.setOnOff(tabular.getRotateCell(actcell));
1782 case LyXTabular::UNSET_ROTATE_CELL:
1783 status.setOnOff(!tabular.getRotateCell(actcell));
1786 case LyXTabular::SET_USEBOX:
1787 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1790 case LyXTabular::SET_LTFIRSTHEAD:
1791 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1794 case LyXTabular::SET_LTHEAD:
1795 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1798 case LyXTabular::SET_LTFOOT:
1799 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1802 case LyXTabular::SET_LTLASTFOOT:
1803 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1806 case LyXTabular::SET_LTNEWPAGE:
1807 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1812 status.disabled(true);
1819 void InsetTabular::getLabelList(Buffer const & buffer,
1820 std::vector<string> & list) const
1822 tabular.getLabelList(buffer, list);
1826 bool InsetTabular::copySelection(BufferView & bv)
1828 if (!hasSelection())
1831 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1832 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1833 if (sel_col_start > sel_col_end) {
1834 sel_col_start = sel_col_end;
1835 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1837 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1840 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1841 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1842 if (sel_row_start > sel_row_end)
1843 swap(sel_row_start, sel_row_end);
1845 paste_tabular.reset(new LyXTabular(tabular));
1846 paste_tabular->setOwner(this);
1848 for (int i = 0; i < sel_row_start; ++i)
1849 paste_tabular->deleteRow(0);
1851 int const rows = sel_row_end - sel_row_start + 1;
1852 while (paste_tabular->rows() > rows)
1853 paste_tabular->deleteRow(rows);
1855 paste_tabular->setTopLine(0, true, true);
1856 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1859 for (int i = 0; i < sel_col_start; ++i)
1860 paste_tabular->deleteColumn(0);
1862 int const columns = sel_col_end - sel_col_start + 1;
1863 while (paste_tabular->columns() > columns)
1864 paste_tabular->deleteColumn(columns);
1866 paste_tabular->setLeftLine(0, true, true);
1867 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1871 OutputParams const runparams;
1872 paste_tabular->plaintext(*bv.buffer(), os, runparams,
1873 ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1874 bv.stuffClipboard(os.str());
1879 bool InsetTabular::pasteSelection(BufferView & bv)
1883 int actcell = bv.cursor().idx();
1884 int actcol = tabular.column_of_cell(actcell);
1885 int actrow = tabular.row_of_cell(actcell);
1886 for (int r1 = 0, r2 = actrow;
1887 r1 < paste_tabular->rows() && r2 < tabular.rows();
1889 for (int c1 = 0, c2 = actcol;
1890 c1 < paste_tabular->columns() && c2 < tabular.columns();
1892 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1893 tabular.isPartOfMultiColumn(r2, c2))
1895 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1899 if (tabular.isPartOfMultiColumn(r2, c2)) {
1903 InsetText & inset = tabular.getCellInset(r2, c2);
1904 inset = paste_tabular->getCellInset(r1, c1);
1905 inset.setOwner(this);
1913 bool InsetTabular::cutSelection(BufferParams const & bp)
1915 if (!hasSelection())
1918 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1919 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1920 if (sel_col_start > sel_col_end) {
1921 sel_col_start = sel_col_end;
1922 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1924 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1927 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1928 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1930 if (sel_row_start > sel_row_end)
1931 swap(sel_row_start, sel_row_end);
1933 if (sel_cell_start > sel_cell_end)
1934 swap(sel_cell_start, sel_cell_end);
1936 for (int i = sel_row_start; i <= sel_row_end; ++i)
1937 for (int j = sel_col_start; j <= sel_col_end; ++j)
1938 tabular.getCellInset(tabular.getCellNumber(i, j))
1939 .clear(bp.tracking_changes);
1944 bool InsetTabular::isRightToLeft(BufferView & bv)
1946 return bv.getParentLanguage(this)->RightToLeft();
1950 void InsetTabular::getSelection(int actcell,
1951 int & srow, int & erow, int & scol, int & ecol) const
1953 int const start = hasSelection() ? sel_cell_start : actcell;
1954 int const end = hasSelection() ? sel_cell_end : actcell;
1956 srow = tabular.row_of_cell(start);
1957 erow = tabular.row_of_cell(end);
1961 scol = tabular.column_of_cell(start);
1962 ecol = tabular.column_of_cell(end);
1966 ecol = tabular.right_column_of_cell(end);
1970 int InsetTabular::numParagraphs() const
1972 return tabular.getNumberOfCells();
1976 LyXText * InsetTabular::getText(int i) const
1978 return i < tabular.getNumberOfCells()
1979 ? tabular.getCellInset(i).getText(0)
1984 void InsetTabular::markErased()
1986 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1987 tabular.getCellInset(cell).markErased();
1991 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1993 const int cell = tabular.getCellFromInset(in);
1996 return tabular.getPWidth(cell).zero();
1998 // this is a workaround for a crash (New, Insert->Tabular,
1999 // Insert->FootNote)
2003 // well we didn't obviously find it so maybe our owner knows more
2004 BOOST_ASSERT(owner());
2005 return owner()->forceDefaultParagraphs(in);
2009 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2012 if (buf.length() <= 0)
2018 string::size_type len = buf.length();
2019 string::size_type p = 0;
2021 int actcell = bv.cursor().idx();
2022 int actcol = tabular.column_of_cell(actcell);
2023 int actrow = tabular.row_of_cell(actcell);
2025 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2033 maxCols = max(cols, maxCols);
2039 maxCols = max(cols, maxCols);
2040 LyXTabular * loctab;
2045 paste_tabular.reset(
2046 new LyXTabular(bv.buffer()->params(), rows, maxCols));
2047 paste_tabular->setOwner(this);
2048 loctab = paste_tabular.get();
2057 string::size_type op = 0;
2058 int cells = loctab->getNumberOfCells();
2061 rows = loctab->rows();
2062 int const columns = loctab->columns();
2064 while (cell < cells && p < len && row < rows &&
2065 (p = buf.find_first_of("\t\n", p)) != string::npos)
2071 // we can only set this if we are not too far right
2072 if (cols < columns) {
2073 InsetText & inset = loctab->getCellInset(cell);
2074 LyXFont const font = inset.text_.
2075 getFont(inset.paragraphs().begin(), 0);
2076 inset.setText(buf.substr(op, p - op), font);
2082 // we can only set this if we are not too far right
2083 if (cols < columns) {
2084 InsetText & inset = tabular.getCellInset(cell);
2085 LyXFont const font = inset.text_.
2086 getFont(inset.paragraphs().begin(), 0);
2087 inset.setText(buf.substr(op, p - op), font);
2092 cell = loctab->getCellNumber(row, cols);
2098 // check for the last cell if there is no trailing '\n'
2099 if (cell < cells && op < len) {
2100 InsetText & inset = loctab->getCellInset(cell);
2101 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2102 inset.setText(buf.substr(op, len - op), font);
2108 void InsetTabular::addPreview(PreviewLoader & loader) const
2110 int const rows = tabular.rows();
2111 int const columns = tabular.columns();
2112 for (int i = 0; i < rows; ++i) {
2113 for (int j = 0; j < columns; ++j)
2114 tabular.getCellInset(i, j).addPreview(loader);
2120 void InsetTabular::clearSelection() const
2124 has_selection = false;
2128 void InsetTabular::setSelection(int start, int end) const
2130 sel_cell_start = start;
2132 has_selection = true;
2136 string const InsetTabularMailer::name_("tabular");
2138 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2139 : inset_(const_cast<InsetTabular &>(inset))
2143 string const InsetTabularMailer::inset2string(Buffer const &) const
2145 return params2string(inset_);
2149 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2151 istringstream data(in);
2153 lex.setStream(data);
2155 #warning CHECK verify that this is a sane value to return.
2161 string const token = lex.getString();
2169 string const token = lex.getString();
2170 if (token != "\\active_cell")
2173 cell = lex.getInteger();
2176 // This is part of the inset proper that is usually swallowed
2177 // by Buffer::readInset
2180 string const token = lex.getString();
2181 if (token != "Tabular")
2188 Buffer const & buffer = inset.buffer();
2189 inset.read(buffer, lex);
2191 // We can't set the active cell, but we can tell the frontend
2197 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2201 //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2202 data << name_ << " \\active_cell " << 0 << '\n';
2203 inset.write(inset.buffer(), data);
2204 data << "\\end_inset\n";