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"
14 #include "insettext.h"
17 #include "BufferView.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
28 #include "lyxlength.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "metricsinfo.h"
35 #include "paragraph_funcs.h"
37 #include "frontends/Alert.h"
38 #include "frontends/Dialogs.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
43 #include "support/LAssert.h"
44 #include "support/lstrings.h"
51 using namespace lyx::support;
52 using namespace lyx::graphics;
66 int const ADD_TO_HEIGHT = 2;
67 int const ADD_TO_TABULAR_WIDTH = 2;
70 LyXTabular * paste_tabular = 0;
73 struct TabularFeature {
74 LyXTabular::Feature action;
79 TabularFeature tabularFeature[] =
81 { LyXTabular::APPEND_ROW, "append-row" },
82 { LyXTabular::APPEND_COLUMN, "append-column" },
83 { LyXTabular::DELETE_ROW, "delete-row" },
84 { LyXTabular::DELETE_COLUMN, "delete-column" },
85 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
86 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
87 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
88 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
89 { LyXTabular::ALIGN_LEFT, "align-left" },
90 { LyXTabular::ALIGN_RIGHT, "align-right" },
91 { LyXTabular::ALIGN_CENTER, "align-center" },
92 { LyXTabular::ALIGN_BLOCK, "align-block" },
93 { LyXTabular::VALIGN_TOP, "valign-top" },
94 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
95 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
96 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
97 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
98 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
99 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
100 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
101 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
102 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
103 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
104 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
105 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
106 { LyXTabular::MULTICOLUMN, "multicolumn" },
107 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
108 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
109 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
110 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
111 { LyXTabular::SET_PWIDTH, "set-pwidth" },
112 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
113 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
114 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
115 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
116 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
117 { LyXTabular::SET_USEBOX, "set-usebox" },
118 { LyXTabular::SET_LTHEAD, "set-lthead" },
119 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
120 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
121 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
122 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
123 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
124 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
125 { LyXTabular::LAST_ACTION, "" }
129 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
130 bool operator()(TabularFeature & tf)
132 return tf.action == feature_;
135 LyXTabular::Feature feature_;
141 string const featureAsString(LyXTabular::Feature feature)
143 TabularFeature * it = tabularFeature;
144 TabularFeature * end = it +
145 sizeof(tabularFeature) / sizeof(TabularFeature);
146 it = std::find_if(it, end, FindFeature(feature));
147 return (it == end) ? string() : it->feature;
151 bool InsetTabular::hasPasteBuffer() const
153 return (paste_tabular != 0);
157 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
158 : tabular(buf.params, this, max(rows, 1), max(columns, 1)),
159 buffer_(&buf), cursorx_(0), cursory_(0)
161 // for now make it always display as display() inset
163 the_locking_inset = 0;
164 old_locking_inset = 0;
167 actrow = actcell = 0;
175 InsetTabular::InsetTabular(InsetTabular const & tab)
176 : UpdatableInset(tab),
177 tabular(tab.buffer_->params, this, tab.tabular),
178 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
180 the_locking_inset = 0;
181 old_locking_inset = 0;
184 actrow = actcell = 0;
192 InsetTabular::~InsetTabular()
194 InsetTabularMailer(*this).hideDialog();
198 auto_ptr<InsetBase> InsetTabular::clone() const
200 return auto_ptr<InsetBase>(new InsetTabular(*this));
204 Buffer const & InsetTabular::buffer() const
210 BufferView * InsetTabular::view() const
217 void InsetTabular::buffer(Buffer * b)
223 void InsetTabular::write(Buffer const & buf, ostream & os) const
225 os << "Tabular" << endl;
226 tabular.write(buf, os);
230 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
232 bool const old_format = (lex.getString() == "\\LyXTable");
234 tabular.read(buf, lex);
240 string token = lex.getString();
241 while (lex.isOK() && (token != "\\end_inset")) {
243 token = lex.getString();
245 if (token != "\\end_inset") {
246 lex.printError("Missing \\end_inset at this point. "
252 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
254 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
255 // mi.base.textwidth << "\n";
257 lyxerr << "InsetTabular::metrics: need bv" << endl;
261 calculate_dimensions_of_cells(mi);
263 dim.asc = tabular.getAscentOfRow(0);
264 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
265 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
270 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
272 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
274 BufferView * bv = pi.base.bv;
277 UpdatableInset::draw(pi, x, y);
285 x += ADD_TO_TABULAR_WIDTH;
288 first_visible_cell = -1;
289 for (int i = 0; i < tabular.rows(); ++i) {
291 cell = tabular.getCellNumber(i, 0);
292 if (y + tabular.getDescentOfRow(i) <= 0 &&
293 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
295 y += tabular.getDescentOfRow(i) +
296 tabular.getAscentOfRow(i + 1) +
297 tabular.getAdditionalHeight(i + 1);
300 for (int j = 0; j < tabular.columns(); ++j) {
301 if (nx > bv->workWidth())
303 if (tabular.isPartOfMultiColumn(i, j))
305 int cx = nx + tabular.getBeginningOfTextInCell(cell);
306 if (first_visible_cell < 0)
307 first_visible_cell = cell;
308 if (hasSelection()) {
309 drawCellSelection(pi.pain, nx, y, i, j, cell);
312 tabular.getCellInset(cell).draw(pi, cx, y);
313 drawCellLines(pi.pain, nx, y, i, cell);
314 nx += tabular.getWidthOfColumn(cell);
318 // Would be nice, but for some completely unfathomable reason,
319 // on a col resize to a new fixed width, even though the insettexts
320 // are resized, the cell isn't, but drawing all cells in a tall table
321 // has the desired effect somehow. Complete dark magic.
323 // avoiding drawing the rest of a long table is
324 // a pretty big speedup
325 if (y > bv->workHeight())
329 y += tabular.getDescentOfRow(i) +
330 tabular.getAscentOfRow(i + 1) +
331 tabular.getAdditionalHeight(i + 1);
336 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
337 int row, int cell) const
339 int x2 = x + tabular.getWidthOfColumn(cell);
342 if (!tabular.topAlreadyDrawn(cell)) {
343 on_off = !tabular.topLine(cell);
344 pain.line(x, y - tabular.getAscentOfRow(row),
345 x2, y - tabular.getAscentOfRow(row),
346 on_off ? LColor::tabularonoffline : LColor::tabularline,
347 on_off ? Painter::line_onoffdash : Painter::line_solid);
349 on_off = !tabular.bottomLine(cell);
350 pain.line(x, y + tabular.getDescentOfRow(row),
351 x2, y + tabular.getDescentOfRow(row),
352 on_off ? LColor::tabularonoffline : LColor::tabularline,
353 on_off ? Painter::line_onoffdash : Painter::line_solid);
354 if (!tabular.leftAlreadyDrawn(cell)) {
355 on_off = !tabular.leftLine(cell);
356 pain.line(x, y - tabular.getAscentOfRow(row),
357 x, y + tabular.getDescentOfRow(row),
358 on_off ? LColor::tabularonoffline : LColor::tabularline,
359 on_off ? Painter::line_onoffdash : Painter::line_solid);
361 on_off = !tabular.rightLine(cell);
362 pain.line(x2 - tabular.getAdditionalWidth(cell),
363 y - tabular.getAscentOfRow(row),
364 x2 - tabular.getAdditionalWidth(cell),
365 y + tabular.getDescentOfRow(row),
366 on_off ? LColor::tabularonoffline : LColor::tabularline,
367 on_off ? Painter::line_onoffdash : Painter::line_solid);
371 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
372 int row, int column, int cell) const
374 Assert(hasSelection());
375 int cs = tabular.column_of_cell(sel_cell_start);
376 int ce = tabular.column_of_cell(sel_cell_end);
379 cs = tabular.column_of_cell(sel_cell_end);
381 ce = tabular.right_column_of_cell(sel_cell_end);
384 int rs = tabular.row_of_cell(sel_cell_start);
385 int re = tabular.row_of_cell(sel_cell_end);
389 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
390 int w = tabular.getWidthOfColumn(cell);
391 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
392 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
393 w, h, LColor::selection);
398 string const InsetTabular::editMessage() const
400 return _("Opened table");
404 void InsetTabular::insetUnlock(BufferView * bv)
406 if (the_locking_inset) {
407 the_locking_inset->insetUnlock(bv);
409 the_locking_inset = 0;
414 if (scroll(false) || hasSelection()) {
423 void InsetTabular::updateLocal(BufferView * bv) const
425 bv->updateInset(this);
431 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
433 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
438 if (inset == &tabular.getCellInset(actcell)) {
439 lyxerr[Debug::INSETTEXT] << "OK" << endl;
440 the_locking_inset = &tabular.getCellInset(actcell);
443 } else if (!the_locking_inset) {
444 int const n = tabular.getNumberOfCells();
445 int const id = inset->id();
446 for (int i = 0; i < n; ++i) {
447 InsetText * in = &tabular.getCellInset(i);
450 the_locking_inset = in;
455 if (in->getInsetFromID(id)) {
457 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
458 return the_locking_inset->lockInsetInInset(bv, inset);
461 } else if (the_locking_inset && (the_locking_inset == inset)) {
462 lyxerr[Debug::INSETTEXT] << "OK" << endl;
464 } else if (the_locking_inset) {
465 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
466 return the_locking_inset->lockInsetInInset(bv, inset);
468 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
473 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
476 if (!the_locking_inset)
478 if (the_locking_inset == inset) {
479 the_locking_inset->insetUnlock(bv);
481 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
488 // this has to be here otherwise we don't redraw the cell!
489 the_locking_inset = 0;
492 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
493 if (inset->lyxCode() == TABULAR_CODE &&
494 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
495 InsetTabularMailer(*this).updateDialog(bv);
504 int InsetTabular::insetInInsetY() const
506 if (!the_locking_inset)
508 return inset_y + the_locking_inset->insetInInsetY();
512 UpdatableInset * InsetTabular::getLockingInset() const
514 return the_locking_inset ? the_locking_inset->getLockingInset() :
515 const_cast<InsetTabular *>(this);
519 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
523 if (the_locking_inset)
524 return the_locking_inset->getFirstLockingInsetOfType(c);
529 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
531 if (the_locking_inset)
532 return the_locking_inset->insertInset(bv, inset);
537 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
539 if (hasSelection() && cmd.button() == mouse_button::button3)
542 if (hasSelection()) {
544 updateLocal(cmd.view());
547 int const ocell = actcell;
548 BufferView * bv = cmd.view();
552 the_locking_inset = 0;
556 setPos(bv, cmd.x, cmd.y);
559 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
561 if ((ocell == actcell) && the_locking_inset && inset_hit) {
563 FuncRequest cmd1 = cmd;
566 the_locking_inset->localDispatch(cmd1);
570 if (the_locking_inset) {
571 the_locking_inset->insetUnlock(bv);
573 the_locking_inset = 0;
576 if (cmd.button() == mouse_button::button2) {
577 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
581 if (inset_hit && bv->theLockingInset()) {
582 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
583 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
586 FuncRequest cmd1 = cmd;
589 the_locking_inset->localDispatch(cmd1);
594 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
597 if (the_locking_inset) {
598 FuncRequest cmd1 = cmd;
601 ret = the_locking_inset->localDispatch(cmd1);
603 if (cmd.button() == mouse_button::button3 && !ret) {
604 InsetTabularMailer(*this).showDialog(cmd.view());
611 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
613 if (the_locking_inset) {
614 FuncRequest cmd1 = cmd;
617 the_locking_inset->localDispatch(cmd1);
621 BufferView * bv = cmd.view();
622 int const old_cell = actcell;
624 setPos(bv, cmd.x, cmd.y);
625 if (!hasSelection()) {
626 setSelection(actcell, actcell);
628 } else if (old_cell != actcell) {
629 setSelection(sel_cell_start, actcell);
635 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
637 // We need to save the value of the_locking_inset as the call to
638 // the_locking_inset->localDispatch might unlock it.
639 old_locking_inset = the_locking_inset;
640 RESULT result = UpdatableInset::localDispatch(cmd);
641 BufferView * bv = cmd.view();
643 if (cmd.action == LFUN_INSET_EDIT) {
645 if (!bv->lockInset(this)) {
646 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
652 the_locking_inset = 0;
656 if (cmd.argument.size()) {
657 if (cmd.argument == "left") {
658 if (isRightToLeft(bv))
659 actcell = tabular.getLastCellInRow(0);
663 if (isRightToLeft(bv))
664 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
666 actcell = tabular.getNumberOfCells() - 1;
674 setPos(bv, cmd.x, cmd.y);
677 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
678 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
684 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
689 if (cmd.action < 0 && cmd.argument.empty())
692 bool hs = hasSelection();
695 // this one have priority over the locked InsetText, if we're not already
696 // inside another tabular then that one get's priority!
697 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
698 switch (cmd.action) {
699 case LFUN_MOUSE_PRESS:
703 case LFUN_MOUSE_MOTION:
704 lfunMouseMotion(cmd);
707 case LFUN_MOUSE_RELEASE:
708 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
710 case LFUN_CELL_BACKWARD:
711 case LFUN_CELL_FORWARD:
712 unlockInsetInInset(bv, the_locking_inset);
713 if (cmd.action == LFUN_CELL_FORWARD)
714 moveNextCell(bv, old_locking_inset != 0);
716 movePrevCell(bv, old_locking_inset != 0);
720 if (!the_locking_inset)
721 return DISPATCHED_NOUPDATE;
723 // this to avoid compiler warnings.
729 kb_action action = cmd.action;
730 string arg = cmd.argument;
731 if (the_locking_inset) {
732 result = the_locking_inset->localDispatch(cmd);
733 if (result == DISPATCHED_NOUPDATE) {
736 if (sc != scroll()) { // inset has been scrolled
740 } else if (result == DISPATCHED) {
743 } else if (result == FINISHED_UP) {
745 // Make sure to reset status message after
746 // exiting, e.g. math inset
747 bv->owner()->clearMessage();
748 } else if (result == FINISHED_DOWN) {
750 bv->owner()->clearMessage();
751 } else if (result == FINISHED_RIGHT) {
753 bv->owner()->clearMessage();
754 } else if (result == FINISHED) {
755 bv->owner()->clearMessage();
761 // --- Cursor Movements ----------------------------------
762 case LFUN_RIGHTSEL: {
763 int const start = hasSelection() ? sel_cell_start : actcell;
764 if (tabular.isLastCellInRow(actcell)) {
765 setSelection(start, actcell);
770 // if we are starting a selection, only select
771 // the current cell at the beginning
772 if (hasSelection()) {
773 moveRight(bv, false);
776 setSelection(start, end);
781 result = moveRight(bv);
787 int const start = hasSelection() ? sel_cell_start : actcell;
788 if (tabular.isFirstCellInRow(actcell)) {
789 setSelection(start, actcell);
794 // if we are starting a selection, only select
795 // the current cell at the beginning
796 if (hasSelection()) {
800 setSelection(start, end);
805 result = moveLeft(bv);
811 int const start = hasSelection() ? sel_cell_start : actcell;
812 int const ocell = actcell;
813 // if we are starting a selection, only select
814 // the current cell at the beginning
815 if (hasSelection()) {
817 if (ocell == sel_cell_end ||
818 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
819 setSelection(start, tabular.getCellBelow(sel_cell_end));
821 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
823 setSelection(start, start);
829 result = moveDown(bv, old_locking_inset != 0);
835 int const start = hasSelection() ? sel_cell_start : actcell;
836 int const ocell = actcell;
837 // if we are starting a selection, only select
838 // the current cell at the beginning
839 if (hasSelection()) {
841 if ((ocell == sel_cell_end) ||
842 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
843 setSelection(start, tabular.getCellAbove(sel_cell_end));
845 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
847 setSelection(start, start);
853 result = moveUp(bv, old_locking_inset != 0);
862 unlockInsetInInset(bv, the_locking_inset);
863 if (bv->top_y() + bv->painter().paperHeight() <
864 top_baseline + tabular.getHeightOfTabular())
866 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
867 actcell = tabular.getCellBelow(first_visible_cell) + column;
869 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
879 unlockInsetInInset(bv, the_locking_inset);
880 if (top_baseline < 0) {
881 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
882 if (top_baseline > 0)
885 actcell = tabular.getCellBelow(first_visible_cell) + column;
893 // none of these make sense for insettabular,
894 // but we must catch them to prevent any
895 // selection from being confused
899 case LFUN_WORDLEFTSEL:
901 case LFUN_WORDRIGHTSEL:
903 case LFUN_DOWN_PARAGRAPH:
904 case LFUN_DOWN_PARAGRAPHSEL:
905 case LFUN_UP_PARAGRAPH:
906 case LFUN_UP_PARAGRAPHSEL:
912 case LFUN_BEGINNINGBUF:
913 case LFUN_BEGINNINGBUFSEL:
917 case LFUN_LAYOUT_TABULAR: {
918 InsetTabularMailer(*this).showDialog(bv);
921 case LFUN_INSET_DIALOG_UPDATE: {
922 InsetTabularMailer(*this).updateDialog(bv);
925 case LFUN_TABULAR_FEATURE:
926 if (!tabularFeatures(bv, arg))
927 result = UNDISPATCHED;
929 // insert file functions
930 case LFUN_FILE_INSERT_ASCII_PARA:
931 case LFUN_FILE_INSERT_ASCII:
933 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
936 if (insertAsciiString(bv, tmpstr, false))
939 result = UNDISPATCHED;
942 // cut and paste functions
944 if (!copySelection(bv))
948 recordUndo(bv, Undo::DELETE);
949 cutSelection(bv->buffer()->params);
958 case LFUN_PASTESELECTION:
960 string const clip(bv->getClipboard());
964 if (clip.find('\t') != string::npos) {
968 string::size_type len = clip.length();
969 string::size_type p = 0;
972 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
980 maxCols = max(cols, maxCols);
986 maxCols = max(cols, maxCols);
987 delete paste_tabular;
988 paste_tabular = new LyXTabular(bv->buffer()->params,
989 this, rows, maxCols);
990 string::size_type op = 0;
992 int cells = paste_tabular->getNumberOfCells();
994 while ((cell < cells) && (p < len) &&
995 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1000 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1005 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1006 while (cols++ < maxCols)
1014 // check for the last cell if there is no trailing '\n'
1015 if ((cell < cells) && (op < len))
1016 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1019 if (!insertAsciiString(bv, clip, true))
1022 // so that the clipboard is used and it goes on
1024 // and executes LFUN_PASTESELECTION in insettext!
1025 delete paste_tabular;
1030 if (hasPasteBuffer()) {
1031 recordUndo(bv, Undo::INSERT);
1036 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1038 // handle font changing stuff on selection before we lock the inset
1039 // in the default part!
1040 result = UNDISPATCHED;
1051 case LFUN_UNDERLINE:
1052 case LFUN_FONT_SIZE:
1053 if (bv->dispatch(FuncRequest(bv, action, arg)))
1054 result = DISPATCHED;
1060 // we try to activate the actual inset and put this event down to
1061 // the insets dispatch function.
1062 if (result == DISPATCHED || the_locking_inset)
1064 if (activateCellInset(bv)) {
1065 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1066 if (result == UNDISPATCHED || result >= FINISHED) {
1067 unlockInsetInInset(bv, the_locking_inset);
1068 // we need to update if this was requested before
1070 return UNDISPATCHED;
1079 if (result < FINISHED) {
1080 if (!the_locking_inset && bv->fitCursor())
1083 bv->unlockInset(this);
1088 int InsetTabular::latex(Buffer const & buf, ostream & os,
1089 LatexRunParams const & runparams) const
1091 return tabular.latex(buf, os, runparams);
1095 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1098 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1100 return tabular.ascii(buf, os, 0, false, 0);
1104 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1106 return tabular.linuxdoc(buf,os);
1110 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1115 // if the table is inside a float it doesn't need the informaltable
1116 // wrapper. Search for it.
1117 for (master = owner();
1118 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1119 master = master->owner());
1122 os << "<informaltable>";
1127 ret += tabular.docbook(buf, os, mixcont);
1129 os << "</informaltable>";
1138 void InsetTabular::validate(LaTeXFeatures & features) const
1140 tabular.validate(features);
1144 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1147 // if we have a locking_inset we should have to check only this cell for
1148 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1149 // This is _really_ important (André)
1150 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1153 for (int j = 0; j < tabular.columns(); ++j) {
1157 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1158 tabular.getCellInset(actrow, j).metrics(m, dim);
1159 maxAsc = max(dim.asc, maxAsc);
1160 maxDesc = max(dim.des, maxDesc);
1162 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1163 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1164 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1170 for (int i = 0; i < tabular.rows(); ++i) {
1173 for (int j = 0; j < tabular.columns(); ++j) {
1174 if (tabular.isPartOfMultiColumn(i, j))
1180 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1181 tabular.getCellInset(cell).metrics(m, dim);
1182 maxAsc = max(maxAsc, dim.asc);
1183 maxDesc = max(maxDesc, dim.des);
1184 tabular.setWidthOfCell(cell, dim.wid);
1186 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1187 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1192 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1194 if (the_locking_inset) {
1195 the_locking_inset->getCursor(bv, x, y);
1200 y = cursory_ + InsetTabular::y();
1203 int desc = tabular.getDescentOfRow(actrow);
1205 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1207 y += ADD_TO_HEIGHT * 2;
1208 y += TEXT_TO_INSET_OFFSET;
1212 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1214 if (the_locking_inset) {
1215 the_locking_inset->getCursorPos(bv, x, y);
1218 x = cursorx_ - top_x;
1223 void InsetTabular::fitInsetCursor(BufferView * bv) const
1225 if (the_locking_inset) {
1226 the_locking_inset->fitInsetCursor(bv);
1231 int const asc = font_metrics::maxAscent(font);
1232 int const desc = font_metrics::maxDescent(font);
1235 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1239 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1242 actcell = actrow = actcol = 0;
1243 int ly = tabular.getDescentOfRow(actrow);
1245 // first search the right row
1246 while (ly < y && actrow + 1 < tabular.rows()) {
1247 cursory_ += tabular.getDescentOfRow(actrow) +
1248 tabular.getAscentOfRow(actrow + 1) +
1249 tabular.getAdditionalHeight(actrow + 1);
1251 ly = cursory_ + tabular.getDescentOfRow(actrow);
1253 actcell = tabular.getCellNumber(actrow, actcol);
1255 // now search the right column
1256 int lx = tabular.getWidthOfColumn(actcell) -
1257 tabular.getAdditionalWidth(actcell);
1259 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1260 lx += tabular.getWidthOfColumn(actcell + 1)
1261 + tabular.getAdditionalWidth(actcell);
1263 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1268 int InsetTabular::getCellXPos(int cell) const
1272 for (; !tabular.isFirstCellInRow(c); --c)
1274 int lx = tabular.getWidthOfColumn(cell);
1275 for (; c < cell; ++c)
1276 lx += tabular.getWidthOfColumn(c);
1278 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1282 void InsetTabular::resetPos(BufferView * bv) const
1284 #ifdef WITH_WARNINGS
1285 #warning This should be fixed in the right manner (20011128 Jug)
1287 // fast hack to fix infinite repaintings!
1288 if (in_reset_pos > 0)
1292 actcol = tabular.column_of_cell(actcell);
1295 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1296 if (tabular.isLastCellInRow(cell)) {
1297 cursory_ += tabular.getDescentOfRow(actrow) +
1298 tabular.getAscentOfRow(actrow + 1) +
1299 tabular.getAdditionalHeight(actrow + 1);
1304 if (the_locking_inset)
1308 // we need this only from here on!!!
1310 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1311 int new_x = getCellXPos(actcell);
1312 int old_x = cursorx_;
1315 // cursor.x(getCellXPos(actcell) + offset);
1316 if (actcol < tabular.columns() - 1 && scroll(false) &&
1317 tabular.getWidthOfTabular() < bv->workWidth()-20)
1321 } else if (the_locking_inset &&
1322 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1324 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1325 if (xx > bv->workWidth()-20) {
1326 scroll(bv, -(xx - bv->workWidth() + 60));
1328 } else if (xx < 20) {
1336 } else if (cursorx_ - offset > 20 &&
1337 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1338 > bv->workWidth() - 20) {
1339 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1341 } else if (cursorx_ - offset < 20) {
1342 scroll(bv, 20 - cursorx_ + offset);
1344 } else if (scroll() && top_x > 20 &&
1345 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1346 scroll(bv, old_x - cursorx_);
1349 if (the_locking_inset) {
1350 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1353 if ((!the_locking_inset ||
1354 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1355 actcell != oldcell) {
1356 InsetTabularMailer(*this).updateDialog(bv);
1363 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1365 if (lock && !old_locking_inset) {
1366 if (activateCellInset(bv))
1369 bool moved = isRightToLeft(bv)
1370 ? movePrevCell(bv) : moveNextCell(bv);
1372 return FINISHED_RIGHT;
1373 if (lock && activateCellInset(bv))
1377 return DISPATCHED_NOUPDATE;
1381 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1383 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1386 if (lock) { // behind the inset
1387 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1391 return DISPATCHED_NOUPDATE;
1395 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1397 int const ocell = actcell;
1398 actcell = tabular.getCellAbove(actcell);
1399 if (actcell == ocell) // we moved out of the inset
1405 if (old_locking_inset) {
1406 old_locking_inset->getCursorPos(bv, x, y);
1407 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1409 if (activateCellInset(bv, x, 0))
1412 return DISPATCHED_NOUPDATE;
1416 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1418 int const ocell = actcell;
1419 actcell = tabular.getCellBelow(actcell);
1420 if (actcell == ocell) // we moved out of the inset
1421 return FINISHED_DOWN;
1426 if (old_locking_inset) {
1427 old_locking_inset->getCursorPos(bv, x, y);
1428 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1430 if (activateCellInset(bv, x, 0))
1433 return DISPATCHED_NOUPDATE;
1437 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1439 if (isRightToLeft(bv)) {
1440 if (tabular.isFirstCellInRow(actcell)) {
1441 int row = tabular.row_of_cell(actcell);
1442 if (row == tabular.rows() - 1)
1444 actcell = tabular.getLastCellInRow(row);
1445 actcell = tabular.getCellBelow(actcell);
1452 if (tabular.isLastCell(actcell))
1457 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1458 isRightToLeftPar(bv->buffer()->params);
1459 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1466 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1468 if (isRightToLeft(bv)) {
1469 if (tabular.isLastCellInRow(actcell)) {
1470 int row = tabular.row_of_cell(actcell);
1473 actcell = tabular.getFirstCellInRow(row);
1474 actcell = tabular.getCellAbove(actcell);
1476 if (tabular.isLastCell(actcell))
1481 if (!actcell) // first cell
1486 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1487 isRightToLeftPar(bv->buffer()->params);
1488 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1495 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1499 setSelection(0, tabular.getNumberOfCells() - 1);
1501 if (hasSelection()) {
1502 recordUndo(bv, Undo::ATOMIC);
1503 bool const frozen = undo_frozen;
1506 // apply the fontchange on the whole selection
1511 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1512 for(int i = sel_row_start; i <= sel_row_end; ++i)
1513 for(int j = sel_col_start; j <= sel_col_end; ++j)
1514 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1522 if (the_locking_inset)
1523 the_locking_inset->setFont(bv, font, tall);
1527 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1529 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1532 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1533 string const tmp = tabularFeature[i].feature;
1535 if (tmp == what.substr(0, tmp.length())) {
1536 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1537 //tabularFeatures[i].feature.length())) {
1538 action = tabularFeature[i].action;
1542 if (action == LyXTabular::LAST_ACTION)
1546 ltrim(what.substr(tabularFeature[i].feature.length()));
1547 tabularFeatures(bv, action, val);
1553 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1554 string const & special, bool & flag)
1556 if (special == "dl_above") {
1559 } else if (special == "dl_below") {
1560 ltt.bottomDL = flag;
1562 } else if (special == "empty") {
1574 void InsetTabular::tabularFeatures(BufferView * bv,
1575 LyXTabular::Feature feature,
1576 string const & value)
1582 bool setLines = false;
1583 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1584 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1588 case LyXTabular::M_ALIGN_LEFT:
1589 case LyXTabular::ALIGN_LEFT:
1590 setAlign = LYX_ALIGN_LEFT;
1593 case LyXTabular::M_ALIGN_RIGHT:
1594 case LyXTabular::ALIGN_RIGHT:
1595 setAlign = LYX_ALIGN_RIGHT;
1598 case LyXTabular::M_ALIGN_CENTER:
1599 case LyXTabular::ALIGN_CENTER:
1600 setAlign = LYX_ALIGN_CENTER;
1603 case LyXTabular::ALIGN_BLOCK:
1604 setAlign = LYX_ALIGN_BLOCK;
1607 case LyXTabular::M_VALIGN_TOP:
1608 case LyXTabular::VALIGN_TOP:
1609 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1612 case LyXTabular::M_VALIGN_BOTTOM:
1613 case LyXTabular::VALIGN_BOTTOM:
1614 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1617 case LyXTabular::M_VALIGN_MIDDLE:
1618 case LyXTabular::VALIGN_MIDDLE:
1619 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1626 if (hasSelection()) {
1627 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1629 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1630 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1632 recordUndo(bv, Undo::ATOMIC);
1634 int row = tabular.row_of_cell(actcell);
1635 int column = tabular.column_of_cell(actcell);
1637 LyXTabular::ltType ltt;
1641 case LyXTabular::SET_PWIDTH:
1643 LyXLength const vallen(value);
1644 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1646 bool const update = (tmplen != vallen);
1647 tabular.setColumnPWidth(actcell, vallen);
1649 // We need this otherwise we won't resize
1650 // the insettext of the active cell (if any)
1651 // until later (see InsetText::do_resize)
1652 unlockInsetInInset(bv, the_locking_inset);
1657 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1658 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1659 else if (!vallen.zero()
1660 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1661 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1665 case LyXTabular::SET_MPWIDTH:
1667 LyXLength const vallen(value);
1668 LyXLength const & tmplen = tabular.getPWidth(actcell);
1670 bool const update = (tmplen != vallen);
1671 tabular.setMColumnPWidth(actcell, vallen);
1673 // We need this otherwise we won't resize
1674 // the insettext of the active cell (if any)
1675 // until later (see InsetText::do_resize)
1676 unlockInsetInInset(bv, the_locking_inset);
1681 case LyXTabular::SET_SPECIAL_COLUMN:
1682 case LyXTabular::SET_SPECIAL_MULTI:
1683 tabular.setAlignSpecial(actcell,value,feature);
1686 case LyXTabular::APPEND_ROW:
1687 // append the row into the tabular
1688 unlockInsetInInset(bv, the_locking_inset);
1689 tabular.appendRow(bv->buffer()->params, actcell);
1692 case LyXTabular::APPEND_COLUMN:
1693 // append the column into the tabular
1694 unlockInsetInInset(bv, the_locking_inset);
1695 tabular.appendColumn(bv->buffer()->params, actcell);
1696 actcell = tabular.getCellNumber(row, column);
1699 case LyXTabular::DELETE_ROW:
1700 unlockInsetInInset(bv, the_locking_inset);
1701 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1702 tabular.deleteRow(sel_row_start);
1704 if (sel_row_start >= tabular.rows())
1706 actcell = tabular.getCellNumber(sel_row_start, column);
1710 case LyXTabular::DELETE_COLUMN:
1711 unlockInsetInInset(bv, the_locking_inset);
1712 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1713 tabular.deleteColumn(sel_col_start);
1715 if (sel_col_start >= tabular.columns())
1717 actcell = tabular.getCellNumber(row, sel_col_start);
1721 case LyXTabular::M_TOGGLE_LINE_TOP:
1723 case LyXTabular::TOGGLE_LINE_TOP:
1725 bool lineSet = !tabular.topLine(actcell, flag);
1726 for (int i = sel_row_start; i <= sel_row_end; ++i)
1727 for (int j = sel_col_start; j <= sel_col_end; ++j)
1729 tabular.getCellNumber(i, j),
1735 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1737 case LyXTabular::TOGGLE_LINE_BOTTOM:
1739 bool lineSet = !tabular.bottomLine(actcell, flag);
1740 for (int i = sel_row_start; i <= sel_row_end; ++i)
1741 for (int j = sel_col_start; j <= sel_col_end; ++j)
1742 tabular.setBottomLine(
1743 tabular.getCellNumber(i, j),
1750 case LyXTabular::M_TOGGLE_LINE_LEFT:
1752 case LyXTabular::TOGGLE_LINE_LEFT:
1754 bool lineSet = !tabular.leftLine(actcell, flag);
1755 for (int i = sel_row_start; i <= sel_row_end; ++i)
1756 for (int j = sel_col_start; j <= sel_col_end; ++j)
1757 tabular.setLeftLine(
1758 tabular.getCellNumber(i,j),
1765 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1767 case LyXTabular::TOGGLE_LINE_RIGHT:
1769 bool lineSet = !tabular.rightLine(actcell, flag);
1770 for (int i = sel_row_start; i <= sel_row_end; ++i)
1771 for (int j = sel_col_start; j <= sel_col_end; ++j)
1772 tabular.setRightLine(
1773 tabular.getCellNumber(i,j),
1780 case LyXTabular::M_ALIGN_LEFT:
1781 case LyXTabular::M_ALIGN_RIGHT:
1782 case LyXTabular::M_ALIGN_CENTER:
1784 case LyXTabular::ALIGN_LEFT:
1785 case LyXTabular::ALIGN_RIGHT:
1786 case LyXTabular::ALIGN_CENTER:
1787 case LyXTabular::ALIGN_BLOCK:
1788 for (int i = sel_row_start; i <= sel_row_end; ++i)
1789 for (int j = sel_col_start; j <= sel_col_end; ++j)
1790 tabular.setAlignment(
1791 tabular.getCellNumber(i, j),
1797 case LyXTabular::M_VALIGN_TOP:
1798 case LyXTabular::M_VALIGN_BOTTOM:
1799 case LyXTabular::M_VALIGN_MIDDLE:
1801 case LyXTabular::VALIGN_TOP:
1802 case LyXTabular::VALIGN_BOTTOM:
1803 case LyXTabular::VALIGN_MIDDLE:
1804 for (int i = sel_row_start; i <= sel_row_end; ++i)
1805 for (int j = sel_col_start; j <= sel_col_end; ++j)
1806 tabular.setVAlignment(
1807 tabular.getCellNumber(i, j),
1812 case LyXTabular::MULTICOLUMN: {
1813 if (sel_row_start != sel_row_end) {
1814 #ifdef WITH_WARNINGS
1815 #warning Need I say it ? This is horrible.
1817 Alert::error(_("Error setting multicolumn"),
1818 _("You cannot set multicolumn vertically."));
1821 // just multicol for one Single Cell
1822 if (!hasSelection()) {
1823 // check wether we are completly in a multicol
1824 if (tabular.isMultiColumn(actcell))
1825 tabular.unsetMultiColumn(actcell);
1827 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1831 // we have a selection so this means we just add all this
1832 // cells to form a multicolumn cell
1836 if (sel_cell_start > sel_cell_end) {
1837 s_start = sel_cell_end;
1838 s_end = sel_cell_start;
1840 s_start = sel_cell_start;
1841 s_end = sel_cell_end;
1843 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1850 case LyXTabular::SET_ALL_LINES:
1852 case LyXTabular::UNSET_ALL_LINES:
1853 for (int i = sel_row_start; i <= sel_row_end; ++i)
1854 for (int j = sel_col_start; j <= sel_col_end; ++j)
1855 tabular.setAllLines(
1856 tabular.getCellNumber(i,j), setLines);
1860 case LyXTabular::SET_LONGTABULAR:
1861 tabular.setLongTabular(true);
1862 updateLocal(bv); // because this toggles displayed
1865 case LyXTabular::UNSET_LONGTABULAR:
1866 tabular.setLongTabular(false);
1867 updateLocal(bv); // because this toggles displayed
1870 case LyXTabular::SET_ROTATE_TABULAR:
1871 tabular.setRotateTabular(true);
1874 case LyXTabular::UNSET_ROTATE_TABULAR:
1875 tabular.setRotateTabular(false);
1878 case LyXTabular::SET_ROTATE_CELL:
1879 for (int i = sel_row_start; i <= sel_row_end; ++i)
1880 for (int j = sel_col_start; j<=sel_col_end; ++j)
1881 tabular.setRotateCell(
1882 tabular.getCellNumber(i, j), true);
1885 case LyXTabular::UNSET_ROTATE_CELL:
1886 for (int i = sel_row_start; i <= sel_row_end; ++i)
1887 for (int j = sel_col_start; j <= sel_col_end; ++j)
1888 tabular.setRotateCell(
1889 tabular.getCellNumber(i, j), false);
1892 case LyXTabular::SET_USEBOX: {
1893 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1894 if (val == tabular.getUsebox(actcell))
1895 val = LyXTabular::BOX_NONE;
1896 for (int i = sel_row_start; i <= sel_row_end; ++i)
1897 for (int j = sel_col_start; j <= sel_col_end; ++j)
1898 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1902 case LyXTabular::UNSET_LTFIRSTHEAD:
1904 case LyXTabular::SET_LTFIRSTHEAD:
1905 tabular.getRowOfLTFirstHead(row, ltt);
1906 checkLongtableSpecial(ltt, value, flag);
1907 tabular.setLTHead(row, flag, ltt, true);
1910 case LyXTabular::UNSET_LTHEAD:
1912 case LyXTabular::SET_LTHEAD:
1913 tabular.getRowOfLTHead(row, ltt);
1914 checkLongtableSpecial(ltt, value, flag);
1915 tabular.setLTHead(row, flag, ltt, false);
1918 case LyXTabular::UNSET_LTFOOT:
1920 case LyXTabular::SET_LTFOOT:
1921 tabular.getRowOfLTFoot(row, ltt);
1922 checkLongtableSpecial(ltt, value, flag);
1923 tabular.setLTFoot(row, flag, ltt, false);
1926 case LyXTabular::UNSET_LTLASTFOOT:
1928 case LyXTabular::SET_LTLASTFOOT:
1929 tabular.getRowOfLTLastFoot(row, ltt);
1930 checkLongtableSpecial(ltt, value, flag);
1931 tabular.setLTFoot(row, flag, ltt, true);
1934 case LyXTabular::SET_LTNEWPAGE: {
1935 bool what = !tabular.getLTNewPage(row);
1936 tabular.setLTNewPage(row, what);
1940 // dummy stuff just to avoid warnings
1941 case LyXTabular::LAST_ACTION:
1945 InsetTabularMailer(*this).updateDialog(bv);
1949 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1950 mouse_button::state button, bool behind)
1952 UpdatableInset & inset = tabular.getCellInset(actcell);
1955 x = inset.x() + inset.width();
1956 y = inset.descent();
1958 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1959 //inset_y = cursory_;
1960 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1961 if (!the_locking_inset)
1964 return the_locking_inset;
1968 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1969 mouse_button::state button)
1971 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1973 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1977 bool InsetTabular::insetHit(BufferView *, int x, int) const
1979 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1983 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1985 //resizeLyXText(bv, recursive);
1989 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1990 bool const recursive) const
1992 if (the_locking_inset)
1993 return the_locking_inset->getLyXText(bv, recursive);
1994 return InsetOld::getLyXText(bv, recursive);
1998 bool InsetTabular::showInsetDialog(BufferView * bv) const
2000 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2001 InsetTabularMailer(*this).showDialog(bv);
2006 void InsetTabular::openLayoutDialog(BufferView * bv) const
2008 if (the_locking_inset) {
2009 InsetTabular * inset = static_cast<InsetTabular *>
2010 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2012 inset->openLayoutDialog(bv);
2016 InsetTabularMailer(*this).showDialog(bv);
2021 // function returns an object as defined in func_status.h:
2022 // states OK, Unknown, Disabled, On, Off.
2024 FuncStatus InsetTabular::getStatus(string const & what) const
2026 int action = LyXTabular::LAST_ACTION;
2030 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2031 string const tmp = tabularFeature[i].feature;
2032 if (tmp == what.substr(0, tmp.length())) {
2033 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2034 // tabularFeatures[i].feature.length())) {
2035 action = tabularFeature[i].action;
2039 if (action == LyXTabular::LAST_ACTION) {
2041 return status.unknown(true);
2044 string const argument
2045 = ltrim(what.substr(tabularFeature[i].feature.length()));
2050 LyXTabular::ltType dummyltt;
2054 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2056 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2059 case LyXTabular::SET_PWIDTH:
2060 case LyXTabular::SET_MPWIDTH:
2061 case LyXTabular::SET_SPECIAL_COLUMN:
2062 case LyXTabular::SET_SPECIAL_MULTI:
2063 case LyXTabular::APPEND_ROW:
2064 case LyXTabular::APPEND_COLUMN:
2065 case LyXTabular::DELETE_ROW:
2066 case LyXTabular::DELETE_COLUMN:
2067 case LyXTabular::SET_ALL_LINES:
2068 case LyXTabular::UNSET_ALL_LINES:
2069 return status.clear();
2071 case LyXTabular::MULTICOLUMN:
2072 status.setOnOff(tabular.isMultiColumn(actcell));
2075 case LyXTabular::M_TOGGLE_LINE_TOP:
2077 case LyXTabular::TOGGLE_LINE_TOP:
2078 status.setOnOff(tabular.topLine(actcell, flag));
2081 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2083 case LyXTabular::TOGGLE_LINE_BOTTOM:
2084 status.setOnOff(tabular.bottomLine(actcell, flag));
2087 case LyXTabular::M_TOGGLE_LINE_LEFT:
2089 case LyXTabular::TOGGLE_LINE_LEFT:
2090 status.setOnOff(tabular.leftLine(actcell, flag));
2093 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2095 case LyXTabular::TOGGLE_LINE_RIGHT:
2096 status.setOnOff(tabular.rightLine(actcell, flag));
2099 case LyXTabular::M_ALIGN_LEFT:
2101 case LyXTabular::ALIGN_LEFT:
2102 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2105 case LyXTabular::M_ALIGN_RIGHT:
2107 case LyXTabular::ALIGN_RIGHT:
2108 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2111 case LyXTabular::M_ALIGN_CENTER:
2113 case LyXTabular::ALIGN_CENTER:
2114 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2117 case LyXTabular::ALIGN_BLOCK:
2118 status.disabled(tabular.getPWidth(actcell).zero());
2119 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2122 case LyXTabular::M_VALIGN_TOP:
2124 case LyXTabular::VALIGN_TOP:
2125 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2128 case LyXTabular::M_VALIGN_BOTTOM:
2130 case LyXTabular::VALIGN_BOTTOM:
2131 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2134 case LyXTabular::M_VALIGN_MIDDLE:
2136 case LyXTabular::VALIGN_MIDDLE:
2137 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2140 case LyXTabular::SET_LONGTABULAR:
2141 status.setOnOff(tabular.isLongTabular());
2144 case LyXTabular::UNSET_LONGTABULAR:
2145 status.setOnOff(!tabular.isLongTabular());
2148 case LyXTabular::SET_ROTATE_TABULAR:
2149 status.setOnOff(tabular.getRotateTabular());
2152 case LyXTabular::UNSET_ROTATE_TABULAR:
2153 status.setOnOff(!tabular.getRotateTabular());
2156 case LyXTabular::SET_ROTATE_CELL:
2157 status.setOnOff(tabular.getRotateCell(actcell));
2160 case LyXTabular::UNSET_ROTATE_CELL:
2161 status.setOnOff(!tabular.getRotateCell(actcell));
2164 case LyXTabular::SET_USEBOX:
2165 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2168 case LyXTabular::SET_LTFIRSTHEAD:
2169 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2172 case LyXTabular::SET_LTHEAD:
2173 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2176 case LyXTabular::SET_LTFOOT:
2177 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2180 case LyXTabular::SET_LTLASTFOOT:
2181 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2184 case LyXTabular::SET_LTNEWPAGE:
2185 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2190 status.disabled(true);
2197 void InsetTabular::getLabelList(std::vector<string> & list) const
2199 tabular.getLabelList(list);
2203 bool InsetTabular::copySelection(BufferView * bv)
2205 if (!hasSelection())
2208 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2209 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2210 if (sel_col_start > sel_col_end) {
2211 sel_col_start = sel_col_end;
2212 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2214 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2217 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2218 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2219 if (sel_row_start > sel_row_end)
2220 swap(sel_row_start, sel_row_end);
2222 delete paste_tabular;
2223 paste_tabular = new LyXTabular(bv->buffer()->params, this, tabular);
2225 for (int i = 0; i < sel_row_start; ++i)
2226 paste_tabular->deleteRow(0);
2228 int const rows = sel_row_end - sel_row_start + 1;
2229 while (paste_tabular->rows() > rows)
2230 paste_tabular->deleteRow(rows);
2232 paste_tabular->setTopLine(0, true, true);
2233 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2236 for (int i = 0; i < sel_col_start; ++i)
2237 paste_tabular->deleteColumn(0);
2239 int const columns = sel_col_end - sel_col_start + 1;
2240 while (paste_tabular->columns() > columns)
2241 paste_tabular->deleteColumn(columns);
2243 paste_tabular->setLeftLine(0, true, true);
2244 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2248 paste_tabular->ascii(*bv->buffer(), os,
2249 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2250 bv->stuffClipboard(STRCONV(os.str()));
2255 bool InsetTabular::pasteSelection(BufferView * bv)
2260 for (int r1 = 0, r2 = actrow;
2261 r1 < paste_tabular->rows() && r2 < tabular.rows();
2263 for (int c1 = 0, c2 = actcol;
2264 c1 < paste_tabular->columns() && c2 < tabular.columns();
2266 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2267 tabular.isPartOfMultiColumn(r2, c2))
2269 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2273 if (tabular.isPartOfMultiColumn(r2, c2)) {
2277 InsetText & inset = tabular.getCellInset(r2, c2);
2278 inset = paste_tabular->getCellInset(r1, c1);
2279 inset.setOwner(this);
2280 inset.deleteLyXText(bv);
2288 bool InsetTabular::cutSelection(BufferParams const & bp)
2290 if (!hasSelection())
2293 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2294 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2295 if (sel_col_start > sel_col_end) {
2296 sel_col_start = sel_col_end;
2297 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2299 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2302 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2303 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2305 if (sel_row_start > sel_row_end)
2306 swap(sel_row_start, sel_row_end);
2308 if (sel_cell_start > sel_cell_end)
2309 swap(sel_cell_start, sel_cell_end);
2311 for (int i = sel_row_start; i <= sel_row_end; ++i)
2312 for (int j = sel_col_start; j <= sel_col_end; ++j)
2313 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2318 bool InsetTabular::isRightToLeft(BufferView * bv)
2320 return bv->getParentLanguage(this)->RightToLeft();
2324 int InsetTabular::scroll(bool recursive) const
2326 int sx = UpdatableInset::scroll(false);
2328 if (recursive && the_locking_inset)
2329 sx += the_locking_inset->scroll(recursive);
2335 void InsetTabular::getSelection(int & srow, int & erow,
2336 int & scol, int & ecol) const
2338 int const start = hasSelection() ? sel_cell_start : actcell;
2339 int const end = hasSelection() ? sel_cell_end : actcell;
2341 srow = tabular.row_of_cell(start);
2342 erow = tabular.row_of_cell(end);
2346 scol = tabular.column_of_cell(start);
2347 ecol = tabular.column_of_cell(end);
2351 ecol = tabular.right_column_of_cell(end);
2355 ParagraphList * InsetTabular::getParagraphs(int i) const
2357 return (i < tabular.getNumberOfCells())
2358 ? tabular.getCellInset(i).getParagraphs(0)
2363 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2365 if (the_locking_inset)
2366 return the_locking_inset->cursor(bv);
2367 return InsetOld::cursor(bv);
2371 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2374 return const_cast<InsetTabular *>(this);
2376 for (int i = 0; i < tabular.rows(); ++i) {
2377 for (int j = 0; j < tabular.columns(); ++j) {
2378 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2388 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2390 if (the_locking_inset) {
2391 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2392 if (!word.word().empty())
2394 if (tabular.isLastCell(actcell)) {
2395 bv->unlockInset(const_cast<InsetTabular *>(this));
2396 return WordLangTuple();
2400 // otherwise we have to lock the next inset and ask for it's selecttion
2401 tabular.getCellInset(actcell)
2402 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2403 WordLangTuple word(selectNextWordInt(bv, value));
2404 if (!word.word().empty())
2410 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2412 // when entering this function the inset should be ALWAYS locked!
2413 Assert(the_locking_inset);
2415 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2416 if (!word.word().empty())
2419 if (tabular.isLastCell(actcell)) {
2420 bv->unlockInset(const_cast<InsetTabular *>(this));
2421 return WordLangTuple();
2424 // otherwise we have to lock the next inset and ask for it's selecttion
2426 tabular.getCellInset(actcell)
2427 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2428 return selectNextWordInt(bv, value);
2432 void InsetTabular::selectSelectedWord(BufferView * bv)
2434 if (the_locking_inset)
2435 the_locking_inset->selectSelectedWord(bv);
2439 void InsetTabular::markErased()
2441 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2442 tabular.getCellInset(cell).markErased();
2446 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2448 if (the_locking_inset) {
2449 if (the_locking_inset->nextChange(bv, length)) {
2453 if (tabular.isLastCell(actcell))
2457 InsetText & inset = tabular.getCellInset(actcell);
2458 if (inset.nextChange(bv, length)) {
2462 while (!tabular.isLastCell(actcell)) {
2464 InsetText & inset = tabular.getCellInset(actcell);
2465 if (inset.nextChange(bv, length)) {
2474 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2478 if (the_locking_inset) {
2479 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2483 if (tabular.isLastCell(actcell))
2487 InsetText & inset = tabular.getCellInset(cell);
2488 if (inset.searchForward(bv, str, cs, mw)) {
2492 while (!tabular.isLastCell(cell)) {
2494 InsetText & inset = tabular.getCellInset(cell);
2495 if (inset.searchForward(bv, str, cs, mw)) {
2504 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2507 int cell = tabular.getNumberOfCells();
2508 if (the_locking_inset) {
2509 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2518 InsetText & inset = tabular.getCellInset(cell);
2519 if (inset.searchBackward(bv, str, cs, mw)) {
2528 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2530 if (the_locking_inset)
2531 return the_locking_inset->insetAllowed(code);
2532 // we return true here because if the inset is not locked someone
2533 // wants to insert something in one of our insettexts and we generally
2539 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2541 const int cell = tabular.getCellFromInset(in, actcell);
2544 return tabular.getPWidth(cell).zero();
2546 // well we didn't obviously find it so maybe our owner knows more
2548 return owner()->forceDefaultParagraphs(in);
2550 lyxerr << "If we're here there is really something strange going on!"
2556 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2559 if (buf.length() <= 0)
2565 string::size_type len = buf.length();
2566 string::size_type p = 0;
2568 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2576 maxCols = max(cols, maxCols);
2582 maxCols = max(cols, maxCols);
2583 LyXTabular * loctab;
2588 delete paste_tabular;
2589 paste_tabular = new LyXTabular(bv->buffer()->params,
2590 this, rows, maxCols);
2591 loctab = paste_tabular;
2600 string::size_type op = 0;
2601 int cells = loctab->getNumberOfCells();
2604 rows = loctab->rows();
2605 int const columns = loctab->columns();
2607 while (cell < cells && p < len && row < rows &&
2608 (p = buf.find_first_of("\t\n", p)) != string::npos)
2614 // we can only set this if we are not too far right
2615 if (cols < columns) {
2616 InsetText & inset = loctab->getCellInset(cell);
2617 LyXFont const font = inset.getLyXText(bv)->
2618 getFont(inset.paragraphs.begin(), 0);
2619 inset.setText(buf.substr(op, p - op), font);
2625 // we can only set this if we are not too far right
2626 if (cols < columns) {
2627 InsetText & inset = tabular.getCellInset(cell);
2628 LyXFont const font = inset.getLyXText(bv)->
2629 getFont(inset.paragraphs.begin(), 0);
2630 inset.setText(buf.substr(op, p - op), font);
2635 cell = loctab->getCellNumber(row, cols);
2641 // check for the last cell if there is no trailing '\n'
2642 if (cell < cells && op < len) {
2643 InsetText & inset = loctab->getCellInset(cell);
2644 LyXFont const font = inset.getLyXText(bv)->
2645 getFont(inset.paragraphs.begin(), 0);
2646 inset.setText(buf.substr(op, len - op), font);
2653 void InsetTabular::addPreview(PreviewLoader & loader) const
2655 int const rows = tabular.rows();
2656 int const columns = tabular.columns();
2657 for (int i = 0; i < rows; ++i)
2658 for (int j = 0; j < columns; ++j)
2659 tabular.getCellInset(i, j).addPreview(loader);
2663 string const InsetTabularMailer::name_("tabular");
2665 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2666 : inset_(const_cast<InsetTabular &>(inset))
2670 string const InsetTabularMailer::inset2string(Buffer const &) const
2672 return params2string(inset_);
2676 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2678 istringstream data(STRCONV(in));
2680 lex.setStream(data);
2682 #warning CHECK verify that this is a sane value to return.
2688 string const token = lex.getString();
2696 string const token = lex.getString();
2697 if (token != "\\active_cell")
2700 cell = lex.getInteger();
2703 // This is part of the inset proper that is usually swallowed
2704 // by Buffer::readInset
2707 string const token = lex.getString();
2708 if (token != "Tabular")
2715 Buffer const & buffer = inset.buffer();
2716 inset.read(buffer, lex);
2718 // We can't set the active cell, but we can tell the frontend
2724 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2726 Buffer const & buffer = inset.buffer();
2729 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2730 inset.write(buffer, data);
2731 data << "\\end_inset\n";
2732 return STRCONV(data.str());