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();
764 if (clip.find('\t') != string::npos) {
768 string::size_type len = clip.length();
769 string::size_type p = 0;
772 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
780 maxCols = max(cols, maxCols);
786 maxCols = max(cols, maxCols);
789 new LyXTabular(bv->buffer()->params(), rows, maxCols));
791 string::size_type op = 0;
793 int cells = paste_tabular->getNumberOfCells();
797 while (cell < cells && p < len &&
798 (p = clip.find_first_of("\t\n", p)) != string::npos) {
803 paste_tabular->getCellInset(cell).
804 setText(clip.substr(op, p-op), font);
809 paste_tabular->getCellInset(cell).
810 setText(clip.substr(op, p-op), font);
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).
822 setText(clip.substr(op, len-op), font);
823 } else if (!insertAsciiString(bv, clip, true))
825 // so that the clipboard is used and it goes on
827 // and executes LFUN_PASTESELECTION in insettext!
828 paste_tabular.reset();
834 if (hasPasteBuffer()) {
835 recordUndo(bv, Undo::INSERT);
841 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
844 // handle font changing stuff on selection before we lock the inset
845 // in the default part!
846 result = DispatchResult(false);
847 // we try to activate the actual inset and put this event down to
848 // the insets dispatch function.
849 if (!the_locking_inset && activateCellInset(bv)) {
850 result = the_locking_inset->dispatch(cmd);
851 if (!result.dispatched()) {
852 // we need to update if this was requested before
853 result = DispatchResult(false);
863 InsetTabularMailer(*this).updateDialog(bv);
868 int InsetTabular::latex(Buffer const & buf, ostream & os,
869 OutputParams const & runparams) const
871 return tabular.latex(buf, os, runparams);
875 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
876 OutputParams const & runparams) const
878 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
879 return tabular.plaintext(buf, os, runparams, dp, false, 0);
883 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
884 OutputParams const & runparams) const
886 return tabular.linuxdoc(buf,os, runparams);
890 int InsetTabular::docbook(Buffer const & buf, ostream & os,
891 OutputParams const & runparams) const
896 // if the table is inside a float it doesn't need the informaltable
897 // wrapper. Search for it.
898 for (master = owner();
899 master && master->lyxCode() != InsetOld::FLOAT_CODE;
900 master = master->owner());
903 os << "<informaltable>";
904 if (runparams.mixed_content)
908 ret += tabular.docbook(buf, os, runparams);
910 os << "</informaltable>";
911 if (runparams.mixed_content)
919 void InsetTabular::validate(LaTeXFeatures & features) const
921 tabular.validate(features);
925 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
928 // if we have a locking_inset we should have to check only this cell for
929 // change so I'll try this to have a boost, but who knows ;) (Jug?)
930 // This is _really_ important (André)
931 if (the_locking_inset == &tabular.getCellInset(actcell)) {
934 for (int j = 0; j < tabular.columns(); ++j) {
938 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
939 tabular.getCellInset(actrow, j).metrics(m, dim);
940 maxAsc = max(dim.asc, maxAsc);
941 maxDesc = max(dim.des, maxDesc);
943 tabular.setWidthOfCell(actcell, the_locking_inset->width());
944 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
945 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
951 for (int i = 0; i < tabular.rows(); ++i) {
954 for (int j = 0; j < tabular.columns(); ++j) {
955 if (tabular.isPartOfMultiColumn(i, j))
961 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
962 tabular.getCellInset(cell).metrics(m, dim);
963 maxAsc = max(maxAsc, dim.asc);
964 maxDesc = max(maxDesc, dim.des);
965 tabular.setWidthOfCell(cell, dim.wid);
967 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
968 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
973 void InsetTabular::getCursorPos(int & x, int & y) const
975 x = TEXT_TO_INSET_OFFSET + cursorx_ - xo_;
976 y = TEXT_TO_INSET_OFFSET + cursory_;
980 void InsetTabular::setPos(BufferView * bv, int x, int y) const
986 int ly = tabular.getDescentOfRow(actrow);
988 // first search the right row
989 while (ly < y && actrow + 1 < tabular.rows()) {
990 cursory_ += tabular.getDescentOfRow(actrow) +
991 tabular.getAscentOfRow(actrow + 1) +
992 tabular.getAdditionalHeight(actrow + 1);
994 ly = cursory_ + tabular.getDescentOfRow(actrow);
996 actcell = tabular.getCellNumber(actrow, actcol);
998 // now search the right column
999 int lx = tabular.getWidthOfColumn(actcell) -
1000 tabular.getAdditionalWidth(actcell);
1002 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1003 lx += tabular.getWidthOfColumn(actcell + 1)
1004 + tabular.getAdditionalWidth(actcell);
1006 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1011 int InsetTabular::getCellXPos(int cell) const
1015 for (; !tabular.isFirstCellInRow(c); --c)
1017 int lx = tabular.getWidthOfColumn(cell);
1018 for (; c < cell; ++c)
1019 lx += tabular.getWidthOfColumn(c);
1021 return lx - tabular.getWidthOfColumn(cell) + xo_;
1025 void InsetTabular::resetPos(BufferView * bv) const
1027 #ifdef WITH_WARNINGS
1028 #warning This should be fixed in the right manner (20011128 Jug)
1030 // fast hack to fix infinite repaintings!
1031 if (in_reset_pos > 0)
1035 actcol = tabular.column_of_cell(actcell);
1038 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1039 if (tabular.isLastCellInRow(cell)) {
1040 cursory_ += tabular.getDescentOfRow(actrow) +
1041 tabular.getAscentOfRow(actrow + 1) +
1042 tabular.getAdditionalHeight(actrow + 1);
1047 // we need this only from here on!!!
1049 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1050 int new_x = getCellXPos(actcell) + offset;
1051 int old_x = cursorx_;
1053 // cursor.x(getCellXPos(actcell) + offset);
1054 if (actcol < tabular.columns() - 1 && scroll(false) &&
1055 tabular.getWidthOfTabular() < bv->workWidth()-20)
1059 } else if (the_locking_inset &&
1060 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1062 int xx = cursorx_ - offset;
1063 if (xx > bv->workWidth()-20) {
1064 scroll(bv, - xx + bv->workWidth() - 60);
1066 } else if (xx < 20) {
1074 } else if (cursorx_ - offset > 20 &&
1075 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1076 > bv->workWidth() - 20) {
1077 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1079 } else if (cursorx_ - offset < 20) {
1080 scroll(bv, 20 - cursorx_ + offset);
1082 } else if (scroll() && xo_ > 20 &&
1083 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1084 scroll(bv, old_x - cursorx_);
1087 InsetTabularMailer(*this).updateDialog(bv);
1092 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1095 if (activateCellInset(bv))
1096 return DispatchResult(true, true);
1098 bool moved = isRightToLeft(bv)
1099 ? movePrevCell(bv) : moveNextCell(bv);
1101 return DispatchResult(false, FINISHED_RIGHT);
1102 if (lock && activateCellInset(bv))
1103 return DispatchResult(true, true);
1106 return DispatchResult(true);
1110 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1112 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1114 return DispatchResult(false, FINISHED);
1116 if (lock && activateCellInset(bv, 0, 0, true))
1117 return DispatchResult(true, true);
1119 return DispatchResult(true);
1123 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1125 int const ocell = actcell;
1126 actcell = tabular.getCellAbove(actcell);
1127 if (actcell == ocell) { // we moved out of the inset
1128 return DispatchResult(false, FINISHED_UP);
1132 activateCellInset(bv, bv->x_target(), 0);
1133 return DispatchResult(true, true);
1137 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1139 int const ocell = actcell;
1140 actcell = tabular.getCellBelow(actcell);
1141 if (actcell == ocell) { // we moved out of the inset
1142 return DispatchResult(false, FINISHED_DOWN);
1146 activateCellInset(bv, bv->x_target(), 0);
1147 return DispatchResult(true, true);
1151 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1153 if (isRightToLeft(bv)) {
1154 if (tabular.isFirstCellInRow(actcell)) {
1155 int row = tabular.row_of_cell(actcell);
1156 if (row == tabular.rows() - 1)
1158 actcell = tabular.getLastCellInRow(row);
1159 actcell = tabular.getCellBelow(actcell);
1166 if (tabular.isLastCell(actcell))
1171 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1172 isRightToLeftPar(bv->buffer()->params());
1173 activateCellInset(bv, 0, 0, !rtl);
1180 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1182 lyxerr << "move prevcell 1" << endl;
1183 if (isRightToLeft(bv)) {
1184 lyxerr << "move prevcell a" << endl;
1185 if (tabular.isLastCellInRow(actcell)) {
1186 int row = tabular.row_of_cell(actcell);
1189 actcell = tabular.getFirstCellInRow(row);
1190 actcell = tabular.getCellAbove(actcell);
1192 if (tabular.isLastCell(actcell))
1197 lyxerr << "move prevcell b" << endl;
1198 if (actcell == 0) // first cell
1202 lyxerr << "move prevcell 2" << endl;
1204 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1205 isRightToLeftPar(bv->buffer()->params());
1206 activateCellInset(bv, 0, 0, !rtl);
1208 lyxerr << "move prevcell 3" << endl;
1210 lyxerr << "move prevcell 4" << endl;
1215 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1217 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1220 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1221 string const tmp = tabularFeature[i].feature;
1223 if (tmp == what.substr(0, tmp.length())) {
1224 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1225 //tabularFeatures[i].feature.length())) {
1226 action = tabularFeature[i].action;
1230 if (action == LyXTabular::LAST_ACTION)
1234 ltrim(what.substr(tabularFeature[i].feature.length()));
1235 tabularFeatures(bv, action, val);
1241 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1242 string const & special, bool & flag)
1244 if (special == "dl_above") {
1247 } else if (special == "dl_below") {
1248 ltt.bottomDL = flag;
1250 } else if (special == "empty") {
1262 void InsetTabular::tabularFeatures(BufferView * bv,
1263 LyXTabular::Feature feature, string const & value)
1269 bool setLines = false;
1270 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1271 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1275 case LyXTabular::M_ALIGN_LEFT:
1276 case LyXTabular::ALIGN_LEFT:
1277 setAlign = LYX_ALIGN_LEFT;
1280 case LyXTabular::M_ALIGN_RIGHT:
1281 case LyXTabular::ALIGN_RIGHT:
1282 setAlign = LYX_ALIGN_RIGHT;
1285 case LyXTabular::M_ALIGN_CENTER:
1286 case LyXTabular::ALIGN_CENTER:
1287 setAlign = LYX_ALIGN_CENTER;
1290 case LyXTabular::ALIGN_BLOCK:
1291 setAlign = LYX_ALIGN_BLOCK;
1294 case LyXTabular::M_VALIGN_TOP:
1295 case LyXTabular::VALIGN_TOP:
1296 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1299 case LyXTabular::M_VALIGN_BOTTOM:
1300 case LyXTabular::VALIGN_BOTTOM:
1301 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1304 case LyXTabular::M_VALIGN_MIDDLE:
1305 case LyXTabular::VALIGN_MIDDLE:
1306 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1313 if (hasSelection()) {
1314 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1316 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1317 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1319 recordUndo(bv, Undo::ATOMIC);
1321 int row = tabular.row_of_cell(actcell);
1322 int column = tabular.column_of_cell(actcell);
1324 LyXTabular::ltType ltt;
1328 case LyXTabular::SET_PWIDTH: {
1329 LyXLength const len(value);
1330 tabular.setColumnPWidth(actcell, len);
1332 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1333 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1334 else if (!len.zero()
1335 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1336 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1340 case LyXTabular::SET_MPWIDTH:
1341 tabular.setMColumnPWidth(actcell, LyXLength(value));
1344 case LyXTabular::SET_SPECIAL_COLUMN:
1345 case LyXTabular::SET_SPECIAL_MULTI:
1346 tabular.setAlignSpecial(actcell,value,feature);
1349 case LyXTabular::APPEND_ROW:
1350 // append the row into the tabular
1351 tabular.appendRow(bv->buffer()->params(), actcell);
1352 tabular.setOwner(this);
1355 case LyXTabular::APPEND_COLUMN:
1356 // append the column into the tabular
1357 tabular.appendColumn(bv->buffer()->params(), actcell);
1358 tabular.setOwner(this);
1359 actcell = tabular.getCellNumber(row, column);
1362 case LyXTabular::DELETE_ROW:
1363 for (int i = sel_row_start; i <= sel_row_end; ++i)
1364 tabular.deleteRow(sel_row_start);
1365 if (sel_row_start >= tabular.rows())
1367 actcell = tabular.getCellNumber(sel_row_start, column);
1371 case LyXTabular::DELETE_COLUMN:
1372 for (int i = sel_col_start; i <= sel_col_end; ++i)
1373 tabular.deleteColumn(sel_col_start);
1374 if (sel_col_start >= tabular.columns())
1376 actcell = tabular.getCellNumber(row, sel_col_start);
1380 case LyXTabular::M_TOGGLE_LINE_TOP:
1382 case LyXTabular::TOGGLE_LINE_TOP: {
1383 bool lineSet = !tabular.topLine(actcell, flag);
1384 for (int i = sel_row_start; i <= sel_row_end; ++i)
1385 for (int j = sel_col_start; j <= sel_col_end; ++j)
1387 tabular.getCellNumber(i, j),
1392 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1394 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1395 bool lineSet = !tabular.bottomLine(actcell, flag);
1396 for (int i = sel_row_start; i <= sel_row_end; ++i)
1397 for (int j = sel_col_start; j <= sel_col_end; ++j)
1398 tabular.setBottomLine(
1399 tabular.getCellNumber(i, j),
1405 case LyXTabular::M_TOGGLE_LINE_LEFT:
1407 case LyXTabular::TOGGLE_LINE_LEFT: {
1408 bool lineSet = !tabular.leftLine(actcell, flag);
1409 for (int i = sel_row_start; i <= sel_row_end; ++i)
1410 for (int j = sel_col_start; j <= sel_col_end; ++j)
1411 tabular.setLeftLine(
1412 tabular.getCellNumber(i,j),
1418 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1420 case LyXTabular::TOGGLE_LINE_RIGHT: {
1421 bool lineSet = !tabular.rightLine(actcell, flag);
1422 for (int i = sel_row_start; i <= sel_row_end; ++i)
1423 for (int j = sel_col_start; j <= sel_col_end; ++j)
1424 tabular.setRightLine(
1425 tabular.getCellNumber(i,j),
1431 case LyXTabular::M_ALIGN_LEFT:
1432 case LyXTabular::M_ALIGN_RIGHT:
1433 case LyXTabular::M_ALIGN_CENTER:
1435 case LyXTabular::ALIGN_LEFT:
1436 case LyXTabular::ALIGN_RIGHT:
1437 case LyXTabular::ALIGN_CENTER:
1438 case LyXTabular::ALIGN_BLOCK:
1439 for (int i = sel_row_start; i <= sel_row_end; ++i)
1440 for (int j = sel_col_start; j <= sel_col_end; ++j)
1441 tabular.setAlignment(
1442 tabular.getCellNumber(i, j),
1447 case LyXTabular::M_VALIGN_TOP:
1448 case LyXTabular::M_VALIGN_BOTTOM:
1449 case LyXTabular::M_VALIGN_MIDDLE:
1451 case LyXTabular::VALIGN_TOP:
1452 case LyXTabular::VALIGN_BOTTOM:
1453 case LyXTabular::VALIGN_MIDDLE:
1454 for (int i = sel_row_start; i <= sel_row_end; ++i)
1455 for (int j = sel_col_start; j <= sel_col_end; ++j)
1456 tabular.setVAlignment(
1457 tabular.getCellNumber(i, j),
1461 case LyXTabular::MULTICOLUMN: {
1462 if (sel_row_start != sel_row_end) {
1463 #ifdef WITH_WARNINGS
1464 #warning Need I say it ? This is horrible.
1466 Alert::error(_("Error setting multicolumn"),
1467 _("You cannot set multicolumn vertically."));
1470 // just multicol for one Single Cell
1471 if (!hasSelection()) {
1472 // check wether we are completly in a multicol
1473 if (tabular.isMultiColumn(actcell))
1474 tabular.unsetMultiColumn(actcell);
1476 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1479 // we have a selection so this means we just add all this
1480 // cells to form a multicolumn cell
1484 if (sel_cell_start > sel_cell_end) {
1485 s_start = sel_cell_end;
1486 s_end = sel_cell_start;
1488 s_start = sel_cell_start;
1489 s_end = sel_cell_end;
1491 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1497 case LyXTabular::SET_ALL_LINES:
1499 case LyXTabular::UNSET_ALL_LINES:
1500 for (int i = sel_row_start; i <= sel_row_end; ++i)
1501 for (int j = sel_col_start; j <= sel_col_end; ++j)
1502 tabular.setAllLines(
1503 tabular.getCellNumber(i,j), setLines);
1506 case LyXTabular::SET_LONGTABULAR:
1507 tabular.setLongTabular(true);
1510 case LyXTabular::UNSET_LONGTABULAR:
1511 tabular.setLongTabular(false);
1514 case LyXTabular::SET_ROTATE_TABULAR:
1515 tabular.setRotateTabular(true);
1518 case LyXTabular::UNSET_ROTATE_TABULAR:
1519 tabular.setRotateTabular(false);
1522 case LyXTabular::SET_ROTATE_CELL:
1523 for (int i = sel_row_start; i <= sel_row_end; ++i)
1524 for (int j = sel_col_start; j <= sel_col_end; ++j)
1525 tabular.setRotateCell(
1526 tabular.getCellNumber(i, j), true);
1529 case LyXTabular::UNSET_ROTATE_CELL:
1530 for (int i = sel_row_start; i <= sel_row_end; ++i)
1531 for (int j = sel_col_start; j <= sel_col_end; ++j)
1532 tabular.setRotateCell(
1533 tabular.getCellNumber(i, j), false);
1536 case LyXTabular::SET_USEBOX: {
1537 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1538 if (val == tabular.getUsebox(actcell))
1539 val = LyXTabular::BOX_NONE;
1540 for (int i = sel_row_start; i <= sel_row_end; ++i)
1541 for (int j = sel_col_start; j <= sel_col_end; ++j)
1542 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1546 case LyXTabular::UNSET_LTFIRSTHEAD:
1548 case LyXTabular::SET_LTFIRSTHEAD:
1549 tabular.getRowOfLTFirstHead(row, ltt);
1550 checkLongtableSpecial(ltt, value, flag);
1551 tabular.setLTHead(row, flag, ltt, true);
1554 case LyXTabular::UNSET_LTHEAD:
1556 case LyXTabular::SET_LTHEAD:
1557 tabular.getRowOfLTHead(row, ltt);
1558 checkLongtableSpecial(ltt, value, flag);
1559 tabular.setLTHead(row, flag, ltt, false);
1562 case LyXTabular::UNSET_LTFOOT:
1564 case LyXTabular::SET_LTFOOT:
1565 tabular.getRowOfLTFoot(row, ltt);
1566 checkLongtableSpecial(ltt, value, flag);
1567 tabular.setLTFoot(row, flag, ltt, false);
1570 case LyXTabular::UNSET_LTLASTFOOT:
1572 case LyXTabular::SET_LTLASTFOOT:
1573 tabular.getRowOfLTLastFoot(row, ltt);
1574 checkLongtableSpecial(ltt, value, flag);
1575 tabular.setLTFoot(row, flag, ltt, true);
1578 case LyXTabular::SET_LTNEWPAGE:
1579 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1582 // dummy stuff just to avoid warnings
1583 case LyXTabular::LAST_ACTION:
1588 InsetTabularMailer(*this).updateDialog(bv);
1592 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1594 UpdatableInset & inset = tabular.getCellInset(actcell);
1597 x = inset.x() + inset.width();
1598 y = inset.descent();
1600 inset.edit(bv, x, y);
1601 bv->cursor().push(&inset);
1602 if (!the_locking_inset)
1605 return the_locking_inset;
1609 bool InsetTabular::showInsetDialog(BufferView * bv) const
1611 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1612 InsetTabularMailer(*this).showDialog(bv);
1617 void InsetTabular::openLayoutDialog(BufferView * bv) const
1621 if (the_locking_inset) {
1622 InsetTabular * inset = static_cast<InsetTabular *>
1623 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
1625 inset->openLayoutDialog(bv);
1630 InsetTabularMailer(*this).showDialog(bv);
1635 // function returns an object as defined in func_status.h:
1636 // states OK, Unknown, Disabled, On, Off.
1638 FuncStatus InsetTabular::getStatus(string const & what) const
1640 int action = LyXTabular::LAST_ACTION;
1644 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1645 string const tmp = tabularFeature[i].feature;
1646 if (tmp == what.substr(0, tmp.length())) {
1647 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1648 // tabularFeatures[i].feature.length())) {
1649 action = tabularFeature[i].action;
1653 if (action == LyXTabular::LAST_ACTION) {
1655 status.unknown(true);
1659 string const argument
1660 = ltrim(what.substr(tabularFeature[i].feature.length()));
1665 LyXTabular::ltType dummyltt;
1669 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1671 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1674 case LyXTabular::SET_PWIDTH:
1675 case LyXTabular::SET_MPWIDTH:
1676 case LyXTabular::SET_SPECIAL_COLUMN:
1677 case LyXTabular::SET_SPECIAL_MULTI:
1678 case LyXTabular::APPEND_ROW:
1679 case LyXTabular::APPEND_COLUMN:
1680 case LyXTabular::DELETE_ROW:
1681 case LyXTabular::DELETE_COLUMN:
1682 case LyXTabular::SET_ALL_LINES:
1683 case LyXTabular::UNSET_ALL_LINES:
1687 case LyXTabular::MULTICOLUMN:
1688 status.setOnOff(tabular.isMultiColumn(actcell));
1691 case LyXTabular::M_TOGGLE_LINE_TOP:
1693 case LyXTabular::TOGGLE_LINE_TOP:
1694 status.setOnOff(tabular.topLine(actcell, flag));
1697 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1699 case LyXTabular::TOGGLE_LINE_BOTTOM:
1700 status.setOnOff(tabular.bottomLine(actcell, flag));
1703 case LyXTabular::M_TOGGLE_LINE_LEFT:
1705 case LyXTabular::TOGGLE_LINE_LEFT:
1706 status.setOnOff(tabular.leftLine(actcell, flag));
1709 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1711 case LyXTabular::TOGGLE_LINE_RIGHT:
1712 status.setOnOff(tabular.rightLine(actcell, flag));
1715 case LyXTabular::M_ALIGN_LEFT:
1717 case LyXTabular::ALIGN_LEFT:
1718 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1721 case LyXTabular::M_ALIGN_RIGHT:
1723 case LyXTabular::ALIGN_RIGHT:
1724 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1727 case LyXTabular::M_ALIGN_CENTER:
1729 case LyXTabular::ALIGN_CENTER:
1730 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1733 case LyXTabular::ALIGN_BLOCK:
1734 status.disabled(tabular.getPWidth(actcell).zero());
1735 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1738 case LyXTabular::M_VALIGN_TOP:
1740 case LyXTabular::VALIGN_TOP:
1742 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1745 case LyXTabular::M_VALIGN_BOTTOM:
1747 case LyXTabular::VALIGN_BOTTOM:
1749 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1752 case LyXTabular::M_VALIGN_MIDDLE:
1754 case LyXTabular::VALIGN_MIDDLE:
1756 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1759 case LyXTabular::SET_LONGTABULAR:
1760 status.setOnOff(tabular.isLongTabular());
1763 case LyXTabular::UNSET_LONGTABULAR:
1764 status.setOnOff(!tabular.isLongTabular());
1767 case LyXTabular::SET_ROTATE_TABULAR:
1768 status.setOnOff(tabular.getRotateTabular());
1771 case LyXTabular::UNSET_ROTATE_TABULAR:
1772 status.setOnOff(!tabular.getRotateTabular());
1775 case LyXTabular::SET_ROTATE_CELL:
1776 status.setOnOff(tabular.getRotateCell(actcell));
1779 case LyXTabular::UNSET_ROTATE_CELL:
1780 status.setOnOff(!tabular.getRotateCell(actcell));
1783 case LyXTabular::SET_USEBOX:
1784 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1787 case LyXTabular::SET_LTFIRSTHEAD:
1788 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1791 case LyXTabular::SET_LTHEAD:
1792 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1795 case LyXTabular::SET_LTFOOT:
1796 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1799 case LyXTabular::SET_LTLASTFOOT:
1800 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1803 case LyXTabular::SET_LTNEWPAGE:
1804 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1809 status.disabled(true);
1816 void InsetTabular::getLabelList(Buffer const & buffer,
1817 std::vector<string> & list) const
1819 tabular.getLabelList(buffer, list);
1823 bool InsetTabular::copySelection(BufferView * bv)
1825 if (!hasSelection())
1828 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1829 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1830 if (sel_col_start > sel_col_end) {
1831 sel_col_start = sel_col_end;
1832 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1834 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1837 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1838 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1839 if (sel_row_start > sel_row_end)
1840 swap(sel_row_start, sel_row_end);
1842 paste_tabular.reset(new LyXTabular(tabular));
1843 paste_tabular->setOwner(this);
1845 for (int i = 0; i < sel_row_start; ++i)
1846 paste_tabular->deleteRow(0);
1848 int const rows = sel_row_end - sel_row_start + 1;
1849 while (paste_tabular->rows() > rows)
1850 paste_tabular->deleteRow(rows);
1852 paste_tabular->setTopLine(0, true, true);
1853 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1856 for (int i = 0; i < sel_col_start; ++i)
1857 paste_tabular->deleteColumn(0);
1859 int const columns = sel_col_end - sel_col_start + 1;
1860 while (paste_tabular->columns() > columns)
1861 paste_tabular->deleteColumn(columns);
1863 paste_tabular->setLeftLine(0, true, true);
1864 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1868 OutputParams const runparams;
1869 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1870 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1871 bv->stuffClipboard(os.str());
1876 bool InsetTabular::pasteSelection(BufferView *)
1881 for (int r1 = 0, r2 = actrow;
1882 r1 < paste_tabular->rows() && r2 < tabular.rows();
1884 for (int c1 = 0, c2 = actcol;
1885 c1 < paste_tabular->columns() && c2 < tabular.columns();
1887 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1888 tabular.isPartOfMultiColumn(r2, c2))
1890 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1894 if (tabular.isPartOfMultiColumn(r2, c2)) {
1898 InsetText & inset = tabular.getCellInset(r2, c2);
1899 inset = paste_tabular->getCellInset(r1, c1);
1900 inset.setOwner(this);
1908 bool InsetTabular::cutSelection(BufferParams const & bp)
1910 if (!hasSelection())
1913 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1914 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1915 if (sel_col_start > sel_col_end) {
1916 sel_col_start = sel_col_end;
1917 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1919 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1922 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1923 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1925 if (sel_row_start > sel_row_end)
1926 swap(sel_row_start, sel_row_end);
1928 if (sel_cell_start > sel_cell_end)
1929 swap(sel_cell_start, sel_cell_end);
1931 for (int i = sel_row_start; i <= sel_row_end; ++i)
1932 for (int j = sel_col_start; j <= sel_col_end; ++j)
1933 tabular.getCellInset(tabular.getCellNumber(i, j))
1934 .clear(bp.tracking_changes);
1939 bool InsetTabular::isRightToLeft(BufferView * bv)
1941 return bv->getParentLanguage(this)->RightToLeft();
1945 int InsetTabular::scroll(bool recursive) const
1947 int sx = UpdatableInset::scroll(false);
1949 if (recursive && the_locking_inset)
1950 sx += the_locking_inset->scroll(recursive);
1956 void InsetTabular::getSelection(int & srow, int & erow,
1957 int & scol, int & ecol) const
1959 int const start = hasSelection() ? sel_cell_start : actcell;
1960 int const end = hasSelection() ? sel_cell_end : actcell;
1962 srow = tabular.row_of_cell(start);
1963 erow = tabular.row_of_cell(end);
1967 scol = tabular.column_of_cell(start);
1968 ecol = tabular.column_of_cell(end);
1972 ecol = tabular.right_column_of_cell(end);
1976 ParagraphList * InsetTabular::getParagraphs(int i) const
1978 return i < tabular.getNumberOfCells()
1979 ? tabular.getCellInset(i).getParagraphs(0)
1984 int InsetTabular::numParagraphs() const
1986 return tabular.getNumberOfCells();
1990 LyXText * InsetTabular::getText(int i) const
1992 return i < tabular.getNumberOfCells()
1993 ? tabular.getCellInset(i).getText(0)
1998 void InsetTabular::markErased()
2000 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2001 tabular.getCellInset(cell).markErased();
2005 bool InsetTabular::insetAllowed(InsetOld::Code) const
2011 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2013 const int cell = tabular.getCellFromInset(in);
2016 return tabular.getPWidth(cell).zero();
2018 // this is a workaround for a crash (New, Insert->Tabular,
2019 // Insert->FootNote)
2023 // well we didn't obviously find it so maybe our owner knows more
2024 BOOST_ASSERT(owner());
2025 return owner()->forceDefaultParagraphs(in);
2029 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2032 if (buf.length() <= 0)
2038 string::size_type len = buf.length();
2039 string::size_type p = 0;
2041 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2049 maxCols = max(cols, maxCols);
2055 maxCols = max(cols, maxCols);
2056 LyXTabular * loctab;
2061 paste_tabular.reset(
2062 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2065 paste_tabular->setOwner(this);
2066 loctab = paste_tabular.get();
2075 string::size_type op = 0;
2076 int cells = loctab->getNumberOfCells();
2079 rows = loctab->rows();
2080 int const columns = loctab->columns();
2082 while (cell < cells && p < len && row < rows &&
2083 (p = buf.find_first_of("\t\n", p)) != string::npos)
2089 // we can only set this if we are not too far right
2090 if (cols < columns) {
2091 InsetText & inset = loctab->getCellInset(cell);
2092 LyXFont const font = inset.text_.
2093 getFont(inset.paragraphs.begin(), 0);
2094 inset.setText(buf.substr(op, p - op), font);
2100 // we can only set this if we are not too far right
2101 if (cols < columns) {
2102 InsetText & inset = tabular.getCellInset(cell);
2103 LyXFont const font = inset.text_.
2104 getFont(inset.paragraphs.begin(), 0);
2105 inset.setText(buf.substr(op, p - op), font);
2110 cell = loctab->getCellNumber(row, cols);
2116 // check for the last cell if there is no trailing '\n'
2117 if (cell < cells && op < len) {
2118 InsetText & inset = loctab->getCellInset(cell);
2119 LyXFont const font = inset.text_.getFont(inset.paragraphs.begin(), 0);
2120 inset.setText(buf.substr(op, len - op), font);
2127 void InsetTabular::addPreview(PreviewLoader & loader) const
2129 int const rows = tabular.rows();
2130 int const columns = tabular.columns();
2131 for (int i = 0; i < rows; ++i) {
2132 for (int j = 0; j < columns; ++j)
2133 tabular.getCellInset(i, j).addPreview(loader);
2138 string const InsetTabularMailer::name_("tabular");
2140 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2141 : inset_(const_cast<InsetTabular &>(inset))
2145 string const InsetTabularMailer::inset2string(Buffer const &) const
2147 return params2string(inset_);
2151 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2153 istringstream data(in);
2155 lex.setStream(data);
2157 #warning CHECK verify that this is a sane value to return.
2163 string const token = lex.getString();
2171 string const token = lex.getString();
2172 if (token != "\\active_cell")
2175 cell = lex.getInteger();
2178 // This is part of the inset proper that is usually swallowed
2179 // by Buffer::readInset
2182 string const token = lex.getString();
2183 if (token != "Tabular")
2190 Buffer const & buffer = inset.buffer();
2191 inset.read(buffer, lex);
2193 // We can't set the active cell, but we can tell the frontend
2199 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2202 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2203 inset.write(inset.buffer(), data);
2204 data << "\\end_inset\n";