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"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
30 #include "undo_funcs.h"
31 #include "WordLangTuple.h"
33 #include "frontends/Alert.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/Painter.h"
38 #include "support/std_sstream.h"
40 using lyx::graphics::PreviewLoader;
42 using lyx::support::ltrim;
43 using lyx::support::strToInt;
50 using std::istringstream;
52 using std::ostringstream;
57 int const ADD_TO_HEIGHT = 2;
58 int const ADD_TO_TABULAR_WIDTH = 2;
61 LyXTabular * paste_tabular = 0;
64 struct TabularFeature {
65 LyXTabular::Feature action;
70 TabularFeature tabularFeature[] =
72 { LyXTabular::APPEND_ROW, "append-row" },
73 { LyXTabular::APPEND_COLUMN, "append-column" },
74 { LyXTabular::DELETE_ROW, "delete-row" },
75 { LyXTabular::DELETE_COLUMN, "delete-column" },
76 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
77 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
78 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
79 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
80 { LyXTabular::ALIGN_LEFT, "align-left" },
81 { LyXTabular::ALIGN_RIGHT, "align-right" },
82 { LyXTabular::ALIGN_CENTER, "align-center" },
83 { LyXTabular::ALIGN_BLOCK, "align-block" },
84 { LyXTabular::VALIGN_TOP, "valign-top" },
85 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
86 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
87 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
88 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
89 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
90 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
91 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
92 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
93 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
94 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
95 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
96 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
97 { LyXTabular::MULTICOLUMN, "multicolumn" },
98 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
99 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
100 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
101 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
102 { LyXTabular::SET_PWIDTH, "set-pwidth" },
103 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
104 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
105 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
106 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
107 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
108 { LyXTabular::SET_USEBOX, "set-usebox" },
109 { LyXTabular::SET_LTHEAD, "set-lthead" },
110 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
111 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
112 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
113 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
114 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
115 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
116 { LyXTabular::LAST_ACTION, "" }
120 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
121 bool operator()(TabularFeature & tf)
123 return tf.action == feature_;
126 LyXTabular::Feature feature_;
132 string const featureAsString(LyXTabular::Feature feature)
134 TabularFeature * it = tabularFeature;
135 TabularFeature * end = it +
136 sizeof(tabularFeature) / sizeof(TabularFeature);
137 it = std::find_if(it, end, FindFeature(feature));
138 return (it == end) ? string() : it->feature;
142 bool InsetTabular::hasPasteBuffer() const
144 return (paste_tabular != 0);
148 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
149 : tabular(buf.params(), this, max(rows, 1), max(columns, 1)),
150 buffer_(&buf), cursorx_(0), cursory_(0)
152 // for now make it always display as display() inset
154 the_locking_inset = 0;
155 old_locking_inset = 0;
158 actrow = actcell = 0;
166 InsetTabular::InsetTabular(InsetTabular const & tab)
167 : UpdatableInset(tab),
168 tabular(tab.buffer_->params(), this, tab.tabular),
169 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
171 the_locking_inset = 0;
172 old_locking_inset = 0;
175 actrow = actcell = 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 BufferView * InsetTabular::view() const
208 void InsetTabular::buffer(Buffer * b)
214 void InsetTabular::write(Buffer const & buf, ostream & os) const
216 os << "Tabular" << endl;
217 tabular.write(buf, os);
221 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
223 bool const old_format = (lex.getString() == "\\LyXTable");
225 tabular.read(buf, lex);
231 string token = lex.getString();
232 while (lex.isOK() && (token != "\\end_inset")) {
234 token = lex.getString();
236 if (token != "\\end_inset") {
237 lex.printError("Missing \\end_inset at this point. "
243 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
245 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
246 // mi.base.textwidth << "\n";
248 lyxerr << "InsetTabular::metrics: need bv" << endl;
252 calculate_dimensions_of_cells(mi);
254 dim.asc = tabular.getAscentOfRow(0);
255 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
256 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
261 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
263 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
265 BufferView * bv = pi.base.bv;
268 UpdatableInset::draw(pi, x, y);
276 x += ADD_TO_TABULAR_WIDTH;
279 first_visible_cell = -1;
280 for (int i = 0; i < tabular.rows(); ++i) {
282 cell = tabular.getCellNumber(i, 0);
283 if (y + tabular.getDescentOfRow(i) <= 0 &&
284 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
286 y += tabular.getDescentOfRow(i) +
287 tabular.getAscentOfRow(i + 1) +
288 tabular.getAdditionalHeight(i + 1);
291 for (int j = 0; j < tabular.columns(); ++j) {
292 if (nx > bv->workWidth())
294 if (tabular.isPartOfMultiColumn(i, j))
296 int cx = nx + tabular.getBeginningOfTextInCell(cell);
297 if (first_visible_cell < 0)
298 first_visible_cell = cell;
299 if (hasSelection()) {
300 drawCellSelection(pi.pain, nx, y, i, j, cell);
303 tabular.getCellInset(cell).draw(pi, cx, y);
304 drawCellLines(pi.pain, nx, y, i, cell);
305 nx += tabular.getWidthOfColumn(cell);
309 // Would be nice, but for some completely unfathomable reason,
310 // on a col resize to a new fixed width, even though the insettexts
311 // are resized, the cell isn't, but drawing all cells in a tall table
312 // has the desired effect somehow. Complete dark magic.
314 // avoiding drawing the rest of a long table is
315 // a pretty big speedup
316 if (y > bv->workHeight())
320 y += tabular.getDescentOfRow(i) +
321 tabular.getAscentOfRow(i + 1) +
322 tabular.getAdditionalHeight(i + 1);
327 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
328 int row, int cell) const
330 int x2 = x + tabular.getWidthOfColumn(cell);
333 if (!tabular.topAlreadyDrawn(cell)) {
334 on_off = !tabular.topLine(cell);
335 pain.line(x, y - tabular.getAscentOfRow(row),
336 x2, y - tabular.getAscentOfRow(row),
337 on_off ? LColor::tabularonoffline : LColor::tabularline,
338 on_off ? Painter::line_onoffdash : Painter::line_solid);
340 on_off = !tabular.bottomLine(cell);
341 pain.line(x, y + tabular.getDescentOfRow(row),
342 x2, y + tabular.getDescentOfRow(row),
343 on_off ? LColor::tabularonoffline : LColor::tabularline,
344 on_off ? Painter::line_onoffdash : Painter::line_solid);
345 if (!tabular.leftAlreadyDrawn(cell)) {
346 on_off = !tabular.leftLine(cell);
347 pain.line(x, y - tabular.getAscentOfRow(row),
348 x, y + tabular.getDescentOfRow(row),
349 on_off ? LColor::tabularonoffline : LColor::tabularline,
350 on_off ? Painter::line_onoffdash : Painter::line_solid);
352 on_off = !tabular.rightLine(cell);
353 pain.line(x2 - tabular.getAdditionalWidth(cell),
354 y - tabular.getAscentOfRow(row),
355 x2 - tabular.getAdditionalWidth(cell),
356 y + tabular.getDescentOfRow(row),
357 on_off ? LColor::tabularonoffline : LColor::tabularline,
358 on_off ? Painter::line_onoffdash : Painter::line_solid);
362 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
363 int row, int column, int cell) const
365 BOOST_ASSERT(hasSelection());
366 int cs = tabular.column_of_cell(sel_cell_start);
367 int ce = tabular.column_of_cell(sel_cell_end);
370 cs = tabular.column_of_cell(sel_cell_end);
372 ce = tabular.right_column_of_cell(sel_cell_end);
375 int rs = tabular.row_of_cell(sel_cell_start);
376 int re = tabular.row_of_cell(sel_cell_end);
380 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
381 int w = tabular.getWidthOfColumn(cell);
382 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
383 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
384 w, h, LColor::selection);
389 string const InsetTabular::editMessage() const
391 return _("Opened table");
395 void InsetTabular::insetUnlock(BufferView * bv)
397 if (the_locking_inset) {
398 the_locking_inset->insetUnlock(bv);
400 the_locking_inset = 0;
405 if (scroll(false) || hasSelection()) {
414 void InsetTabular::updateLocal(BufferView * bv) const
416 bv->updateInset(this);
422 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
424 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
429 if (inset == &tabular.getCellInset(actcell)) {
430 lyxerr[Debug::INSETTEXT] << "OK" << endl;
431 the_locking_inset = &tabular.getCellInset(actcell);
434 } else if (!the_locking_inset) {
435 int const n = tabular.getNumberOfCells();
436 int const id = inset->id();
437 for (int i = 0; i < n; ++i) {
438 InsetText * in = &tabular.getCellInset(i);
441 the_locking_inset = in;
446 if (in->getInsetFromID(id)) {
448 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
449 return the_locking_inset->lockInsetInInset(bv, inset);
452 } else if (the_locking_inset && (the_locking_inset == inset)) {
453 lyxerr[Debug::INSETTEXT] << "OK" << endl;
455 } else if (the_locking_inset) {
456 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
457 return the_locking_inset->lockInsetInInset(bv, inset);
459 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
464 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
467 if (!the_locking_inset)
469 if (the_locking_inset == inset) {
470 the_locking_inset->insetUnlock(bv);
472 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
479 // this has to be here otherwise we don't redraw the cell!
480 the_locking_inset = 0;
483 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
484 if (inset->lyxCode() == TABULAR_CODE &&
485 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
486 InsetTabularMailer(*this).updateDialog(bv);
495 int InsetTabular::insetInInsetY() const
497 if (!the_locking_inset)
499 return inset_y + the_locking_inset->insetInInsetY();
503 UpdatableInset * InsetTabular::getLockingInset() const
505 return the_locking_inset ? the_locking_inset->getLockingInset() :
506 const_cast<InsetTabular *>(this);
510 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
514 if (the_locking_inset)
515 return the_locking_inset->getFirstLockingInsetOfType(c);
520 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
522 if (the_locking_inset)
523 return the_locking_inset->insertInset(bv, inset);
528 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
530 if (hasSelection() && cmd.button() == mouse_button::button3)
533 if (hasSelection()) {
535 updateLocal(cmd.view());
538 int const ocell = actcell;
539 BufferView * bv = cmd.view();
543 the_locking_inset = 0;
547 setPos(bv, cmd.x, cmd.y);
550 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
552 if ((ocell == actcell) && the_locking_inset && inset_hit) {
554 FuncRequest cmd1 = cmd;
557 the_locking_inset->localDispatch(cmd1);
561 if (the_locking_inset) {
562 the_locking_inset->insetUnlock(bv);
564 the_locking_inset = 0;
567 if (cmd.button() == mouse_button::button2) {
568 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
572 if (inset_hit && bv->theLockingInset()) {
573 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
574 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
577 FuncRequest cmd1 = cmd;
580 the_locking_inset->localDispatch(cmd1);
585 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
588 if (the_locking_inset) {
589 FuncRequest cmd1 = cmd;
592 ret = the_locking_inset->localDispatch(cmd1);
594 if (cmd.button() == mouse_button::button3 && !ret) {
595 InsetTabularMailer(*this).showDialog(cmd.view());
602 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
604 if (the_locking_inset) {
605 FuncRequest cmd1 = cmd;
608 the_locking_inset->localDispatch(cmd1);
612 BufferView * bv = cmd.view();
613 int const old_cell = actcell;
615 setPos(bv, cmd.x, cmd.y);
616 if (!hasSelection()) {
617 setSelection(actcell, actcell);
619 } else if (old_cell != actcell) {
620 setSelection(sel_cell_start, actcell);
626 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
628 // We need to save the value of the_locking_inset as the call to
629 // the_locking_inset->localDispatch might unlock it.
630 old_locking_inset = the_locking_inset;
631 RESULT result = UpdatableInset::localDispatch(cmd);
632 BufferView * bv = cmd.view();
634 if (cmd.action == LFUN_INSET_EDIT) {
636 if (!bv->lockInset(this)) {
637 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
643 the_locking_inset = 0;
647 if (cmd.argument.size()) {
648 if (cmd.argument == "left") {
649 if (isRightToLeft(bv))
650 actcell = tabular.getLastCellInRow(0);
654 if (isRightToLeft(bv))
655 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
657 actcell = tabular.getNumberOfCells() - 1;
665 setPos(bv, cmd.x, cmd.y);
668 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
669 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
675 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
680 if (cmd.action < 0 && cmd.argument.empty())
683 bool hs = hasSelection();
686 // this one have priority over the locked InsetText, if we're not already
687 // inside another tabular then that one get's priority!
688 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
689 switch (cmd.action) {
690 case LFUN_MOUSE_PRESS:
694 case LFUN_MOUSE_MOTION:
695 lfunMouseMotion(cmd);
698 case LFUN_MOUSE_RELEASE:
699 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
701 case LFUN_CELL_BACKWARD:
702 case LFUN_CELL_FORWARD:
703 unlockInsetInInset(bv, the_locking_inset);
704 if (cmd.action == LFUN_CELL_FORWARD)
705 moveNextCell(bv, old_locking_inset != 0);
707 movePrevCell(bv, old_locking_inset != 0);
711 if (!the_locking_inset)
712 return DISPATCHED_NOUPDATE;
714 // this to avoid compiler warnings.
720 kb_action action = cmd.action;
721 string arg = cmd.argument;
722 if (the_locking_inset) {
723 result = the_locking_inset->localDispatch(cmd);
724 if (result == DISPATCHED_NOUPDATE) {
727 if (sc != scroll()) { // inset has been scrolled
731 } else if (result == DISPATCHED) {
734 } else if (result == FINISHED_UP) {
736 // Make sure to reset status message after
737 // exiting, e.g. math inset
738 bv->owner()->clearMessage();
739 } else if (result == FINISHED_DOWN) {
741 bv->owner()->clearMessage();
742 } else if (result == FINISHED_RIGHT) {
744 bv->owner()->clearMessage();
745 } else if (result == FINISHED) {
746 bv->owner()->clearMessage();
752 // --- Cursor Movements ----------------------------------
753 case LFUN_RIGHTSEL: {
754 int const start = hasSelection() ? sel_cell_start : actcell;
755 if (tabular.isLastCellInRow(actcell)) {
756 setSelection(start, actcell);
761 // if we are starting a selection, only select
762 // the current cell at the beginning
763 if (hasSelection()) {
764 moveRight(bv, false);
767 setSelection(start, end);
772 result = moveRight(bv);
778 int const start = hasSelection() ? sel_cell_start : actcell;
779 if (tabular.isFirstCellInRow(actcell)) {
780 setSelection(start, actcell);
785 // if we are starting a selection, only select
786 // the current cell at the beginning
787 if (hasSelection()) {
791 setSelection(start, end);
796 result = moveLeft(bv);
802 int const start = hasSelection() ? sel_cell_start : actcell;
803 int const ocell = actcell;
804 // if we are starting a selection, only select
805 // the current cell at the beginning
806 if (hasSelection()) {
808 if (ocell == sel_cell_end ||
809 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
810 setSelection(start, tabular.getCellBelow(sel_cell_end));
812 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
814 setSelection(start, start);
820 result = moveDown(bv, old_locking_inset != 0);
826 int const start = hasSelection() ? sel_cell_start : actcell;
827 int const ocell = actcell;
828 // if we are starting a selection, only select
829 // the current cell at the beginning
830 if (hasSelection()) {
832 if ((ocell == sel_cell_end) ||
833 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
834 setSelection(start, tabular.getCellAbove(sel_cell_end));
836 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
838 setSelection(start, start);
844 result = moveUp(bv, old_locking_inset != 0);
853 unlockInsetInInset(bv, the_locking_inset);
854 if (bv->top_y() + bv->painter().paperHeight() <
855 top_baseline + tabular.getHeightOfTabular())
857 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
858 actcell = tabular.getCellBelow(first_visible_cell) + column;
860 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
870 unlockInsetInInset(bv, the_locking_inset);
871 if (top_baseline < 0) {
872 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
873 if (top_baseline > 0)
876 actcell = tabular.getCellBelow(first_visible_cell) + column;
884 // none of these make sense for insettabular,
885 // but we must catch them to prevent any
886 // selection from being confused
890 case LFUN_WORDLEFTSEL:
892 case LFUN_WORDRIGHTSEL:
894 case LFUN_DOWN_PARAGRAPH:
895 case LFUN_DOWN_PARAGRAPHSEL:
896 case LFUN_UP_PARAGRAPH:
897 case LFUN_UP_PARAGRAPHSEL:
903 case LFUN_BEGINNINGBUF:
904 case LFUN_BEGINNINGBUFSEL:
908 case LFUN_LAYOUT_TABULAR: {
909 InsetTabularMailer(*this).showDialog(bv);
912 case LFUN_INSET_DIALOG_UPDATE: {
913 InsetTabularMailer(*this).updateDialog(bv);
916 case LFUN_TABULAR_FEATURE:
917 if (!tabularFeatures(bv, arg))
918 result = UNDISPATCHED;
920 // insert file functions
921 case LFUN_FILE_INSERT_ASCII_PARA:
922 case LFUN_FILE_INSERT_ASCII:
924 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
927 if (insertAsciiString(bv, tmpstr, false))
930 result = UNDISPATCHED;
933 // cut and paste functions
935 if (!copySelection(bv))
939 recordUndo(bv, Undo::DELETE);
940 cutSelection(bv->buffer()->params());
949 case LFUN_PASTESELECTION:
951 string const clip(bv->getClipboard());
955 if (clip.find('\t') != string::npos) {
959 string::size_type len = clip.length();
960 string::size_type p = 0;
963 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
971 maxCols = max(cols, maxCols);
977 maxCols = max(cols, maxCols);
978 delete paste_tabular;
979 paste_tabular = new LyXTabular(bv->buffer()->params(),
980 this, rows, maxCols);
981 string::size_type op = 0;
983 int cells = paste_tabular->getNumberOfCells();
985 while ((cell < cells) && (p < len) &&
986 (p = clip.find_first_of("\t\n", p)) != string::npos) {
991 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
996 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
997 while (cols++ < maxCols)
1005 // check for the last cell if there is no trailing '\n'
1006 if ((cell < cells) && (op < len))
1007 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1010 if (!insertAsciiString(bv, clip, true))
1013 // so that the clipboard is used and it goes on
1015 // and executes LFUN_PASTESELECTION in insettext!
1016 delete paste_tabular;
1021 if (hasPasteBuffer()) {
1022 recordUndo(bv, Undo::INSERT);
1027 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1029 // handle font changing stuff on selection before we lock the inset
1030 // in the default part!
1031 result = UNDISPATCHED;
1042 case LFUN_UNDERLINE:
1043 case LFUN_FONT_SIZE:
1044 if (bv->dispatch(FuncRequest(bv, action, arg)))
1045 result = DISPATCHED;
1051 // we try to activate the actual inset and put this event down to
1052 // the insets dispatch function.
1053 if (result == DISPATCHED || the_locking_inset)
1055 if (activateCellInset(bv)) {
1056 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1057 if (result == UNDISPATCHED || result >= FINISHED) {
1058 unlockInsetInInset(bv, the_locking_inset);
1059 // we need to update if this was requested before
1061 return UNDISPATCHED;
1070 if (result < FINISHED) {
1071 if (!the_locking_inset && bv->fitCursor())
1074 bv->unlockInset(this);
1079 int InsetTabular::latex(Buffer const & buf, ostream & os,
1080 LatexRunParams const & runparams) const
1082 return tabular.latex(buf, os, runparams);
1086 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1089 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1091 return tabular.ascii(buf, os, 0, false, 0);
1095 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1097 return tabular.linuxdoc(buf,os);
1101 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1106 // if the table is inside a float it doesn't need the informaltable
1107 // wrapper. Search for it.
1108 for (master = owner();
1109 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1110 master = master->owner());
1113 os << "<informaltable>";
1118 ret += tabular.docbook(buf, os, mixcont);
1120 os << "</informaltable>";
1129 void InsetTabular::validate(LaTeXFeatures & features) const
1131 tabular.validate(features);
1135 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1138 // if we have a locking_inset we should have to check only this cell for
1139 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1140 // This is _really_ important (André)
1141 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1144 for (int j = 0; j < tabular.columns(); ++j) {
1148 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1149 tabular.getCellInset(actrow, j).metrics(m, dim);
1150 maxAsc = max(dim.asc, maxAsc);
1151 maxDesc = max(dim.des, maxDesc);
1153 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1154 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1155 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1161 for (int i = 0; i < tabular.rows(); ++i) {
1164 for (int j = 0; j < tabular.columns(); ++j) {
1165 if (tabular.isPartOfMultiColumn(i, j))
1171 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1172 tabular.getCellInset(cell).metrics(m, dim);
1173 maxAsc = max(maxAsc, dim.asc);
1174 maxDesc = max(maxDesc, dim.des);
1175 tabular.setWidthOfCell(cell, dim.wid);
1177 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1178 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1183 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1185 if (the_locking_inset) {
1186 the_locking_inset->getCursor(bv, x, y);
1191 y = cursory_ + InsetTabular::y();
1194 int desc = tabular.getDescentOfRow(actrow);
1196 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1198 y += ADD_TO_HEIGHT * 2;
1199 y += TEXT_TO_INSET_OFFSET;
1203 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1205 if (the_locking_inset) {
1206 the_locking_inset->getCursorPos(bv, x, y);
1209 x = cursorx_ - top_x;
1214 void InsetTabular::fitInsetCursor(BufferView * bv) const
1216 if (the_locking_inset) {
1217 the_locking_inset->fitInsetCursor(bv);
1222 int const asc = font_metrics::maxAscent(font);
1223 int const desc = font_metrics::maxDescent(font);
1226 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1230 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1233 actcell = actrow = actcol = 0;
1234 int ly = tabular.getDescentOfRow(actrow);
1236 // first search the right row
1237 while (ly < y && actrow + 1 < tabular.rows()) {
1238 cursory_ += tabular.getDescentOfRow(actrow) +
1239 tabular.getAscentOfRow(actrow + 1) +
1240 tabular.getAdditionalHeight(actrow + 1);
1242 ly = cursory_ + tabular.getDescentOfRow(actrow);
1244 actcell = tabular.getCellNumber(actrow, actcol);
1246 // now search the right column
1247 int lx = tabular.getWidthOfColumn(actcell) -
1248 tabular.getAdditionalWidth(actcell);
1250 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1251 lx += tabular.getWidthOfColumn(actcell + 1)
1252 + tabular.getAdditionalWidth(actcell);
1254 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1259 int InsetTabular::getCellXPos(int cell) const
1263 for (; !tabular.isFirstCellInRow(c); --c)
1265 int lx = tabular.getWidthOfColumn(cell);
1266 for (; c < cell; ++c)
1267 lx += tabular.getWidthOfColumn(c);
1269 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1273 void InsetTabular::resetPos(BufferView * bv) const
1275 #ifdef WITH_WARNINGS
1276 #warning This should be fixed in the right manner (20011128 Jug)
1278 // fast hack to fix infinite repaintings!
1279 if (in_reset_pos > 0)
1283 actcol = tabular.column_of_cell(actcell);
1286 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1287 if (tabular.isLastCellInRow(cell)) {
1288 cursory_ += tabular.getDescentOfRow(actrow) +
1289 tabular.getAscentOfRow(actrow + 1) +
1290 tabular.getAdditionalHeight(actrow + 1);
1295 if (the_locking_inset)
1299 // we need this only from here on!!!
1301 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1302 int new_x = getCellXPos(actcell);
1303 int old_x = cursorx_;
1306 // cursor.x(getCellXPos(actcell) + offset);
1307 if (actcol < tabular.columns() - 1 && scroll(false) &&
1308 tabular.getWidthOfTabular() < bv->workWidth()-20)
1312 } else if (the_locking_inset &&
1313 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1315 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1316 if (xx > bv->workWidth()-20) {
1317 scroll(bv, -(xx - bv->workWidth() + 60));
1319 } else if (xx < 20) {
1327 } else if (cursorx_ - offset > 20 &&
1328 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1329 > bv->workWidth() - 20) {
1330 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1332 } else if (cursorx_ - offset < 20) {
1333 scroll(bv, 20 - cursorx_ + offset);
1335 } else if (scroll() && top_x > 20 &&
1336 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1337 scroll(bv, old_x - cursorx_);
1340 if (the_locking_inset) {
1341 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1344 if ((!the_locking_inset ||
1345 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1346 actcell != oldcell) {
1347 InsetTabularMailer(*this).updateDialog(bv);
1354 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1356 if (lock && !old_locking_inset) {
1357 if (activateCellInset(bv))
1360 bool moved = isRightToLeft(bv)
1361 ? movePrevCell(bv) : moveNextCell(bv);
1363 return FINISHED_RIGHT;
1364 if (lock && activateCellInset(bv))
1368 return DISPATCHED_NOUPDATE;
1372 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1374 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1377 if (lock) { // behind the inset
1378 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1382 return DISPATCHED_NOUPDATE;
1386 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1388 int const ocell = actcell;
1389 actcell = tabular.getCellAbove(actcell);
1390 if (actcell == ocell) // we moved out of the inset
1396 if (old_locking_inset) {
1397 old_locking_inset->getCursorPos(bv, x, y);
1398 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1400 if (activateCellInset(bv, x, 0))
1403 return DISPATCHED_NOUPDATE;
1407 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1409 int const ocell = actcell;
1410 actcell = tabular.getCellBelow(actcell);
1411 if (actcell == ocell) // we moved out of the inset
1412 return FINISHED_DOWN;
1417 if (old_locking_inset) {
1418 old_locking_inset->getCursorPos(bv, x, y);
1419 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1421 if (activateCellInset(bv, x, 0))
1424 return DISPATCHED_NOUPDATE;
1428 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1430 if (isRightToLeft(bv)) {
1431 if (tabular.isFirstCellInRow(actcell)) {
1432 int row = tabular.row_of_cell(actcell);
1433 if (row == tabular.rows() - 1)
1435 actcell = tabular.getLastCellInRow(row);
1436 actcell = tabular.getCellBelow(actcell);
1443 if (tabular.isLastCell(actcell))
1448 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1449 isRightToLeftPar(bv->buffer()->params());
1450 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1457 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1459 if (isRightToLeft(bv)) {
1460 if (tabular.isLastCellInRow(actcell)) {
1461 int row = tabular.row_of_cell(actcell);
1464 actcell = tabular.getFirstCellInRow(row);
1465 actcell = tabular.getCellAbove(actcell);
1467 if (tabular.isLastCell(actcell))
1472 if (!actcell) // first cell
1477 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1478 isRightToLeftPar(bv->buffer()->params());
1479 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1486 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1490 setSelection(0, tabular.getNumberOfCells() - 1);
1492 if (hasSelection()) {
1493 recordUndo(bv, Undo::ATOMIC);
1494 bool const frozen = undo_frozen;
1497 // apply the fontchange on the whole selection
1502 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1503 for(int i = sel_row_start; i <= sel_row_end; ++i)
1504 for(int j = sel_col_start; j <= sel_col_end; ++j)
1505 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1513 if (the_locking_inset)
1514 the_locking_inset->setFont(bv, font, tall);
1518 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1520 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1523 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1524 string const tmp = tabularFeature[i].feature;
1526 if (tmp == what.substr(0, tmp.length())) {
1527 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1528 //tabularFeatures[i].feature.length())) {
1529 action = tabularFeature[i].action;
1533 if (action == LyXTabular::LAST_ACTION)
1537 ltrim(what.substr(tabularFeature[i].feature.length()));
1538 tabularFeatures(bv, action, val);
1544 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1545 string const & special, bool & flag)
1547 if (special == "dl_above") {
1550 } else if (special == "dl_below") {
1551 ltt.bottomDL = flag;
1553 } else if (special == "empty") {
1565 void InsetTabular::tabularFeatures(BufferView * bv,
1566 LyXTabular::Feature feature,
1567 string const & value)
1573 bool setLines = false;
1574 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1575 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1579 case LyXTabular::M_ALIGN_LEFT:
1580 case LyXTabular::ALIGN_LEFT:
1581 setAlign = LYX_ALIGN_LEFT;
1584 case LyXTabular::M_ALIGN_RIGHT:
1585 case LyXTabular::ALIGN_RIGHT:
1586 setAlign = LYX_ALIGN_RIGHT;
1589 case LyXTabular::M_ALIGN_CENTER:
1590 case LyXTabular::ALIGN_CENTER:
1591 setAlign = LYX_ALIGN_CENTER;
1594 case LyXTabular::ALIGN_BLOCK:
1595 setAlign = LYX_ALIGN_BLOCK;
1598 case LyXTabular::M_VALIGN_TOP:
1599 case LyXTabular::VALIGN_TOP:
1600 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1603 case LyXTabular::M_VALIGN_BOTTOM:
1604 case LyXTabular::VALIGN_BOTTOM:
1605 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1608 case LyXTabular::M_VALIGN_MIDDLE:
1609 case LyXTabular::VALIGN_MIDDLE:
1610 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1617 if (hasSelection()) {
1618 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1620 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1621 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1623 recordUndo(bv, Undo::ATOMIC);
1625 int row = tabular.row_of_cell(actcell);
1626 int column = tabular.column_of_cell(actcell);
1628 LyXTabular::ltType ltt;
1632 case LyXTabular::SET_PWIDTH:
1634 LyXLength const vallen(value);
1635 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1637 bool const update = (tmplen != vallen);
1638 tabular.setColumnPWidth(actcell, vallen);
1640 // We need this otherwise we won't resize
1641 // the insettext of the active cell (if any)
1642 // until later (see InsetText::do_resize)
1643 unlockInsetInInset(bv, the_locking_inset);
1648 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1649 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1650 else if (!vallen.zero()
1651 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1652 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1656 case LyXTabular::SET_MPWIDTH:
1658 LyXLength const vallen(value);
1659 LyXLength const & tmplen = tabular.getPWidth(actcell);
1661 bool const update = (tmplen != vallen);
1662 tabular.setMColumnPWidth(actcell, vallen);
1664 // We need this otherwise we won't resize
1665 // the insettext of the active cell (if any)
1666 // until later (see InsetText::do_resize)
1667 unlockInsetInInset(bv, the_locking_inset);
1672 case LyXTabular::SET_SPECIAL_COLUMN:
1673 case LyXTabular::SET_SPECIAL_MULTI:
1674 tabular.setAlignSpecial(actcell,value,feature);
1677 case LyXTabular::APPEND_ROW:
1678 // append the row into the tabular
1679 unlockInsetInInset(bv, the_locking_inset);
1680 tabular.appendRow(bv->buffer()->params(), actcell);
1683 case LyXTabular::APPEND_COLUMN:
1684 // append the column into the tabular
1685 unlockInsetInInset(bv, the_locking_inset);
1686 tabular.appendColumn(bv->buffer()->params(), actcell);
1687 actcell = tabular.getCellNumber(row, column);
1690 case LyXTabular::DELETE_ROW:
1691 unlockInsetInInset(bv, the_locking_inset);
1692 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1693 tabular.deleteRow(sel_row_start);
1695 if (sel_row_start >= tabular.rows())
1697 actcell = tabular.getCellNumber(sel_row_start, column);
1701 case LyXTabular::DELETE_COLUMN:
1702 unlockInsetInInset(bv, the_locking_inset);
1703 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1704 tabular.deleteColumn(sel_col_start);
1706 if (sel_col_start >= tabular.columns())
1708 actcell = tabular.getCellNumber(row, sel_col_start);
1712 case LyXTabular::M_TOGGLE_LINE_TOP:
1714 case LyXTabular::TOGGLE_LINE_TOP:
1716 bool lineSet = !tabular.topLine(actcell, flag);
1717 for (int i = sel_row_start; i <= sel_row_end; ++i)
1718 for (int j = sel_col_start; j <= sel_col_end; ++j)
1720 tabular.getCellNumber(i, j),
1726 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1728 case LyXTabular::TOGGLE_LINE_BOTTOM:
1730 bool lineSet = !tabular.bottomLine(actcell, flag);
1731 for (int i = sel_row_start; i <= sel_row_end; ++i)
1732 for (int j = sel_col_start; j <= sel_col_end; ++j)
1733 tabular.setBottomLine(
1734 tabular.getCellNumber(i, j),
1741 case LyXTabular::M_TOGGLE_LINE_LEFT:
1743 case LyXTabular::TOGGLE_LINE_LEFT:
1745 bool lineSet = !tabular.leftLine(actcell, flag);
1746 for (int i = sel_row_start; i <= sel_row_end; ++i)
1747 for (int j = sel_col_start; j <= sel_col_end; ++j)
1748 tabular.setLeftLine(
1749 tabular.getCellNumber(i,j),
1756 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1758 case LyXTabular::TOGGLE_LINE_RIGHT:
1760 bool lineSet = !tabular.rightLine(actcell, flag);
1761 for (int i = sel_row_start; i <= sel_row_end; ++i)
1762 for (int j = sel_col_start; j <= sel_col_end; ++j)
1763 tabular.setRightLine(
1764 tabular.getCellNumber(i,j),
1771 case LyXTabular::M_ALIGN_LEFT:
1772 case LyXTabular::M_ALIGN_RIGHT:
1773 case LyXTabular::M_ALIGN_CENTER:
1775 case LyXTabular::ALIGN_LEFT:
1776 case LyXTabular::ALIGN_RIGHT:
1777 case LyXTabular::ALIGN_CENTER:
1778 case LyXTabular::ALIGN_BLOCK:
1779 for (int i = sel_row_start; i <= sel_row_end; ++i)
1780 for (int j = sel_col_start; j <= sel_col_end; ++j)
1781 tabular.setAlignment(
1782 tabular.getCellNumber(i, j),
1788 case LyXTabular::M_VALIGN_TOP:
1789 case LyXTabular::M_VALIGN_BOTTOM:
1790 case LyXTabular::M_VALIGN_MIDDLE:
1792 case LyXTabular::VALIGN_TOP:
1793 case LyXTabular::VALIGN_BOTTOM:
1794 case LyXTabular::VALIGN_MIDDLE:
1795 for (int i = sel_row_start; i <= sel_row_end; ++i)
1796 for (int j = sel_col_start; j <= sel_col_end; ++j)
1797 tabular.setVAlignment(
1798 tabular.getCellNumber(i, j),
1803 case LyXTabular::MULTICOLUMN: {
1804 if (sel_row_start != sel_row_end) {
1805 #ifdef WITH_WARNINGS
1806 #warning Need I say it ? This is horrible.
1808 Alert::error(_("Error setting multicolumn"),
1809 _("You cannot set multicolumn vertically."));
1812 // just multicol for one Single Cell
1813 if (!hasSelection()) {
1814 // check wether we are completly in a multicol
1815 if (tabular.isMultiColumn(actcell))
1816 tabular.unsetMultiColumn(actcell);
1818 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1822 // we have a selection so this means we just add all this
1823 // cells to form a multicolumn cell
1827 if (sel_cell_start > sel_cell_end) {
1828 s_start = sel_cell_end;
1829 s_end = sel_cell_start;
1831 s_start = sel_cell_start;
1832 s_end = sel_cell_end;
1834 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1841 case LyXTabular::SET_ALL_LINES:
1843 case LyXTabular::UNSET_ALL_LINES:
1844 for (int i = sel_row_start; i <= sel_row_end; ++i)
1845 for (int j = sel_col_start; j <= sel_col_end; ++j)
1846 tabular.setAllLines(
1847 tabular.getCellNumber(i,j), setLines);
1851 case LyXTabular::SET_LONGTABULAR:
1852 tabular.setLongTabular(true);
1853 updateLocal(bv); // because this toggles displayed
1856 case LyXTabular::UNSET_LONGTABULAR:
1857 tabular.setLongTabular(false);
1858 updateLocal(bv); // because this toggles displayed
1861 case LyXTabular::SET_ROTATE_TABULAR:
1862 tabular.setRotateTabular(true);
1865 case LyXTabular::UNSET_ROTATE_TABULAR:
1866 tabular.setRotateTabular(false);
1869 case LyXTabular::SET_ROTATE_CELL:
1870 for (int i = sel_row_start; i <= sel_row_end; ++i)
1871 for (int j = sel_col_start; j<=sel_col_end; ++j)
1872 tabular.setRotateCell(
1873 tabular.getCellNumber(i, j), true);
1876 case LyXTabular::UNSET_ROTATE_CELL:
1877 for (int i = sel_row_start; i <= sel_row_end; ++i)
1878 for (int j = sel_col_start; j <= sel_col_end; ++j)
1879 tabular.setRotateCell(
1880 tabular.getCellNumber(i, j), false);
1883 case LyXTabular::SET_USEBOX: {
1884 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1885 if (val == tabular.getUsebox(actcell))
1886 val = LyXTabular::BOX_NONE;
1887 for (int i = sel_row_start; i <= sel_row_end; ++i)
1888 for (int j = sel_col_start; j <= sel_col_end; ++j)
1889 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1893 case LyXTabular::UNSET_LTFIRSTHEAD:
1895 case LyXTabular::SET_LTFIRSTHEAD:
1896 tabular.getRowOfLTFirstHead(row, ltt);
1897 checkLongtableSpecial(ltt, value, flag);
1898 tabular.setLTHead(row, flag, ltt, true);
1901 case LyXTabular::UNSET_LTHEAD:
1903 case LyXTabular::SET_LTHEAD:
1904 tabular.getRowOfLTHead(row, ltt);
1905 checkLongtableSpecial(ltt, value, flag);
1906 tabular.setLTHead(row, flag, ltt, false);
1909 case LyXTabular::UNSET_LTFOOT:
1911 case LyXTabular::SET_LTFOOT:
1912 tabular.getRowOfLTFoot(row, ltt);
1913 checkLongtableSpecial(ltt, value, flag);
1914 tabular.setLTFoot(row, flag, ltt, false);
1917 case LyXTabular::UNSET_LTLASTFOOT:
1919 case LyXTabular::SET_LTLASTFOOT:
1920 tabular.getRowOfLTLastFoot(row, ltt);
1921 checkLongtableSpecial(ltt, value, flag);
1922 tabular.setLTFoot(row, flag, ltt, true);
1925 case LyXTabular::SET_LTNEWPAGE: {
1926 bool what = !tabular.getLTNewPage(row);
1927 tabular.setLTNewPage(row, what);
1931 // dummy stuff just to avoid warnings
1932 case LyXTabular::LAST_ACTION:
1936 InsetTabularMailer(*this).updateDialog(bv);
1940 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1941 mouse_button::state button, bool behind)
1943 UpdatableInset & inset = tabular.getCellInset(actcell);
1946 x = inset.x() + inset.width();
1947 y = inset.descent();
1949 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1950 //inset_y = cursory_;
1951 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1952 if (!the_locking_inset)
1955 return the_locking_inset;
1959 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1960 mouse_button::state button)
1962 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1964 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1968 bool InsetTabular::insetHit(BufferView *, int x, int) const
1970 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1974 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1976 //resizeLyXText(bv, recursive);
1980 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1981 bool const recursive) const
1983 if (the_locking_inset)
1984 return the_locking_inset->getLyXText(bv, recursive);
1985 return InsetOld::getLyXText(bv, recursive);
1989 bool InsetTabular::showInsetDialog(BufferView * bv) const
1991 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1992 InsetTabularMailer(*this).showDialog(bv);
1997 void InsetTabular::openLayoutDialog(BufferView * bv) const
1999 if (the_locking_inset) {
2000 InsetTabular * inset = static_cast<InsetTabular *>
2001 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2003 inset->openLayoutDialog(bv);
2007 InsetTabularMailer(*this).showDialog(bv);
2012 // function returns an object as defined in func_status.h:
2013 // states OK, Unknown, Disabled, On, Off.
2015 FuncStatus InsetTabular::getStatus(string const & what) const
2017 int action = LyXTabular::LAST_ACTION;
2021 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2022 string const tmp = tabularFeature[i].feature;
2023 if (tmp == what.substr(0, tmp.length())) {
2024 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2025 // tabularFeatures[i].feature.length())) {
2026 action = tabularFeature[i].action;
2030 if (action == LyXTabular::LAST_ACTION) {
2032 return status.unknown(true);
2035 string const argument
2036 = ltrim(what.substr(tabularFeature[i].feature.length()));
2041 LyXTabular::ltType dummyltt;
2045 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2047 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2050 case LyXTabular::SET_PWIDTH:
2051 case LyXTabular::SET_MPWIDTH:
2052 case LyXTabular::SET_SPECIAL_COLUMN:
2053 case LyXTabular::SET_SPECIAL_MULTI:
2054 case LyXTabular::APPEND_ROW:
2055 case LyXTabular::APPEND_COLUMN:
2056 case LyXTabular::DELETE_ROW:
2057 case LyXTabular::DELETE_COLUMN:
2058 case LyXTabular::SET_ALL_LINES:
2059 case LyXTabular::UNSET_ALL_LINES:
2060 return status.clear();
2062 case LyXTabular::MULTICOLUMN:
2063 status.setOnOff(tabular.isMultiColumn(actcell));
2066 case LyXTabular::M_TOGGLE_LINE_TOP:
2068 case LyXTabular::TOGGLE_LINE_TOP:
2069 status.setOnOff(tabular.topLine(actcell, flag));
2072 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2074 case LyXTabular::TOGGLE_LINE_BOTTOM:
2075 status.setOnOff(tabular.bottomLine(actcell, flag));
2078 case LyXTabular::M_TOGGLE_LINE_LEFT:
2080 case LyXTabular::TOGGLE_LINE_LEFT:
2081 status.setOnOff(tabular.leftLine(actcell, flag));
2084 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2086 case LyXTabular::TOGGLE_LINE_RIGHT:
2087 status.setOnOff(tabular.rightLine(actcell, flag));
2090 case LyXTabular::M_ALIGN_LEFT:
2092 case LyXTabular::ALIGN_LEFT:
2093 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2096 case LyXTabular::M_ALIGN_RIGHT:
2098 case LyXTabular::ALIGN_RIGHT:
2099 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2102 case LyXTabular::M_ALIGN_CENTER:
2104 case LyXTabular::ALIGN_CENTER:
2105 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2108 case LyXTabular::ALIGN_BLOCK:
2109 status.disabled(tabular.getPWidth(actcell).zero());
2110 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2113 case LyXTabular::M_VALIGN_TOP:
2115 case LyXTabular::VALIGN_TOP:
2116 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2119 case LyXTabular::M_VALIGN_BOTTOM:
2121 case LyXTabular::VALIGN_BOTTOM:
2122 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2125 case LyXTabular::M_VALIGN_MIDDLE:
2127 case LyXTabular::VALIGN_MIDDLE:
2128 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2131 case LyXTabular::SET_LONGTABULAR:
2132 status.setOnOff(tabular.isLongTabular());
2135 case LyXTabular::UNSET_LONGTABULAR:
2136 status.setOnOff(!tabular.isLongTabular());
2139 case LyXTabular::SET_ROTATE_TABULAR:
2140 status.setOnOff(tabular.getRotateTabular());
2143 case LyXTabular::UNSET_ROTATE_TABULAR:
2144 status.setOnOff(!tabular.getRotateTabular());
2147 case LyXTabular::SET_ROTATE_CELL:
2148 status.setOnOff(tabular.getRotateCell(actcell));
2151 case LyXTabular::UNSET_ROTATE_CELL:
2152 status.setOnOff(!tabular.getRotateCell(actcell));
2155 case LyXTabular::SET_USEBOX:
2156 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2159 case LyXTabular::SET_LTFIRSTHEAD:
2160 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2163 case LyXTabular::SET_LTHEAD:
2164 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2167 case LyXTabular::SET_LTFOOT:
2168 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2171 case LyXTabular::SET_LTLASTFOOT:
2172 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2175 case LyXTabular::SET_LTNEWPAGE:
2176 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2181 status.disabled(true);
2188 void InsetTabular::getLabelList(Buffer const & buffer,
2189 std::vector<string> & list) const
2191 tabular.getLabelList(buffer, list);
2195 bool InsetTabular::copySelection(BufferView * bv)
2197 if (!hasSelection())
2200 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2201 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2202 if (sel_col_start > sel_col_end) {
2203 sel_col_start = sel_col_end;
2204 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2206 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2209 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2210 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2211 if (sel_row_start > sel_row_end)
2212 swap(sel_row_start, sel_row_end);
2214 delete paste_tabular;
2215 paste_tabular = new LyXTabular(bv->buffer()->params(), this, tabular);
2217 for (int i = 0; i < sel_row_start; ++i)
2218 paste_tabular->deleteRow(0);
2220 int const rows = sel_row_end - sel_row_start + 1;
2221 while (paste_tabular->rows() > rows)
2222 paste_tabular->deleteRow(rows);
2224 paste_tabular->setTopLine(0, true, true);
2225 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2228 for (int i = 0; i < sel_col_start; ++i)
2229 paste_tabular->deleteColumn(0);
2231 int const columns = sel_col_end - sel_col_start + 1;
2232 while (paste_tabular->columns() > columns)
2233 paste_tabular->deleteColumn(columns);
2235 paste_tabular->setLeftLine(0, true, true);
2236 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2240 paste_tabular->ascii(*bv->buffer(), os,
2241 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2242 bv->stuffClipboard(os.str());
2247 bool InsetTabular::pasteSelection(BufferView * bv)
2252 for (int r1 = 0, r2 = actrow;
2253 r1 < paste_tabular->rows() && r2 < tabular.rows();
2255 for (int c1 = 0, c2 = actcol;
2256 c1 < paste_tabular->columns() && c2 < tabular.columns();
2258 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2259 tabular.isPartOfMultiColumn(r2, c2))
2261 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2265 if (tabular.isPartOfMultiColumn(r2, c2)) {
2269 InsetText & inset = tabular.getCellInset(r2, c2);
2270 inset = paste_tabular->getCellInset(r1, c1);
2271 inset.setOwner(this);
2272 inset.deleteLyXText(bv);
2280 bool InsetTabular::cutSelection(BufferParams const & bp)
2282 if (!hasSelection())
2285 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2286 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2287 if (sel_col_start > sel_col_end) {
2288 sel_col_start = sel_col_end;
2289 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2291 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2294 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2295 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2297 if (sel_row_start > sel_row_end)
2298 swap(sel_row_start, sel_row_end);
2300 if (sel_cell_start > sel_cell_end)
2301 swap(sel_cell_start, sel_cell_end);
2303 for (int i = sel_row_start; i <= sel_row_end; ++i)
2304 for (int j = sel_col_start; j <= sel_col_end; ++j)
2305 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2310 bool InsetTabular::isRightToLeft(BufferView * bv)
2312 return bv->getParentLanguage(this)->RightToLeft();
2316 int InsetTabular::scroll(bool recursive) const
2318 int sx = UpdatableInset::scroll(false);
2320 if (recursive && the_locking_inset)
2321 sx += the_locking_inset->scroll(recursive);
2327 void InsetTabular::getSelection(int & srow, int & erow,
2328 int & scol, int & ecol) const
2330 int const start = hasSelection() ? sel_cell_start : actcell;
2331 int const end = hasSelection() ? sel_cell_end : actcell;
2333 srow = tabular.row_of_cell(start);
2334 erow = tabular.row_of_cell(end);
2338 scol = tabular.column_of_cell(start);
2339 ecol = tabular.column_of_cell(end);
2343 ecol = tabular.right_column_of_cell(end);
2347 ParagraphList * InsetTabular::getParagraphs(int i) const
2349 return (i < tabular.getNumberOfCells())
2350 ? tabular.getCellInset(i).getParagraphs(0)
2355 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2357 if (the_locking_inset)
2358 return the_locking_inset->cursor(bv);
2359 return InsetOld::cursor(bv);
2363 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2366 return const_cast<InsetTabular *>(this);
2368 for (int i = 0; i < tabular.rows(); ++i) {
2369 for (int j = 0; j < tabular.columns(); ++j) {
2370 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2380 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2382 if (the_locking_inset) {
2383 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2384 if (!word.word().empty())
2386 if (tabular.isLastCell(actcell)) {
2387 bv->unlockInset(const_cast<InsetTabular *>(this));
2388 return WordLangTuple();
2392 // otherwise we have to lock the next inset and ask for it's selecttion
2393 tabular.getCellInset(actcell)
2394 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2395 WordLangTuple word(selectNextWordInt(bv, value));
2396 if (!word.word().empty())
2402 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2404 // when entering this function the inset should be ALWAYS locked!
2405 BOOST_ASSERT(the_locking_inset);
2407 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2408 if (!word.word().empty())
2411 if (tabular.isLastCell(actcell)) {
2412 bv->unlockInset(const_cast<InsetTabular *>(this));
2413 return WordLangTuple();
2416 // otherwise we have to lock the next inset and ask for it's selecttion
2418 tabular.getCellInset(actcell)
2419 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2420 return selectNextWordInt(bv, value);
2424 void InsetTabular::selectSelectedWord(BufferView * bv)
2426 if (the_locking_inset)
2427 the_locking_inset->selectSelectedWord(bv);
2431 void InsetTabular::markErased()
2433 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2434 tabular.getCellInset(cell).markErased();
2438 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2440 if (the_locking_inset) {
2441 if (the_locking_inset->nextChange(bv, length)) {
2445 if (tabular.isLastCell(actcell))
2449 InsetText & inset = tabular.getCellInset(actcell);
2450 if (inset.nextChange(bv, length)) {
2454 while (!tabular.isLastCell(actcell)) {
2456 InsetText & inset = tabular.getCellInset(actcell);
2457 if (inset.nextChange(bv, length)) {
2466 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2470 if (the_locking_inset) {
2471 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2475 if (tabular.isLastCell(actcell))
2479 InsetText & inset = tabular.getCellInset(cell);
2480 if (inset.searchForward(bv, str, cs, mw)) {
2484 while (!tabular.isLastCell(cell)) {
2486 InsetText & inset = tabular.getCellInset(cell);
2487 if (inset.searchForward(bv, str, cs, mw)) {
2496 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2499 int cell = tabular.getNumberOfCells();
2500 if (the_locking_inset) {
2501 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2510 InsetText & inset = tabular.getCellInset(cell);
2511 if (inset.searchBackward(bv, str, cs, mw)) {
2520 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2522 if (the_locking_inset)
2523 return the_locking_inset->insetAllowed(code);
2524 // we return true here because if the inset is not locked someone
2525 // wants to insert something in one of our insettexts and we generally
2531 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2533 const int cell = tabular.getCellFromInset(in, actcell);
2536 return tabular.getPWidth(cell).zero();
2538 // well we didn't obviously find it so maybe our owner knows more
2540 return owner()->forceDefaultParagraphs(in);
2542 lyxerr << "If we're here there is really something strange going on!"
2548 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2551 if (buf.length() <= 0)
2557 string::size_type len = buf.length();
2558 string::size_type p = 0;
2560 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2568 maxCols = max(cols, maxCols);
2574 maxCols = max(cols, maxCols);
2575 LyXTabular * loctab;
2580 delete paste_tabular;
2581 paste_tabular = new LyXTabular(bv->buffer()->params(),
2582 this, rows, maxCols);
2583 loctab = paste_tabular;
2592 string::size_type op = 0;
2593 int cells = loctab->getNumberOfCells();
2596 rows = loctab->rows();
2597 int const columns = loctab->columns();
2599 while (cell < cells && p < len && row < rows &&
2600 (p = buf.find_first_of("\t\n", p)) != string::npos)
2606 // we can only set this if we are not too far right
2607 if (cols < columns) {
2608 InsetText & inset = loctab->getCellInset(cell);
2609 LyXFont const font = inset.getLyXText(bv)->
2610 getFont(inset.paragraphs.begin(), 0);
2611 inset.setText(buf.substr(op, p - op), font);
2617 // we can only set this if we are not too far right
2618 if (cols < columns) {
2619 InsetText & inset = tabular.getCellInset(cell);
2620 LyXFont const font = inset.getLyXText(bv)->
2621 getFont(inset.paragraphs.begin(), 0);
2622 inset.setText(buf.substr(op, p - op), font);
2627 cell = loctab->getCellNumber(row, cols);
2633 // check for the last cell if there is no trailing '\n'
2634 if (cell < cells && op < len) {
2635 InsetText & inset = loctab->getCellInset(cell);
2636 LyXFont const font = inset.getLyXText(bv)->
2637 getFont(inset.paragraphs.begin(), 0);
2638 inset.setText(buf.substr(op, len - op), font);
2645 void InsetTabular::addPreview(PreviewLoader & loader) const
2647 int const rows = tabular.rows();
2648 int const columns = tabular.columns();
2649 for (int i = 0; i < rows; ++i)
2650 for (int j = 0; j < columns; ++j)
2651 tabular.getCellInset(i, j).addPreview(loader);
2655 string const InsetTabularMailer::name_("tabular");
2657 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2658 : inset_(const_cast<InsetTabular &>(inset))
2662 string const InsetTabularMailer::inset2string(Buffer const &) const
2664 return params2string(inset_);
2668 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2670 istringstream data(in);
2672 lex.setStream(data);
2674 #warning CHECK verify that this is a sane value to return.
2680 string const token = lex.getString();
2688 string const token = lex.getString();
2689 if (token != "\\active_cell")
2692 cell = lex.getInteger();
2695 // This is part of the inset proper that is usually swallowed
2696 // by Buffer::readInset
2699 string const token = lex.getString();
2700 if (token != "Tabular")
2707 Buffer const & buffer = inset.buffer();
2708 inset.read(buffer, lex);
2710 // We can't set the active cell, but we can tell the frontend
2716 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2718 Buffer const & buffer = inset.buffer();
2721 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2722 inset.write(buffer, data);
2723 data << "\\end_inset\n";