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 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
393 return the_locking_inset && the_locking_inset->insertInset(bv, inset);
397 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
399 if (hasSelection() && cmd.button() == mouse_button::button3)
405 BufferView * bv = cmd.view();
407 the_locking_inset = 0;
408 setPos(bv, cmd.x, cmd.y);
410 the_locking_inset = 0;
412 if (cmd.button() == mouse_button::button2)
413 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
417 void InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
419 if (cmd.button() == mouse_button::button3)
420 InsetTabularMailer(*this).showDialog(cmd.view());
424 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
426 BufferView * bv = cmd.view();
427 int const old_cell = actcell;
429 setPos(bv, cmd.x, cmd.y);
431 setSelection(actcell, actcell);
432 else if (old_cell != actcell)
433 setSelection(sel_cell_start, actcell);
437 void InsetTabular::edit(BufferView * bv, bool left)
439 lyxerr << "InsetTabular::edit: " << this
440 << " first text: " << tabular.cell_info[0][0].inset.getText(0)
441 << " first cell: " << &tabular.cell_info[0][0].inset << endl;
444 the_locking_inset = 0;
447 if (isRightToLeft(bv))
448 actcell = tabular.getLastCellInRow(0);
452 if (isRightToLeft(bv))
453 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
455 actcell = tabular.getNumberOfCells() - 1;
460 bv->cursor().push(this);
464 void InsetTabular::edit(BufferView * bv, int x, int y)
466 lyxerr << "InsetTabular::edit: " << this << " first cell "
467 << &tabular.cell_info[0][0].inset << endl;
470 the_locking_inset = 0;
474 int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
475 bv->cursor().push(this);
477 activateCellInset(bv, x - xx, y - cursory_);
482 InsetTabular::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
484 lyxerr << "InsetTabular::dispatch: " << cmd << endl;
485 // We need to save the value of the_locking_inset as the call to
486 // the_locking_inset->localDispatch might unlock it.
487 DispatchResult result(true, true);
488 BufferView * bv = cmd.view();
489 bool hs = hasSelection();
491 switch (cmd.action) {
493 case LFUN_MOUSE_PRESS:
495 return DispatchResult(true, true);
497 case LFUN_MOUSE_MOTION:
498 lfunMouseMotion(cmd);
499 return DispatchResult(true, true);
501 case LFUN_MOUSE_RELEASE:
502 lfunMouseRelease(cmd);
503 return DispatchResult(true, true);
505 case LFUN_CELL_BACKWARD:
506 case LFUN_CELL_FORWARD:
507 if (cmd.action == LFUN_CELL_FORWARD)
508 moveNextCell(bv, the_locking_inset != 0);
510 movePrevCell(bv, the_locking_inset != 0);
512 if (!the_locking_inset)
513 return DispatchResult(true);
516 case LFUN_SCROLL_INSET:
517 if (!cmd.argument.empty()) {
518 if (cmd.argument.find('.') != cmd.argument.npos)
519 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
521 scroll(cmd.view(), strToInt(cmd.argument));
522 cmd.view()->update();
523 return DispatchResult(true, true);
526 case LFUN_RIGHTSEL: {
527 int const start = hasSelection() ? sel_cell_start : actcell;
528 if (tabular.isLastCellInRow(actcell)) {
529 setSelection(start, actcell);
534 // if we are starting a selection, only select
535 // the current cell at the beginning
536 if (hasSelection()) {
537 moveRight(bv, false);
540 setSelection(start, end);
545 result = moveRight(bv, true);
550 int const start = hasSelection() ? sel_cell_start : actcell;
551 if (tabular.isFirstCellInRow(actcell)) {
552 setSelection(start, actcell);
557 // if we are starting a selection, only select
558 // the current cell at the beginning
559 if (hasSelection()) {
563 setSelection(start, end);
568 result = moveLeft(bv, true);
573 int const start = hasSelection() ? sel_cell_start : actcell;
574 int const ocell = actcell;
575 // if we are starting a selection, only select
576 // the current cell at the beginning
577 if (hasSelection()) {
579 if (ocell == sel_cell_end ||
580 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
581 setSelection(start, tabular.getCellBelow(sel_cell_end));
583 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
585 setSelection(start, start);
591 result = moveDown(bv, the_locking_inset != 0);
596 int const start = hasSelection() ? sel_cell_start : actcell;
597 int const ocell = actcell;
598 // if we are starting a selection, only select
599 // the current cell at the beginning
600 if (hasSelection()) {
602 if (ocell == sel_cell_end ||
603 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
604 setSelection(start, tabular.getCellAbove(sel_cell_end));
606 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
608 setSelection(start, start);
614 result = moveUp(bv, the_locking_inset != 0);
622 if (bv->top_y() + bv->painter().paperHeight()
623 < yo_ + tabular.getHeightOfTabular())
625 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
626 actcell = tabular.getCellBelow(first_visible_cell) + column;
628 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
639 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
643 actcell = tabular.getCellBelow(first_visible_cell) + column;
651 // none of these make sense for insettabular,
652 // but we must catch them to prevent any
653 // selection from being confused
657 case LFUN_WORDLEFTSEL:
659 case LFUN_WORDRIGHTSEL:
661 case LFUN_DOWN_PARAGRAPH:
662 case LFUN_DOWN_PARAGRAPHSEL:
663 case LFUN_UP_PARAGRAPH:
664 case LFUN_UP_PARAGRAPHSEL:
670 case LFUN_BEGINNINGBUF:
671 case LFUN_BEGINNINGBUFSEL:
676 case LFUN_LAYOUT_TABULAR:
677 InsetTabularMailer(*this).showDialog(bv);
680 case LFUN_INSET_DIALOG_UPDATE:
681 InsetTabularMailer(*this).updateDialog(bv);
684 case LFUN_TABULAR_FEATURE:
685 if (!tabularFeatures(bv, cmd.argument))
686 result = DispatchResult(false);
689 // insert file functions
690 case LFUN_FILE_INSERT_ASCII_PARA:
691 case LFUN_FILE_INSERT_ASCII: {
692 string tmpstr = getContentsOfAsciiFile(bv, cmd.argument, false);
693 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
694 result = DispatchResult(false);
708 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
711 case LFUN_FINISHED_LEFT:
712 lyxerr << "swallow LFUN_FINISHED_LEFT, act: " << actcell << endl;
713 if (!movePrevCell(bv, false))
714 result = DispatchResult(FINISHED);
717 case LFUN_FINISHED_RIGHT:
718 lyxerr << "swallow LFUN_FINISHED_RIGHT, act: " << actcell << endl;
719 if (!moveNextCell(bv, false))
720 result = DispatchResult(FINISHED_RIGHT);
723 case LFUN_FINISHED_UP:
724 lyxerr << "swallow LFUN_FINISHED_UP, act: " << actcell << endl;
725 result = moveUp(bv, true);
728 case LFUN_FINISHED_DOWN:
729 lyxerr << "swallow LFUN_FINISHED_DOWN, act: " << actcell << endl;
730 result = moveDown(bv, true);
734 if (copySelection(bv)) {
735 recordUndo(bv, Undo::DELETE);
736 cutSelection(bv->buffer()->params());
741 recordUndo(bv, Undo::DELETE);
742 cutSelection(bv->buffer()->params());
752 case LFUN_PASTESELECTION: {
753 string const clip = bv->getClipboard();
756 if (clip.find('\t') != string::npos) {
760 string::size_type len = clip.length();
761 string::size_type p = 0;
764 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
772 maxCols = max(cols, maxCols);
778 maxCols = max(cols, maxCols);
781 new LyXTabular(bv->buffer()->params(), rows, maxCols));
783 string::size_type op = 0;
785 int cells = paste_tabular->getNumberOfCells();
789 while (cell < cells && p < len &&
790 (p = clip.find_first_of("\t\n", p)) != string::npos) {
795 paste_tabular->getCellInset(cell).
796 setText(clip.substr(op, p-op), font);
801 paste_tabular->getCellInset(cell).
802 setText(clip.substr(op, p-op), font);
803 while (cols++ < maxCols)
811 // check for the last cell if there is no trailing '\n'
812 if (cell < cells && op < len)
813 paste_tabular->getCellInset(cell).
814 setText(clip.substr(op, len-op), font);
815 } else if (!insertAsciiString(bv, clip, true))
817 // so that the clipboard is used and it goes on
819 // and executes LFUN_PASTESELECTION in insettext!
820 paste_tabular.reset();
826 if (hasPasteBuffer()) {
827 recordUndo(bv, Undo::INSERT);
833 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
836 // handle font changing stuff on selection before we lock the inset
837 // in the default part!
838 result = DispatchResult(false);
839 // we try to activate the actual inset and put this event down to
840 // the insets dispatch function.
841 if (!the_locking_inset && activateCellInset(bv)) {
842 result = the_locking_inset->dispatch(cmd);
843 if (!result.dispatched()) {
844 // we need to update if this was requested before
845 result = DispatchResult(false);
855 InsetTabularMailer(*this).updateDialog(bv);
860 int InsetTabular::latex(Buffer const & buf, ostream & os,
861 OutputParams const & runparams) const
863 return tabular.latex(buf, os, runparams);
867 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
868 OutputParams const & runparams) const
870 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
871 return tabular.plaintext(buf, os, runparams, dp, false, 0);
875 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
876 OutputParams const & runparams) const
878 return tabular.linuxdoc(buf,os, runparams);
882 int InsetTabular::docbook(Buffer const & buf, ostream & os,
883 OutputParams const & runparams) const
888 // if the table is inside a float it doesn't need the informaltable
889 // wrapper. Search for it.
890 for (master = owner();
891 master && master->lyxCode() != InsetOld::FLOAT_CODE;
892 master = master->owner());
895 os << "<informaltable>";
896 if (runparams.mixed_content)
900 ret += tabular.docbook(buf, os, runparams);
902 os << "</informaltable>";
903 if (runparams.mixed_content)
911 void InsetTabular::validate(LaTeXFeatures & features) const
913 tabular.validate(features);
917 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
920 // if we have a locking_inset we should have to check only this cell for
921 // change so I'll try this to have a boost, but who knows ;) (Jug?)
922 // This is _really_ important (André)
923 if (the_locking_inset == &tabular.getCellInset(actcell)) {
926 for (int j = 0; j < tabular.columns(); ++j) {
930 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
931 tabular.getCellInset(actrow, j).metrics(m, dim);
932 maxAsc = max(dim.asc, maxAsc);
933 maxDesc = max(dim.des, maxDesc);
935 tabular.setWidthOfCell(actcell, the_locking_inset->width());
936 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
937 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
943 for (int i = 0; i < tabular.rows(); ++i) {
946 for (int j = 0; j < tabular.columns(); ++j) {
947 if (tabular.isPartOfMultiColumn(i, j))
953 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
954 tabular.getCellInset(cell).metrics(m, dim);
955 maxAsc = max(maxAsc, dim.asc);
956 maxDesc = max(maxDesc, dim.des);
957 tabular.setWidthOfCell(cell, dim.wid);
959 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
960 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
965 void InsetTabular::getCursorPos(int & x, int & y) const
967 x = TEXT_TO_INSET_OFFSET + cursorx_ - xo_;
968 y = TEXT_TO_INSET_OFFSET + cursory_;
972 void InsetTabular::setPos(BufferView * bv, int x, int y) const
978 int ly = tabular.getDescentOfRow(actrow);
980 // first search the right row
981 while (ly < y && actrow + 1 < tabular.rows()) {
982 cursory_ += tabular.getDescentOfRow(actrow) +
983 tabular.getAscentOfRow(actrow + 1) +
984 tabular.getAdditionalHeight(actrow + 1);
986 ly = cursory_ + tabular.getDescentOfRow(actrow);
988 actcell = tabular.getCellNumber(actrow, actcol);
990 // now search the right column
991 int lx = tabular.getWidthOfColumn(actcell) -
992 tabular.getAdditionalWidth(actcell);
994 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
995 lx += tabular.getWidthOfColumn(actcell + 1)
996 + tabular.getAdditionalWidth(actcell);
998 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1003 int InsetTabular::getCellXPos(int cell) const
1007 for (; !tabular.isFirstCellInRow(c); --c)
1009 int lx = tabular.getWidthOfColumn(cell);
1010 for (; c < cell; ++c)
1011 lx += tabular.getWidthOfColumn(c);
1013 return lx - tabular.getWidthOfColumn(cell) + xo_;
1017 void InsetTabular::resetPos(BufferView * bv) const
1019 #ifdef WITH_WARNINGS
1020 #warning This should be fixed in the right manner (20011128 Jug)
1022 // fast hack to fix infinite repaintings!
1023 if (in_reset_pos > 0)
1027 actcol = tabular.column_of_cell(actcell);
1030 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1031 if (tabular.isLastCellInRow(cell)) {
1032 cursory_ += tabular.getDescentOfRow(actrow) +
1033 tabular.getAscentOfRow(actrow + 1) +
1034 tabular.getAdditionalHeight(actrow + 1);
1039 // we need this only from here on!!!
1041 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1042 int new_x = getCellXPos(actcell) + offset;
1043 int old_x = cursorx_;
1045 // cursor.x(getCellXPos(actcell) + offset);
1046 if (actcol < tabular.columns() - 1 && scroll(false) &&
1047 tabular.getWidthOfTabular() < bv->workWidth()-20)
1051 } else if (the_locking_inset &&
1052 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1054 int xx = cursorx_ - offset;
1055 if (xx > bv->workWidth()-20) {
1056 scroll(bv, - xx + bv->workWidth() - 60);
1058 } else if (xx < 20) {
1066 } else if (cursorx_ - offset > 20 &&
1067 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1068 > bv->workWidth() - 20) {
1069 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1071 } else if (cursorx_ - offset < 20) {
1072 scroll(bv, 20 - cursorx_ + offset);
1074 } else if (scroll() && xo_ > 20 &&
1075 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1076 scroll(bv, old_x - cursorx_);
1079 InsetTabularMailer(*this).updateDialog(bv);
1084 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1087 if (activateCellInset(bv))
1088 return DispatchResult(true, true);
1090 bool moved = isRightToLeft(bv)
1091 ? movePrevCell(bv) : moveNextCell(bv);
1093 return DispatchResult(false, FINISHED_RIGHT);
1094 if (lock && activateCellInset(bv))
1095 return DispatchResult(true, true);
1098 return DispatchResult(true);
1102 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1104 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1106 return DispatchResult(false, FINISHED);
1108 if (lock && activateCellInset(bv, 0, 0, true))
1109 return DispatchResult(true, true);
1111 return DispatchResult(true);
1115 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1117 int const ocell = actcell;
1118 actcell = tabular.getCellAbove(actcell);
1119 if (actcell == ocell) { // we moved out of the inset
1120 return DispatchResult(false, FINISHED_UP);
1124 activateCellInset(bv, bv->x_target(), 0);
1125 return DispatchResult(true, true);
1129 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1131 int const ocell = actcell;
1132 actcell = tabular.getCellBelow(actcell);
1133 if (actcell == ocell) { // we moved out of the inset
1134 return DispatchResult(false, FINISHED_DOWN);
1138 activateCellInset(bv, bv->x_target(), 0);
1139 return DispatchResult(true, true);
1143 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1145 if (isRightToLeft(bv)) {
1146 if (tabular.isFirstCellInRow(actcell)) {
1147 int row = tabular.row_of_cell(actcell);
1148 if (row == tabular.rows() - 1)
1150 actcell = tabular.getLastCellInRow(row);
1151 actcell = tabular.getCellBelow(actcell);
1158 if (tabular.isLastCell(actcell))
1163 bool rtl = tabular.getCellInset(actcell).paragraphs().begin()->
1164 isRightToLeftPar(bv->buffer()->params());
1165 activateCellInset(bv, 0, 0, !rtl);
1172 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1174 lyxerr << "move prevcell 1" << endl;
1175 if (isRightToLeft(bv)) {
1176 lyxerr << "move prevcell a" << endl;
1177 if (tabular.isLastCellInRow(actcell)) {
1178 int row = tabular.row_of_cell(actcell);
1181 actcell = tabular.getFirstCellInRow(row);
1182 actcell = tabular.getCellAbove(actcell);
1184 if (tabular.isLastCell(actcell))
1189 lyxerr << "move prevcell b" << endl;
1190 if (actcell == 0) // first cell
1194 lyxerr << "move prevcell 2" << endl;
1196 bool rtl = tabular.getCellInset(actcell).paragraphs().begin()->
1197 isRightToLeftPar(bv->buffer()->params());
1198 activateCellInset(bv, 0, 0, !rtl);
1200 lyxerr << "move prevcell 3" << endl;
1202 lyxerr << "move prevcell 4" << endl;
1207 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1209 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1212 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1213 string const tmp = tabularFeature[i].feature;
1215 if (tmp == what.substr(0, tmp.length())) {
1216 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1217 //tabularFeatures[i].feature.length())) {
1218 action = tabularFeature[i].action;
1222 if (action == LyXTabular::LAST_ACTION)
1226 ltrim(what.substr(tabularFeature[i].feature.length()));
1227 tabularFeatures(bv, action, val);
1233 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1234 string const & special, bool & flag)
1236 if (special == "dl_above") {
1239 } else if (special == "dl_below") {
1240 ltt.bottomDL = flag;
1242 } else if (special == "empty") {
1254 void InsetTabular::tabularFeatures(BufferView * bv,
1255 LyXTabular::Feature feature, string const & value)
1261 bool setLines = false;
1262 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1263 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1267 case LyXTabular::M_ALIGN_LEFT:
1268 case LyXTabular::ALIGN_LEFT:
1269 setAlign = LYX_ALIGN_LEFT;
1272 case LyXTabular::M_ALIGN_RIGHT:
1273 case LyXTabular::ALIGN_RIGHT:
1274 setAlign = LYX_ALIGN_RIGHT;
1277 case LyXTabular::M_ALIGN_CENTER:
1278 case LyXTabular::ALIGN_CENTER:
1279 setAlign = LYX_ALIGN_CENTER;
1282 case LyXTabular::ALIGN_BLOCK:
1283 setAlign = LYX_ALIGN_BLOCK;
1286 case LyXTabular::M_VALIGN_TOP:
1287 case LyXTabular::VALIGN_TOP:
1288 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1291 case LyXTabular::M_VALIGN_BOTTOM:
1292 case LyXTabular::VALIGN_BOTTOM:
1293 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1296 case LyXTabular::M_VALIGN_MIDDLE:
1297 case LyXTabular::VALIGN_MIDDLE:
1298 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1305 if (hasSelection()) {
1306 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1308 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1309 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1311 recordUndo(bv, Undo::ATOMIC);
1313 int row = tabular.row_of_cell(actcell);
1314 int column = tabular.column_of_cell(actcell);
1316 LyXTabular::ltType ltt;
1320 case LyXTabular::SET_PWIDTH: {
1321 LyXLength const len(value);
1322 tabular.setColumnPWidth(actcell, len);
1324 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1325 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1326 else if (!len.zero()
1327 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1328 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1332 case LyXTabular::SET_MPWIDTH:
1333 tabular.setMColumnPWidth(actcell, LyXLength(value));
1336 case LyXTabular::SET_SPECIAL_COLUMN:
1337 case LyXTabular::SET_SPECIAL_MULTI:
1338 tabular.setAlignSpecial(actcell,value,feature);
1341 case LyXTabular::APPEND_ROW:
1342 // append the row into the tabular
1343 tabular.appendRow(bv->buffer()->params(), actcell);
1344 tabular.setOwner(this);
1347 case LyXTabular::APPEND_COLUMN:
1348 // append the column into the tabular
1349 tabular.appendColumn(bv->buffer()->params(), actcell);
1350 tabular.setOwner(this);
1351 actcell = tabular.getCellNumber(row, column);
1354 case LyXTabular::DELETE_ROW:
1355 for (int i = sel_row_start; i <= sel_row_end; ++i)
1356 tabular.deleteRow(sel_row_start);
1357 if (sel_row_start >= tabular.rows())
1359 actcell = tabular.getCellNumber(sel_row_start, column);
1363 case LyXTabular::DELETE_COLUMN:
1364 for (int i = sel_col_start; i <= sel_col_end; ++i)
1365 tabular.deleteColumn(sel_col_start);
1366 if (sel_col_start >= tabular.columns())
1368 actcell = tabular.getCellNumber(row, sel_col_start);
1372 case LyXTabular::M_TOGGLE_LINE_TOP:
1374 case LyXTabular::TOGGLE_LINE_TOP: {
1375 bool lineSet = !tabular.topLine(actcell, flag);
1376 for (int i = sel_row_start; i <= sel_row_end; ++i)
1377 for (int j = sel_col_start; j <= sel_col_end; ++j)
1379 tabular.getCellNumber(i, j),
1384 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1386 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1387 bool lineSet = !tabular.bottomLine(actcell, flag);
1388 for (int i = sel_row_start; i <= sel_row_end; ++i)
1389 for (int j = sel_col_start; j <= sel_col_end; ++j)
1390 tabular.setBottomLine(
1391 tabular.getCellNumber(i, j),
1397 case LyXTabular::M_TOGGLE_LINE_LEFT:
1399 case LyXTabular::TOGGLE_LINE_LEFT: {
1400 bool lineSet = !tabular.leftLine(actcell, flag);
1401 for (int i = sel_row_start; i <= sel_row_end; ++i)
1402 for (int j = sel_col_start; j <= sel_col_end; ++j)
1403 tabular.setLeftLine(
1404 tabular.getCellNumber(i,j),
1410 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1412 case LyXTabular::TOGGLE_LINE_RIGHT: {
1413 bool lineSet = !tabular.rightLine(actcell, flag);
1414 for (int i = sel_row_start; i <= sel_row_end; ++i)
1415 for (int j = sel_col_start; j <= sel_col_end; ++j)
1416 tabular.setRightLine(
1417 tabular.getCellNumber(i,j),
1423 case LyXTabular::M_ALIGN_LEFT:
1424 case LyXTabular::M_ALIGN_RIGHT:
1425 case LyXTabular::M_ALIGN_CENTER:
1427 case LyXTabular::ALIGN_LEFT:
1428 case LyXTabular::ALIGN_RIGHT:
1429 case LyXTabular::ALIGN_CENTER:
1430 case LyXTabular::ALIGN_BLOCK:
1431 for (int i = sel_row_start; i <= sel_row_end; ++i)
1432 for (int j = sel_col_start; j <= sel_col_end; ++j)
1433 tabular.setAlignment(
1434 tabular.getCellNumber(i, j),
1439 case LyXTabular::M_VALIGN_TOP:
1440 case LyXTabular::M_VALIGN_BOTTOM:
1441 case LyXTabular::M_VALIGN_MIDDLE:
1443 case LyXTabular::VALIGN_TOP:
1444 case LyXTabular::VALIGN_BOTTOM:
1445 case LyXTabular::VALIGN_MIDDLE:
1446 for (int i = sel_row_start; i <= sel_row_end; ++i)
1447 for (int j = sel_col_start; j <= sel_col_end; ++j)
1448 tabular.setVAlignment(
1449 tabular.getCellNumber(i, j),
1453 case LyXTabular::MULTICOLUMN: {
1454 if (sel_row_start != sel_row_end) {
1455 #ifdef WITH_WARNINGS
1456 #warning Need I say it ? This is horrible.
1458 Alert::error(_("Error setting multicolumn"),
1459 _("You cannot set multicolumn vertically."));
1462 // just multicol for one Single Cell
1463 if (!hasSelection()) {
1464 // check wether we are completly in a multicol
1465 if (tabular.isMultiColumn(actcell))
1466 tabular.unsetMultiColumn(actcell);
1468 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1471 // we have a selection so this means we just add all this
1472 // cells to form a multicolumn cell
1476 if (sel_cell_start > sel_cell_end) {
1477 s_start = sel_cell_end;
1478 s_end = sel_cell_start;
1480 s_start = sel_cell_start;
1481 s_end = sel_cell_end;
1483 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1489 case LyXTabular::SET_ALL_LINES:
1491 case LyXTabular::UNSET_ALL_LINES:
1492 for (int i = sel_row_start; i <= sel_row_end; ++i)
1493 for (int j = sel_col_start; j <= sel_col_end; ++j)
1494 tabular.setAllLines(
1495 tabular.getCellNumber(i,j), setLines);
1498 case LyXTabular::SET_LONGTABULAR:
1499 tabular.setLongTabular(true);
1502 case LyXTabular::UNSET_LONGTABULAR:
1503 tabular.setLongTabular(false);
1506 case LyXTabular::SET_ROTATE_TABULAR:
1507 tabular.setRotateTabular(true);
1510 case LyXTabular::UNSET_ROTATE_TABULAR:
1511 tabular.setRotateTabular(false);
1514 case LyXTabular::SET_ROTATE_CELL:
1515 for (int i = sel_row_start; i <= sel_row_end; ++i)
1516 for (int j = sel_col_start; j <= sel_col_end; ++j)
1517 tabular.setRotateCell(
1518 tabular.getCellNumber(i, j), true);
1521 case LyXTabular::UNSET_ROTATE_CELL:
1522 for (int i = sel_row_start; i <= sel_row_end; ++i)
1523 for (int j = sel_col_start; j <= sel_col_end; ++j)
1524 tabular.setRotateCell(
1525 tabular.getCellNumber(i, j), false);
1528 case LyXTabular::SET_USEBOX: {
1529 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1530 if (val == tabular.getUsebox(actcell))
1531 val = LyXTabular::BOX_NONE;
1532 for (int i = sel_row_start; i <= sel_row_end; ++i)
1533 for (int j = sel_col_start; j <= sel_col_end; ++j)
1534 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1538 case LyXTabular::UNSET_LTFIRSTHEAD:
1540 case LyXTabular::SET_LTFIRSTHEAD:
1541 tabular.getRowOfLTFirstHead(row, ltt);
1542 checkLongtableSpecial(ltt, value, flag);
1543 tabular.setLTHead(row, flag, ltt, true);
1546 case LyXTabular::UNSET_LTHEAD:
1548 case LyXTabular::SET_LTHEAD:
1549 tabular.getRowOfLTHead(row, ltt);
1550 checkLongtableSpecial(ltt, value, flag);
1551 tabular.setLTHead(row, flag, ltt, false);
1554 case LyXTabular::UNSET_LTFOOT:
1556 case LyXTabular::SET_LTFOOT:
1557 tabular.getRowOfLTFoot(row, ltt);
1558 checkLongtableSpecial(ltt, value, flag);
1559 tabular.setLTFoot(row, flag, ltt, false);
1562 case LyXTabular::UNSET_LTLASTFOOT:
1564 case LyXTabular::SET_LTLASTFOOT:
1565 tabular.getRowOfLTLastFoot(row, ltt);
1566 checkLongtableSpecial(ltt, value, flag);
1567 tabular.setLTFoot(row, flag, ltt, true);
1570 case LyXTabular::SET_LTNEWPAGE:
1571 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1574 // dummy stuff just to avoid warnings
1575 case LyXTabular::LAST_ACTION:
1580 InsetTabularMailer(*this).updateDialog(bv);
1584 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1586 UpdatableInset & inset = tabular.getCellInset(actcell);
1589 x = inset.x() + inset.width();
1590 y = inset.descent();
1592 inset.edit(bv, x, y);
1593 bv->cursor().push(&inset);
1594 if (!the_locking_inset)
1597 return the_locking_inset;
1601 bool InsetTabular::showInsetDialog(BufferView * bv) const
1603 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1604 InsetTabularMailer(*this).showDialog(bv);
1609 void InsetTabular::openLayoutDialog(BufferView * bv) const
1613 if (the_locking_inset) {
1614 InsetTabular * inset = static_cast<InsetTabular *>
1615 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
1617 inset->openLayoutDialog(bv);
1622 InsetTabularMailer(*this).showDialog(bv);
1627 // function returns an object as defined in func_status.h:
1628 // states OK, Unknown, Disabled, On, Off.
1630 FuncStatus InsetTabular::getStatus(string const & what) const
1632 int action = LyXTabular::LAST_ACTION;
1636 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1637 string const tmp = tabularFeature[i].feature;
1638 if (tmp == what.substr(0, tmp.length())) {
1639 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1640 // tabularFeatures[i].feature.length())) {
1641 action = tabularFeature[i].action;
1645 if (action == LyXTabular::LAST_ACTION) {
1647 status.unknown(true);
1651 string const argument
1652 = ltrim(what.substr(tabularFeature[i].feature.length()));
1657 LyXTabular::ltType dummyltt;
1661 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1663 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1666 case LyXTabular::SET_PWIDTH:
1667 case LyXTabular::SET_MPWIDTH:
1668 case LyXTabular::SET_SPECIAL_COLUMN:
1669 case LyXTabular::SET_SPECIAL_MULTI:
1670 case LyXTabular::APPEND_ROW:
1671 case LyXTabular::APPEND_COLUMN:
1672 case LyXTabular::DELETE_ROW:
1673 case LyXTabular::DELETE_COLUMN:
1674 case LyXTabular::SET_ALL_LINES:
1675 case LyXTabular::UNSET_ALL_LINES:
1679 case LyXTabular::MULTICOLUMN:
1680 status.setOnOff(tabular.isMultiColumn(actcell));
1683 case LyXTabular::M_TOGGLE_LINE_TOP:
1685 case LyXTabular::TOGGLE_LINE_TOP:
1686 status.setOnOff(tabular.topLine(actcell, flag));
1689 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1691 case LyXTabular::TOGGLE_LINE_BOTTOM:
1692 status.setOnOff(tabular.bottomLine(actcell, flag));
1695 case LyXTabular::M_TOGGLE_LINE_LEFT:
1697 case LyXTabular::TOGGLE_LINE_LEFT:
1698 status.setOnOff(tabular.leftLine(actcell, flag));
1701 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1703 case LyXTabular::TOGGLE_LINE_RIGHT:
1704 status.setOnOff(tabular.rightLine(actcell, flag));
1707 case LyXTabular::M_ALIGN_LEFT:
1709 case LyXTabular::ALIGN_LEFT:
1710 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1713 case LyXTabular::M_ALIGN_RIGHT:
1715 case LyXTabular::ALIGN_RIGHT:
1716 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1719 case LyXTabular::M_ALIGN_CENTER:
1721 case LyXTabular::ALIGN_CENTER:
1722 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1725 case LyXTabular::ALIGN_BLOCK:
1726 status.disabled(tabular.getPWidth(actcell).zero());
1727 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1730 case LyXTabular::M_VALIGN_TOP:
1732 case LyXTabular::VALIGN_TOP:
1734 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1737 case LyXTabular::M_VALIGN_BOTTOM:
1739 case LyXTabular::VALIGN_BOTTOM:
1741 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1744 case LyXTabular::M_VALIGN_MIDDLE:
1746 case LyXTabular::VALIGN_MIDDLE:
1748 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1751 case LyXTabular::SET_LONGTABULAR:
1752 status.setOnOff(tabular.isLongTabular());
1755 case LyXTabular::UNSET_LONGTABULAR:
1756 status.setOnOff(!tabular.isLongTabular());
1759 case LyXTabular::SET_ROTATE_TABULAR:
1760 status.setOnOff(tabular.getRotateTabular());
1763 case LyXTabular::UNSET_ROTATE_TABULAR:
1764 status.setOnOff(!tabular.getRotateTabular());
1767 case LyXTabular::SET_ROTATE_CELL:
1768 status.setOnOff(tabular.getRotateCell(actcell));
1771 case LyXTabular::UNSET_ROTATE_CELL:
1772 status.setOnOff(!tabular.getRotateCell(actcell));
1775 case LyXTabular::SET_USEBOX:
1776 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1779 case LyXTabular::SET_LTFIRSTHEAD:
1780 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1783 case LyXTabular::SET_LTHEAD:
1784 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1787 case LyXTabular::SET_LTFOOT:
1788 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1791 case LyXTabular::SET_LTLASTFOOT:
1792 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1795 case LyXTabular::SET_LTNEWPAGE:
1796 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1801 status.disabled(true);
1808 void InsetTabular::getLabelList(Buffer const & buffer,
1809 std::vector<string> & list) const
1811 tabular.getLabelList(buffer, list);
1815 bool InsetTabular::copySelection(BufferView * bv)
1817 if (!hasSelection())
1820 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1821 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1822 if (sel_col_start > sel_col_end) {
1823 sel_col_start = sel_col_end;
1824 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1826 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1829 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1830 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1831 if (sel_row_start > sel_row_end)
1832 swap(sel_row_start, sel_row_end);
1834 paste_tabular.reset(new LyXTabular(tabular));
1835 paste_tabular->setOwner(this);
1837 for (int i = 0; i < sel_row_start; ++i)
1838 paste_tabular->deleteRow(0);
1840 int const rows = sel_row_end - sel_row_start + 1;
1841 while (paste_tabular->rows() > rows)
1842 paste_tabular->deleteRow(rows);
1844 paste_tabular->setTopLine(0, true, true);
1845 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1848 for (int i = 0; i < sel_col_start; ++i)
1849 paste_tabular->deleteColumn(0);
1851 int const columns = sel_col_end - sel_col_start + 1;
1852 while (paste_tabular->columns() > columns)
1853 paste_tabular->deleteColumn(columns);
1855 paste_tabular->setLeftLine(0, true, true);
1856 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1860 OutputParams const runparams;
1861 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1862 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1863 bv->stuffClipboard(os.str());
1868 bool InsetTabular::pasteSelection(BufferView *)
1873 for (int r1 = 0, r2 = actrow;
1874 r1 < paste_tabular->rows() && r2 < tabular.rows();
1876 for (int c1 = 0, c2 = actcol;
1877 c1 < paste_tabular->columns() && c2 < tabular.columns();
1879 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1880 tabular.isPartOfMultiColumn(r2, c2))
1882 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1886 if (tabular.isPartOfMultiColumn(r2, c2)) {
1890 InsetText & inset = tabular.getCellInset(r2, c2);
1891 inset = paste_tabular->getCellInset(r1, c1);
1892 inset.setOwner(this);
1900 bool InsetTabular::cutSelection(BufferParams const & bp)
1902 if (!hasSelection())
1905 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1906 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1907 if (sel_col_start > sel_col_end) {
1908 sel_col_start = sel_col_end;
1909 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1911 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1914 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1915 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1917 if (sel_row_start > sel_row_end)
1918 swap(sel_row_start, sel_row_end);
1920 if (sel_cell_start > sel_cell_end)
1921 swap(sel_cell_start, sel_cell_end);
1923 for (int i = sel_row_start; i <= sel_row_end; ++i)
1924 for (int j = sel_col_start; j <= sel_col_end; ++j)
1925 tabular.getCellInset(tabular.getCellNumber(i, j))
1926 .clear(bp.tracking_changes);
1931 bool InsetTabular::isRightToLeft(BufferView * bv)
1933 return bv->getParentLanguage(this)->RightToLeft();
1937 int InsetTabular::scroll(bool recursive) const
1939 int sx = UpdatableInset::scroll(false);
1941 if (recursive && the_locking_inset)
1942 sx += the_locking_inset->scroll(recursive);
1948 void InsetTabular::getSelection(int & srow, int & erow,
1949 int & scol, int & ecol) const
1951 int const start = hasSelection() ? sel_cell_start : actcell;
1952 int const end = hasSelection() ? sel_cell_end : actcell;
1954 srow = tabular.row_of_cell(start);
1955 erow = tabular.row_of_cell(end);
1959 scol = tabular.column_of_cell(start);
1960 ecol = tabular.column_of_cell(end);
1964 ecol = tabular.right_column_of_cell(end);
1968 int InsetTabular::numParagraphs() const
1970 return tabular.getNumberOfCells();
1974 LyXText * InsetTabular::getText(int i) const
1976 return i < tabular.getNumberOfCells()
1977 ? tabular.getCellInset(i).getText(0)
1982 void InsetTabular::markErased()
1984 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1985 tabular.getCellInset(cell).markErased();
1989 bool InsetTabular::insetAllowed(InsetOld::Code) const
1995 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1997 const int cell = tabular.getCellFromInset(in);
2000 return tabular.getPWidth(cell).zero();
2002 // this is a workaround for a crash (New, Insert->Tabular,
2003 // Insert->FootNote)
2007 // well we didn't obviously find it so maybe our owner knows more
2008 BOOST_ASSERT(owner());
2009 return owner()->forceDefaultParagraphs(in);
2013 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2016 if (buf.length() <= 0)
2022 string::size_type len = buf.length();
2023 string::size_type p = 0;
2025 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2033 maxCols = max(cols, maxCols);
2039 maxCols = max(cols, maxCols);
2040 LyXTabular * loctab;
2045 paste_tabular.reset(
2046 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2049 paste_tabular->setOwner(this);
2050 loctab = paste_tabular.get();
2059 string::size_type op = 0;
2060 int cells = loctab->getNumberOfCells();
2063 rows = loctab->rows();
2064 int const columns = loctab->columns();
2066 while (cell < cells && p < len && row < rows &&
2067 (p = buf.find_first_of("\t\n", p)) != string::npos)
2073 // we can only set this if we are not too far right
2074 if (cols < columns) {
2075 InsetText & inset = loctab->getCellInset(cell);
2076 LyXFont const font = inset.text_.
2077 getFont(inset.paragraphs().begin(), 0);
2078 inset.setText(buf.substr(op, p - op), font);
2084 // we can only set this if we are not too far right
2085 if (cols < columns) {
2086 InsetText & inset = tabular.getCellInset(cell);
2087 LyXFont const font = inset.text_.
2088 getFont(inset.paragraphs().begin(), 0);
2089 inset.setText(buf.substr(op, p - op), font);
2094 cell = loctab->getCellNumber(row, cols);
2100 // check for the last cell if there is no trailing '\n'
2101 if (cell < cells && op < len) {
2102 InsetText & inset = loctab->getCellInset(cell);
2103 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2104 inset.setText(buf.substr(op, len - op), font);
2111 void InsetTabular::addPreview(PreviewLoader & loader) const
2113 int const rows = tabular.rows();
2114 int const columns = tabular.columns();
2115 for (int i = 0; i < rows; ++i) {
2116 for (int j = 0; j < columns; ++j)
2117 tabular.getCellInset(i, j).addPreview(loader);
2122 string const InsetTabularMailer::name_("tabular");
2124 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2125 : inset_(const_cast<InsetTabular &>(inset))
2129 string const InsetTabularMailer::inset2string(Buffer const &) const
2131 return params2string(inset_);
2135 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2137 istringstream data(in);
2139 lex.setStream(data);
2141 #warning CHECK verify that this is a sane value to return.
2147 string const token = lex.getString();
2155 string const token = lex.getString();
2156 if (token != "\\active_cell")
2159 cell = lex.getInteger();
2162 // This is part of the inset proper that is usually swallowed
2163 // by Buffer::readInset
2166 string const token = lex.getString();
2167 if (token != "Tabular")
2174 Buffer const & buffer = inset.buffer();
2175 inset.read(buffer, lex);
2177 // We can't set the active cell, but we can tell the frontend
2183 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2186 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2187 inset.write(inset.buffer(), data);
2188 data << "\\end_inset\n";