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::fitInsetCursor(BufferView * bv) const
978 if (the_locking_inset) {
979 the_locking_inset->fitInsetCursor(bv);
984 int const asc = font_metrics::maxAscent(font);
985 int const desc = font_metrics::maxDescent(font);
988 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
992 void InsetTabular::setPos(BufferView * bv, int x, int y) const
998 int ly = tabular.getDescentOfRow(actrow);
1000 // first search the right row
1001 while (ly < y && actrow + 1 < tabular.rows()) {
1002 cursory_ += tabular.getDescentOfRow(actrow) +
1003 tabular.getAscentOfRow(actrow + 1) +
1004 tabular.getAdditionalHeight(actrow + 1);
1006 ly = cursory_ + tabular.getDescentOfRow(actrow);
1008 actcell = tabular.getCellNumber(actrow, actcol);
1010 // now search the right column
1011 int lx = tabular.getWidthOfColumn(actcell) -
1012 tabular.getAdditionalWidth(actcell);
1014 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1015 lx += tabular.getWidthOfColumn(actcell + 1)
1016 + tabular.getAdditionalWidth(actcell);
1018 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1023 int InsetTabular::getCellXPos(int cell) const
1027 for (; !tabular.isFirstCellInRow(c); --c)
1029 int lx = tabular.getWidthOfColumn(cell);
1030 for (; c < cell; ++c)
1031 lx += tabular.getWidthOfColumn(c);
1033 return lx - tabular.getWidthOfColumn(cell) + xo_;
1037 void InsetTabular::resetPos(BufferView * bv) const
1039 #ifdef WITH_WARNINGS
1040 #warning This should be fixed in the right manner (20011128 Jug)
1042 // fast hack to fix infinite repaintings!
1043 if (in_reset_pos > 0)
1047 actcol = tabular.column_of_cell(actcell);
1050 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1051 if (tabular.isLastCellInRow(cell)) {
1052 cursory_ += tabular.getDescentOfRow(actrow) +
1053 tabular.getAscentOfRow(actrow + 1) +
1054 tabular.getAdditionalHeight(actrow + 1);
1059 // we need this only from here on!!!
1061 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1062 int new_x = getCellXPos(actcell) + offset;
1063 int old_x = cursorx_;
1065 // cursor.x(getCellXPos(actcell) + offset);
1066 if (actcol < tabular.columns() - 1 && scroll(false) &&
1067 tabular.getWidthOfTabular() < bv->workWidth()-20)
1071 } else if (the_locking_inset &&
1072 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1074 int xx = cursorx_ - offset;
1075 if (xx > bv->workWidth()-20) {
1076 scroll(bv, - xx + bv->workWidth() - 60);
1078 } else if (xx < 20) {
1086 } else if (cursorx_ - offset > 20 &&
1087 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1088 > bv->workWidth() - 20) {
1089 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1091 } else if (cursorx_ - offset < 20) {
1092 scroll(bv, 20 - cursorx_ + offset);
1094 } else if (scroll() && xo_ > 20 &&
1095 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1096 scroll(bv, old_x - cursorx_);
1099 InsetTabularMailer(*this).updateDialog(bv);
1104 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1107 if (activateCellInset(bv))
1108 return DispatchResult(true, true);
1110 bool moved = isRightToLeft(bv)
1111 ? movePrevCell(bv) : moveNextCell(bv);
1113 return DispatchResult(false, FINISHED_RIGHT);
1114 if (lock && activateCellInset(bv))
1115 return DispatchResult(true, true);
1118 return DispatchResult(true);
1122 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1124 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1126 return DispatchResult(false, FINISHED);
1128 if (lock && activateCellInset(bv, 0, 0, true))
1129 return DispatchResult(true, true);
1131 return DispatchResult(true);
1135 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1137 int const ocell = actcell;
1138 actcell = tabular.getCellAbove(actcell);
1139 if (actcell == ocell) { // we moved out of the inset
1140 return DispatchResult(false, FINISHED_UP);
1144 activateCellInset(bv, bv->x_target(), 0);
1145 return DispatchResult(true, true);
1149 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1151 int const ocell = actcell;
1152 actcell = tabular.getCellBelow(actcell);
1153 if (actcell == ocell) { // we moved out of the inset
1154 return DispatchResult(false, FINISHED_DOWN);
1158 activateCellInset(bv, bv->x_target(), 0);
1159 return DispatchResult(true, true);
1163 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1165 if (isRightToLeft(bv)) {
1166 if (tabular.isFirstCellInRow(actcell)) {
1167 int row = tabular.row_of_cell(actcell);
1168 if (row == tabular.rows() - 1)
1170 actcell = tabular.getLastCellInRow(row);
1171 actcell = tabular.getCellBelow(actcell);
1178 if (tabular.isLastCell(actcell))
1183 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1184 isRightToLeftPar(bv->buffer()->params());
1185 activateCellInset(bv, 0, 0, !rtl);
1192 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1194 lyxerr << "move prevcell 1" << endl;
1195 if (isRightToLeft(bv)) {
1196 lyxerr << "move prevcell a" << endl;
1197 if (tabular.isLastCellInRow(actcell)) {
1198 int row = tabular.row_of_cell(actcell);
1201 actcell = tabular.getFirstCellInRow(row);
1202 actcell = tabular.getCellAbove(actcell);
1204 if (tabular.isLastCell(actcell))
1209 lyxerr << "move prevcell b" << endl;
1210 if (actcell == 0) // first cell
1214 lyxerr << "move prevcell 2" << endl;
1216 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1217 isRightToLeftPar(bv->buffer()->params());
1218 activateCellInset(bv, 0, 0, !rtl);
1220 lyxerr << "move prevcell 3" << endl;
1222 lyxerr << "move prevcell 4" << endl;
1227 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1231 setSelection(0, tabular.getNumberOfCells() - 1);
1233 if (hasSelection()) {
1234 recordUndo(bv, Undo::ATOMIC);
1235 bool const frozen = undo_frozen;
1238 // apply the fontchange on the whole selection
1243 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1244 for (int i = sel_row_start; i <= sel_row_end; ++i)
1245 for (int j = sel_col_start; j <= sel_col_end; ++j)
1246 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1254 if (the_locking_inset)
1255 the_locking_inset->setFont(bv, font, tall);
1259 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1261 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1264 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1265 string const tmp = tabularFeature[i].feature;
1267 if (tmp == what.substr(0, tmp.length())) {
1268 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1269 //tabularFeatures[i].feature.length())) {
1270 action = tabularFeature[i].action;
1274 if (action == LyXTabular::LAST_ACTION)
1278 ltrim(what.substr(tabularFeature[i].feature.length()));
1279 tabularFeatures(bv, action, val);
1285 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1286 string const & special, bool & flag)
1288 if (special == "dl_above") {
1291 } else if (special == "dl_below") {
1292 ltt.bottomDL = flag;
1294 } else if (special == "empty") {
1306 void InsetTabular::tabularFeatures(BufferView * bv,
1307 LyXTabular::Feature feature, string const & value)
1313 bool setLines = false;
1314 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1315 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1319 case LyXTabular::M_ALIGN_LEFT:
1320 case LyXTabular::ALIGN_LEFT:
1321 setAlign = LYX_ALIGN_LEFT;
1324 case LyXTabular::M_ALIGN_RIGHT:
1325 case LyXTabular::ALIGN_RIGHT:
1326 setAlign = LYX_ALIGN_RIGHT;
1329 case LyXTabular::M_ALIGN_CENTER:
1330 case LyXTabular::ALIGN_CENTER:
1331 setAlign = LYX_ALIGN_CENTER;
1334 case LyXTabular::ALIGN_BLOCK:
1335 setAlign = LYX_ALIGN_BLOCK;
1338 case LyXTabular::M_VALIGN_TOP:
1339 case LyXTabular::VALIGN_TOP:
1340 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1343 case LyXTabular::M_VALIGN_BOTTOM:
1344 case LyXTabular::VALIGN_BOTTOM:
1345 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1348 case LyXTabular::M_VALIGN_MIDDLE:
1349 case LyXTabular::VALIGN_MIDDLE:
1350 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1357 if (hasSelection()) {
1358 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1360 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1361 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1363 recordUndo(bv, Undo::ATOMIC);
1365 int row = tabular.row_of_cell(actcell);
1366 int column = tabular.column_of_cell(actcell);
1368 LyXTabular::ltType ltt;
1372 case LyXTabular::SET_PWIDTH: {
1373 LyXLength const len(value);
1374 tabular.setColumnPWidth(actcell, len);
1376 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1377 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1378 else if (!len.zero()
1379 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1380 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1384 case LyXTabular::SET_MPWIDTH:
1385 tabular.setMColumnPWidth(actcell, LyXLength(value));
1388 case LyXTabular::SET_SPECIAL_COLUMN:
1389 case LyXTabular::SET_SPECIAL_MULTI:
1390 tabular.setAlignSpecial(actcell,value,feature);
1393 case LyXTabular::APPEND_ROW:
1394 // append the row into the tabular
1395 tabular.appendRow(bv->buffer()->params(), actcell);
1396 tabular.setOwner(this);
1399 case LyXTabular::APPEND_COLUMN:
1400 // append the column into the tabular
1401 tabular.appendColumn(bv->buffer()->params(), actcell);
1402 tabular.setOwner(this);
1403 actcell = tabular.getCellNumber(row, column);
1406 case LyXTabular::DELETE_ROW:
1407 for (int i = sel_row_start; i <= sel_row_end; ++i)
1408 tabular.deleteRow(sel_row_start);
1409 if (sel_row_start >= tabular.rows())
1411 actcell = tabular.getCellNumber(sel_row_start, column);
1415 case LyXTabular::DELETE_COLUMN:
1416 for (int i = sel_col_start; i <= sel_col_end; ++i)
1417 tabular.deleteColumn(sel_col_start);
1418 if (sel_col_start >= tabular.columns())
1420 actcell = tabular.getCellNumber(row, sel_col_start);
1424 case LyXTabular::M_TOGGLE_LINE_TOP:
1426 case LyXTabular::TOGGLE_LINE_TOP: {
1427 bool lineSet = !tabular.topLine(actcell, flag);
1428 for (int i = sel_row_start; i <= sel_row_end; ++i)
1429 for (int j = sel_col_start; j <= sel_col_end; ++j)
1431 tabular.getCellNumber(i, j),
1436 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1438 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1439 bool lineSet = !tabular.bottomLine(actcell, flag);
1440 for (int i = sel_row_start; i <= sel_row_end; ++i)
1441 for (int j = sel_col_start; j <= sel_col_end; ++j)
1442 tabular.setBottomLine(
1443 tabular.getCellNumber(i, j),
1449 case LyXTabular::M_TOGGLE_LINE_LEFT:
1451 case LyXTabular::TOGGLE_LINE_LEFT: {
1452 bool lineSet = !tabular.leftLine(actcell, flag);
1453 for (int i = sel_row_start; i <= sel_row_end; ++i)
1454 for (int j = sel_col_start; j <= sel_col_end; ++j)
1455 tabular.setLeftLine(
1456 tabular.getCellNumber(i,j),
1462 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1464 case LyXTabular::TOGGLE_LINE_RIGHT: {
1465 bool lineSet = !tabular.rightLine(actcell, flag);
1466 for (int i = sel_row_start; i <= sel_row_end; ++i)
1467 for (int j = sel_col_start; j <= sel_col_end; ++j)
1468 tabular.setRightLine(
1469 tabular.getCellNumber(i,j),
1475 case LyXTabular::M_ALIGN_LEFT:
1476 case LyXTabular::M_ALIGN_RIGHT:
1477 case LyXTabular::M_ALIGN_CENTER:
1479 case LyXTabular::ALIGN_LEFT:
1480 case LyXTabular::ALIGN_RIGHT:
1481 case LyXTabular::ALIGN_CENTER:
1482 case LyXTabular::ALIGN_BLOCK:
1483 for (int i = sel_row_start; i <= sel_row_end; ++i)
1484 for (int j = sel_col_start; j <= sel_col_end; ++j)
1485 tabular.setAlignment(
1486 tabular.getCellNumber(i, j),
1491 case LyXTabular::M_VALIGN_TOP:
1492 case LyXTabular::M_VALIGN_BOTTOM:
1493 case LyXTabular::M_VALIGN_MIDDLE:
1495 case LyXTabular::VALIGN_TOP:
1496 case LyXTabular::VALIGN_BOTTOM:
1497 case LyXTabular::VALIGN_MIDDLE:
1498 for (int i = sel_row_start; i <= sel_row_end; ++i)
1499 for (int j = sel_col_start; j <= sel_col_end; ++j)
1500 tabular.setVAlignment(
1501 tabular.getCellNumber(i, j),
1505 case LyXTabular::MULTICOLUMN: {
1506 if (sel_row_start != sel_row_end) {
1507 #ifdef WITH_WARNINGS
1508 #warning Need I say it ? This is horrible.
1510 Alert::error(_("Error setting multicolumn"),
1511 _("You cannot set multicolumn vertically."));
1514 // just multicol for one Single Cell
1515 if (!hasSelection()) {
1516 // check wether we are completly in a multicol
1517 if (tabular.isMultiColumn(actcell))
1518 tabular.unsetMultiColumn(actcell);
1520 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1523 // we have a selection so this means we just add all this
1524 // cells to form a multicolumn cell
1528 if (sel_cell_start > sel_cell_end) {
1529 s_start = sel_cell_end;
1530 s_end = sel_cell_start;
1532 s_start = sel_cell_start;
1533 s_end = sel_cell_end;
1535 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1541 case LyXTabular::SET_ALL_LINES:
1543 case LyXTabular::UNSET_ALL_LINES:
1544 for (int i = sel_row_start; i <= sel_row_end; ++i)
1545 for (int j = sel_col_start; j <= sel_col_end; ++j)
1546 tabular.setAllLines(
1547 tabular.getCellNumber(i,j), setLines);
1550 case LyXTabular::SET_LONGTABULAR:
1551 tabular.setLongTabular(true);
1554 case LyXTabular::UNSET_LONGTABULAR:
1555 tabular.setLongTabular(false);
1558 case LyXTabular::SET_ROTATE_TABULAR:
1559 tabular.setRotateTabular(true);
1562 case LyXTabular::UNSET_ROTATE_TABULAR:
1563 tabular.setRotateTabular(false);
1566 case LyXTabular::SET_ROTATE_CELL:
1567 for (int i = sel_row_start; i <= sel_row_end; ++i)
1568 for (int j = sel_col_start; j <= sel_col_end; ++j)
1569 tabular.setRotateCell(
1570 tabular.getCellNumber(i, j), true);
1573 case LyXTabular::UNSET_ROTATE_CELL:
1574 for (int i = sel_row_start; i <= sel_row_end; ++i)
1575 for (int j = sel_col_start; j <= sel_col_end; ++j)
1576 tabular.setRotateCell(
1577 tabular.getCellNumber(i, j), false);
1580 case LyXTabular::SET_USEBOX: {
1581 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1582 if (val == tabular.getUsebox(actcell))
1583 val = LyXTabular::BOX_NONE;
1584 for (int i = sel_row_start; i <= sel_row_end; ++i)
1585 for (int j = sel_col_start; j <= sel_col_end; ++j)
1586 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1590 case LyXTabular::UNSET_LTFIRSTHEAD:
1592 case LyXTabular::SET_LTFIRSTHEAD:
1593 tabular.getRowOfLTFirstHead(row, ltt);
1594 checkLongtableSpecial(ltt, value, flag);
1595 tabular.setLTHead(row, flag, ltt, true);
1598 case LyXTabular::UNSET_LTHEAD:
1600 case LyXTabular::SET_LTHEAD:
1601 tabular.getRowOfLTHead(row, ltt);
1602 checkLongtableSpecial(ltt, value, flag);
1603 tabular.setLTHead(row, flag, ltt, false);
1606 case LyXTabular::UNSET_LTFOOT:
1608 case LyXTabular::SET_LTFOOT:
1609 tabular.getRowOfLTFoot(row, ltt);
1610 checkLongtableSpecial(ltt, value, flag);
1611 tabular.setLTFoot(row, flag, ltt, false);
1614 case LyXTabular::UNSET_LTLASTFOOT:
1616 case LyXTabular::SET_LTLASTFOOT:
1617 tabular.getRowOfLTLastFoot(row, ltt);
1618 checkLongtableSpecial(ltt, value, flag);
1619 tabular.setLTFoot(row, flag, ltt, true);
1622 case LyXTabular::SET_LTNEWPAGE:
1623 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1626 // dummy stuff just to avoid warnings
1627 case LyXTabular::LAST_ACTION:
1632 InsetTabularMailer(*this).updateDialog(bv);
1636 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1638 UpdatableInset & inset = tabular.getCellInset(actcell);
1641 x = inset.x() + inset.width();
1642 y = inset.descent();
1644 inset.edit(bv, x, y);
1645 bv->cursor().push(&inset);
1646 if (!the_locking_inset)
1649 return the_locking_inset;
1653 void InsetTabular::deleteLyXText(BufferView * /*bv*/) const
1655 #warning this is strange, isnt it? But this is 1.3.x code...
1656 //resizeLyXText(bv, recursive);
1660 bool InsetTabular::showInsetDialog(BufferView * bv) const
1662 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1663 InsetTabularMailer(*this).showDialog(bv);
1668 void InsetTabular::openLayoutDialog(BufferView * bv) const
1672 if (the_locking_inset) {
1673 InsetTabular * inset = static_cast<InsetTabular *>
1674 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
1676 inset->openLayoutDialog(bv);
1681 InsetTabularMailer(*this).showDialog(bv);
1686 // function returns an object as defined in func_status.h:
1687 // states OK, Unknown, Disabled, On, Off.
1689 FuncStatus InsetTabular::getStatus(string const & what) const
1691 int action = LyXTabular::LAST_ACTION;
1695 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1696 string const tmp = tabularFeature[i].feature;
1697 if (tmp == what.substr(0, tmp.length())) {
1698 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1699 // tabularFeatures[i].feature.length())) {
1700 action = tabularFeature[i].action;
1704 if (action == LyXTabular::LAST_ACTION) {
1706 status.unknown(true);
1710 string const argument
1711 = ltrim(what.substr(tabularFeature[i].feature.length()));
1716 LyXTabular::ltType dummyltt;
1720 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1722 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1725 case LyXTabular::SET_PWIDTH:
1726 case LyXTabular::SET_MPWIDTH:
1727 case LyXTabular::SET_SPECIAL_COLUMN:
1728 case LyXTabular::SET_SPECIAL_MULTI:
1729 case LyXTabular::APPEND_ROW:
1730 case LyXTabular::APPEND_COLUMN:
1731 case LyXTabular::DELETE_ROW:
1732 case LyXTabular::DELETE_COLUMN:
1733 case LyXTabular::SET_ALL_LINES:
1734 case LyXTabular::UNSET_ALL_LINES:
1738 case LyXTabular::MULTICOLUMN:
1739 status.setOnOff(tabular.isMultiColumn(actcell));
1742 case LyXTabular::M_TOGGLE_LINE_TOP:
1744 case LyXTabular::TOGGLE_LINE_TOP:
1745 status.setOnOff(tabular.topLine(actcell, flag));
1748 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1750 case LyXTabular::TOGGLE_LINE_BOTTOM:
1751 status.setOnOff(tabular.bottomLine(actcell, flag));
1754 case LyXTabular::M_TOGGLE_LINE_LEFT:
1756 case LyXTabular::TOGGLE_LINE_LEFT:
1757 status.setOnOff(tabular.leftLine(actcell, flag));
1760 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1762 case LyXTabular::TOGGLE_LINE_RIGHT:
1763 status.setOnOff(tabular.rightLine(actcell, flag));
1766 case LyXTabular::M_ALIGN_LEFT:
1768 case LyXTabular::ALIGN_LEFT:
1769 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1772 case LyXTabular::M_ALIGN_RIGHT:
1774 case LyXTabular::ALIGN_RIGHT:
1775 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1778 case LyXTabular::M_ALIGN_CENTER:
1780 case LyXTabular::ALIGN_CENTER:
1781 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1784 case LyXTabular::ALIGN_BLOCK:
1785 status.disabled(tabular.getPWidth(actcell).zero());
1786 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1789 case LyXTabular::M_VALIGN_TOP:
1791 case LyXTabular::VALIGN_TOP:
1792 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1795 case LyXTabular::M_VALIGN_BOTTOM:
1797 case LyXTabular::VALIGN_BOTTOM:
1798 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1801 case LyXTabular::M_VALIGN_MIDDLE:
1803 case LyXTabular::VALIGN_MIDDLE:
1804 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1807 case LyXTabular::SET_LONGTABULAR:
1808 status.setOnOff(tabular.isLongTabular());
1811 case LyXTabular::UNSET_LONGTABULAR:
1812 status.setOnOff(!tabular.isLongTabular());
1815 case LyXTabular::SET_ROTATE_TABULAR:
1816 status.setOnOff(tabular.getRotateTabular());
1819 case LyXTabular::UNSET_ROTATE_TABULAR:
1820 status.setOnOff(!tabular.getRotateTabular());
1823 case LyXTabular::SET_ROTATE_CELL:
1824 status.setOnOff(tabular.getRotateCell(actcell));
1827 case LyXTabular::UNSET_ROTATE_CELL:
1828 status.setOnOff(!tabular.getRotateCell(actcell));
1831 case LyXTabular::SET_USEBOX:
1832 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1835 case LyXTabular::SET_LTFIRSTHEAD:
1836 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1839 case LyXTabular::SET_LTHEAD:
1840 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1843 case LyXTabular::SET_LTFOOT:
1844 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1847 case LyXTabular::SET_LTLASTFOOT:
1848 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1851 case LyXTabular::SET_LTNEWPAGE:
1852 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1857 status.disabled(true);
1864 void InsetTabular::getLabelList(Buffer const & buffer,
1865 std::vector<string> & list) const
1867 tabular.getLabelList(buffer, list);
1871 bool InsetTabular::copySelection(BufferView * bv)
1873 if (!hasSelection())
1876 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1877 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1878 if (sel_col_start > sel_col_end) {
1879 sel_col_start = sel_col_end;
1880 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1882 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1885 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1886 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1887 if (sel_row_start > sel_row_end)
1888 swap(sel_row_start, sel_row_end);
1890 paste_tabular.reset(new LyXTabular(tabular));
1891 paste_tabular->setOwner(this);
1893 for (int i = 0; i < sel_row_start; ++i)
1894 paste_tabular->deleteRow(0);
1896 int const rows = sel_row_end - sel_row_start + 1;
1897 while (paste_tabular->rows() > rows)
1898 paste_tabular->deleteRow(rows);
1900 paste_tabular->setTopLine(0, true, true);
1901 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1904 for (int i = 0; i < sel_col_start; ++i)
1905 paste_tabular->deleteColumn(0);
1907 int const columns = sel_col_end - sel_col_start + 1;
1908 while (paste_tabular->columns() > columns)
1909 paste_tabular->deleteColumn(columns);
1911 paste_tabular->setLeftLine(0, true, true);
1912 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1916 OutputParams const runparams;
1917 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1918 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1919 bv->stuffClipboard(os.str());
1924 bool InsetTabular::pasteSelection(BufferView * bv)
1929 for (int r1 = 0, r2 = actrow;
1930 r1 < paste_tabular->rows() && r2 < tabular.rows();
1932 for (int c1 = 0, c2 = actcol;
1933 c1 < paste_tabular->columns() && c2 < tabular.columns();
1935 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1936 tabular.isPartOfMultiColumn(r2, c2))
1938 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1942 if (tabular.isPartOfMultiColumn(r2, c2)) {
1946 InsetText & inset = tabular.getCellInset(r2, c2);
1947 inset = paste_tabular->getCellInset(r1, c1);
1948 inset.setOwner(this);
1949 inset.deleteLyXText(bv);
1957 bool InsetTabular::cutSelection(BufferParams const & bp)
1959 if (!hasSelection())
1962 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1963 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1964 if (sel_col_start > sel_col_end) {
1965 sel_col_start = sel_col_end;
1966 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1968 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1971 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1972 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1974 if (sel_row_start > sel_row_end)
1975 swap(sel_row_start, sel_row_end);
1977 if (sel_cell_start > sel_cell_end)
1978 swap(sel_cell_start, sel_cell_end);
1980 for (int i = sel_row_start; i <= sel_row_end; ++i)
1981 for (int j = sel_col_start; j <= sel_col_end; ++j)
1982 tabular.getCellInset(tabular.getCellNumber(i, j))
1983 .clear(bp.tracking_changes);
1988 bool InsetTabular::isRightToLeft(BufferView * bv)
1990 return bv->getParentLanguage(this)->RightToLeft();
1994 int InsetTabular::scroll(bool recursive) const
1996 int sx = UpdatableInset::scroll(false);
1998 if (recursive && the_locking_inset)
1999 sx += the_locking_inset->scroll(recursive);
2005 void InsetTabular::getSelection(int & srow, int & erow,
2006 int & scol, int & ecol) const
2008 int const start = hasSelection() ? sel_cell_start : actcell;
2009 int const end = hasSelection() ? sel_cell_end : actcell;
2011 srow = tabular.row_of_cell(start);
2012 erow = tabular.row_of_cell(end);
2016 scol = tabular.column_of_cell(start);
2017 ecol = tabular.column_of_cell(end);
2021 ecol = tabular.right_column_of_cell(end);
2025 ParagraphList * InsetTabular::getParagraphs(int i) const
2027 return i < tabular.getNumberOfCells()
2028 ? tabular.getCellInset(i).getParagraphs(0)
2033 int InsetTabular::numParagraphs() const
2035 return tabular.getNumberOfCells();
2039 LyXText * InsetTabular::getText(int i) const
2041 return i < tabular.getNumberOfCells()
2042 ? tabular.getCellInset(i).getText(0)
2047 void InsetTabular::markErased()
2049 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2050 tabular.getCellInset(cell).markErased();
2054 bool InsetTabular::insetAllowed(InsetOld::Code) const
2060 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2062 const int cell = tabular.getCellFromInset(in);
2065 return tabular.getPWidth(cell).zero();
2067 // this is a workaround for a crash (New, Insert->Tabular,
2068 // Insert->FootNote)
2072 // well we didn't obviously find it so maybe our owner knows more
2073 BOOST_ASSERT(owner());
2074 return owner()->forceDefaultParagraphs(in);
2078 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2081 if (buf.length() <= 0)
2087 string::size_type len = buf.length();
2088 string::size_type p = 0;
2090 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2098 maxCols = max(cols, maxCols);
2104 maxCols = max(cols, maxCols);
2105 LyXTabular * loctab;
2110 paste_tabular.reset(
2111 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2114 paste_tabular->setOwner(this);
2115 loctab = paste_tabular.get();
2124 string::size_type op = 0;
2125 int cells = loctab->getNumberOfCells();
2128 rows = loctab->rows();
2129 int const columns = loctab->columns();
2131 while (cell < cells && p < len && row < rows &&
2132 (p = buf.find_first_of("\t\n", p)) != string::npos)
2138 // we can only set this if we are not too far right
2139 if (cols < columns) {
2140 InsetText & inset = loctab->getCellInset(cell);
2141 LyXFont const font = inset.text_.
2142 getFont(inset.paragraphs.begin(), 0);
2143 inset.setText(buf.substr(op, p - op), font);
2149 // we can only set this if we are not too far right
2150 if (cols < columns) {
2151 InsetText & inset = tabular.getCellInset(cell);
2152 LyXFont const font = inset.text_.
2153 getFont(inset.paragraphs.begin(), 0);
2154 inset.setText(buf.substr(op, p - op), font);
2159 cell = loctab->getCellNumber(row, cols);
2165 // check for the last cell if there is no trailing '\n'
2166 if (cell < cells && op < len) {
2167 InsetText & inset = loctab->getCellInset(cell);
2168 LyXFont const font = inset.text_.getFont(inset.paragraphs.begin(), 0);
2169 inset.setText(buf.substr(op, len - op), font);
2176 void InsetTabular::addPreview(PreviewLoader & loader) const
2178 int const rows = tabular.rows();
2179 int const columns = tabular.columns();
2180 for (int i = 0; i < rows; ++i) {
2181 for (int j = 0; j < columns; ++j)
2182 tabular.getCellInset(i, j).addPreview(loader);
2187 string const InsetTabularMailer::name_("tabular");
2189 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2190 : inset_(const_cast<InsetTabular &>(inset))
2194 string const InsetTabularMailer::inset2string(Buffer const &) const
2196 return params2string(inset_);
2200 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2202 istringstream data(in);
2204 lex.setStream(data);
2206 #warning CHECK verify that this is a sane value to return.
2212 string const token = lex.getString();
2220 string const token = lex.getString();
2221 if (token != "\\active_cell")
2224 cell = lex.getInteger();
2227 // This is part of the inset proper that is usually swallowed
2228 // by Buffer::readInset
2231 string const token = lex.getString();
2232 if (token != "Tabular")
2239 Buffer const & buffer = inset.buffer();
2240 inset.read(buffer, lex);
2242 // We can't set the active cell, but we can tell the frontend
2248 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2251 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2252 inset.write(inset.buffer(), data);
2253 data << "\\end_inset\n";