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"
43 using lyx::graphics::PreviewLoader;
45 using lyx::support::ltrim;
46 using lyx::support::strToInt;
47 using lyx::support::strToDbl;
54 using std::istringstream;
56 using std::ostringstream;
61 int const ADD_TO_HEIGHT = 2;
62 int const ADD_TO_TABULAR_WIDTH = 2;
65 boost::scoped_ptr<LyXTabular> paste_tabular;
68 struct TabularFeature {
69 LyXTabular::Feature action;
74 TabularFeature tabularFeature[] =
76 { LyXTabular::APPEND_ROW, "append-row" },
77 { LyXTabular::APPEND_COLUMN, "append-column" },
78 { LyXTabular::DELETE_ROW, "delete-row" },
79 { LyXTabular::DELETE_COLUMN, "delete-column" },
80 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
81 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
82 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
83 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
84 { LyXTabular::ALIGN_LEFT, "align-left" },
85 { LyXTabular::ALIGN_RIGHT, "align-right" },
86 { LyXTabular::ALIGN_CENTER, "align-center" },
87 { LyXTabular::ALIGN_BLOCK, "align-block" },
88 { LyXTabular::VALIGN_TOP, "valign-top" },
89 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
90 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
91 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
92 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
93 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
94 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
95 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
96 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
97 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
98 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
99 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
100 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
101 { LyXTabular::MULTICOLUMN, "multicolumn" },
102 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
103 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
104 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
105 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
106 { LyXTabular::SET_PWIDTH, "set-pwidth" },
107 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
108 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
109 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
110 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
111 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
112 { LyXTabular::SET_USEBOX, "set-usebox" },
113 { LyXTabular::SET_LTHEAD, "set-lthead" },
114 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
115 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
116 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
117 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
118 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
119 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
120 { LyXTabular::LAST_ACTION, "" }
124 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
125 bool operator()(TabularFeature & tf)
127 return tf.action == feature_;
130 LyXTabular::Feature feature_;
136 string const featureAsString(LyXTabular::Feature feature)
138 TabularFeature * it = tabularFeature;
139 TabularFeature * end = it +
140 sizeof(tabularFeature) / sizeof(TabularFeature);
141 it = std::find_if(it, end, FindFeature(feature));
142 return (it == end) ? string() : it->feature;
146 bool InsetTabular::hasPasteBuffer() const
148 return (paste_tabular.get() != 0);
152 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
153 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
154 buffer_(&buf), cursorx_(0), cursory_(0)
156 tabular.setOwner(this);
157 the_locking_inset = 0;
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166 : UpdatableInset(tab), tabular(tab.tabular),
167 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
169 tabular.setOwner(this);
170 the_locking_inset = 0;
178 InsetTabular::~InsetTabular()
180 InsetTabularMailer(*this).hideDialog();
184 auto_ptr<InsetBase> InsetTabular::clone() const
186 return auto_ptr<InsetBase>(new InsetTabular(*this));
190 Buffer const & InsetTabular::buffer() const
196 void InsetTabular::buffer(Buffer * b)
202 void InsetTabular::write(Buffer const & buf, ostream & os) const
204 os << "Tabular" << endl;
205 tabular.write(buf, os);
209 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
211 bool const old_format = (lex.getString() == "\\LyXTable");
213 tabular.read(buf, lex);
219 string token = lex.getString();
220 while (lex.isOK() && (token != "\\end_inset")) {
222 token = lex.getString();
224 if (token != "\\end_inset") {
225 lex.printError("Missing \\end_inset at this point. "
231 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
233 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
234 // mi.base.textwidth << "\n";
236 lyxerr << "InsetTabular::metrics: need bv" << endl;
240 calculate_dimensions_of_cells(mi);
242 dim.asc = tabular.getAscentOfRow(0);
243 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
244 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
249 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
251 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
253 BufferView * bv = pi.base.bv;
260 x += ADD_TO_TABULAR_WIDTH;
263 first_visible_cell = -1;
264 for (int i = 0; i < tabular.rows(); ++i) {
266 cell = tabular.getCellNumber(i, 0);
267 if (y + tabular.getDescentOfRow(i) <= 0 &&
268 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
270 y += tabular.getDescentOfRow(i) +
271 tabular.getAscentOfRow(i + 1) +
272 tabular.getAdditionalHeight(i + 1);
275 for (int j = 0; j < tabular.columns(); ++j) {
276 if (nx > bv->workWidth())
278 if (tabular.isPartOfMultiColumn(i, j))
280 int cx = nx + tabular.getBeginningOfTextInCell(cell);
281 if (first_visible_cell < 0)
282 first_visible_cell = cell;
283 if (hasSelection()) {
284 drawCellSelection(pi.pain, nx, y, i, j, 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
349 BOOST_ASSERT(hasSelection());
350 int cs = tabular.column_of_cell(sel_cell_start);
351 int ce = tabular.column_of_cell(sel_cell_end);
354 cs = tabular.column_of_cell(sel_cell_end);
356 ce = tabular.right_column_of_cell(sel_cell_end);
359 int rs = tabular.row_of_cell(sel_cell_start);
360 int re = tabular.row_of_cell(sel_cell_end);
364 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
365 int w = tabular.getWidthOfColumn(cell);
366 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
367 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
368 w, h, LColor::selection);
373 string const InsetTabular::editMessage() const
375 return _("Opened table");
379 void InsetTabular::updateLocal(BufferView * bv) const
386 int InsetTabular::insetInInsetY() const
388 if (the_locking_inset)
389 return cursory_ + the_locking_inset->insetInInsetY();
394 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
396 return the_locking_inset && the_locking_inset->insertInset(bv, inset);
400 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
402 if (hasSelection() && cmd.button() == mouse_button::button3)
408 BufferView * bv = cmd.view();
410 the_locking_inset = 0;
411 setPos(bv, cmd.x, cmd.y);
413 the_locking_inset = 0;
415 if (cmd.button() == mouse_button::button2)
416 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
420 void InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
422 if (cmd.button() == mouse_button::button3)
423 InsetTabularMailer(*this).showDialog(cmd.view());
427 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
429 BufferView * bv = cmd.view();
430 int const old_cell = actcell;
432 setPos(bv, cmd.x, cmd.y);
434 setSelection(actcell, actcell);
435 else if (old_cell != actcell)
436 setSelection(sel_cell_start, actcell);
440 void InsetTabular::edit(BufferView * bv, bool left)
442 lyxerr << "InsetTabular::edit: " << this
443 << " first text: " << tabular.cell_info[0][0].inset.getText(0)
444 << " first cell: " << &tabular.cell_info[0][0].inset << endl;
447 the_locking_inset = 0;
450 if (isRightToLeft(bv))
451 actcell = tabular.getLastCellInRow(0);
455 if (isRightToLeft(bv))
456 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
458 actcell = tabular.getNumberOfCells() - 1;
463 bv->cursor().push(this);
467 void InsetTabular::edit(BufferView * bv, int x, int y)
469 lyxerr << "InsetTabular::edit: " << this << " first cell "
470 << &tabular.cell_info[0][0].inset << endl;
473 the_locking_inset = 0;
477 int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
478 bv->cursor().push(this);
480 activateCellInset(bv, x - xx, y - cursory_);
485 InsetTabular::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
487 lyxerr << "InsetTabular::dispatch: " << cmd << endl;
488 // We need to save the value of the_locking_inset as the call to
489 // the_locking_inset->localDispatch might unlock it.
490 DispatchResult result(true, true);
491 BufferView * bv = cmd.view();
492 bool hs = hasSelection();
494 switch (cmd.action) {
496 case LFUN_MOUSE_PRESS:
498 return DispatchResult(true, true);
500 case LFUN_MOUSE_MOTION:
501 lfunMouseMotion(cmd);
502 return DispatchResult(true, true);
504 case LFUN_MOUSE_RELEASE:
505 lfunMouseRelease(cmd);
506 return DispatchResult(true, true);
508 case LFUN_CELL_BACKWARD:
509 case LFUN_CELL_FORWARD:
510 if (cmd.action == LFUN_CELL_FORWARD)
511 moveNextCell(bv, the_locking_inset != 0);
513 movePrevCell(bv, the_locking_inset != 0);
515 if (!the_locking_inset)
516 return DispatchResult(true);
519 case LFUN_SCROLL_INSET:
520 if (!cmd.argument.empty()) {
521 if (cmd.argument.find('.') != cmd.argument.npos)
522 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
524 scroll(cmd.view(), strToInt(cmd.argument));
525 cmd.view()->update();
526 return DispatchResult(true, true);
529 case LFUN_RIGHTSEL: {
530 int const start = hasSelection() ? sel_cell_start : actcell;
531 if (tabular.isLastCellInRow(actcell)) {
532 setSelection(start, actcell);
537 // if we are starting a selection, only select
538 // the current cell at the beginning
539 if (hasSelection()) {
540 moveRight(bv, false);
543 setSelection(start, end);
548 result = moveRight(bv, true);
553 int const start = hasSelection() ? sel_cell_start : actcell;
554 if (tabular.isFirstCellInRow(actcell)) {
555 setSelection(start, actcell);
560 // if we are starting a selection, only select
561 // the current cell at the beginning
562 if (hasSelection()) {
566 setSelection(start, end);
571 result = moveLeft(bv, true);
576 int const start = hasSelection() ? sel_cell_start : actcell;
577 int const ocell = actcell;
578 // if we are starting a selection, only select
579 // the current cell at the beginning
580 if (hasSelection()) {
582 if (ocell == sel_cell_end ||
583 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
584 setSelection(start, tabular.getCellBelow(sel_cell_end));
586 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
588 setSelection(start, start);
594 result = moveDown(bv, the_locking_inset != 0);
599 int const start = hasSelection() ? sel_cell_start : actcell;
600 int const ocell = actcell;
601 // if we are starting a selection, only select
602 // the current cell at the beginning
603 if (hasSelection()) {
605 if (ocell == sel_cell_end ||
606 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
607 setSelection(start, tabular.getCellAbove(sel_cell_end));
609 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
611 setSelection(start, start);
617 result = moveUp(bv, the_locking_inset != 0);
625 if (bv->top_y() + bv->painter().paperHeight()
626 < yo_ + tabular.getHeightOfTabular())
628 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
629 actcell = tabular.getCellBelow(first_visible_cell) + column;
631 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
642 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
646 actcell = tabular.getCellBelow(first_visible_cell) + column;
654 // none of these make sense for insettabular,
655 // but we must catch them to prevent any
656 // selection from being confused
660 case LFUN_WORDLEFTSEL:
662 case LFUN_WORDRIGHTSEL:
664 case LFUN_DOWN_PARAGRAPH:
665 case LFUN_DOWN_PARAGRAPHSEL:
666 case LFUN_UP_PARAGRAPH:
667 case LFUN_UP_PARAGRAPHSEL:
673 case LFUN_BEGINNINGBUF:
674 case LFUN_BEGINNINGBUFSEL:
679 case LFUN_LAYOUT_TABULAR:
680 InsetTabularMailer(*this).showDialog(bv);
683 case LFUN_INSET_DIALOG_UPDATE:
684 InsetTabularMailer(*this).updateDialog(bv);
687 case LFUN_TABULAR_FEATURE:
688 if (!tabularFeatures(bv, cmd.argument))
689 result = DispatchResult(false);
692 // insert file functions
693 case LFUN_FILE_INSERT_ASCII_PARA:
694 case LFUN_FILE_INSERT_ASCII: {
695 string tmpstr = getContentsOfAsciiFile(bv, cmd.argument, false);
696 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
697 result = DispatchResult(false);
711 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
714 case LFUN_FINISHED_LEFT:
715 lyxerr << "swallow LFUN_FINISHED_LEFT, act: " << actcell << endl;
716 if (!movePrevCell(bv, false))
717 result = DispatchResult(FINISHED);
720 case LFUN_FINISHED_RIGHT:
721 lyxerr << "swallow LFUN_FINISHED_RIGHT, act: " << actcell << endl;
722 if (!moveNextCell(bv, false))
723 result = DispatchResult(FINISHED_RIGHT);
726 case LFUN_FINISHED_UP:
727 lyxerr << "swallow LFUN_FINISHED_UP, act: " << actcell << endl;
728 result = moveUp(bv, true);
731 case LFUN_FINISHED_DOWN:
732 lyxerr << "swallow LFUN_FINISHED_DOWN, act: " << actcell << endl;
733 result = moveDown(bv, true);
737 if (copySelection(bv)) {
738 recordUndo(bv, Undo::DELETE);
739 cutSelection(bv->buffer()->params());
744 recordUndo(bv, Undo::DELETE);
745 cutSelection(bv->buffer()->params());
755 case LFUN_PASTESELECTION: {
756 string const clip = bv->getClipboard();
760 if (clip.find('\t') != string::npos) {
764 string::size_type len = clip.length();
765 string::size_type p = 0;
768 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
776 maxCols = max(cols, maxCols);
782 maxCols = max(cols, maxCols);
785 new LyXTabular(bv->buffer()->params(),
789 string::size_type op = 0;
791 int cells = paste_tabular->getNumberOfCells();
793 while ((cell < cells) && (p < len) &&
794 (p = clip.find_first_of("\t\n", p)) != string::npos) {
799 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
804 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
805 while (cols++ < maxCols)
813 // check for the last cell if there is no trailing '\n'
814 if ((cell < cells) && (op < len))
815 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
818 if (!insertAsciiString(bv, clip, true))
821 // so that the clipboard is used and it goes on
823 // and executes LFUN_PASTESELECTION in insettext!
824 paste_tabular.reset();
830 if (hasPasteBuffer()) {
831 recordUndo(bv, Undo::INSERT);
837 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
840 // handle font changing stuff on selection before we lock the inset
841 // in the default part!
842 result = DispatchResult(false);
843 // we try to activate the actual inset and put this event down to
844 // the insets dispatch function.
845 if (!the_locking_inset && activateCellInset(bv)) {
846 result = the_locking_inset->dispatch(cmd);
847 if (!result.dispatched()) {
848 // we need to update if this was requested before
849 result = DispatchResult(false);
859 InsetTabularMailer(*this).updateDialog(bv);
864 int InsetTabular::latex(Buffer const & buf, ostream & os,
865 OutputParams const & runparams) const
867 return tabular.latex(buf, os, runparams);
871 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
872 OutputParams const & runparams) const
874 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
875 return tabular.plaintext(buf, os, runparams, dp, false, 0);
879 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
880 OutputParams const & runparams) const
882 return tabular.linuxdoc(buf,os, runparams);
886 int InsetTabular::docbook(Buffer const & buf, ostream & os,
887 OutputParams const & runparams) const
892 // if the table is inside a float it doesn't need the informaltable
893 // wrapper. Search for it.
894 for (master = owner();
895 master && master->lyxCode() != InsetOld::FLOAT_CODE;
896 master = master->owner());
899 os << "<informaltable>";
900 if (runparams.mixed_content)
904 ret += tabular.docbook(buf, os, runparams);
906 os << "</informaltable>";
907 if (runparams.mixed_content)
915 void InsetTabular::validate(LaTeXFeatures & features) const
917 tabular.validate(features);
921 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
924 // if we have a locking_inset we should have to check only this cell for
925 // change so I'll try this to have a boost, but who knows ;) (Jug?)
926 // This is _really_ important (André)
927 if (the_locking_inset == &tabular.getCellInset(actcell)) {
930 for (int j = 0; j < tabular.columns(); ++j) {
934 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
935 tabular.getCellInset(actrow, j).metrics(m, dim);
936 maxAsc = max(dim.asc, maxAsc);
937 maxDesc = max(dim.des, maxDesc);
939 tabular.setWidthOfCell(actcell, the_locking_inset->width());
940 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
941 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
947 for (int i = 0; i < tabular.rows(); ++i) {
950 for (int j = 0; j < tabular.columns(); ++j) {
951 if (tabular.isPartOfMultiColumn(i, j))
957 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
958 tabular.getCellInset(cell).metrics(m, dim);
959 maxAsc = max(maxAsc, dim.asc);
960 maxDesc = max(maxDesc, dim.des);
961 tabular.setWidthOfCell(cell, dim.wid);
963 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
964 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
969 void InsetTabular::getCursorPos(BufferView *, int & x, int & y) const
971 x = TEXT_TO_INSET_OFFSET + cursorx_ - xo_;
972 y = TEXT_TO_INSET_OFFSET + cursory_;
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 (the_locking_inset &&
1056 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1058 int xx = cursorx_ - offset;
1059 if (xx > bv->workWidth()-20) {
1060 scroll(bv, - xx + bv->workWidth() - 60);
1062 } else if (xx < 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);
1088 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1091 if (activateCellInset(bv))
1092 return DispatchResult(true, true);
1094 bool moved = isRightToLeft(bv)
1095 ? movePrevCell(bv) : moveNextCell(bv);
1097 return DispatchResult(false, FINISHED_RIGHT);
1098 if (lock && activateCellInset(bv))
1099 return DispatchResult(true, true);
1102 return DispatchResult(true);
1106 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1108 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1110 return DispatchResult(false, FINISHED);
1112 if (lock && activateCellInset(bv, 0, 0, true))
1113 return DispatchResult(true, true);
1115 return DispatchResult(true);
1119 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1121 int const ocell = actcell;
1122 actcell = tabular.getCellAbove(actcell);
1123 if (actcell == ocell) { // we moved out of the inset
1124 return DispatchResult(false, FINISHED_UP);
1128 activateCellInset(bv, bv->x_target(), 0);
1129 return DispatchResult(true, true);
1133 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1135 int const ocell = actcell;
1136 actcell = tabular.getCellBelow(actcell);
1137 if (actcell == ocell) { // we moved out of the inset
1138 return DispatchResult(false, FINISHED_DOWN);
1142 activateCellInset(bv, bv->x_target(), 0);
1143 return DispatchResult(true, true);
1147 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1149 if (isRightToLeft(bv)) {
1150 if (tabular.isFirstCellInRow(actcell)) {
1151 int row = tabular.row_of_cell(actcell);
1152 if (row == tabular.rows() - 1)
1154 actcell = tabular.getLastCellInRow(row);
1155 actcell = tabular.getCellBelow(actcell);
1162 if (tabular.isLastCell(actcell))
1167 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1168 isRightToLeftPar(bv->buffer()->params());
1169 activateCellInset(bv, 0, 0, !rtl);
1176 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1178 lyxerr << "move prevcell 1" << endl;
1179 if (isRightToLeft(bv)) {
1180 lyxerr << "move prevcell a" << endl;
1181 if (tabular.isLastCellInRow(actcell)) {
1182 int row = tabular.row_of_cell(actcell);
1185 actcell = tabular.getFirstCellInRow(row);
1186 actcell = tabular.getCellAbove(actcell);
1188 if (tabular.isLastCell(actcell))
1193 lyxerr << "move prevcell b" << endl;
1194 if (actcell == 0) // first cell
1198 lyxerr << "move prevcell 2" << endl;
1200 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1201 isRightToLeftPar(bv->buffer()->params());
1202 activateCellInset(bv, 0, 0, !rtl);
1204 lyxerr << "move prevcell 3" << endl;
1206 lyxerr << "move prevcell 4" << endl;
1211 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1215 setSelection(0, tabular.getNumberOfCells() - 1);
1217 if (hasSelection()) {
1218 recordUndo(bv, Undo::ATOMIC);
1219 bool const frozen = undo_frozen;
1222 // apply the fontchange on the whole selection
1227 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1228 for (int i = sel_row_start; i <= sel_row_end; ++i)
1229 for (int j = sel_col_start; j <= sel_col_end; ++j)
1230 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1238 if (the_locking_inset)
1239 the_locking_inset->setFont(bv, font, tall);
1243 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1245 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1248 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1249 string const tmp = tabularFeature[i].feature;
1251 if (tmp == what.substr(0, tmp.length())) {
1252 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1253 //tabularFeatures[i].feature.length())) {
1254 action = tabularFeature[i].action;
1258 if (action == LyXTabular::LAST_ACTION)
1262 ltrim(what.substr(tabularFeature[i].feature.length()));
1263 tabularFeatures(bv, action, val);
1269 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1270 string const & special, bool & flag)
1272 if (special == "dl_above") {
1275 } else if (special == "dl_below") {
1276 ltt.bottomDL = flag;
1278 } else if (special == "empty") {
1290 void InsetTabular::tabularFeatures(BufferView * bv,
1291 LyXTabular::Feature feature, string const & value)
1297 bool setLines = false;
1298 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1299 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1303 case LyXTabular::M_ALIGN_LEFT:
1304 case LyXTabular::ALIGN_LEFT:
1305 setAlign = LYX_ALIGN_LEFT;
1308 case LyXTabular::M_ALIGN_RIGHT:
1309 case LyXTabular::ALIGN_RIGHT:
1310 setAlign = LYX_ALIGN_RIGHT;
1313 case LyXTabular::M_ALIGN_CENTER:
1314 case LyXTabular::ALIGN_CENTER:
1315 setAlign = LYX_ALIGN_CENTER;
1318 case LyXTabular::ALIGN_BLOCK:
1319 setAlign = LYX_ALIGN_BLOCK;
1322 case LyXTabular::M_VALIGN_TOP:
1323 case LyXTabular::VALIGN_TOP:
1324 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1327 case LyXTabular::M_VALIGN_BOTTOM:
1328 case LyXTabular::VALIGN_BOTTOM:
1329 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1332 case LyXTabular::M_VALIGN_MIDDLE:
1333 case LyXTabular::VALIGN_MIDDLE:
1334 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1341 if (hasSelection()) {
1342 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1344 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1345 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1347 recordUndo(bv, Undo::ATOMIC);
1349 int row = tabular.row_of_cell(actcell);
1350 int column = tabular.column_of_cell(actcell);
1352 LyXTabular::ltType ltt;
1356 case LyXTabular::SET_PWIDTH: {
1357 LyXLength const len(value);
1358 tabular.setColumnPWidth(actcell, len);
1360 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1361 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1362 else if (!len.zero()
1363 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1364 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1368 case LyXTabular::SET_MPWIDTH:
1369 tabular.setMColumnPWidth(actcell, LyXLength(value));
1372 case LyXTabular::SET_SPECIAL_COLUMN:
1373 case LyXTabular::SET_SPECIAL_MULTI:
1374 tabular.setAlignSpecial(actcell,value,feature);
1377 case LyXTabular::APPEND_ROW:
1378 // append the row into the tabular
1379 tabular.appendRow(bv->buffer()->params(), actcell);
1380 tabular.setOwner(this);
1383 case LyXTabular::APPEND_COLUMN:
1384 // append the column into the tabular
1385 tabular.appendColumn(bv->buffer()->params(), actcell);
1386 tabular.setOwner(this);
1387 actcell = tabular.getCellNumber(row, column);
1390 case LyXTabular::DELETE_ROW:
1391 for (int i = sel_row_start; i <= sel_row_end; ++i)
1392 tabular.deleteRow(sel_row_start);
1393 if (sel_row_start >= tabular.rows())
1395 actcell = tabular.getCellNumber(sel_row_start, column);
1399 case LyXTabular::DELETE_COLUMN:
1400 for (int i = sel_col_start; i <= sel_col_end; ++i)
1401 tabular.deleteColumn(sel_col_start);
1402 if (sel_col_start >= tabular.columns())
1404 actcell = tabular.getCellNumber(row, sel_col_start);
1408 case LyXTabular::M_TOGGLE_LINE_TOP:
1410 case LyXTabular::TOGGLE_LINE_TOP: {
1411 bool lineSet = !tabular.topLine(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)
1415 tabular.getCellNumber(i, j),
1420 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1422 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1423 bool lineSet = !tabular.bottomLine(actcell, flag);
1424 for (int i = sel_row_start; i <= sel_row_end; ++i)
1425 for (int j = sel_col_start; j <= sel_col_end; ++j)
1426 tabular.setBottomLine(
1427 tabular.getCellNumber(i, j),
1433 case LyXTabular::M_TOGGLE_LINE_LEFT:
1435 case LyXTabular::TOGGLE_LINE_LEFT: {
1436 bool lineSet = !tabular.leftLine(actcell, flag);
1437 for (int i = sel_row_start; i <= sel_row_end; ++i)
1438 for (int j = sel_col_start; j <= sel_col_end; ++j)
1439 tabular.setLeftLine(
1440 tabular.getCellNumber(i,j),
1446 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1448 case LyXTabular::TOGGLE_LINE_RIGHT: {
1449 bool lineSet = !tabular.rightLine(actcell, flag);
1450 for (int i = sel_row_start; i <= sel_row_end; ++i)
1451 for (int j = sel_col_start; j <= sel_col_end; ++j)
1452 tabular.setRightLine(
1453 tabular.getCellNumber(i,j),
1459 case LyXTabular::M_ALIGN_LEFT:
1460 case LyXTabular::M_ALIGN_RIGHT:
1461 case LyXTabular::M_ALIGN_CENTER:
1463 case LyXTabular::ALIGN_LEFT:
1464 case LyXTabular::ALIGN_RIGHT:
1465 case LyXTabular::ALIGN_CENTER:
1466 case LyXTabular::ALIGN_BLOCK:
1467 for (int i = sel_row_start; i <= sel_row_end; ++i)
1468 for (int j = sel_col_start; j <= sel_col_end; ++j)
1469 tabular.setAlignment(
1470 tabular.getCellNumber(i, j),
1475 case LyXTabular::M_VALIGN_TOP:
1476 case LyXTabular::M_VALIGN_BOTTOM:
1477 case LyXTabular::M_VALIGN_MIDDLE:
1479 case LyXTabular::VALIGN_TOP:
1480 case LyXTabular::VALIGN_BOTTOM:
1481 case LyXTabular::VALIGN_MIDDLE:
1482 for (int i = sel_row_start; i <= sel_row_end; ++i)
1483 for (int j = sel_col_start; j <= sel_col_end; ++j)
1484 tabular.setVAlignment(
1485 tabular.getCellNumber(i, j),
1489 case LyXTabular::MULTICOLUMN: {
1490 if (sel_row_start != sel_row_end) {
1491 #ifdef WITH_WARNINGS
1492 #warning Need I say it ? This is horrible.
1494 Alert::error(_("Error setting multicolumn"),
1495 _("You cannot set multicolumn vertically."));
1498 // just multicol for one Single Cell
1499 if (!hasSelection()) {
1500 // check wether we are completly in a multicol
1501 if (tabular.isMultiColumn(actcell))
1502 tabular.unsetMultiColumn(actcell);
1504 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1507 // we have a selection so this means we just add all this
1508 // cells to form a multicolumn cell
1512 if (sel_cell_start > sel_cell_end) {
1513 s_start = sel_cell_end;
1514 s_end = sel_cell_start;
1516 s_start = sel_cell_start;
1517 s_end = sel_cell_end;
1519 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1525 case LyXTabular::SET_ALL_LINES:
1527 case LyXTabular::UNSET_ALL_LINES:
1528 for (int i = sel_row_start; i <= sel_row_end; ++i)
1529 for (int j = sel_col_start; j <= sel_col_end; ++j)
1530 tabular.setAllLines(
1531 tabular.getCellNumber(i,j), setLines);
1534 case LyXTabular::SET_LONGTABULAR:
1535 tabular.setLongTabular(true);
1538 case LyXTabular::UNSET_LONGTABULAR:
1539 tabular.setLongTabular(false);
1542 case LyXTabular::SET_ROTATE_TABULAR:
1543 tabular.setRotateTabular(true);
1546 case LyXTabular::UNSET_ROTATE_TABULAR:
1547 tabular.setRotateTabular(false);
1550 case LyXTabular::SET_ROTATE_CELL:
1551 for (int i = sel_row_start; i <= sel_row_end; ++i)
1552 for (int j = sel_col_start; j <= sel_col_end; ++j)
1553 tabular.setRotateCell(
1554 tabular.getCellNumber(i, j), true);
1557 case LyXTabular::UNSET_ROTATE_CELL:
1558 for (int i = sel_row_start; i <= sel_row_end; ++i)
1559 for (int j = sel_col_start; j <= sel_col_end; ++j)
1560 tabular.setRotateCell(
1561 tabular.getCellNumber(i, j), false);
1564 case LyXTabular::SET_USEBOX: {
1565 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1566 if (val == tabular.getUsebox(actcell))
1567 val = LyXTabular::BOX_NONE;
1568 for (int i = sel_row_start; i <= sel_row_end; ++i)
1569 for (int j = sel_col_start; j <= sel_col_end; ++j)
1570 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1574 case LyXTabular::UNSET_LTFIRSTHEAD:
1576 case LyXTabular::SET_LTFIRSTHEAD:
1577 tabular.getRowOfLTFirstHead(row, ltt);
1578 checkLongtableSpecial(ltt, value, flag);
1579 tabular.setLTHead(row, flag, ltt, true);
1582 case LyXTabular::UNSET_LTHEAD:
1584 case LyXTabular::SET_LTHEAD:
1585 tabular.getRowOfLTHead(row, ltt);
1586 checkLongtableSpecial(ltt, value, flag);
1587 tabular.setLTHead(row, flag, ltt, false);
1590 case LyXTabular::UNSET_LTFOOT:
1592 case LyXTabular::SET_LTFOOT:
1593 tabular.getRowOfLTFoot(row, ltt);
1594 checkLongtableSpecial(ltt, value, flag);
1595 tabular.setLTFoot(row, flag, ltt, false);
1598 case LyXTabular::UNSET_LTLASTFOOT:
1600 case LyXTabular::SET_LTLASTFOOT:
1601 tabular.getRowOfLTLastFoot(row, ltt);
1602 checkLongtableSpecial(ltt, value, flag);
1603 tabular.setLTFoot(row, flag, ltt, true);
1606 case LyXTabular::SET_LTNEWPAGE:
1607 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1610 // dummy stuff just to avoid warnings
1611 case LyXTabular::LAST_ACTION:
1616 InsetTabularMailer(*this).updateDialog(bv);
1620 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1622 UpdatableInset & inset = tabular.getCellInset(actcell);
1625 x = inset.x() + inset.width();
1626 y = inset.descent();
1628 inset.edit(bv, x, y);
1629 bv->cursor().push(&inset);
1630 if (!the_locking_inset)
1633 return the_locking_inset;
1637 void InsetTabular::deleteLyXText(BufferView * /*bv*/) const
1639 #warning this is strange, isnt it? But this is 1.3.x code...
1640 //resizeLyXText(bv, recursive);
1644 bool InsetTabular::showInsetDialog(BufferView * bv) const
1646 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1647 InsetTabularMailer(*this).showDialog(bv);
1652 void InsetTabular::openLayoutDialog(BufferView * bv) const
1656 if (the_locking_inset) {
1657 InsetTabular * inset = static_cast<InsetTabular *>
1658 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
1660 inset->openLayoutDialog(bv);
1665 InsetTabularMailer(*this).showDialog(bv);
1670 // function returns an object as defined in func_status.h:
1671 // states OK, Unknown, Disabled, On, Off.
1673 FuncStatus InsetTabular::getStatus(string const & what) const
1675 int action = LyXTabular::LAST_ACTION;
1679 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1680 string const tmp = tabularFeature[i].feature;
1681 if (tmp == what.substr(0, tmp.length())) {
1682 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1683 // tabularFeatures[i].feature.length())) {
1684 action = tabularFeature[i].action;
1688 if (action == LyXTabular::LAST_ACTION) {
1690 status.unknown(true);
1694 string const argument
1695 = ltrim(what.substr(tabularFeature[i].feature.length()));
1700 LyXTabular::ltType dummyltt;
1704 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1706 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1709 case LyXTabular::SET_PWIDTH:
1710 case LyXTabular::SET_MPWIDTH:
1711 case LyXTabular::SET_SPECIAL_COLUMN:
1712 case LyXTabular::SET_SPECIAL_MULTI:
1713 case LyXTabular::APPEND_ROW:
1714 case LyXTabular::APPEND_COLUMN:
1715 case LyXTabular::DELETE_ROW:
1716 case LyXTabular::DELETE_COLUMN:
1717 case LyXTabular::SET_ALL_LINES:
1718 case LyXTabular::UNSET_ALL_LINES:
1722 case LyXTabular::MULTICOLUMN:
1723 status.setOnOff(tabular.isMultiColumn(actcell));
1726 case LyXTabular::M_TOGGLE_LINE_TOP:
1728 case LyXTabular::TOGGLE_LINE_TOP:
1729 status.setOnOff(tabular.topLine(actcell, flag));
1732 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1734 case LyXTabular::TOGGLE_LINE_BOTTOM:
1735 status.setOnOff(tabular.bottomLine(actcell, flag));
1738 case LyXTabular::M_TOGGLE_LINE_LEFT:
1740 case LyXTabular::TOGGLE_LINE_LEFT:
1741 status.setOnOff(tabular.leftLine(actcell, flag));
1744 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1746 case LyXTabular::TOGGLE_LINE_RIGHT:
1747 status.setOnOff(tabular.rightLine(actcell, flag));
1750 case LyXTabular::M_ALIGN_LEFT:
1752 case LyXTabular::ALIGN_LEFT:
1753 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1756 case LyXTabular::M_ALIGN_RIGHT:
1758 case LyXTabular::ALIGN_RIGHT:
1759 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1762 case LyXTabular::M_ALIGN_CENTER:
1764 case LyXTabular::ALIGN_CENTER:
1765 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1768 case LyXTabular::ALIGN_BLOCK:
1769 status.disabled(tabular.getPWidth(actcell).zero());
1770 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1773 case LyXTabular::M_VALIGN_TOP:
1775 case LyXTabular::VALIGN_TOP:
1776 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1779 case LyXTabular::M_VALIGN_BOTTOM:
1781 case LyXTabular::VALIGN_BOTTOM:
1782 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1785 case LyXTabular::M_VALIGN_MIDDLE:
1787 case LyXTabular::VALIGN_MIDDLE:
1788 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1791 case LyXTabular::SET_LONGTABULAR:
1792 status.setOnOff(tabular.isLongTabular());
1795 case LyXTabular::UNSET_LONGTABULAR:
1796 status.setOnOff(!tabular.isLongTabular());
1799 case LyXTabular::SET_ROTATE_TABULAR:
1800 status.setOnOff(tabular.getRotateTabular());
1803 case LyXTabular::UNSET_ROTATE_TABULAR:
1804 status.setOnOff(!tabular.getRotateTabular());
1807 case LyXTabular::SET_ROTATE_CELL:
1808 status.setOnOff(tabular.getRotateCell(actcell));
1811 case LyXTabular::UNSET_ROTATE_CELL:
1812 status.setOnOff(!tabular.getRotateCell(actcell));
1815 case LyXTabular::SET_USEBOX:
1816 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1819 case LyXTabular::SET_LTFIRSTHEAD:
1820 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1823 case LyXTabular::SET_LTHEAD:
1824 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1827 case LyXTabular::SET_LTFOOT:
1828 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1831 case LyXTabular::SET_LTLASTFOOT:
1832 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1835 case LyXTabular::SET_LTNEWPAGE:
1836 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1841 status.disabled(true);
1848 void InsetTabular::getLabelList(Buffer const & buffer,
1849 std::vector<string> & list) const
1851 tabular.getLabelList(buffer, list);
1855 bool InsetTabular::copySelection(BufferView * bv)
1857 if (!hasSelection())
1860 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1861 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1862 if (sel_col_start > sel_col_end) {
1863 sel_col_start = sel_col_end;
1864 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1866 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1869 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1870 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1871 if (sel_row_start > sel_row_end)
1872 swap(sel_row_start, sel_row_end);
1874 paste_tabular.reset(new LyXTabular(tabular));
1875 paste_tabular->setOwner(this);
1877 for (int i = 0; i < sel_row_start; ++i)
1878 paste_tabular->deleteRow(0);
1880 int const rows = sel_row_end - sel_row_start + 1;
1881 while (paste_tabular->rows() > rows)
1882 paste_tabular->deleteRow(rows);
1884 paste_tabular->setTopLine(0, true, true);
1885 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1888 for (int i = 0; i < sel_col_start; ++i)
1889 paste_tabular->deleteColumn(0);
1891 int const columns = sel_col_end - sel_col_start + 1;
1892 while (paste_tabular->columns() > columns)
1893 paste_tabular->deleteColumn(columns);
1895 paste_tabular->setLeftLine(0, true, true);
1896 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1900 OutputParams const runparams;
1901 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1902 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1903 bv->stuffClipboard(os.str());
1908 bool InsetTabular::pasteSelection(BufferView * bv)
1913 for (int r1 = 0, r2 = actrow;
1914 r1 < paste_tabular->rows() && r2 < tabular.rows();
1916 for (int c1 = 0, c2 = actcol;
1917 c1 < paste_tabular->columns() && c2 < tabular.columns();
1919 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1920 tabular.isPartOfMultiColumn(r2, c2))
1922 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1926 if (tabular.isPartOfMultiColumn(r2, c2)) {
1930 InsetText & inset = tabular.getCellInset(r2, c2);
1931 inset = paste_tabular->getCellInset(r1, c1);
1932 inset.setOwner(this);
1933 inset.deleteLyXText(bv);
1941 bool InsetTabular::cutSelection(BufferParams const & bp)
1943 if (!hasSelection())
1946 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1947 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1948 if (sel_col_start > sel_col_end) {
1949 sel_col_start = sel_col_end;
1950 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1952 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1955 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1956 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1958 if (sel_row_start > sel_row_end)
1959 swap(sel_row_start, sel_row_end);
1961 if (sel_cell_start > sel_cell_end)
1962 swap(sel_cell_start, sel_cell_end);
1964 for (int i = sel_row_start; i <= sel_row_end; ++i)
1965 for (int j = sel_col_start; j <= sel_col_end; ++j)
1966 tabular.getCellInset(tabular.getCellNumber(i, j))
1967 .clear(bp.tracking_changes);
1972 bool InsetTabular::isRightToLeft(BufferView * bv)
1974 return bv->getParentLanguage(this)->RightToLeft();
1978 int InsetTabular::scroll(bool recursive) const
1980 int sx = UpdatableInset::scroll(false);
1982 if (recursive && the_locking_inset)
1983 sx += the_locking_inset->scroll(recursive);
1989 void InsetTabular::getSelection(int & srow, int & erow,
1990 int & scol, int & ecol) const
1992 int const start = hasSelection() ? sel_cell_start : actcell;
1993 int const end = hasSelection() ? sel_cell_end : actcell;
1995 srow = tabular.row_of_cell(start);
1996 erow = tabular.row_of_cell(end);
2000 scol = tabular.column_of_cell(start);
2001 ecol = tabular.column_of_cell(end);
2005 ecol = tabular.right_column_of_cell(end);
2009 ParagraphList * InsetTabular::getParagraphs(int i) const
2011 return i < tabular.getNumberOfCells()
2012 ? tabular.getCellInset(i).getParagraphs(0)
2017 int InsetTabular::numParagraphs() const
2019 return tabular.getNumberOfCells();
2023 LyXText * InsetTabular::getText(int i) const
2025 return i < tabular.getNumberOfCells()
2026 ? tabular.getCellInset(i).getText(0)
2031 void InsetTabular::markErased()
2033 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2034 tabular.getCellInset(cell).markErased();
2038 bool InsetTabular::insetAllowed(InsetOld::Code) const
2044 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2046 const int cell = tabular.getCellFromInset(in);
2049 return tabular.getPWidth(cell).zero();
2051 // this is a workaround for a crash (New, Insert->Tabular,
2052 // Insert->FootNote)
2056 // well we didn't obviously find it so maybe our owner knows more
2057 BOOST_ASSERT(owner());
2058 return owner()->forceDefaultParagraphs(in);
2062 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2065 if (buf.length() <= 0)
2071 string::size_type len = buf.length();
2072 string::size_type p = 0;
2074 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2082 maxCols = max(cols, maxCols);
2088 maxCols = max(cols, maxCols);
2089 LyXTabular * loctab;
2094 paste_tabular.reset(
2095 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2098 paste_tabular->setOwner(this);
2099 loctab = paste_tabular.get();
2108 string::size_type op = 0;
2109 int cells = loctab->getNumberOfCells();
2112 rows = loctab->rows();
2113 int const columns = loctab->columns();
2115 while (cell < cells && p < len && row < rows &&
2116 (p = buf.find_first_of("\t\n", p)) != string::npos)
2122 // we can only set this if we are not too far right
2123 if (cols < columns) {
2124 InsetText & inset = loctab->getCellInset(cell);
2125 LyXFont const font = inset.text_.
2126 getFont(inset.paragraphs.begin(), 0);
2127 inset.setText(buf.substr(op, p - op), font);
2133 // we can only set this if we are not too far right
2134 if (cols < columns) {
2135 InsetText & inset = tabular.getCellInset(cell);
2136 LyXFont const font = inset.text_.
2137 getFont(inset.paragraphs.begin(), 0);
2138 inset.setText(buf.substr(op, p - op), font);
2143 cell = loctab->getCellNumber(row, cols);
2149 // check for the last cell if there is no trailing '\n'
2150 if (cell < cells && op < len) {
2151 InsetText & inset = loctab->getCellInset(cell);
2152 LyXFont const font = inset.text_.getFont(inset.paragraphs.begin(), 0);
2153 inset.setText(buf.substr(op, len - op), font);
2160 void InsetTabular::addPreview(PreviewLoader & loader) const
2162 int const rows = tabular.rows();
2163 int const columns = tabular.columns();
2164 for (int i = 0; i < rows; ++i) {
2165 for (int j = 0; j < columns; ++j)
2166 tabular.getCellInset(i, j).addPreview(loader);
2171 string const InsetTabularMailer::name_("tabular");
2173 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2174 : inset_(const_cast<InsetTabular &>(inset))
2178 string const InsetTabularMailer::inset2string(Buffer const &) const
2180 return params2string(inset_);
2184 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2186 istringstream data(in);
2188 lex.setStream(data);
2190 #warning CHECK verify that this is a sane value to return.
2196 string const token = lex.getString();
2204 string const token = lex.getString();
2205 if (token != "\\active_cell")
2208 cell = lex.getInteger();
2211 // This is part of the inset proper that is usually swallowed
2212 // by Buffer::readInset
2215 string const token = lex.getString();
2216 if (token != "Tabular")
2223 Buffer const & buffer = inset.buffer();
2224 inset.read(buffer, lex);
2226 // We can't set the active cell, but we can tell the frontend
2232 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2235 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2236 inset.write(inset.buffer(), data);
2237 data << "\\end_inset\n";