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"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27 #include "paragraph_funcs.h"
28 #include "ParagraphParameters.h"
29 #include "undo_funcs.h"
30 #include "WordLangTuple.h"
32 #include "frontends/Alert.h"
33 #include "frontends/font_metrics.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/Painter.h"
37 #include "support/std_sstream.h"
39 using namespace lyx::support;
40 using namespace lyx::graphics;
47 using std::istringstream;
49 using std::ostringstream;
54 int const ADD_TO_HEIGHT = 2;
55 int const ADD_TO_TABULAR_WIDTH = 2;
58 LyXTabular * paste_tabular = 0;
61 struct TabularFeature {
62 LyXTabular::Feature action;
67 TabularFeature tabularFeature[] =
69 { LyXTabular::APPEND_ROW, "append-row" },
70 { LyXTabular::APPEND_COLUMN, "append-column" },
71 { LyXTabular::DELETE_ROW, "delete-row" },
72 { LyXTabular::DELETE_COLUMN, "delete-column" },
73 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
74 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
75 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
76 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
77 { LyXTabular::ALIGN_LEFT, "align-left" },
78 { LyXTabular::ALIGN_RIGHT, "align-right" },
79 { LyXTabular::ALIGN_CENTER, "align-center" },
80 { LyXTabular::ALIGN_BLOCK, "align-block" },
81 { LyXTabular::VALIGN_TOP, "valign-top" },
82 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
83 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
84 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
85 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
86 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
87 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
88 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
89 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
90 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
91 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
92 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
93 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
94 { LyXTabular::MULTICOLUMN, "multicolumn" },
95 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
96 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
97 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
98 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
99 { LyXTabular::SET_PWIDTH, "set-pwidth" },
100 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
101 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
102 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
103 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
104 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
105 { LyXTabular::SET_USEBOX, "set-usebox" },
106 { LyXTabular::SET_LTHEAD, "set-lthead" },
107 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
108 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
109 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
110 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
111 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
112 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
113 { LyXTabular::LAST_ACTION, "" }
117 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
118 bool operator()(TabularFeature & tf)
120 return tf.action == feature_;
123 LyXTabular::Feature feature_;
129 string const featureAsString(LyXTabular::Feature feature)
131 TabularFeature * it = tabularFeature;
132 TabularFeature * end = it +
133 sizeof(tabularFeature) / sizeof(TabularFeature);
134 it = std::find_if(it, end, FindFeature(feature));
135 return (it == end) ? string() : it->feature;
139 bool InsetTabular::hasPasteBuffer() const
141 return (paste_tabular != 0);
145 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
146 : tabular(buf.params(), this, max(rows, 1), max(columns, 1)),
147 buffer_(&buf), cursorx_(0), cursory_(0)
149 // for now make it always display as display() inset
151 the_locking_inset = 0;
152 old_locking_inset = 0;
155 actrow = actcell = 0;
163 InsetTabular::InsetTabular(InsetTabular const & tab)
164 : UpdatableInset(tab),
165 tabular(tab.buffer_->params(), this, tab.tabular),
166 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
168 the_locking_inset = 0;
169 old_locking_inset = 0;
172 actrow = actcell = 0;
180 InsetTabular::~InsetTabular()
182 InsetTabularMailer(*this).hideDialog();
186 auto_ptr<InsetBase> InsetTabular::clone() const
188 return auto_ptr<InsetBase>(new InsetTabular(*this));
192 Buffer const & InsetTabular::buffer() const
198 BufferView * InsetTabular::view() const
205 void InsetTabular::buffer(Buffer * b)
211 void InsetTabular::write(Buffer const & buf, ostream & os) const
213 os << "Tabular" << endl;
214 tabular.write(buf, os);
218 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
220 bool const old_format = (lex.getString() == "\\LyXTable");
222 tabular.read(buf, lex);
228 string token = lex.getString();
229 while (lex.isOK() && (token != "\\end_inset")) {
231 token = lex.getString();
233 if (token != "\\end_inset") {
234 lex.printError("Missing \\end_inset at this point. "
240 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
242 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
243 // mi.base.textwidth << "\n";
245 lyxerr << "InsetTabular::metrics: need bv" << endl;
249 calculate_dimensions_of_cells(mi);
251 dim.asc = tabular.getAscentOfRow(0);
252 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
253 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
258 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
260 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
262 BufferView * bv = pi.base.bv;
265 UpdatableInset::draw(pi, x, y);
273 x += ADD_TO_TABULAR_WIDTH;
276 first_visible_cell = -1;
277 for (int i = 0; i < tabular.rows(); ++i) {
279 cell = tabular.getCellNumber(i, 0);
280 if (y + tabular.getDescentOfRow(i) <= 0 &&
281 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
283 y += tabular.getDescentOfRow(i) +
284 tabular.getAscentOfRow(i + 1) +
285 tabular.getAdditionalHeight(i + 1);
288 for (int j = 0; j < tabular.columns(); ++j) {
289 if (nx > bv->workWidth())
291 if (tabular.isPartOfMultiColumn(i, j))
293 int cx = nx + tabular.getBeginningOfTextInCell(cell);
294 if (first_visible_cell < 0)
295 first_visible_cell = cell;
296 if (hasSelection()) {
297 drawCellSelection(pi.pain, nx, y, i, j, cell);
300 tabular.getCellInset(cell).draw(pi, cx, y);
301 drawCellLines(pi.pain, nx, y, i, cell);
302 nx += tabular.getWidthOfColumn(cell);
306 // Would be nice, but for some completely unfathomable reason,
307 // on a col resize to a new fixed width, even though the insettexts
308 // are resized, the cell isn't, but drawing all cells in a tall table
309 // has the desired effect somehow. Complete dark magic.
311 // avoiding drawing the rest of a long table is
312 // a pretty big speedup
313 if (y > bv->workHeight())
317 y += tabular.getDescentOfRow(i) +
318 tabular.getAscentOfRow(i + 1) +
319 tabular.getAdditionalHeight(i + 1);
324 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
325 int row, int cell) const
327 int x2 = x + tabular.getWidthOfColumn(cell);
330 if (!tabular.topAlreadyDrawn(cell)) {
331 on_off = !tabular.topLine(cell);
332 pain.line(x, y - tabular.getAscentOfRow(row),
333 x2, y - tabular.getAscentOfRow(row),
334 on_off ? LColor::tabularonoffline : LColor::tabularline,
335 on_off ? Painter::line_onoffdash : Painter::line_solid);
337 on_off = !tabular.bottomLine(cell);
338 pain.line(x, y + tabular.getDescentOfRow(row),
339 x2, y + tabular.getDescentOfRow(row),
340 on_off ? LColor::tabularonoffline : LColor::tabularline,
341 on_off ? Painter::line_onoffdash : Painter::line_solid);
342 if (!tabular.leftAlreadyDrawn(cell)) {
343 on_off = !tabular.leftLine(cell);
344 pain.line(x, y - tabular.getAscentOfRow(row),
345 x, y + tabular.getDescentOfRow(row),
346 on_off ? LColor::tabularonoffline : LColor::tabularline,
347 on_off ? Painter::line_onoffdash : Painter::line_solid);
349 on_off = !tabular.rightLine(cell);
350 pain.line(x2 - tabular.getAdditionalWidth(cell),
351 y - tabular.getAscentOfRow(row),
352 x2 - tabular.getAdditionalWidth(cell),
353 y + tabular.getDescentOfRow(row),
354 on_off ? LColor::tabularonoffline : LColor::tabularline,
355 on_off ? Painter::line_onoffdash : Painter::line_solid);
359 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
360 int row, int column, int cell) const
362 BOOST_ASSERT(hasSelection());
363 int cs = tabular.column_of_cell(sel_cell_start);
364 int ce = tabular.column_of_cell(sel_cell_end);
367 cs = tabular.column_of_cell(sel_cell_end);
369 ce = tabular.right_column_of_cell(sel_cell_end);
372 int rs = tabular.row_of_cell(sel_cell_start);
373 int re = tabular.row_of_cell(sel_cell_end);
377 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
378 int w = tabular.getWidthOfColumn(cell);
379 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
380 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
381 w, h, LColor::selection);
386 string const InsetTabular::editMessage() const
388 return _("Opened table");
392 void InsetTabular::insetUnlock(BufferView * bv)
394 if (the_locking_inset) {
395 the_locking_inset->insetUnlock(bv);
397 the_locking_inset = 0;
402 if (scroll(false) || hasSelection()) {
411 void InsetTabular::updateLocal(BufferView * bv) const
413 bv->updateInset(this);
419 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
421 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
426 if (inset == &tabular.getCellInset(actcell)) {
427 lyxerr[Debug::INSETTEXT] << "OK" << endl;
428 the_locking_inset = &tabular.getCellInset(actcell);
431 } else if (!the_locking_inset) {
432 int const n = tabular.getNumberOfCells();
433 int const id = inset->id();
434 for (int i = 0; i < n; ++i) {
435 InsetText * in = &tabular.getCellInset(i);
438 the_locking_inset = in;
443 if (in->getInsetFromID(id)) {
445 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
446 return the_locking_inset->lockInsetInInset(bv, inset);
449 } else if (the_locking_inset && (the_locking_inset == inset)) {
450 lyxerr[Debug::INSETTEXT] << "OK" << endl;
452 } else if (the_locking_inset) {
453 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
454 return the_locking_inset->lockInsetInInset(bv, inset);
456 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
461 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
464 if (!the_locking_inset)
466 if (the_locking_inset == inset) {
467 the_locking_inset->insetUnlock(bv);
469 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
476 // this has to be here otherwise we don't redraw the cell!
477 the_locking_inset = 0;
480 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
481 if (inset->lyxCode() == TABULAR_CODE &&
482 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
483 InsetTabularMailer(*this).updateDialog(bv);
492 int InsetTabular::insetInInsetY() const
494 if (!the_locking_inset)
496 return inset_y + the_locking_inset->insetInInsetY();
500 UpdatableInset * InsetTabular::getLockingInset() const
502 return the_locking_inset ? the_locking_inset->getLockingInset() :
503 const_cast<InsetTabular *>(this);
507 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
511 if (the_locking_inset)
512 return the_locking_inset->getFirstLockingInsetOfType(c);
517 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
519 if (the_locking_inset)
520 return the_locking_inset->insertInset(bv, inset);
525 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
527 if (hasSelection() && cmd.button() == mouse_button::button3)
530 if (hasSelection()) {
532 updateLocal(cmd.view());
535 int const ocell = actcell;
536 BufferView * bv = cmd.view();
540 the_locking_inset = 0;
544 setPos(bv, cmd.x, cmd.y);
547 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
549 if ((ocell == actcell) && the_locking_inset && inset_hit) {
551 FuncRequest cmd1 = cmd;
554 the_locking_inset->localDispatch(cmd1);
558 if (the_locking_inset) {
559 the_locking_inset->insetUnlock(bv);
561 the_locking_inset = 0;
564 if (cmd.button() == mouse_button::button2) {
565 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
569 if (inset_hit && bv->theLockingInset()) {
570 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
571 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
574 FuncRequest cmd1 = cmd;
577 the_locking_inset->localDispatch(cmd1);
582 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
585 if (the_locking_inset) {
586 FuncRequest cmd1 = cmd;
589 ret = the_locking_inset->localDispatch(cmd1);
591 if (cmd.button() == mouse_button::button3 && !ret) {
592 InsetTabularMailer(*this).showDialog(cmd.view());
599 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
601 if (the_locking_inset) {
602 FuncRequest cmd1 = cmd;
605 the_locking_inset->localDispatch(cmd1);
609 BufferView * bv = cmd.view();
610 int const old_cell = actcell;
612 setPos(bv, cmd.x, cmd.y);
613 if (!hasSelection()) {
614 setSelection(actcell, actcell);
616 } else if (old_cell != actcell) {
617 setSelection(sel_cell_start, actcell);
623 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
625 // We need to save the value of the_locking_inset as the call to
626 // the_locking_inset->localDispatch might unlock it.
627 old_locking_inset = the_locking_inset;
628 RESULT result = UpdatableInset::localDispatch(cmd);
629 BufferView * bv = cmd.view();
631 if (cmd.action == LFUN_INSET_EDIT) {
633 if (!bv->lockInset(this)) {
634 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
640 the_locking_inset = 0;
644 if (cmd.argument.size()) {
645 if (cmd.argument == "left") {
646 if (isRightToLeft(bv))
647 actcell = tabular.getLastCellInRow(0);
651 if (isRightToLeft(bv))
652 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
654 actcell = tabular.getNumberOfCells() - 1;
662 setPos(bv, cmd.x, cmd.y);
665 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
666 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
672 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
677 if (cmd.action < 0 && cmd.argument.empty())
680 bool hs = hasSelection();
683 // this one have priority over the locked InsetText, if we're not already
684 // inside another tabular then that one get's priority!
685 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
686 switch (cmd.action) {
687 case LFUN_MOUSE_PRESS:
691 case LFUN_MOUSE_MOTION:
692 lfunMouseMotion(cmd);
695 case LFUN_MOUSE_RELEASE:
696 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
698 case LFUN_CELL_BACKWARD:
699 case LFUN_CELL_FORWARD:
700 unlockInsetInInset(bv, the_locking_inset);
701 if (cmd.action == LFUN_CELL_FORWARD)
702 moveNextCell(bv, old_locking_inset != 0);
704 movePrevCell(bv, old_locking_inset != 0);
708 if (!the_locking_inset)
709 return DISPATCHED_NOUPDATE;
711 // this to avoid compiler warnings.
717 kb_action action = cmd.action;
718 string arg = cmd.argument;
719 if (the_locking_inset) {
720 result = the_locking_inset->localDispatch(cmd);
721 if (result == DISPATCHED_NOUPDATE) {
724 if (sc != scroll()) { // inset has been scrolled
728 } else if (result == DISPATCHED) {
731 } else if (result == FINISHED_UP) {
733 // Make sure to reset status message after
734 // exiting, e.g. math inset
735 bv->owner()->clearMessage();
736 } else if (result == FINISHED_DOWN) {
738 bv->owner()->clearMessage();
739 } else if (result == FINISHED_RIGHT) {
741 bv->owner()->clearMessage();
742 } else if (result == FINISHED) {
743 bv->owner()->clearMessage();
749 // --- Cursor Movements ----------------------------------
750 case LFUN_RIGHTSEL: {
751 int const start = hasSelection() ? sel_cell_start : actcell;
752 if (tabular.isLastCellInRow(actcell)) {
753 setSelection(start, actcell);
758 // if we are starting a selection, only select
759 // the current cell at the beginning
760 if (hasSelection()) {
761 moveRight(bv, false);
764 setSelection(start, end);
769 result = moveRight(bv);
775 int const start = hasSelection() ? sel_cell_start : actcell;
776 if (tabular.isFirstCellInRow(actcell)) {
777 setSelection(start, actcell);
782 // if we are starting a selection, only select
783 // the current cell at the beginning
784 if (hasSelection()) {
788 setSelection(start, end);
793 result = moveLeft(bv);
799 int const start = hasSelection() ? sel_cell_start : actcell;
800 int const ocell = actcell;
801 // if we are starting a selection, only select
802 // the current cell at the beginning
803 if (hasSelection()) {
805 if (ocell == sel_cell_end ||
806 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
807 setSelection(start, tabular.getCellBelow(sel_cell_end));
809 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
811 setSelection(start, start);
817 result = moveDown(bv, old_locking_inset != 0);
823 int const start = hasSelection() ? sel_cell_start : actcell;
824 int const ocell = actcell;
825 // if we are starting a selection, only select
826 // the current cell at the beginning
827 if (hasSelection()) {
829 if ((ocell == sel_cell_end) ||
830 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
831 setSelection(start, tabular.getCellAbove(sel_cell_end));
833 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
835 setSelection(start, start);
841 result = moveUp(bv, old_locking_inset != 0);
850 unlockInsetInInset(bv, the_locking_inset);
851 if (bv->top_y() + bv->painter().paperHeight() <
852 top_baseline + tabular.getHeightOfTabular())
854 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
855 actcell = tabular.getCellBelow(first_visible_cell) + column;
857 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
867 unlockInsetInInset(bv, the_locking_inset);
868 if (top_baseline < 0) {
869 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
870 if (top_baseline > 0)
873 actcell = tabular.getCellBelow(first_visible_cell) + column;
881 // none of these make sense for insettabular,
882 // but we must catch them to prevent any
883 // selection from being confused
887 case LFUN_WORDLEFTSEL:
889 case LFUN_WORDRIGHTSEL:
891 case LFUN_DOWN_PARAGRAPH:
892 case LFUN_DOWN_PARAGRAPHSEL:
893 case LFUN_UP_PARAGRAPH:
894 case LFUN_UP_PARAGRAPHSEL:
900 case LFUN_BEGINNINGBUF:
901 case LFUN_BEGINNINGBUFSEL:
905 case LFUN_LAYOUT_TABULAR: {
906 InsetTabularMailer(*this).showDialog(bv);
909 case LFUN_INSET_DIALOG_UPDATE: {
910 InsetTabularMailer(*this).updateDialog(bv);
913 case LFUN_TABULAR_FEATURE:
914 if (!tabularFeatures(bv, arg))
915 result = UNDISPATCHED;
917 // insert file functions
918 case LFUN_FILE_INSERT_ASCII_PARA:
919 case LFUN_FILE_INSERT_ASCII:
921 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
924 if (insertAsciiString(bv, tmpstr, false))
927 result = UNDISPATCHED;
930 // cut and paste functions
932 if (!copySelection(bv))
936 recordUndo(bv, Undo::DELETE);
937 cutSelection(bv->buffer()->params());
946 case LFUN_PASTESELECTION:
948 string const clip(bv->getClipboard());
952 if (clip.find('\t') != string::npos) {
956 string::size_type len = clip.length();
957 string::size_type p = 0;
960 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
968 maxCols = max(cols, maxCols);
974 maxCols = max(cols, maxCols);
975 delete paste_tabular;
976 paste_tabular = new LyXTabular(bv->buffer()->params(),
977 this, rows, maxCols);
978 string::size_type op = 0;
980 int cells = paste_tabular->getNumberOfCells();
982 while ((cell < cells) && (p < len) &&
983 (p = clip.find_first_of("\t\n", p)) != string::npos) {
988 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
993 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
994 while (cols++ < maxCols)
1002 // check for the last cell if there is no trailing '\n'
1003 if ((cell < cells) && (op < len))
1004 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1007 if (!insertAsciiString(bv, clip, true))
1010 // so that the clipboard is used and it goes on
1012 // and executes LFUN_PASTESELECTION in insettext!
1013 delete paste_tabular;
1018 if (hasPasteBuffer()) {
1019 recordUndo(bv, Undo::INSERT);
1024 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1026 // handle font changing stuff on selection before we lock the inset
1027 // in the default part!
1028 result = UNDISPATCHED;
1039 case LFUN_UNDERLINE:
1040 case LFUN_FONT_SIZE:
1041 if (bv->dispatch(FuncRequest(bv, action, arg)))
1042 result = DISPATCHED;
1048 // we try to activate the actual inset and put this event down to
1049 // the insets dispatch function.
1050 if (result == DISPATCHED || the_locking_inset)
1052 if (activateCellInset(bv)) {
1053 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1054 if (result == UNDISPATCHED || result >= FINISHED) {
1055 unlockInsetInInset(bv, the_locking_inset);
1056 // we need to update if this was requested before
1058 return UNDISPATCHED;
1067 if (result < FINISHED) {
1068 if (!the_locking_inset && bv->fitCursor())
1071 bv->unlockInset(this);
1076 int InsetTabular::latex(Buffer const & buf, ostream & os,
1077 LatexRunParams const & runparams) const
1079 return tabular.latex(buf, os, runparams);
1083 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1086 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1088 return tabular.ascii(buf, os, 0, false, 0);
1092 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1094 return tabular.linuxdoc(buf,os);
1098 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1103 // if the table is inside a float it doesn't need the informaltable
1104 // wrapper. Search for it.
1105 for (master = owner();
1106 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1107 master = master->owner());
1110 os << "<informaltable>";
1115 ret += tabular.docbook(buf, os, mixcont);
1117 os << "</informaltable>";
1126 void InsetTabular::validate(LaTeXFeatures & features) const
1128 tabular.validate(features);
1132 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1135 // if we have a locking_inset we should have to check only this cell for
1136 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1137 // This is _really_ important (André)
1138 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1141 for (int j = 0; j < tabular.columns(); ++j) {
1145 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1146 tabular.getCellInset(actrow, j).metrics(m, dim);
1147 maxAsc = max(dim.asc, maxAsc);
1148 maxDesc = max(dim.des, maxDesc);
1150 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1151 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1152 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1158 for (int i = 0; i < tabular.rows(); ++i) {
1161 for (int j = 0; j < tabular.columns(); ++j) {
1162 if (tabular.isPartOfMultiColumn(i, j))
1168 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1169 tabular.getCellInset(cell).metrics(m, dim);
1170 maxAsc = max(maxAsc, dim.asc);
1171 maxDesc = max(maxDesc, dim.des);
1172 tabular.setWidthOfCell(cell, dim.wid);
1174 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1175 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1180 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1182 if (the_locking_inset) {
1183 the_locking_inset->getCursor(bv, x, y);
1188 y = cursory_ + InsetTabular::y();
1191 int desc = tabular.getDescentOfRow(actrow);
1193 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1195 y += ADD_TO_HEIGHT * 2;
1196 y += TEXT_TO_INSET_OFFSET;
1200 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1202 if (the_locking_inset) {
1203 the_locking_inset->getCursorPos(bv, x, y);
1206 x = cursorx_ - top_x;
1211 void InsetTabular::fitInsetCursor(BufferView * bv) const
1213 if (the_locking_inset) {
1214 the_locking_inset->fitInsetCursor(bv);
1219 int const asc = font_metrics::maxAscent(font);
1220 int const desc = font_metrics::maxDescent(font);
1223 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1227 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1230 actcell = actrow = actcol = 0;
1231 int ly = tabular.getDescentOfRow(actrow);
1233 // first search the right row
1234 while (ly < y && actrow + 1 < tabular.rows()) {
1235 cursory_ += tabular.getDescentOfRow(actrow) +
1236 tabular.getAscentOfRow(actrow + 1) +
1237 tabular.getAdditionalHeight(actrow + 1);
1239 ly = cursory_ + tabular.getDescentOfRow(actrow);
1241 actcell = tabular.getCellNumber(actrow, actcol);
1243 // now search the right column
1244 int lx = tabular.getWidthOfColumn(actcell) -
1245 tabular.getAdditionalWidth(actcell);
1247 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1248 lx += tabular.getWidthOfColumn(actcell + 1)
1249 + tabular.getAdditionalWidth(actcell);
1251 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1256 int InsetTabular::getCellXPos(int cell) const
1260 for (; !tabular.isFirstCellInRow(c); --c)
1262 int lx = tabular.getWidthOfColumn(cell);
1263 for (; c < cell; ++c)
1264 lx += tabular.getWidthOfColumn(c);
1266 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1270 void InsetTabular::resetPos(BufferView * bv) const
1272 #ifdef WITH_WARNINGS
1273 #warning This should be fixed in the right manner (20011128 Jug)
1275 // fast hack to fix infinite repaintings!
1276 if (in_reset_pos > 0)
1280 actcol = tabular.column_of_cell(actcell);
1283 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1284 if (tabular.isLastCellInRow(cell)) {
1285 cursory_ += tabular.getDescentOfRow(actrow) +
1286 tabular.getAscentOfRow(actrow + 1) +
1287 tabular.getAdditionalHeight(actrow + 1);
1292 if (the_locking_inset)
1296 // we need this only from here on!!!
1298 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1299 int new_x = getCellXPos(actcell);
1300 int old_x = cursorx_;
1303 // cursor.x(getCellXPos(actcell) + offset);
1304 if (actcol < tabular.columns() - 1 && scroll(false) &&
1305 tabular.getWidthOfTabular() < bv->workWidth()-20)
1309 } else if (the_locking_inset &&
1310 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1312 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1313 if (xx > bv->workWidth()-20) {
1314 scroll(bv, -(xx - bv->workWidth() + 60));
1316 } else if (xx < 20) {
1324 } else if (cursorx_ - offset > 20 &&
1325 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1326 > bv->workWidth() - 20) {
1327 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1329 } else if (cursorx_ - offset < 20) {
1330 scroll(bv, 20 - cursorx_ + offset);
1332 } else if (scroll() && top_x > 20 &&
1333 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1334 scroll(bv, old_x - cursorx_);
1337 if (the_locking_inset) {
1338 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1341 if ((!the_locking_inset ||
1342 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1343 actcell != oldcell) {
1344 InsetTabularMailer(*this).updateDialog(bv);
1351 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1353 if (lock && !old_locking_inset) {
1354 if (activateCellInset(bv))
1357 bool moved = isRightToLeft(bv)
1358 ? movePrevCell(bv) : moveNextCell(bv);
1360 return FINISHED_RIGHT;
1361 if (lock && activateCellInset(bv))
1365 return DISPATCHED_NOUPDATE;
1369 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1371 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1374 if (lock) { // behind the inset
1375 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1379 return DISPATCHED_NOUPDATE;
1383 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1385 int const ocell = actcell;
1386 actcell = tabular.getCellAbove(actcell);
1387 if (actcell == ocell) // we moved out of the inset
1393 if (old_locking_inset) {
1394 old_locking_inset->getCursorPos(bv, x, y);
1395 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1397 if (activateCellInset(bv, x, 0))
1400 return DISPATCHED_NOUPDATE;
1404 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1406 int const ocell = actcell;
1407 actcell = tabular.getCellBelow(actcell);
1408 if (actcell == ocell) // we moved out of the inset
1409 return FINISHED_DOWN;
1414 if (old_locking_inset) {
1415 old_locking_inset->getCursorPos(bv, x, y);
1416 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1418 if (activateCellInset(bv, x, 0))
1421 return DISPATCHED_NOUPDATE;
1425 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1427 if (isRightToLeft(bv)) {
1428 if (tabular.isFirstCellInRow(actcell)) {
1429 int row = tabular.row_of_cell(actcell);
1430 if (row == tabular.rows() - 1)
1432 actcell = tabular.getLastCellInRow(row);
1433 actcell = tabular.getCellBelow(actcell);
1440 if (tabular.isLastCell(actcell))
1445 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1446 isRightToLeftPar(bv->buffer()->params());
1447 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1454 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1456 if (isRightToLeft(bv)) {
1457 if (tabular.isLastCellInRow(actcell)) {
1458 int row = tabular.row_of_cell(actcell);
1461 actcell = tabular.getFirstCellInRow(row);
1462 actcell = tabular.getCellAbove(actcell);
1464 if (tabular.isLastCell(actcell))
1469 if (!actcell) // first cell
1474 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1475 isRightToLeftPar(bv->buffer()->params());
1476 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1483 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1487 setSelection(0, tabular.getNumberOfCells() - 1);
1489 if (hasSelection()) {
1490 recordUndo(bv, Undo::ATOMIC);
1491 bool const frozen = undo_frozen;
1494 // apply the fontchange on the whole selection
1499 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1500 for(int i = sel_row_start; i <= sel_row_end; ++i)
1501 for(int j = sel_col_start; j <= sel_col_end; ++j)
1502 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1510 if (the_locking_inset)
1511 the_locking_inset->setFont(bv, font, tall);
1515 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1517 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1520 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1521 string const tmp = tabularFeature[i].feature;
1523 if (tmp == what.substr(0, tmp.length())) {
1524 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1525 //tabularFeatures[i].feature.length())) {
1526 action = tabularFeature[i].action;
1530 if (action == LyXTabular::LAST_ACTION)
1534 ltrim(what.substr(tabularFeature[i].feature.length()));
1535 tabularFeatures(bv, action, val);
1541 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1542 string const & special, bool & flag)
1544 if (special == "dl_above") {
1547 } else if (special == "dl_below") {
1548 ltt.bottomDL = flag;
1550 } else if (special == "empty") {
1562 void InsetTabular::tabularFeatures(BufferView * bv,
1563 LyXTabular::Feature feature,
1564 string const & value)
1570 bool setLines = false;
1571 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1572 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1576 case LyXTabular::M_ALIGN_LEFT:
1577 case LyXTabular::ALIGN_LEFT:
1578 setAlign = LYX_ALIGN_LEFT;
1581 case LyXTabular::M_ALIGN_RIGHT:
1582 case LyXTabular::ALIGN_RIGHT:
1583 setAlign = LYX_ALIGN_RIGHT;
1586 case LyXTabular::M_ALIGN_CENTER:
1587 case LyXTabular::ALIGN_CENTER:
1588 setAlign = LYX_ALIGN_CENTER;
1591 case LyXTabular::ALIGN_BLOCK:
1592 setAlign = LYX_ALIGN_BLOCK;
1595 case LyXTabular::M_VALIGN_TOP:
1596 case LyXTabular::VALIGN_TOP:
1597 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1600 case LyXTabular::M_VALIGN_BOTTOM:
1601 case LyXTabular::VALIGN_BOTTOM:
1602 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1605 case LyXTabular::M_VALIGN_MIDDLE:
1606 case LyXTabular::VALIGN_MIDDLE:
1607 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1614 if (hasSelection()) {
1615 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1617 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1618 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1620 recordUndo(bv, Undo::ATOMIC);
1622 int row = tabular.row_of_cell(actcell);
1623 int column = tabular.column_of_cell(actcell);
1625 LyXTabular::ltType ltt;
1629 case LyXTabular::SET_PWIDTH:
1631 LyXLength const vallen(value);
1632 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1634 bool const update = (tmplen != vallen);
1635 tabular.setColumnPWidth(actcell, vallen);
1637 // We need this otherwise we won't resize
1638 // the insettext of the active cell (if any)
1639 // until later (see InsetText::do_resize)
1640 unlockInsetInInset(bv, the_locking_inset);
1645 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1646 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1647 else if (!vallen.zero()
1648 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1649 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1653 case LyXTabular::SET_MPWIDTH:
1655 LyXLength const vallen(value);
1656 LyXLength const & tmplen = tabular.getPWidth(actcell);
1658 bool const update = (tmplen != vallen);
1659 tabular.setMColumnPWidth(actcell, vallen);
1661 // We need this otherwise we won't resize
1662 // the insettext of the active cell (if any)
1663 // until later (see InsetText::do_resize)
1664 unlockInsetInInset(bv, the_locking_inset);
1669 case LyXTabular::SET_SPECIAL_COLUMN:
1670 case LyXTabular::SET_SPECIAL_MULTI:
1671 tabular.setAlignSpecial(actcell,value,feature);
1674 case LyXTabular::APPEND_ROW:
1675 // append the row into the tabular
1676 unlockInsetInInset(bv, the_locking_inset);
1677 tabular.appendRow(bv->buffer()->params(), actcell);
1680 case LyXTabular::APPEND_COLUMN:
1681 // append the column into the tabular
1682 unlockInsetInInset(bv, the_locking_inset);
1683 tabular.appendColumn(bv->buffer()->params(), actcell);
1684 actcell = tabular.getCellNumber(row, column);
1687 case LyXTabular::DELETE_ROW:
1688 unlockInsetInInset(bv, the_locking_inset);
1689 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1690 tabular.deleteRow(sel_row_start);
1692 if (sel_row_start >= tabular.rows())
1694 actcell = tabular.getCellNumber(sel_row_start, column);
1698 case LyXTabular::DELETE_COLUMN:
1699 unlockInsetInInset(bv, the_locking_inset);
1700 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1701 tabular.deleteColumn(sel_col_start);
1703 if (sel_col_start >= tabular.columns())
1705 actcell = tabular.getCellNumber(row, sel_col_start);
1709 case LyXTabular::M_TOGGLE_LINE_TOP:
1711 case LyXTabular::TOGGLE_LINE_TOP:
1713 bool lineSet = !tabular.topLine(actcell, flag);
1714 for (int i = sel_row_start; i <= sel_row_end; ++i)
1715 for (int j = sel_col_start; j <= sel_col_end; ++j)
1717 tabular.getCellNumber(i, j),
1723 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1725 case LyXTabular::TOGGLE_LINE_BOTTOM:
1727 bool lineSet = !tabular.bottomLine(actcell, flag);
1728 for (int i = sel_row_start; i <= sel_row_end; ++i)
1729 for (int j = sel_col_start; j <= sel_col_end; ++j)
1730 tabular.setBottomLine(
1731 tabular.getCellNumber(i, j),
1738 case LyXTabular::M_TOGGLE_LINE_LEFT:
1740 case LyXTabular::TOGGLE_LINE_LEFT:
1742 bool lineSet = !tabular.leftLine(actcell, flag);
1743 for (int i = sel_row_start; i <= sel_row_end; ++i)
1744 for (int j = sel_col_start; j <= sel_col_end; ++j)
1745 tabular.setLeftLine(
1746 tabular.getCellNumber(i,j),
1753 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1755 case LyXTabular::TOGGLE_LINE_RIGHT:
1757 bool lineSet = !tabular.rightLine(actcell, flag);
1758 for (int i = sel_row_start; i <= sel_row_end; ++i)
1759 for (int j = sel_col_start; j <= sel_col_end; ++j)
1760 tabular.setRightLine(
1761 tabular.getCellNumber(i,j),
1768 case LyXTabular::M_ALIGN_LEFT:
1769 case LyXTabular::M_ALIGN_RIGHT:
1770 case LyXTabular::M_ALIGN_CENTER:
1772 case LyXTabular::ALIGN_LEFT:
1773 case LyXTabular::ALIGN_RIGHT:
1774 case LyXTabular::ALIGN_CENTER:
1775 case LyXTabular::ALIGN_BLOCK:
1776 for (int i = sel_row_start; i <= sel_row_end; ++i)
1777 for (int j = sel_col_start; j <= sel_col_end; ++j)
1778 tabular.setAlignment(
1779 tabular.getCellNumber(i, j),
1785 case LyXTabular::M_VALIGN_TOP:
1786 case LyXTabular::M_VALIGN_BOTTOM:
1787 case LyXTabular::M_VALIGN_MIDDLE:
1789 case LyXTabular::VALIGN_TOP:
1790 case LyXTabular::VALIGN_BOTTOM:
1791 case LyXTabular::VALIGN_MIDDLE:
1792 for (int i = sel_row_start; i <= sel_row_end; ++i)
1793 for (int j = sel_col_start; j <= sel_col_end; ++j)
1794 tabular.setVAlignment(
1795 tabular.getCellNumber(i, j),
1800 case LyXTabular::MULTICOLUMN: {
1801 if (sel_row_start != sel_row_end) {
1802 #ifdef WITH_WARNINGS
1803 #warning Need I say it ? This is horrible.
1805 Alert::error(_("Error setting multicolumn"),
1806 _("You cannot set multicolumn vertically."));
1809 // just multicol for one Single Cell
1810 if (!hasSelection()) {
1811 // check wether we are completly in a multicol
1812 if (tabular.isMultiColumn(actcell))
1813 tabular.unsetMultiColumn(actcell);
1815 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1819 // we have a selection so this means we just add all this
1820 // cells to form a multicolumn cell
1824 if (sel_cell_start > sel_cell_end) {
1825 s_start = sel_cell_end;
1826 s_end = sel_cell_start;
1828 s_start = sel_cell_start;
1829 s_end = sel_cell_end;
1831 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1838 case LyXTabular::SET_ALL_LINES:
1840 case LyXTabular::UNSET_ALL_LINES:
1841 for (int i = sel_row_start; i <= sel_row_end; ++i)
1842 for (int j = sel_col_start; j <= sel_col_end; ++j)
1843 tabular.setAllLines(
1844 tabular.getCellNumber(i,j), setLines);
1848 case LyXTabular::SET_LONGTABULAR:
1849 tabular.setLongTabular(true);
1850 updateLocal(bv); // because this toggles displayed
1853 case LyXTabular::UNSET_LONGTABULAR:
1854 tabular.setLongTabular(false);
1855 updateLocal(bv); // because this toggles displayed
1858 case LyXTabular::SET_ROTATE_TABULAR:
1859 tabular.setRotateTabular(true);
1862 case LyXTabular::UNSET_ROTATE_TABULAR:
1863 tabular.setRotateTabular(false);
1866 case LyXTabular::SET_ROTATE_CELL:
1867 for (int i = sel_row_start; i <= sel_row_end; ++i)
1868 for (int j = sel_col_start; j<=sel_col_end; ++j)
1869 tabular.setRotateCell(
1870 tabular.getCellNumber(i, j), true);
1873 case LyXTabular::UNSET_ROTATE_CELL:
1874 for (int i = sel_row_start; i <= sel_row_end; ++i)
1875 for (int j = sel_col_start; j <= sel_col_end; ++j)
1876 tabular.setRotateCell(
1877 tabular.getCellNumber(i, j), false);
1880 case LyXTabular::SET_USEBOX: {
1881 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1882 if (val == tabular.getUsebox(actcell))
1883 val = LyXTabular::BOX_NONE;
1884 for (int i = sel_row_start; i <= sel_row_end; ++i)
1885 for (int j = sel_col_start; j <= sel_col_end; ++j)
1886 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1890 case LyXTabular::UNSET_LTFIRSTHEAD:
1892 case LyXTabular::SET_LTFIRSTHEAD:
1893 tabular.getRowOfLTFirstHead(row, ltt);
1894 checkLongtableSpecial(ltt, value, flag);
1895 tabular.setLTHead(row, flag, ltt, true);
1898 case LyXTabular::UNSET_LTHEAD:
1900 case LyXTabular::SET_LTHEAD:
1901 tabular.getRowOfLTHead(row, ltt);
1902 checkLongtableSpecial(ltt, value, flag);
1903 tabular.setLTHead(row, flag, ltt, false);
1906 case LyXTabular::UNSET_LTFOOT:
1908 case LyXTabular::SET_LTFOOT:
1909 tabular.getRowOfLTFoot(row, ltt);
1910 checkLongtableSpecial(ltt, value, flag);
1911 tabular.setLTFoot(row, flag, ltt, false);
1914 case LyXTabular::UNSET_LTLASTFOOT:
1916 case LyXTabular::SET_LTLASTFOOT:
1917 tabular.getRowOfLTLastFoot(row, ltt);
1918 checkLongtableSpecial(ltt, value, flag);
1919 tabular.setLTFoot(row, flag, ltt, true);
1922 case LyXTabular::SET_LTNEWPAGE: {
1923 bool what = !tabular.getLTNewPage(row);
1924 tabular.setLTNewPage(row, what);
1928 // dummy stuff just to avoid warnings
1929 case LyXTabular::LAST_ACTION:
1933 InsetTabularMailer(*this).updateDialog(bv);
1937 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1938 mouse_button::state button, bool behind)
1940 UpdatableInset & inset = tabular.getCellInset(actcell);
1943 x = inset.x() + inset.width();
1944 y = inset.descent();
1946 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1947 //inset_y = cursory_;
1948 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1949 if (!the_locking_inset)
1952 return the_locking_inset;
1956 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1957 mouse_button::state button)
1959 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1961 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1965 bool InsetTabular::insetHit(BufferView *, int x, int) const
1967 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1971 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1973 //resizeLyXText(bv, recursive);
1977 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1978 bool const recursive) const
1980 if (the_locking_inset)
1981 return the_locking_inset->getLyXText(bv, recursive);
1982 return InsetOld::getLyXText(bv, recursive);
1986 bool InsetTabular::showInsetDialog(BufferView * bv) const
1988 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1989 InsetTabularMailer(*this).showDialog(bv);
1994 void InsetTabular::openLayoutDialog(BufferView * bv) const
1996 if (the_locking_inset) {
1997 InsetTabular * inset = static_cast<InsetTabular *>
1998 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2000 inset->openLayoutDialog(bv);
2004 InsetTabularMailer(*this).showDialog(bv);
2009 // function returns an object as defined in func_status.h:
2010 // states OK, Unknown, Disabled, On, Off.
2012 FuncStatus InsetTabular::getStatus(string const & what) const
2014 int action = LyXTabular::LAST_ACTION;
2018 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2019 string const tmp = tabularFeature[i].feature;
2020 if (tmp == what.substr(0, tmp.length())) {
2021 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2022 // tabularFeatures[i].feature.length())) {
2023 action = tabularFeature[i].action;
2027 if (action == LyXTabular::LAST_ACTION) {
2029 return status.unknown(true);
2032 string const argument
2033 = ltrim(what.substr(tabularFeature[i].feature.length()));
2038 LyXTabular::ltType dummyltt;
2042 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2044 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2047 case LyXTabular::SET_PWIDTH:
2048 case LyXTabular::SET_MPWIDTH:
2049 case LyXTabular::SET_SPECIAL_COLUMN:
2050 case LyXTabular::SET_SPECIAL_MULTI:
2051 case LyXTabular::APPEND_ROW:
2052 case LyXTabular::APPEND_COLUMN:
2053 case LyXTabular::DELETE_ROW:
2054 case LyXTabular::DELETE_COLUMN:
2055 case LyXTabular::SET_ALL_LINES:
2056 case LyXTabular::UNSET_ALL_LINES:
2057 return status.clear();
2059 case LyXTabular::MULTICOLUMN:
2060 status.setOnOff(tabular.isMultiColumn(actcell));
2063 case LyXTabular::M_TOGGLE_LINE_TOP:
2065 case LyXTabular::TOGGLE_LINE_TOP:
2066 status.setOnOff(tabular.topLine(actcell, flag));
2069 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2071 case LyXTabular::TOGGLE_LINE_BOTTOM:
2072 status.setOnOff(tabular.bottomLine(actcell, flag));
2075 case LyXTabular::M_TOGGLE_LINE_LEFT:
2077 case LyXTabular::TOGGLE_LINE_LEFT:
2078 status.setOnOff(tabular.leftLine(actcell, flag));
2081 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2083 case LyXTabular::TOGGLE_LINE_RIGHT:
2084 status.setOnOff(tabular.rightLine(actcell, flag));
2087 case LyXTabular::M_ALIGN_LEFT:
2089 case LyXTabular::ALIGN_LEFT:
2090 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2093 case LyXTabular::M_ALIGN_RIGHT:
2095 case LyXTabular::ALIGN_RIGHT:
2096 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2099 case LyXTabular::M_ALIGN_CENTER:
2101 case LyXTabular::ALIGN_CENTER:
2102 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2105 case LyXTabular::ALIGN_BLOCK:
2106 status.disabled(tabular.getPWidth(actcell).zero());
2107 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2110 case LyXTabular::M_VALIGN_TOP:
2112 case LyXTabular::VALIGN_TOP:
2113 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2116 case LyXTabular::M_VALIGN_BOTTOM:
2118 case LyXTabular::VALIGN_BOTTOM:
2119 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2122 case LyXTabular::M_VALIGN_MIDDLE:
2124 case LyXTabular::VALIGN_MIDDLE:
2125 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2128 case LyXTabular::SET_LONGTABULAR:
2129 status.setOnOff(tabular.isLongTabular());
2132 case LyXTabular::UNSET_LONGTABULAR:
2133 status.setOnOff(!tabular.isLongTabular());
2136 case LyXTabular::SET_ROTATE_TABULAR:
2137 status.setOnOff(tabular.getRotateTabular());
2140 case LyXTabular::UNSET_ROTATE_TABULAR:
2141 status.setOnOff(!tabular.getRotateTabular());
2144 case LyXTabular::SET_ROTATE_CELL:
2145 status.setOnOff(tabular.getRotateCell(actcell));
2148 case LyXTabular::UNSET_ROTATE_CELL:
2149 status.setOnOff(!tabular.getRotateCell(actcell));
2152 case LyXTabular::SET_USEBOX:
2153 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2156 case LyXTabular::SET_LTFIRSTHEAD:
2157 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2160 case LyXTabular::SET_LTHEAD:
2161 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2164 case LyXTabular::SET_LTFOOT:
2165 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2168 case LyXTabular::SET_LTLASTFOOT:
2169 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2172 case LyXTabular::SET_LTNEWPAGE:
2173 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2178 status.disabled(true);
2185 void InsetTabular::getLabelList(std::vector<string> & list) const
2187 tabular.getLabelList(list);
2191 bool InsetTabular::copySelection(BufferView * bv)
2193 if (!hasSelection())
2196 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2197 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2198 if (sel_col_start > sel_col_end) {
2199 sel_col_start = sel_col_end;
2200 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2202 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2205 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2206 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2207 if (sel_row_start > sel_row_end)
2208 swap(sel_row_start, sel_row_end);
2210 delete paste_tabular;
2211 paste_tabular = new LyXTabular(bv->buffer()->params(), this, tabular);
2213 for (int i = 0; i < sel_row_start; ++i)
2214 paste_tabular->deleteRow(0);
2216 int const rows = sel_row_end - sel_row_start + 1;
2217 while (paste_tabular->rows() > rows)
2218 paste_tabular->deleteRow(rows);
2220 paste_tabular->setTopLine(0, true, true);
2221 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2224 for (int i = 0; i < sel_col_start; ++i)
2225 paste_tabular->deleteColumn(0);
2227 int const columns = sel_col_end - sel_col_start + 1;
2228 while (paste_tabular->columns() > columns)
2229 paste_tabular->deleteColumn(columns);
2231 paste_tabular->setLeftLine(0, true, true);
2232 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2236 paste_tabular->ascii(*bv->buffer(), os,
2237 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2238 bv->stuffClipboard(STRCONV(os.str()));
2243 bool InsetTabular::pasteSelection(BufferView * bv)
2248 for (int r1 = 0, r2 = actrow;
2249 r1 < paste_tabular->rows() && r2 < tabular.rows();
2251 for (int c1 = 0, c2 = actcol;
2252 c1 < paste_tabular->columns() && c2 < tabular.columns();
2254 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2255 tabular.isPartOfMultiColumn(r2, c2))
2257 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2261 if (tabular.isPartOfMultiColumn(r2, c2)) {
2265 InsetText & inset = tabular.getCellInset(r2, c2);
2266 inset = paste_tabular->getCellInset(r1, c1);
2267 inset.setOwner(this);
2268 inset.deleteLyXText(bv);
2276 bool InsetTabular::cutSelection(BufferParams const & bp)
2278 if (!hasSelection())
2281 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2282 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2283 if (sel_col_start > sel_col_end) {
2284 sel_col_start = sel_col_end;
2285 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2287 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2290 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2291 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2293 if (sel_row_start > sel_row_end)
2294 swap(sel_row_start, sel_row_end);
2296 if (sel_cell_start > sel_cell_end)
2297 swap(sel_cell_start, sel_cell_end);
2299 for (int i = sel_row_start; i <= sel_row_end; ++i)
2300 for (int j = sel_col_start; j <= sel_col_end; ++j)
2301 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2306 bool InsetTabular::isRightToLeft(BufferView * bv)
2308 return bv->getParentLanguage(this)->RightToLeft();
2312 int InsetTabular::scroll(bool recursive) const
2314 int sx = UpdatableInset::scroll(false);
2316 if (recursive && the_locking_inset)
2317 sx += the_locking_inset->scroll(recursive);
2323 void InsetTabular::getSelection(int & srow, int & erow,
2324 int & scol, int & ecol) const
2326 int const start = hasSelection() ? sel_cell_start : actcell;
2327 int const end = hasSelection() ? sel_cell_end : actcell;
2329 srow = tabular.row_of_cell(start);
2330 erow = tabular.row_of_cell(end);
2334 scol = tabular.column_of_cell(start);
2335 ecol = tabular.column_of_cell(end);
2339 ecol = tabular.right_column_of_cell(end);
2343 ParagraphList * InsetTabular::getParagraphs(int i) const
2345 return (i < tabular.getNumberOfCells())
2346 ? tabular.getCellInset(i).getParagraphs(0)
2351 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2353 if (the_locking_inset)
2354 return the_locking_inset->cursor(bv);
2355 return InsetOld::cursor(bv);
2359 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2362 return const_cast<InsetTabular *>(this);
2364 for (int i = 0; i < tabular.rows(); ++i) {
2365 for (int j = 0; j < tabular.columns(); ++j) {
2366 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2376 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2378 if (the_locking_inset) {
2379 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2380 if (!word.word().empty())
2382 if (tabular.isLastCell(actcell)) {
2383 bv->unlockInset(const_cast<InsetTabular *>(this));
2384 return WordLangTuple();
2388 // otherwise we have to lock the next inset and ask for it's selecttion
2389 tabular.getCellInset(actcell)
2390 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2391 WordLangTuple word(selectNextWordInt(bv, value));
2392 if (!word.word().empty())
2398 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2400 // when entering this function the inset should be ALWAYS locked!
2401 BOOST_ASSERT(the_locking_inset);
2403 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2404 if (!word.word().empty())
2407 if (tabular.isLastCell(actcell)) {
2408 bv->unlockInset(const_cast<InsetTabular *>(this));
2409 return WordLangTuple();
2412 // otherwise we have to lock the next inset and ask for it's selecttion
2414 tabular.getCellInset(actcell)
2415 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2416 return selectNextWordInt(bv, value);
2420 void InsetTabular::selectSelectedWord(BufferView * bv)
2422 if (the_locking_inset)
2423 the_locking_inset->selectSelectedWord(bv);
2427 void InsetTabular::markErased()
2429 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2430 tabular.getCellInset(cell).markErased();
2434 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2436 if (the_locking_inset) {
2437 if (the_locking_inset->nextChange(bv, length)) {
2441 if (tabular.isLastCell(actcell))
2445 InsetText & inset = tabular.getCellInset(actcell);
2446 if (inset.nextChange(bv, length)) {
2450 while (!tabular.isLastCell(actcell)) {
2452 InsetText & inset = tabular.getCellInset(actcell);
2453 if (inset.nextChange(bv, length)) {
2462 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2466 if (the_locking_inset) {
2467 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2471 if (tabular.isLastCell(actcell))
2475 InsetText & inset = tabular.getCellInset(cell);
2476 if (inset.searchForward(bv, str, cs, mw)) {
2480 while (!tabular.isLastCell(cell)) {
2482 InsetText & inset = tabular.getCellInset(cell);
2483 if (inset.searchForward(bv, str, cs, mw)) {
2492 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2495 int cell = tabular.getNumberOfCells();
2496 if (the_locking_inset) {
2497 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2506 InsetText & inset = tabular.getCellInset(cell);
2507 if (inset.searchBackward(bv, str, cs, mw)) {
2516 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2518 if (the_locking_inset)
2519 return the_locking_inset->insetAllowed(code);
2520 // we return true here because if the inset is not locked someone
2521 // wants to insert something in one of our insettexts and we generally
2527 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2529 const int cell = tabular.getCellFromInset(in, actcell);
2532 return tabular.getPWidth(cell).zero();
2534 // well we didn't obviously find it so maybe our owner knows more
2536 return owner()->forceDefaultParagraphs(in);
2538 lyxerr << "If we're here there is really something strange going on!"
2544 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2547 if (buf.length() <= 0)
2553 string::size_type len = buf.length();
2554 string::size_type p = 0;
2556 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2564 maxCols = max(cols, maxCols);
2570 maxCols = max(cols, maxCols);
2571 LyXTabular * loctab;
2576 delete paste_tabular;
2577 paste_tabular = new LyXTabular(bv->buffer()->params(),
2578 this, rows, maxCols);
2579 loctab = paste_tabular;
2588 string::size_type op = 0;
2589 int cells = loctab->getNumberOfCells();
2592 rows = loctab->rows();
2593 int const columns = loctab->columns();
2595 while (cell < cells && p < len && row < rows &&
2596 (p = buf.find_first_of("\t\n", p)) != string::npos)
2602 // we can only set this if we are not too far right
2603 if (cols < columns) {
2604 InsetText & inset = loctab->getCellInset(cell);
2605 LyXFont const font = inset.getLyXText(bv)->
2606 getFont(inset.paragraphs.begin(), 0);
2607 inset.setText(buf.substr(op, p - op), font);
2613 // we can only set this if we are not too far right
2614 if (cols < columns) {
2615 InsetText & inset = tabular.getCellInset(cell);
2616 LyXFont const font = inset.getLyXText(bv)->
2617 getFont(inset.paragraphs.begin(), 0);
2618 inset.setText(buf.substr(op, p - op), font);
2623 cell = loctab->getCellNumber(row, cols);
2629 // check for the last cell if there is no trailing '\n'
2630 if (cell < cells && op < len) {
2631 InsetText & inset = loctab->getCellInset(cell);
2632 LyXFont const font = inset.getLyXText(bv)->
2633 getFont(inset.paragraphs.begin(), 0);
2634 inset.setText(buf.substr(op, len - op), font);
2641 void InsetTabular::addPreview(PreviewLoader & loader) const
2643 int const rows = tabular.rows();
2644 int const columns = tabular.columns();
2645 for (int i = 0; i < rows; ++i)
2646 for (int j = 0; j < columns; ++j)
2647 tabular.getCellInset(i, j).addPreview(loader);
2651 string const InsetTabularMailer::name_("tabular");
2653 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2654 : inset_(const_cast<InsetTabular &>(inset))
2658 string const InsetTabularMailer::inset2string(Buffer const &) const
2660 return params2string(inset_);
2664 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2666 istringstream data(STRCONV(in));
2668 lex.setStream(data);
2670 #warning CHECK verify that this is a sane value to return.
2676 string const token = lex.getString();
2684 string const token = lex.getString();
2685 if (token != "\\active_cell")
2688 cell = lex.getInteger();
2691 // This is part of the inset proper that is usually swallowed
2692 // by Buffer::readInset
2695 string const token = lex.getString();
2696 if (token != "Tabular")
2703 Buffer const & buffer = inset.buffer();
2704 inset.read(buffer, lex);
2706 // We can't set the active cell, but we can tell the frontend
2712 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2714 Buffer const & buffer = inset.buffer();
2717 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2718 inset.write(buffer, data);
2719 data << "\\end_inset\n";
2720 return STRCONV(data.str());