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, "" }
125 FindFeature(LyXTabular::Feature feature)
129 bool operator()(TabularFeature & tf)
131 return tf.action == feature_;
135 LyXTabular::Feature feature_;
141 string const featureAsString(LyXTabular::Feature feature)
143 TabularFeature * it = tabularFeature;
144 TabularFeature * end = it +
145 sizeof(tabularFeature) / sizeof(TabularFeature);
146 it = std::find_if(it, end, FindFeature(feature));
147 return (it == end) ? string() : it->feature;
151 bool InsetTabular::hasPasteBuffer() const
153 return (paste_tabular.get() != 0);
157 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
158 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
159 buffer_(&buf), cursorx_(0), cursory_(0)
161 tabular.setOwner(this);
162 the_locking_inset = 0;
170 InsetTabular::InsetTabular(InsetTabular const & tab)
171 : UpdatableInset(tab), tabular(tab.tabular),
172 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
174 tabular.setOwner(this);
175 the_locking_inset = 0;
183 InsetTabular::~InsetTabular()
185 InsetTabularMailer(*this).hideDialog();
189 auto_ptr<InsetBase> InsetTabular::clone() const
191 return auto_ptr<InsetBase>(new InsetTabular(*this));
195 Buffer const & InsetTabular::buffer() const
201 void InsetTabular::buffer(Buffer * b)
207 void InsetTabular::write(Buffer const & buf, ostream & os) const
209 os << "Tabular" << endl;
210 tabular.write(buf, os);
214 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
216 bool const old_format = (lex.getString() == "\\LyXTable");
218 tabular.read(buf, lex);
224 string token = lex.getString();
225 while (lex.isOK() && (token != "\\end_inset")) {
227 token = lex.getString();
229 if (token != "\\end_inset") {
230 lex.printError("Missing \\end_inset at this point. "
236 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
238 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
239 // mi.base.textwidth << "\n";
241 lyxerr << "InsetTabular::metrics: need bv" << endl;
245 calculate_dimensions_of_cells(mi);
247 dim.asc = tabular.getAscentOfRow(0);
248 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
249 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
254 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
256 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
258 BufferView * bv = pi.base.bv;
265 x += ADD_TO_TABULAR_WIDTH;
268 first_visible_cell = -1;
269 for (int i = 0; i < tabular.rows(); ++i) {
271 cell = tabular.getCellNumber(i, 0);
272 if (y + tabular.getDescentOfRow(i) <= 0 &&
273 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
275 y += tabular.getDescentOfRow(i) +
276 tabular.getAscentOfRow(i + 1) +
277 tabular.getAdditionalHeight(i + 1);
280 for (int j = 0; j < tabular.columns(); ++j) {
281 if (nx > bv->workWidth())
283 if (tabular.isPartOfMultiColumn(i, j))
285 int cx = nx + tabular.getBeginningOfTextInCell(cell);
286 if (first_visible_cell < 0)
287 first_visible_cell = cell;
288 if (hasSelection()) {
289 drawCellSelection(pi.pain, nx, y, i, j, cell);
292 tabular.getCellInset(cell).draw(pi, cx, y);
293 drawCellLines(pi.pain, nx, y, i, cell);
294 nx += tabular.getWidthOfColumn(cell);
298 // Would be nice, but for some completely unfathomable reason,
299 // on a col resize to a new fixed width, even though the insettexts
300 // are resized, the cell isn't, but drawing all cells in a tall table
301 // has the desired effect somehow. Complete dark magic.
303 // avoiding drawing the rest of a long table is
304 // a pretty big speedup
305 if (y > bv->workHeight())
309 y += tabular.getDescentOfRow(i) +
310 tabular.getAscentOfRow(i + 1) +
311 tabular.getAdditionalHeight(i + 1);
316 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
317 int row, int cell) const
319 int x2 = x + tabular.getWidthOfColumn(cell);
322 if (!tabular.topAlreadyDrawn(cell)) {
323 on_off = !tabular.topLine(cell);
324 pain.line(x, y - tabular.getAscentOfRow(row),
325 x2, y - tabular.getAscentOfRow(row),
326 on_off ? LColor::tabularonoffline : LColor::tabularline,
327 on_off ? Painter::line_onoffdash : Painter::line_solid);
329 on_off = !tabular.bottomLine(cell);
330 pain.line(x, y + tabular.getDescentOfRow(row),
331 x2, y + tabular.getDescentOfRow(row),
332 on_off ? LColor::tabularonoffline : LColor::tabularline,
333 on_off ? Painter::line_onoffdash : Painter::line_solid);
334 if (!tabular.leftAlreadyDrawn(cell)) {
335 on_off = !tabular.leftLine(cell);
336 pain.line(x, y - tabular.getAscentOfRow(row),
337 x, y + tabular.getDescentOfRow(row),
338 on_off ? LColor::tabularonoffline : LColor::tabularline,
339 on_off ? Painter::line_onoffdash : Painter::line_solid);
341 on_off = !tabular.rightLine(cell);
342 pain.line(x2 - tabular.getAdditionalWidth(cell),
343 y - tabular.getAscentOfRow(row),
344 x2 - tabular.getAdditionalWidth(cell),
345 y + tabular.getDescentOfRow(row),
346 on_off ? LColor::tabularonoffline : LColor::tabularline,
347 on_off ? Painter::line_onoffdash : Painter::line_solid);
351 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
352 int row, int column, int cell) const
354 BOOST_ASSERT(hasSelection());
355 int cs = tabular.column_of_cell(sel_cell_start);
356 int ce = tabular.column_of_cell(sel_cell_end);
359 cs = tabular.column_of_cell(sel_cell_end);
361 ce = tabular.right_column_of_cell(sel_cell_end);
364 int rs = tabular.row_of_cell(sel_cell_start);
365 int re = tabular.row_of_cell(sel_cell_end);
369 if (column >= cs && column <= ce && row >= rs && row <= re) {
370 int w = tabular.getWidthOfColumn(cell);
371 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
372 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
373 w, h, LColor::selection);
378 string const InsetTabular::editMessage() const
380 return _("Opened table");
384 void InsetTabular::updateLocal(BufferView * bv) const
391 int InsetTabular::insetInInsetY() const
393 if (the_locking_inset)
394 return cursory_ + the_locking_inset->insetInInsetY();
399 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
401 return the_locking_inset && the_locking_inset->insertInset(bv, inset);
405 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
407 if (hasSelection() && cmd.button() == mouse_button::button3)
413 BufferView * bv = cmd.view();
415 the_locking_inset = 0;
416 setPos(bv, cmd.x, cmd.y);
418 the_locking_inset = 0;
420 if (cmd.button() == mouse_button::button2)
421 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
425 void InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
427 if (cmd.button() == mouse_button::button3)
428 InsetTabularMailer(*this).showDialog(cmd.view());
432 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
434 BufferView * bv = cmd.view();
435 int const old_cell = actcell;
437 setPos(bv, cmd.x, cmd.y);
439 setSelection(actcell, actcell);
440 else if (old_cell != actcell)
441 setSelection(sel_cell_start, actcell);
445 void InsetTabular::edit(BufferView * bv, bool left)
447 lyxerr << "InsetTabular::edit: " << this
448 << " first text: " << tabular.cell_info[0][0].inset.getText(0)
449 << " first cell: " << &tabular.cell_info[0][0].inset << endl;
452 the_locking_inset = 0;
455 if (isRightToLeft(bv))
456 actcell = tabular.getLastCellInRow(0);
460 if (isRightToLeft(bv))
461 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
463 actcell = tabular.getNumberOfCells() - 1;
468 bv->cursor().push(this);
472 void InsetTabular::edit(BufferView * bv, int x, int y)
474 lyxerr << "InsetTabular::edit: " << this << " first cell "
475 << &tabular.cell_info[0][0].inset << endl;
478 the_locking_inset = 0;
482 int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
483 bv->cursor().push(this);
485 activateCellInset(bv, x - xx, y - cursory_);
490 InsetTabular::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
492 lyxerr << "InsetTabular::dispatch: " << cmd << endl;
493 // We need to save the value of the_locking_inset as the call to
494 // the_locking_inset->localDispatch might unlock it.
495 DispatchResult result(true, true);
496 BufferView * bv = cmd.view();
497 bool hs = hasSelection();
499 switch (cmd.action) {
501 case LFUN_MOUSE_PRESS:
503 return DispatchResult(true, true);
505 case LFUN_MOUSE_MOTION:
506 lfunMouseMotion(cmd);
507 return DispatchResult(true, true);
509 case LFUN_MOUSE_RELEASE:
510 lfunMouseRelease(cmd);
511 return DispatchResult(true, true);
513 case LFUN_CELL_BACKWARD:
514 case LFUN_CELL_FORWARD:
515 if (cmd.action == LFUN_CELL_FORWARD)
516 moveNextCell(bv, the_locking_inset != 0);
518 movePrevCell(bv, the_locking_inset != 0);
520 if (!the_locking_inset)
521 return DispatchResult(true);
524 case LFUN_SCROLL_INSET:
525 if (!cmd.argument.empty()) {
526 if (cmd.argument.find('.') != cmd.argument.npos)
527 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
529 scroll(cmd.view(), strToInt(cmd.argument));
530 cmd.view()->update();
531 return DispatchResult(true, true);
534 case LFUN_RIGHTSEL: {
535 int const start = hasSelection() ? sel_cell_start : actcell;
536 if (tabular.isLastCellInRow(actcell)) {
537 setSelection(start, actcell);
542 // if we are starting a selection, only select
543 // the current cell at the beginning
544 if (hasSelection()) {
545 moveRight(bv, false);
548 setSelection(start, end);
553 result = moveRight(bv, true);
558 int const start = hasSelection() ? sel_cell_start : actcell;
559 if (tabular.isFirstCellInRow(actcell)) {
560 setSelection(start, actcell);
565 // if we are starting a selection, only select
566 // the current cell at the beginning
567 if (hasSelection()) {
571 setSelection(start, end);
576 result = moveLeft(bv, true);
581 int const start = hasSelection() ? sel_cell_start : actcell;
582 int const ocell = actcell;
583 // if we are starting a selection, only select
584 // the current cell at the beginning
585 if (hasSelection()) {
587 if (ocell == sel_cell_end ||
588 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
589 setSelection(start, tabular.getCellBelow(sel_cell_end));
591 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
593 setSelection(start, start);
599 result = moveDown(bv, the_locking_inset != 0);
604 int const start = hasSelection() ? sel_cell_start : actcell;
605 int const ocell = actcell;
606 // if we are starting a selection, only select
607 // the current cell at the beginning
608 if (hasSelection()) {
610 if (ocell == sel_cell_end ||
611 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
612 setSelection(start, tabular.getCellAbove(sel_cell_end));
614 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
616 setSelection(start, start);
622 result = moveUp(bv, the_locking_inset != 0);
630 if (bv->top_y() + bv->painter().paperHeight()
631 < yo_ + tabular.getHeightOfTabular())
633 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
634 actcell = tabular.getCellBelow(first_visible_cell) + column;
636 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
647 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
651 actcell = tabular.getCellBelow(first_visible_cell) + column;
659 // none of these make sense for insettabular,
660 // but we must catch them to prevent any
661 // selection from being confused
665 case LFUN_WORDLEFTSEL:
667 case LFUN_WORDRIGHTSEL:
669 case LFUN_DOWN_PARAGRAPH:
670 case LFUN_DOWN_PARAGRAPHSEL:
671 case LFUN_UP_PARAGRAPH:
672 case LFUN_UP_PARAGRAPHSEL:
678 case LFUN_BEGINNINGBUF:
679 case LFUN_BEGINNINGBUFSEL:
684 case LFUN_LAYOUT_TABULAR:
685 InsetTabularMailer(*this).showDialog(bv);
688 case LFUN_INSET_DIALOG_UPDATE:
689 InsetTabularMailer(*this).updateDialog(bv);
692 case LFUN_TABULAR_FEATURE:
693 if (!tabularFeatures(bv, cmd.argument))
694 result = DispatchResult(false);
697 // insert file functions
698 case LFUN_FILE_INSERT_ASCII_PARA:
699 case LFUN_FILE_INSERT_ASCII: {
700 string tmpstr = getContentsOfAsciiFile(bv, cmd.argument, false);
701 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
702 result = DispatchResult(false);
716 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
719 case LFUN_FINISHED_LEFT:
720 lyxerr << "swallow LFUN_FINISHED_LEFT, act: " << actcell << endl;
721 if (!movePrevCell(bv, false))
722 result = DispatchResult(FINISHED);
725 case LFUN_FINISHED_RIGHT:
726 lyxerr << "swallow LFUN_FINISHED_RIGHT, act: " << actcell << endl;
727 if (!moveNextCell(bv, false))
728 result = DispatchResult(FINISHED_RIGHT);
731 case LFUN_FINISHED_UP:
732 lyxerr << "swallow LFUN_FINISHED_UP, act: " << actcell << endl;
733 result = moveUp(bv, true);
736 case LFUN_FINISHED_DOWN:
737 lyxerr << "swallow LFUN_FINISHED_DOWN, act: " << actcell << endl;
738 result = moveDown(bv, true);
742 if (copySelection(bv)) {
743 recordUndo(bv, Undo::DELETE);
744 cutSelection(bv->buffer()->params());
749 recordUndo(bv, Undo::DELETE);
750 cutSelection(bv->buffer()->params());
760 case LFUN_PASTESELECTION: {
761 string const clip = bv->getClipboard();
765 if (clip.find('\t') != string::npos) {
769 string::size_type len = clip.length();
770 string::size_type p = 0;
773 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
781 maxCols = max(cols, maxCols);
787 maxCols = max(cols, maxCols);
790 new LyXTabular(bv->buffer()->params(),
794 string::size_type op = 0;
796 int cells = paste_tabular->getNumberOfCells();
799 while (cell < cells && p < len &&
800 (p = clip.find_first_of("\t\n", p)) != string::npos) {
805 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
810 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
811 while (cols++ < maxCols)
819 // check for the last cell if there is no trailing '\n'
820 if (cell < cells && op < len)
821 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
824 if (!insertAsciiString(bv, clip, true))
827 // so that the clipboard is used and it goes on
829 // and executes LFUN_PASTESELECTION in insettext!
830 paste_tabular.reset();
836 if (hasPasteBuffer()) {
837 recordUndo(bv, Undo::INSERT);
843 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
846 // handle font changing stuff on selection before we lock the inset
847 // in the default part!
848 result = DispatchResult(false);
849 // we try to activate the actual inset and put this event down to
850 // the insets dispatch function.
851 if (!the_locking_inset && activateCellInset(bv)) {
852 result = the_locking_inset->dispatch(cmd);
853 if (!result.dispatched()) {
854 // we need to update if this was requested before
855 result = DispatchResult(false);
865 InsetTabularMailer(*this).updateDialog(bv);
870 int InsetTabular::latex(Buffer const & buf, ostream & os,
871 OutputParams const & runparams) const
873 return tabular.latex(buf, os, runparams);
877 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
878 OutputParams const & runparams) const
880 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
881 return tabular.plaintext(buf, os, runparams, dp, false, 0);
885 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
886 OutputParams const & runparams) const
888 return tabular.linuxdoc(buf,os, runparams);
892 int InsetTabular::docbook(Buffer const & buf, ostream & os,
893 OutputParams const & runparams) const
898 // if the table is inside a float it doesn't need the informaltable
899 // wrapper. Search for it.
900 for (master = owner();
901 master && master->lyxCode() != InsetOld::FLOAT_CODE;
902 master = master->owner());
905 os << "<informaltable>";
906 if (runparams.mixed_content)
910 ret += tabular.docbook(buf, os, runparams);
912 os << "</informaltable>";
913 if (runparams.mixed_content)
921 void InsetTabular::validate(LaTeXFeatures & features) const
923 tabular.validate(features);
927 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
930 // if we have a locking_inset we should have to check only this cell for
931 // change so I'll try this to have a boost, but who knows ;) (Jug?)
932 // This is _really_ important (André)
933 if (the_locking_inset == &tabular.getCellInset(actcell)) {
936 for (int j = 0; j < tabular.columns(); ++j) {
940 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
941 tabular.getCellInset(actrow, j).metrics(m, dim);
942 maxAsc = max(dim.asc, maxAsc);
943 maxDesc = max(dim.des, maxDesc);
945 tabular.setWidthOfCell(actcell, the_locking_inset->width());
946 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
947 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
953 for (int i = 0; i < tabular.rows(); ++i) {
956 for (int j = 0; j < tabular.columns(); ++j) {
957 if (tabular.isPartOfMultiColumn(i, j))
963 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
964 tabular.getCellInset(cell).metrics(m, dim);
965 maxAsc = max(maxAsc, dim.asc);
966 maxDesc = max(maxDesc, dim.des);
967 tabular.setWidthOfCell(cell, dim.wid);
969 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
970 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
975 void InsetTabular::getCursorPos(BufferView *, int & x, int & y) const
977 x = TEXT_TO_INSET_OFFSET + cursorx_ - xo_;
978 y = TEXT_TO_INSET_OFFSET + cursory_;
982 void InsetTabular::setPos(BufferView * bv, int x, int y) const
988 int ly = tabular.getDescentOfRow(actrow);
990 // first search the right row
991 while (ly < y && actrow + 1 < tabular.rows()) {
992 cursory_ += tabular.getDescentOfRow(actrow) +
993 tabular.getAscentOfRow(actrow + 1) +
994 tabular.getAdditionalHeight(actrow + 1);
996 ly = cursory_ + tabular.getDescentOfRow(actrow);
998 actcell = tabular.getCellNumber(actrow, actcol);
1000 // now search the right column
1001 int lx = tabular.getWidthOfColumn(actcell) -
1002 tabular.getAdditionalWidth(actcell);
1004 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1005 lx += tabular.getWidthOfColumn(actcell + 1)
1006 + tabular.getAdditionalWidth(actcell);
1008 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1013 int InsetTabular::getCellXPos(int cell) const
1017 for (; !tabular.isFirstCellInRow(c); --c)
1019 int lx = tabular.getWidthOfColumn(cell);
1020 for (; c < cell; ++c)
1021 lx += tabular.getWidthOfColumn(c);
1023 return lx - tabular.getWidthOfColumn(cell) + xo_;
1027 void InsetTabular::resetPos(BufferView * bv) const
1029 #ifdef WITH_WARNINGS
1030 #warning This should be fixed in the right manner (20011128 Jug)
1032 // fast hack to fix infinite repaintings!
1033 if (in_reset_pos > 0)
1037 actcol = tabular.column_of_cell(actcell);
1040 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1041 if (tabular.isLastCellInRow(cell)) {
1042 cursory_ += tabular.getDescentOfRow(actrow) +
1043 tabular.getAscentOfRow(actrow + 1) +
1044 tabular.getAdditionalHeight(actrow + 1);
1049 // we need this only from here on!!!
1051 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1052 int new_x = getCellXPos(actcell) + offset;
1053 int old_x = cursorx_;
1055 // cursor.x(getCellXPos(actcell) + offset);
1056 if (actcol < tabular.columns() - 1 && scroll(false) &&
1057 tabular.getWidthOfTabular() < bv->workWidth()-20)
1061 } else if (the_locking_inset &&
1062 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1064 int xx = cursorx_ - offset;
1065 if (xx > bv->workWidth()-20) {
1066 scroll(bv, - xx + bv->workWidth() - 60);
1068 } else if (xx < 20) {
1076 } else if (cursorx_ - offset > 20 &&
1077 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1078 > bv->workWidth() - 20) {
1079 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1081 } else if (cursorx_ - offset < 20) {
1082 scroll(bv, 20 - cursorx_ + offset);
1084 } else if (scroll() && xo_ > 20 &&
1085 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1086 scroll(bv, old_x - cursorx_);
1089 InsetTabularMailer(*this).updateDialog(bv);
1094 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1097 if (activateCellInset(bv))
1098 return DispatchResult(true, true);
1100 bool moved = isRightToLeft(bv)
1101 ? movePrevCell(bv) : moveNextCell(bv);
1103 return DispatchResult(false, FINISHED_RIGHT);
1104 if (lock && activateCellInset(bv))
1105 return DispatchResult(true, true);
1108 return DispatchResult(true);
1112 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1114 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1116 return DispatchResult(false, FINISHED);
1118 if (lock && activateCellInset(bv, 0, 0, true))
1119 return DispatchResult(true, true);
1121 return DispatchResult(true);
1125 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1127 int const ocell = actcell;
1128 actcell = tabular.getCellAbove(actcell);
1129 if (actcell == ocell) { // we moved out of the inset
1130 return DispatchResult(false, FINISHED_UP);
1134 activateCellInset(bv, bv->x_target(), 0);
1135 return DispatchResult(true, true);
1139 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1141 int const ocell = actcell;
1142 actcell = tabular.getCellBelow(actcell);
1143 if (actcell == ocell) { // we moved out of the inset
1144 return DispatchResult(false, FINISHED_DOWN);
1148 activateCellInset(bv, bv->x_target(), 0);
1149 return DispatchResult(true, true);
1153 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1155 if (isRightToLeft(bv)) {
1156 if (tabular.isFirstCellInRow(actcell)) {
1157 int row = tabular.row_of_cell(actcell);
1158 if (row == tabular.rows() - 1)
1160 actcell = tabular.getLastCellInRow(row);
1161 actcell = tabular.getCellBelow(actcell);
1168 if (tabular.isLastCell(actcell))
1173 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1174 isRightToLeftPar(bv->buffer()->params());
1175 activateCellInset(bv, 0, 0, !rtl);
1182 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1184 lyxerr << "move prevcell 1" << endl;
1185 if (isRightToLeft(bv)) {
1186 lyxerr << "move prevcell a" << endl;
1187 if (tabular.isLastCellInRow(actcell)) {
1188 int row = tabular.row_of_cell(actcell);
1191 actcell = tabular.getFirstCellInRow(row);
1192 actcell = tabular.getCellAbove(actcell);
1194 if (tabular.isLastCell(actcell))
1199 lyxerr << "move prevcell b" << endl;
1200 if (actcell == 0) // first cell
1204 lyxerr << "move prevcell 2" << endl;
1206 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1207 isRightToLeftPar(bv->buffer()->params());
1208 activateCellInset(bv, 0, 0, !rtl);
1210 lyxerr << "move prevcell 3" << endl;
1212 lyxerr << "move prevcell 4" << endl;
1217 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1221 setSelection(0, tabular.getNumberOfCells() - 1);
1223 if (hasSelection()) {
1224 recordUndo(bv, Undo::ATOMIC);
1225 bool const frozen = undo_frozen;
1228 // apply the fontchange on the whole selection
1233 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1234 for (int i = sel_row_start; i <= sel_row_end; ++i)
1235 for (int j = sel_col_start; j <= sel_col_end; ++j)
1236 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1244 if (the_locking_inset)
1245 the_locking_inset->setFont(bv, font, tall);
1249 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1251 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1254 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1255 string const tmp = tabularFeature[i].feature;
1257 if (tmp == what.substr(0, tmp.length())) {
1258 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1259 //tabularFeatures[i].feature.length())) {
1260 action = tabularFeature[i].action;
1264 if (action == LyXTabular::LAST_ACTION)
1268 ltrim(what.substr(tabularFeature[i].feature.length()));
1269 tabularFeatures(bv, action, val);
1275 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1276 string const & special, bool & flag)
1278 if (special == "dl_above") {
1281 } else if (special == "dl_below") {
1282 ltt.bottomDL = flag;
1284 } else if (special == "empty") {
1296 void InsetTabular::tabularFeatures(BufferView * bv,
1297 LyXTabular::Feature feature, string const & value)
1303 bool setLines = false;
1304 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1305 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1309 case LyXTabular::M_ALIGN_LEFT:
1310 case LyXTabular::ALIGN_LEFT:
1311 setAlign = LYX_ALIGN_LEFT;
1314 case LyXTabular::M_ALIGN_RIGHT:
1315 case LyXTabular::ALIGN_RIGHT:
1316 setAlign = LYX_ALIGN_RIGHT;
1319 case LyXTabular::M_ALIGN_CENTER:
1320 case LyXTabular::ALIGN_CENTER:
1321 setAlign = LYX_ALIGN_CENTER;
1324 case LyXTabular::ALIGN_BLOCK:
1325 setAlign = LYX_ALIGN_BLOCK;
1328 case LyXTabular::M_VALIGN_TOP:
1329 case LyXTabular::VALIGN_TOP:
1330 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1333 case LyXTabular::M_VALIGN_BOTTOM:
1334 case LyXTabular::VALIGN_BOTTOM:
1335 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1338 case LyXTabular::M_VALIGN_MIDDLE:
1339 case LyXTabular::VALIGN_MIDDLE:
1340 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1347 if (hasSelection()) {
1348 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1350 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1351 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1353 recordUndo(bv, Undo::ATOMIC);
1355 int row = tabular.row_of_cell(actcell);
1356 int column = tabular.column_of_cell(actcell);
1358 LyXTabular::ltType ltt;
1362 case LyXTabular::SET_PWIDTH: {
1363 LyXLength const len(value);
1364 tabular.setColumnPWidth(actcell, len);
1366 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1367 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1368 else if (!len.zero()
1369 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1370 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1374 case LyXTabular::SET_MPWIDTH:
1375 tabular.setMColumnPWidth(actcell, LyXLength(value));
1378 case LyXTabular::SET_SPECIAL_COLUMN:
1379 case LyXTabular::SET_SPECIAL_MULTI:
1380 tabular.setAlignSpecial(actcell,value,feature);
1383 case LyXTabular::APPEND_ROW:
1384 // append the row into the tabular
1385 tabular.appendRow(bv->buffer()->params(), actcell);
1386 tabular.setOwner(this);
1389 case LyXTabular::APPEND_COLUMN:
1390 // append the column into the tabular
1391 tabular.appendColumn(bv->buffer()->params(), actcell);
1392 tabular.setOwner(this);
1393 actcell = tabular.getCellNumber(row, column);
1396 case LyXTabular::DELETE_ROW:
1397 for (int i = sel_row_start; i <= sel_row_end; ++i)
1398 tabular.deleteRow(sel_row_start);
1399 if (sel_row_start >= tabular.rows())
1401 actcell = tabular.getCellNumber(sel_row_start, column);
1405 case LyXTabular::DELETE_COLUMN:
1406 for (int i = sel_col_start; i <= sel_col_end; ++i)
1407 tabular.deleteColumn(sel_col_start);
1408 if (sel_col_start >= tabular.columns())
1410 actcell = tabular.getCellNumber(row, sel_col_start);
1414 case LyXTabular::M_TOGGLE_LINE_TOP:
1416 case LyXTabular::TOGGLE_LINE_TOP: {
1417 bool lineSet = !tabular.topLine(actcell, flag);
1418 for (int i = sel_row_start; i <= sel_row_end; ++i)
1419 for (int j = sel_col_start; j <= sel_col_end; ++j)
1421 tabular.getCellNumber(i, j),
1426 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1428 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1429 bool lineSet = !tabular.bottomLine(actcell, flag);
1430 for (int i = sel_row_start; i <= sel_row_end; ++i)
1431 for (int j = sel_col_start; j <= sel_col_end; ++j)
1432 tabular.setBottomLine(
1433 tabular.getCellNumber(i, j),
1439 case LyXTabular::M_TOGGLE_LINE_LEFT:
1441 case LyXTabular::TOGGLE_LINE_LEFT: {
1442 bool lineSet = !tabular.leftLine(actcell, flag);
1443 for (int i = sel_row_start; i <= sel_row_end; ++i)
1444 for (int j = sel_col_start; j <= sel_col_end; ++j)
1445 tabular.setLeftLine(
1446 tabular.getCellNumber(i,j),
1452 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1454 case LyXTabular::TOGGLE_LINE_RIGHT: {
1455 bool lineSet = !tabular.rightLine(actcell, flag);
1456 for (int i = sel_row_start; i <= sel_row_end; ++i)
1457 for (int j = sel_col_start; j <= sel_col_end; ++j)
1458 tabular.setRightLine(
1459 tabular.getCellNumber(i,j),
1465 case LyXTabular::M_ALIGN_LEFT:
1466 case LyXTabular::M_ALIGN_RIGHT:
1467 case LyXTabular::M_ALIGN_CENTER:
1469 case LyXTabular::ALIGN_LEFT:
1470 case LyXTabular::ALIGN_RIGHT:
1471 case LyXTabular::ALIGN_CENTER:
1472 case LyXTabular::ALIGN_BLOCK:
1473 for (int i = sel_row_start; i <= sel_row_end; ++i)
1474 for (int j = sel_col_start; j <= sel_col_end; ++j)
1475 tabular.setAlignment(
1476 tabular.getCellNumber(i, j),
1481 case LyXTabular::M_VALIGN_TOP:
1482 case LyXTabular::M_VALIGN_BOTTOM:
1483 case LyXTabular::M_VALIGN_MIDDLE:
1485 case LyXTabular::VALIGN_TOP:
1486 case LyXTabular::VALIGN_BOTTOM:
1487 case LyXTabular::VALIGN_MIDDLE:
1488 for (int i = sel_row_start; i <= sel_row_end; ++i)
1489 for (int j = sel_col_start; j <= sel_col_end; ++j)
1490 tabular.setVAlignment(
1491 tabular.getCellNumber(i, j),
1495 case LyXTabular::MULTICOLUMN: {
1496 if (sel_row_start != sel_row_end) {
1497 #ifdef WITH_WARNINGS
1498 #warning Need I say it ? This is horrible.
1500 Alert::error(_("Error setting multicolumn"),
1501 _("You cannot set multicolumn vertically."));
1504 // just multicol for one Single Cell
1505 if (!hasSelection()) {
1506 // check wether we are completly in a multicol
1507 if (tabular.isMultiColumn(actcell))
1508 tabular.unsetMultiColumn(actcell);
1510 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1513 // we have a selection so this means we just add all this
1514 // cells to form a multicolumn cell
1518 if (sel_cell_start > sel_cell_end) {
1519 s_start = sel_cell_end;
1520 s_end = sel_cell_start;
1522 s_start = sel_cell_start;
1523 s_end = sel_cell_end;
1525 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1531 case LyXTabular::SET_ALL_LINES:
1533 case LyXTabular::UNSET_ALL_LINES:
1534 for (int i = sel_row_start; i <= sel_row_end; ++i)
1535 for (int j = sel_col_start; j <= sel_col_end; ++j)
1536 tabular.setAllLines(
1537 tabular.getCellNumber(i,j), setLines);
1540 case LyXTabular::SET_LONGTABULAR:
1541 tabular.setLongTabular(true);
1544 case LyXTabular::UNSET_LONGTABULAR:
1545 tabular.setLongTabular(false);
1548 case LyXTabular::SET_ROTATE_TABULAR:
1549 tabular.setRotateTabular(true);
1552 case LyXTabular::UNSET_ROTATE_TABULAR:
1553 tabular.setRotateTabular(false);
1556 case LyXTabular::SET_ROTATE_CELL:
1557 for (int i = sel_row_start; i <= sel_row_end; ++i)
1558 for (int j = sel_col_start; j <= sel_col_end; ++j)
1559 tabular.setRotateCell(
1560 tabular.getCellNumber(i, j), true);
1563 case LyXTabular::UNSET_ROTATE_CELL:
1564 for (int i = sel_row_start; i <= sel_row_end; ++i)
1565 for (int j = sel_col_start; j <= sel_col_end; ++j)
1566 tabular.setRotateCell(
1567 tabular.getCellNumber(i, j), false);
1570 case LyXTabular::SET_USEBOX: {
1571 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1572 if (val == tabular.getUsebox(actcell))
1573 val = LyXTabular::BOX_NONE;
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.setUsebox(tabular.getCellNumber(i, j), val);
1580 case LyXTabular::UNSET_LTFIRSTHEAD:
1582 case LyXTabular::SET_LTFIRSTHEAD:
1583 tabular.getRowOfLTFirstHead(row, ltt);
1584 checkLongtableSpecial(ltt, value, flag);
1585 tabular.setLTHead(row, flag, ltt, true);
1588 case LyXTabular::UNSET_LTHEAD:
1590 case LyXTabular::SET_LTHEAD:
1591 tabular.getRowOfLTHead(row, ltt);
1592 checkLongtableSpecial(ltt, value, flag);
1593 tabular.setLTHead(row, flag, ltt, false);
1596 case LyXTabular::UNSET_LTFOOT:
1598 case LyXTabular::SET_LTFOOT:
1599 tabular.getRowOfLTFoot(row, ltt);
1600 checkLongtableSpecial(ltt, value, flag);
1601 tabular.setLTFoot(row, flag, ltt, false);
1604 case LyXTabular::UNSET_LTLASTFOOT:
1606 case LyXTabular::SET_LTLASTFOOT:
1607 tabular.getRowOfLTLastFoot(row, ltt);
1608 checkLongtableSpecial(ltt, value, flag);
1609 tabular.setLTFoot(row, flag, ltt, true);
1612 case LyXTabular::SET_LTNEWPAGE:
1613 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1616 // dummy stuff just to avoid warnings
1617 case LyXTabular::LAST_ACTION:
1622 InsetTabularMailer(*this).updateDialog(bv);
1626 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1628 UpdatableInset & inset = tabular.getCellInset(actcell);
1631 x = inset.x() + inset.width();
1632 y = inset.descent();
1634 inset.edit(bv, x, y);
1635 bv->cursor().push(&inset);
1636 if (!the_locking_inset)
1639 return the_locking_inset;
1643 bool InsetTabular::showInsetDialog(BufferView * bv) const
1645 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1646 InsetTabularMailer(*this).showDialog(bv);
1651 void InsetTabular::openLayoutDialog(BufferView * bv) const
1655 if (the_locking_inset) {
1656 InsetTabular * inset = static_cast<InsetTabular *>
1657 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
1659 inset->openLayoutDialog(bv);
1664 InsetTabularMailer(*this).showDialog(bv);
1669 // function returns an object as defined in func_status.h:
1670 // states OK, Unknown, Disabled, On, Off.
1672 FuncStatus InsetTabular::getStatus(string const & what) const
1674 int action = LyXTabular::LAST_ACTION;
1678 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1679 string const tmp = tabularFeature[i].feature;
1680 if (tmp == what.substr(0, tmp.length())) {
1681 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1682 // tabularFeatures[i].feature.length())) {
1683 action = tabularFeature[i].action;
1687 if (action == LyXTabular::LAST_ACTION) {
1689 status.unknown(true);
1693 string const argument
1694 = ltrim(what.substr(tabularFeature[i].feature.length()));
1699 LyXTabular::ltType dummyltt;
1703 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1705 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1708 case LyXTabular::SET_PWIDTH:
1709 case LyXTabular::SET_MPWIDTH:
1710 case LyXTabular::SET_SPECIAL_COLUMN:
1711 case LyXTabular::SET_SPECIAL_MULTI:
1712 case LyXTabular::APPEND_ROW:
1713 case LyXTabular::APPEND_COLUMN:
1714 case LyXTabular::DELETE_ROW:
1715 case LyXTabular::DELETE_COLUMN:
1716 case LyXTabular::SET_ALL_LINES:
1717 case LyXTabular::UNSET_ALL_LINES:
1721 case LyXTabular::MULTICOLUMN:
1722 status.setOnOff(tabular.isMultiColumn(actcell));
1725 case LyXTabular::M_TOGGLE_LINE_TOP:
1727 case LyXTabular::TOGGLE_LINE_TOP:
1728 status.setOnOff(tabular.topLine(actcell, flag));
1731 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1733 case LyXTabular::TOGGLE_LINE_BOTTOM:
1734 status.setOnOff(tabular.bottomLine(actcell, flag));
1737 case LyXTabular::M_TOGGLE_LINE_LEFT:
1739 case LyXTabular::TOGGLE_LINE_LEFT:
1740 status.setOnOff(tabular.leftLine(actcell, flag));
1743 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1745 case LyXTabular::TOGGLE_LINE_RIGHT:
1746 status.setOnOff(tabular.rightLine(actcell, flag));
1749 case LyXTabular::M_ALIGN_LEFT:
1751 case LyXTabular::ALIGN_LEFT:
1752 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1755 case LyXTabular::M_ALIGN_RIGHT:
1757 case LyXTabular::ALIGN_RIGHT:
1758 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1761 case LyXTabular::M_ALIGN_CENTER:
1763 case LyXTabular::ALIGN_CENTER:
1764 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1767 case LyXTabular::ALIGN_BLOCK:
1768 status.disabled(tabular.getPWidth(actcell).zero());
1769 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1772 case LyXTabular::M_VALIGN_TOP:
1774 case LyXTabular::VALIGN_TOP:
1776 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1779 case LyXTabular::M_VALIGN_BOTTOM:
1781 case LyXTabular::VALIGN_BOTTOM:
1783 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1786 case LyXTabular::M_VALIGN_MIDDLE:
1788 case LyXTabular::VALIGN_MIDDLE:
1790 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1793 case LyXTabular::SET_LONGTABULAR:
1794 status.setOnOff(tabular.isLongTabular());
1797 case LyXTabular::UNSET_LONGTABULAR:
1798 status.setOnOff(!tabular.isLongTabular());
1801 case LyXTabular::SET_ROTATE_TABULAR:
1802 status.setOnOff(tabular.getRotateTabular());
1805 case LyXTabular::UNSET_ROTATE_TABULAR:
1806 status.setOnOff(!tabular.getRotateTabular());
1809 case LyXTabular::SET_ROTATE_CELL:
1810 status.setOnOff(tabular.getRotateCell(actcell));
1813 case LyXTabular::UNSET_ROTATE_CELL:
1814 status.setOnOff(!tabular.getRotateCell(actcell));
1817 case LyXTabular::SET_USEBOX:
1818 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1821 case LyXTabular::SET_LTFIRSTHEAD:
1822 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1825 case LyXTabular::SET_LTHEAD:
1826 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1829 case LyXTabular::SET_LTFOOT:
1830 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1833 case LyXTabular::SET_LTLASTFOOT:
1834 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1837 case LyXTabular::SET_LTNEWPAGE:
1838 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1843 status.disabled(true);
1850 void InsetTabular::getLabelList(Buffer const & buffer,
1851 std::vector<string> & list) const
1853 tabular.getLabelList(buffer, list);
1857 bool InsetTabular::copySelection(BufferView * bv)
1859 if (!hasSelection())
1862 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1863 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1864 if (sel_col_start > sel_col_end) {
1865 sel_col_start = sel_col_end;
1866 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1868 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1871 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1872 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1873 if (sel_row_start > sel_row_end)
1874 swap(sel_row_start, sel_row_end);
1876 paste_tabular.reset(new LyXTabular(tabular));
1877 paste_tabular->setOwner(this);
1879 for (int i = 0; i < sel_row_start; ++i)
1880 paste_tabular->deleteRow(0);
1882 int const rows = sel_row_end - sel_row_start + 1;
1883 while (paste_tabular->rows() > rows)
1884 paste_tabular->deleteRow(rows);
1886 paste_tabular->setTopLine(0, true, true);
1887 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1890 for (int i = 0; i < sel_col_start; ++i)
1891 paste_tabular->deleteColumn(0);
1893 int const columns = sel_col_end - sel_col_start + 1;
1894 while (paste_tabular->columns() > columns)
1895 paste_tabular->deleteColumn(columns);
1897 paste_tabular->setLeftLine(0, true, true);
1898 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1902 OutputParams const runparams;
1903 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1904 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1905 bv->stuffClipboard(os.str());
1910 bool InsetTabular::pasteSelection(BufferView * bv)
1915 for (int r1 = 0, r2 = actrow;
1916 r1 < paste_tabular->rows() && r2 < tabular.rows();
1918 for (int c1 = 0, c2 = actcol;
1919 c1 < paste_tabular->columns() && c2 < tabular.columns();
1921 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1922 tabular.isPartOfMultiColumn(r2, c2))
1924 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1928 if (tabular.isPartOfMultiColumn(r2, c2)) {
1932 InsetText & inset = tabular.getCellInset(r2, c2);
1933 inset = paste_tabular->getCellInset(r1, c1);
1934 inset.setOwner(this);
1942 bool InsetTabular::cutSelection(BufferParams const & bp)
1944 if (!hasSelection())
1947 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1948 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1949 if (sel_col_start > sel_col_end) {
1950 sel_col_start = sel_col_end;
1951 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1953 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1956 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1957 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1959 if (sel_row_start > sel_row_end)
1960 swap(sel_row_start, sel_row_end);
1962 if (sel_cell_start > sel_cell_end)
1963 swap(sel_cell_start, sel_cell_end);
1965 for (int i = sel_row_start; i <= sel_row_end; ++i)
1966 for (int j = sel_col_start; j <= sel_col_end; ++j)
1967 tabular.getCellInset(tabular.getCellNumber(i, j))
1968 .clear(bp.tracking_changes);
1973 bool InsetTabular::isRightToLeft(BufferView * bv)
1975 return bv->getParentLanguage(this)->RightToLeft();
1979 int InsetTabular::scroll(bool recursive) const
1981 int sx = UpdatableInset::scroll(false);
1983 if (recursive && the_locking_inset)
1984 sx += the_locking_inset->scroll(recursive);
1990 void InsetTabular::getSelection(int & srow, int & erow,
1991 int & scol, int & ecol) const
1993 int const start = hasSelection() ? sel_cell_start : actcell;
1994 int const end = hasSelection() ? sel_cell_end : actcell;
1996 srow = tabular.row_of_cell(start);
1997 erow = tabular.row_of_cell(end);
2001 scol = tabular.column_of_cell(start);
2002 ecol = tabular.column_of_cell(end);
2006 ecol = tabular.right_column_of_cell(end);
2010 ParagraphList * InsetTabular::getParagraphs(int i) const
2012 return i < tabular.getNumberOfCells()
2013 ? tabular.getCellInset(i).getParagraphs(0)
2018 int InsetTabular::numParagraphs() const
2020 return tabular.getNumberOfCells();
2024 LyXText * InsetTabular::getText(int i) const
2026 return i < tabular.getNumberOfCells()
2027 ? tabular.getCellInset(i).getText(0)
2032 void InsetTabular::markErased()
2034 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2035 tabular.getCellInset(cell).markErased();
2039 bool InsetTabular::insetAllowed(InsetOld::Code) const
2045 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2047 const int cell = tabular.getCellFromInset(in);
2050 return tabular.getPWidth(cell).zero();
2052 // this is a workaround for a crash (New, Insert->Tabular,
2053 // Insert->FootNote)
2057 // well we didn't obviously find it so maybe our owner knows more
2058 BOOST_ASSERT(owner());
2059 return owner()->forceDefaultParagraphs(in);
2063 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2066 if (buf.length() <= 0)
2072 string::size_type len = buf.length();
2073 string::size_type p = 0;
2075 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2083 maxCols = max(cols, maxCols);
2089 maxCols = max(cols, maxCols);
2090 LyXTabular * loctab;
2095 paste_tabular.reset(
2096 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2099 paste_tabular->setOwner(this);
2100 loctab = paste_tabular.get();
2109 string::size_type op = 0;
2110 int cells = loctab->getNumberOfCells();
2113 rows = loctab->rows();
2114 int const columns = loctab->columns();
2116 while (cell < cells && p < len && row < rows &&
2117 (p = buf.find_first_of("\t\n", p)) != string::npos)
2123 // we can only set this if we are not too far right
2124 if (cols < columns) {
2125 InsetText & inset = loctab->getCellInset(cell);
2126 LyXFont const font = inset.text_.
2127 getFont(inset.paragraphs.begin(), 0);
2128 inset.setText(buf.substr(op, p - op), font);
2134 // we can only set this if we are not too far right
2135 if (cols < columns) {
2136 InsetText & inset = tabular.getCellInset(cell);
2137 LyXFont const font = inset.text_.
2138 getFont(inset.paragraphs.begin(), 0);
2139 inset.setText(buf.substr(op, p - op), font);
2144 cell = loctab->getCellNumber(row, cols);
2150 // check for the last cell if there is no trailing '\n'
2151 if (cell < cells && op < len) {
2152 InsetText & inset = loctab->getCellInset(cell);
2153 LyXFont const font = inset.text_.getFont(inset.paragraphs.begin(), 0);
2154 inset.setText(buf.substr(op, len - op), font);
2161 void InsetTabular::addPreview(PreviewLoader & loader) const
2163 int const rows = tabular.rows();
2164 int const columns = tabular.columns();
2165 for (int i = 0; i < rows; ++i) {
2166 for (int j = 0; j < columns; ++j)
2167 tabular.getCellInset(i, j).addPreview(loader);
2172 string const InsetTabularMailer::name_("tabular");
2174 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2175 : inset_(const_cast<InsetTabular &>(inset))
2179 string const InsetTabularMailer::inset2string(Buffer const &) const
2181 return params2string(inset_);
2185 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2187 istringstream data(in);
2189 lex.setStream(data);
2191 #warning CHECK verify that this is a sane value to return.
2197 string const token = lex.getString();
2205 string const token = lex.getString();
2206 if (token != "\\active_cell")
2209 cell = lex.getInteger();
2212 // This is part of the inset proper that is usually swallowed
2213 // by Buffer::readInset
2216 string const token = lex.getString();
2217 if (token != "Tabular")
2224 Buffer const & buffer = inset.buffer();
2225 inset.read(buffer, lex);
2227 // We can't set the active cell, but we can tell the frontend
2233 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2236 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2237 inset.write(inset.buffer(), data);
2238 data << "\\end_inset\n";