3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
30 #include "undo_funcs.h"
31 #include "WordLangTuple.h"
33 #include "frontends/Alert.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/Painter.h"
38 #include "support/std_sstream.h"
40 using lyx::graphics::PreviewLoader;
42 using lyx::support::ltrim;
43 using lyx::support::strToInt;
50 using std::istringstream;
52 using std::ostringstream;
57 int const ADD_TO_HEIGHT = 2;
58 int const ADD_TO_TABULAR_WIDTH = 2;
61 LyXTabular * paste_tabular = 0;
64 struct TabularFeature {
65 LyXTabular::Feature action;
70 TabularFeature tabularFeature[] =
72 { LyXTabular::APPEND_ROW, "append-row" },
73 { LyXTabular::APPEND_COLUMN, "append-column" },
74 { LyXTabular::DELETE_ROW, "delete-row" },
75 { LyXTabular::DELETE_COLUMN, "delete-column" },
76 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
77 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
78 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
79 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
80 { LyXTabular::ALIGN_LEFT, "align-left" },
81 { LyXTabular::ALIGN_RIGHT, "align-right" },
82 { LyXTabular::ALIGN_CENTER, "align-center" },
83 { LyXTabular::ALIGN_BLOCK, "align-block" },
84 { LyXTabular::VALIGN_TOP, "valign-top" },
85 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
86 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
87 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
88 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
89 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
90 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
91 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
92 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
93 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
94 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
95 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
96 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
97 { LyXTabular::MULTICOLUMN, "multicolumn" },
98 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
99 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
100 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
101 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
102 { LyXTabular::SET_PWIDTH, "set-pwidth" },
103 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
104 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
105 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
106 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
107 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
108 { LyXTabular::SET_USEBOX, "set-usebox" },
109 { LyXTabular::SET_LTHEAD, "set-lthead" },
110 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
111 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
112 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
113 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
114 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
115 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
116 { LyXTabular::LAST_ACTION, "" }
120 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
121 bool operator()(TabularFeature & tf)
123 return tf.action == feature_;
126 LyXTabular::Feature feature_;
132 string const featureAsString(LyXTabular::Feature feature)
134 TabularFeature * it = tabularFeature;
135 TabularFeature * end = it +
136 sizeof(tabularFeature) / sizeof(TabularFeature);
137 it = std::find_if(it, end, FindFeature(feature));
138 return (it == end) ? string() : it->feature;
142 bool InsetTabular::hasPasteBuffer() const
144 return (paste_tabular != 0);
148 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
149 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
150 buffer_(&buf), cursorx_(0), cursory_(0)
152 tabular.setOwner(this);
153 // for now make it always display as display() inset
155 the_locking_inset = 0;
156 old_locking_inset = 0;
159 actrow = actcell = 0;
167 InsetTabular::InsetTabular(InsetTabular const & tab)
168 : UpdatableInset(tab), tabular(tab.tabular),
169 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
171 tabular.setOwner(this);
172 the_locking_inset = 0;
173 old_locking_inset = 0;
176 actrow = actcell = 0;
184 InsetTabular::~InsetTabular()
186 InsetTabularMailer(*this).hideDialog();
190 auto_ptr<InsetBase> InsetTabular::clone() const
192 return auto_ptr<InsetBase>(new InsetTabular(*this));
196 Buffer const & InsetTabular::buffer() const
202 BufferView * InsetTabular::view() const
209 void InsetTabular::buffer(Buffer * b)
215 void InsetTabular::write(Buffer const & buf, ostream & os) const
217 os << "Tabular" << endl;
218 tabular.write(buf, os);
222 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
224 bool const old_format = (lex.getString() == "\\LyXTable");
226 tabular.read(buf, lex);
232 string token = lex.getString();
233 while (lex.isOK() && (token != "\\end_inset")) {
235 token = lex.getString();
237 if (token != "\\end_inset") {
238 lex.printError("Missing \\end_inset at this point. "
244 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
246 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
247 // mi.base.textwidth << "\n";
249 lyxerr << "InsetTabular::metrics: need bv" << endl;
253 calculate_dimensions_of_cells(mi);
255 dim.asc = tabular.getAscentOfRow(0);
256 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
257 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
262 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
264 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
266 BufferView * bv = pi.base.bv;
269 UpdatableInset::draw(pi, x, y);
277 x += ADD_TO_TABULAR_WIDTH;
280 first_visible_cell = -1;
281 for (int i = 0; i < tabular.rows(); ++i) {
283 cell = tabular.getCellNumber(i, 0);
284 if (y + tabular.getDescentOfRow(i) <= 0 &&
285 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
287 y += tabular.getDescentOfRow(i) +
288 tabular.getAscentOfRow(i + 1) +
289 tabular.getAdditionalHeight(i + 1);
292 for (int j = 0; j < tabular.columns(); ++j) {
293 if (nx > bv->workWidth())
295 if (tabular.isPartOfMultiColumn(i, j))
297 int cx = nx + tabular.getBeginningOfTextInCell(cell);
298 if (first_visible_cell < 0)
299 first_visible_cell = cell;
300 if (hasSelection()) {
301 drawCellSelection(pi.pain, nx, y, i, j, cell);
304 tabular.getCellInset(cell).draw(pi, cx, y);
305 drawCellLines(pi.pain, nx, y, i, cell);
306 nx += tabular.getWidthOfColumn(cell);
310 // Would be nice, but for some completely unfathomable reason,
311 // on a col resize to a new fixed width, even though the insettexts
312 // are resized, the cell isn't, but drawing all cells in a tall table
313 // has the desired effect somehow. Complete dark magic.
315 // avoiding drawing the rest of a long table is
316 // a pretty big speedup
317 if (y > bv->workHeight())
321 y += tabular.getDescentOfRow(i) +
322 tabular.getAscentOfRow(i + 1) +
323 tabular.getAdditionalHeight(i + 1);
328 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
329 int row, int cell) const
331 int x2 = x + tabular.getWidthOfColumn(cell);
334 if (!tabular.topAlreadyDrawn(cell)) {
335 on_off = !tabular.topLine(cell);
336 pain.line(x, y - tabular.getAscentOfRow(row),
337 x2, y - tabular.getAscentOfRow(row),
338 on_off ? LColor::tabularonoffline : LColor::tabularline,
339 on_off ? Painter::line_onoffdash : Painter::line_solid);
341 on_off = !tabular.bottomLine(cell);
342 pain.line(x, y + tabular.getDescentOfRow(row),
343 x2, y + tabular.getDescentOfRow(row),
344 on_off ? LColor::tabularonoffline : LColor::tabularline,
345 on_off ? Painter::line_onoffdash : Painter::line_solid);
346 if (!tabular.leftAlreadyDrawn(cell)) {
347 on_off = !tabular.leftLine(cell);
348 pain.line(x, y - tabular.getAscentOfRow(row),
349 x, y + tabular.getDescentOfRow(row),
350 on_off ? LColor::tabularonoffline : LColor::tabularline,
351 on_off ? Painter::line_onoffdash : Painter::line_solid);
353 on_off = !tabular.rightLine(cell);
354 pain.line(x2 - tabular.getAdditionalWidth(cell),
355 y - tabular.getAscentOfRow(row),
356 x2 - tabular.getAdditionalWidth(cell),
357 y + tabular.getDescentOfRow(row),
358 on_off ? LColor::tabularonoffline : LColor::tabularline,
359 on_off ? Painter::line_onoffdash : Painter::line_solid);
363 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
364 int row, int column, int cell) const
366 BOOST_ASSERT(hasSelection());
367 int cs = tabular.column_of_cell(sel_cell_start);
368 int ce = tabular.column_of_cell(sel_cell_end);
371 cs = tabular.column_of_cell(sel_cell_end);
373 ce = tabular.right_column_of_cell(sel_cell_end);
376 int rs = tabular.row_of_cell(sel_cell_start);
377 int re = tabular.row_of_cell(sel_cell_end);
381 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
382 int w = tabular.getWidthOfColumn(cell);
383 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
384 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
385 w, h, LColor::selection);
390 string const InsetTabular::editMessage() const
392 return _("Opened table");
396 void InsetTabular::insetUnlock(BufferView * bv)
398 if (the_locking_inset) {
399 the_locking_inset->insetUnlock(bv);
401 the_locking_inset = 0;
406 if (scroll(false) || hasSelection()) {
415 void InsetTabular::updateLocal(BufferView * bv) const
417 bv->updateInset(this);
423 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
425 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
430 if (inset == &tabular.getCellInset(actcell)) {
431 lyxerr[Debug::INSETTEXT] << "OK" << endl;
432 the_locking_inset = &tabular.getCellInset(actcell);
435 } else if (!the_locking_inset) {
436 int const n = tabular.getNumberOfCells();
437 int const id = inset->id();
438 for (int i = 0; i < n; ++i) {
439 InsetText * in = &tabular.getCellInset(i);
442 the_locking_inset = in;
447 if (in->getInsetFromID(id)) {
449 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
450 return the_locking_inset->lockInsetInInset(bv, inset);
453 } else if (the_locking_inset && (the_locking_inset == inset)) {
454 lyxerr[Debug::INSETTEXT] << "OK" << endl;
456 } else if (the_locking_inset) {
457 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
458 return the_locking_inset->lockInsetInInset(bv, inset);
460 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
465 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
468 if (!the_locking_inset)
470 if (the_locking_inset == inset) {
471 the_locking_inset->insetUnlock(bv);
473 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
480 // this has to be here otherwise we don't redraw the cell!
481 the_locking_inset = 0;
484 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
485 if (inset->lyxCode() == TABULAR_CODE &&
486 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
487 InsetTabularMailer(*this).updateDialog(bv);
496 int InsetTabular::insetInInsetY() const
498 if (!the_locking_inset)
500 return inset_y + the_locking_inset->insetInInsetY();
504 UpdatableInset * InsetTabular::getLockingInset() const
506 return the_locking_inset ? the_locking_inset->getLockingInset() :
507 const_cast<InsetTabular *>(this);
511 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
515 if (the_locking_inset)
516 return the_locking_inset->getFirstLockingInsetOfType(c);
521 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
523 if (the_locking_inset)
524 return the_locking_inset->insertInset(bv, inset);
529 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
531 if (hasSelection() && cmd.button() == mouse_button::button3)
534 if (hasSelection()) {
536 updateLocal(cmd.view());
539 int const ocell = actcell;
540 BufferView * bv = cmd.view();
544 the_locking_inset = 0;
548 setPos(bv, cmd.x, cmd.y);
551 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
553 if ((ocell == actcell) && the_locking_inset && inset_hit) {
555 FuncRequest cmd1 = cmd;
558 the_locking_inset->localDispatch(cmd1);
562 if (the_locking_inset) {
563 the_locking_inset->insetUnlock(bv);
565 the_locking_inset = 0;
568 if (cmd.button() == mouse_button::button2) {
569 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
573 if (inset_hit && bv->theLockingInset()) {
574 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
575 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
578 FuncRequest cmd1 = cmd;
581 the_locking_inset->localDispatch(cmd1);
586 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
589 if (the_locking_inset) {
590 FuncRequest cmd1 = cmd;
593 ret = the_locking_inset->localDispatch(cmd1);
595 if (cmd.button() == mouse_button::button3 && !ret) {
596 InsetTabularMailer(*this).showDialog(cmd.view());
603 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
605 if (the_locking_inset) {
606 FuncRequest cmd1 = cmd;
609 the_locking_inset->localDispatch(cmd1);
613 BufferView * bv = cmd.view();
614 int const old_cell = actcell;
616 setPos(bv, cmd.x, cmd.y);
617 if (!hasSelection()) {
618 setSelection(actcell, actcell);
620 } else if (old_cell != actcell) {
621 setSelection(sel_cell_start, actcell);
627 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
629 // We need to save the value of the_locking_inset as the call to
630 // the_locking_inset->localDispatch might unlock it.
631 old_locking_inset = the_locking_inset;
632 RESULT result = UpdatableInset::localDispatch(cmd);
633 BufferView * bv = cmd.view();
635 if (cmd.action == LFUN_INSET_EDIT) {
637 if (!bv->lockInset(this)) {
638 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
644 the_locking_inset = 0;
648 if (cmd.argument.size()) {
649 if (cmd.argument == "left") {
650 if (isRightToLeft(bv))
651 actcell = tabular.getLastCellInRow(0);
655 if (isRightToLeft(bv))
656 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
658 actcell = tabular.getNumberOfCells() - 1;
666 setPos(bv, cmd.x, cmd.y);
669 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
670 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
676 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
681 if (cmd.action < 0 && cmd.argument.empty())
684 bool hs = hasSelection();
687 // this one have priority over the locked InsetText, if we're not already
688 // inside another tabular then that one get's priority!
689 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
690 switch (cmd.action) {
691 case LFUN_MOUSE_PRESS:
695 case LFUN_MOUSE_MOTION:
696 lfunMouseMotion(cmd);
699 case LFUN_MOUSE_RELEASE:
700 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
702 case LFUN_CELL_BACKWARD:
703 case LFUN_CELL_FORWARD:
704 unlockInsetInInset(bv, the_locking_inset);
705 if (cmd.action == LFUN_CELL_FORWARD)
706 moveNextCell(bv, old_locking_inset != 0);
708 movePrevCell(bv, old_locking_inset != 0);
712 if (!the_locking_inset)
713 return DISPATCHED_NOUPDATE;
715 // this to avoid compiler warnings.
721 kb_action action = cmd.action;
722 string arg = cmd.argument;
723 if (the_locking_inset) {
724 result = the_locking_inset->localDispatch(cmd);
725 if (result == DISPATCHED_NOUPDATE) {
728 if (sc != scroll()) { // inset has been scrolled
732 } else if (result == DISPATCHED) {
735 } else if (result == FINISHED_UP) {
737 // Make sure to reset status message after
738 // exiting, e.g. math inset
739 bv->owner()->clearMessage();
740 } else if (result == FINISHED_DOWN) {
742 bv->owner()->clearMessage();
743 } else if (result == FINISHED_RIGHT) {
745 bv->owner()->clearMessage();
746 } else if (result == FINISHED) {
747 bv->owner()->clearMessage();
753 // --- Cursor Movements ----------------------------------
754 case LFUN_RIGHTSEL: {
755 int const start = hasSelection() ? sel_cell_start : actcell;
756 if (tabular.isLastCellInRow(actcell)) {
757 setSelection(start, actcell);
762 // if we are starting a selection, only select
763 // the current cell at the beginning
764 if (hasSelection()) {
765 moveRight(bv, false);
768 setSelection(start, end);
773 result = moveRight(bv);
779 int const start = hasSelection() ? sel_cell_start : actcell;
780 if (tabular.isFirstCellInRow(actcell)) {
781 setSelection(start, actcell);
786 // if we are starting a selection, only select
787 // the current cell at the beginning
788 if (hasSelection()) {
792 setSelection(start, end);
797 result = moveLeft(bv);
803 int const start = hasSelection() ? sel_cell_start : actcell;
804 int const ocell = actcell;
805 // if we are starting a selection, only select
806 // the current cell at the beginning
807 if (hasSelection()) {
809 if (ocell == sel_cell_end ||
810 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
811 setSelection(start, tabular.getCellBelow(sel_cell_end));
813 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
815 setSelection(start, start);
821 result = moveDown(bv, old_locking_inset != 0);
827 int const start = hasSelection() ? sel_cell_start : actcell;
828 int const ocell = actcell;
829 // if we are starting a selection, only select
830 // the current cell at the beginning
831 if (hasSelection()) {
833 if ((ocell == sel_cell_end) ||
834 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
835 setSelection(start, tabular.getCellAbove(sel_cell_end));
837 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
839 setSelection(start, start);
845 result = moveUp(bv, old_locking_inset != 0);
854 unlockInsetInInset(bv, the_locking_inset);
855 if (bv->top_y() + bv->painter().paperHeight() <
856 top_baseline + tabular.getHeightOfTabular())
858 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
859 actcell = tabular.getCellBelow(first_visible_cell) + column;
861 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
871 unlockInsetInInset(bv, the_locking_inset);
872 if (top_baseline < 0) {
873 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
874 if (top_baseline > 0)
877 actcell = tabular.getCellBelow(first_visible_cell) + column;
885 // none of these make sense for insettabular,
886 // but we must catch them to prevent any
887 // selection from being confused
891 case LFUN_WORDLEFTSEL:
893 case LFUN_WORDRIGHTSEL:
895 case LFUN_DOWN_PARAGRAPH:
896 case LFUN_DOWN_PARAGRAPHSEL:
897 case LFUN_UP_PARAGRAPH:
898 case LFUN_UP_PARAGRAPHSEL:
904 case LFUN_BEGINNINGBUF:
905 case LFUN_BEGINNINGBUFSEL:
909 case LFUN_LAYOUT_TABULAR: {
910 InsetTabularMailer(*this).showDialog(bv);
913 case LFUN_INSET_DIALOG_UPDATE: {
914 InsetTabularMailer(*this).updateDialog(bv);
917 case LFUN_TABULAR_FEATURE:
918 if (!tabularFeatures(bv, arg))
919 result = UNDISPATCHED;
921 // insert file functions
922 case LFUN_FILE_INSERT_ASCII_PARA:
923 case LFUN_FILE_INSERT_ASCII:
925 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
928 if (insertAsciiString(bv, tmpstr, false))
931 result = UNDISPATCHED;
934 // cut and paste functions
936 if (!copySelection(bv))
940 recordUndo(bv, Undo::DELETE);
941 cutSelection(bv->buffer()->params());
950 case LFUN_PASTESELECTION:
952 string const clip(bv->getClipboard());
956 if (clip.find('\t') != string::npos) {
960 string::size_type len = clip.length();
961 string::size_type p = 0;
964 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
972 maxCols = max(cols, maxCols);
978 maxCols = max(cols, maxCols);
979 delete paste_tabular;
980 paste_tabular = new LyXTabular(bv->buffer()->params(),
981 this, rows, maxCols);
982 string::size_type op = 0;
984 int cells = paste_tabular->getNumberOfCells();
986 while ((cell < cells) && (p < len) &&
987 (p = clip.find_first_of("\t\n", p)) != string::npos) {
992 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
997 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
998 while (cols++ < maxCols)
1006 // check for the last cell if there is no trailing '\n'
1007 if ((cell < cells) && (op < len))
1008 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1011 if (!insertAsciiString(bv, clip, true))
1014 // so that the clipboard is used and it goes on
1016 // and executes LFUN_PASTESELECTION in insettext!
1017 delete paste_tabular;
1022 if (hasPasteBuffer()) {
1023 recordUndo(bv, Undo::INSERT);
1028 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1030 // handle font changing stuff on selection before we lock the inset
1031 // in the default part!
1032 result = UNDISPATCHED;
1043 case LFUN_UNDERLINE:
1044 case LFUN_FONT_SIZE:
1045 if (bv->dispatch(FuncRequest(bv, action, arg)))
1046 result = DISPATCHED;
1052 // we try to activate the actual inset and put this event down to
1053 // the insets dispatch function.
1054 if (result == DISPATCHED || the_locking_inset)
1056 if (activateCellInset(bv)) {
1057 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1058 if (result == UNDISPATCHED || result >= FINISHED) {
1059 unlockInsetInInset(bv, the_locking_inset);
1060 // we need to update if this was requested before
1062 return UNDISPATCHED;
1071 if (result < FINISHED) {
1072 if (!the_locking_inset && bv->fitCursor())
1075 bv->unlockInset(this);
1080 int InsetTabular::latex(Buffer const & buf, ostream & os,
1081 LatexRunParams const & runparams) const
1083 return tabular.latex(buf, os, runparams);
1087 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1090 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1092 return tabular.ascii(buf, os, 0, false, 0);
1096 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1098 return tabular.linuxdoc(buf,os);
1102 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1107 // if the table is inside a float it doesn't need the informaltable
1108 // wrapper. Search for it.
1109 for (master = owner();
1110 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1111 master = master->owner());
1114 os << "<informaltable>";
1119 ret += tabular.docbook(buf, os, mixcont);
1121 os << "</informaltable>";
1130 void InsetTabular::validate(LaTeXFeatures & features) const
1132 tabular.validate(features);
1136 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1139 // if we have a locking_inset we should have to check only this cell for
1140 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1141 // This is _really_ important (André)
1142 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1145 for (int j = 0; j < tabular.columns(); ++j) {
1149 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1150 tabular.getCellInset(actrow, j).metrics(m, dim);
1151 maxAsc = max(dim.asc, maxAsc);
1152 maxDesc = max(dim.des, maxDesc);
1154 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1155 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1156 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1162 for (int i = 0; i < tabular.rows(); ++i) {
1165 for (int j = 0; j < tabular.columns(); ++j) {
1166 if (tabular.isPartOfMultiColumn(i, j))
1172 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1173 tabular.getCellInset(cell).metrics(m, dim);
1174 maxAsc = max(maxAsc, dim.asc);
1175 maxDesc = max(maxDesc, dim.des);
1176 tabular.setWidthOfCell(cell, dim.wid);
1178 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1179 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1184 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1186 if (the_locking_inset) {
1187 the_locking_inset->getCursor(bv, x, y);
1192 y = cursory_ + InsetTabular::y();
1195 int desc = tabular.getDescentOfRow(actrow);
1197 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1199 y += ADD_TO_HEIGHT * 2;
1200 y += TEXT_TO_INSET_OFFSET;
1204 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1206 if (the_locking_inset) {
1207 the_locking_inset->getCursorPos(bv, x, y);
1210 x = cursorx_ - top_x;
1215 void InsetTabular::fitInsetCursor(BufferView * bv) const
1217 if (the_locking_inset) {
1218 the_locking_inset->fitInsetCursor(bv);
1223 int const asc = font_metrics::maxAscent(font);
1224 int const desc = font_metrics::maxDescent(font);
1227 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1231 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1234 actcell = actrow = actcol = 0;
1235 int ly = tabular.getDescentOfRow(actrow);
1237 // first search the right row
1238 while (ly < y && actrow + 1 < tabular.rows()) {
1239 cursory_ += tabular.getDescentOfRow(actrow) +
1240 tabular.getAscentOfRow(actrow + 1) +
1241 tabular.getAdditionalHeight(actrow + 1);
1243 ly = cursory_ + tabular.getDescentOfRow(actrow);
1245 actcell = tabular.getCellNumber(actrow, actcol);
1247 // now search the right column
1248 int lx = tabular.getWidthOfColumn(actcell) -
1249 tabular.getAdditionalWidth(actcell);
1251 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1252 lx += tabular.getWidthOfColumn(actcell + 1)
1253 + tabular.getAdditionalWidth(actcell);
1255 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1260 int InsetTabular::getCellXPos(int cell) const
1264 for (; !tabular.isFirstCellInRow(c); --c)
1266 int lx = tabular.getWidthOfColumn(cell);
1267 for (; c < cell; ++c)
1268 lx += tabular.getWidthOfColumn(c);
1270 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1274 void InsetTabular::resetPos(BufferView * bv) const
1276 #ifdef WITH_WARNINGS
1277 #warning This should be fixed in the right manner (20011128 Jug)
1279 // fast hack to fix infinite repaintings!
1280 if (in_reset_pos > 0)
1284 actcol = tabular.column_of_cell(actcell);
1287 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1288 if (tabular.isLastCellInRow(cell)) {
1289 cursory_ += tabular.getDescentOfRow(actrow) +
1290 tabular.getAscentOfRow(actrow + 1) +
1291 tabular.getAdditionalHeight(actrow + 1);
1296 if (the_locking_inset)
1300 // we need this only from here on!!!
1302 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1303 int new_x = getCellXPos(actcell);
1304 int old_x = cursorx_;
1307 // cursor.x(getCellXPos(actcell) + offset);
1308 if (actcol < tabular.columns() - 1 && scroll(false) &&
1309 tabular.getWidthOfTabular() < bv->workWidth()-20)
1313 } else if (the_locking_inset &&
1314 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1316 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1317 if (xx > bv->workWidth()-20) {
1318 scroll(bv, -(xx - bv->workWidth() + 60));
1320 } else if (xx < 20) {
1328 } else if (cursorx_ - offset > 20 &&
1329 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1330 > bv->workWidth() - 20) {
1331 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1333 } else if (cursorx_ - offset < 20) {
1334 scroll(bv, 20 - cursorx_ + offset);
1336 } else if (scroll() && top_x > 20 &&
1337 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1338 scroll(bv, old_x - cursorx_);
1341 if (the_locking_inset) {
1342 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1345 if ((!the_locking_inset ||
1346 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1347 actcell != oldcell) {
1348 InsetTabularMailer(*this).updateDialog(bv);
1355 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1357 if (lock && !old_locking_inset) {
1358 if (activateCellInset(bv))
1361 bool moved = isRightToLeft(bv)
1362 ? movePrevCell(bv) : moveNextCell(bv);
1364 return FINISHED_RIGHT;
1365 if (lock && activateCellInset(bv))
1369 return DISPATCHED_NOUPDATE;
1373 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1375 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1378 if (lock) { // behind the inset
1379 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1383 return DISPATCHED_NOUPDATE;
1387 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1389 int const ocell = actcell;
1390 actcell = tabular.getCellAbove(actcell);
1391 if (actcell == ocell) // we moved out of the inset
1397 if (old_locking_inset) {
1398 old_locking_inset->getCursorPos(bv, x, y);
1399 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1401 if (activateCellInset(bv, x, 0))
1404 return DISPATCHED_NOUPDATE;
1408 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1410 int const ocell = actcell;
1411 actcell = tabular.getCellBelow(actcell);
1412 if (actcell == ocell) // we moved out of the inset
1413 return FINISHED_DOWN;
1418 if (old_locking_inset) {
1419 old_locking_inset->getCursorPos(bv, x, y);
1420 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1422 if (activateCellInset(bv, x, 0))
1425 return DISPATCHED_NOUPDATE;
1429 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1431 if (isRightToLeft(bv)) {
1432 if (tabular.isFirstCellInRow(actcell)) {
1433 int row = tabular.row_of_cell(actcell);
1434 if (row == tabular.rows() - 1)
1436 actcell = tabular.getLastCellInRow(row);
1437 actcell = tabular.getCellBelow(actcell);
1444 if (tabular.isLastCell(actcell))
1449 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1450 isRightToLeftPar(bv->buffer()->params());
1451 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1458 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1460 if (isRightToLeft(bv)) {
1461 if (tabular.isLastCellInRow(actcell)) {
1462 int row = tabular.row_of_cell(actcell);
1465 actcell = tabular.getFirstCellInRow(row);
1466 actcell = tabular.getCellAbove(actcell);
1468 if (tabular.isLastCell(actcell))
1473 if (!actcell) // first cell
1478 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1479 isRightToLeftPar(bv->buffer()->params());
1480 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1487 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1491 setSelection(0, tabular.getNumberOfCells() - 1);
1493 if (hasSelection()) {
1494 recordUndo(bv, Undo::ATOMIC);
1495 bool const frozen = undo_frozen;
1498 // apply the fontchange on the whole selection
1503 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1504 for(int i = sel_row_start; i <= sel_row_end; ++i)
1505 for(int j = sel_col_start; j <= sel_col_end; ++j)
1506 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1514 if (the_locking_inset)
1515 the_locking_inset->setFont(bv, font, tall);
1519 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1521 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1524 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1525 string const tmp = tabularFeature[i].feature;
1527 if (tmp == what.substr(0, tmp.length())) {
1528 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1529 //tabularFeatures[i].feature.length())) {
1530 action = tabularFeature[i].action;
1534 if (action == LyXTabular::LAST_ACTION)
1538 ltrim(what.substr(tabularFeature[i].feature.length()));
1539 tabularFeatures(bv, action, val);
1545 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1546 string const & special, bool & flag)
1548 if (special == "dl_above") {
1551 } else if (special == "dl_below") {
1552 ltt.bottomDL = flag;
1554 } else if (special == "empty") {
1566 void InsetTabular::tabularFeatures(BufferView * bv,
1567 LyXTabular::Feature feature,
1568 string const & value)
1574 bool setLines = false;
1575 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1576 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1580 case LyXTabular::M_ALIGN_LEFT:
1581 case LyXTabular::ALIGN_LEFT:
1582 setAlign = LYX_ALIGN_LEFT;
1585 case LyXTabular::M_ALIGN_RIGHT:
1586 case LyXTabular::ALIGN_RIGHT:
1587 setAlign = LYX_ALIGN_RIGHT;
1590 case LyXTabular::M_ALIGN_CENTER:
1591 case LyXTabular::ALIGN_CENTER:
1592 setAlign = LYX_ALIGN_CENTER;
1595 case LyXTabular::ALIGN_BLOCK:
1596 setAlign = LYX_ALIGN_BLOCK;
1599 case LyXTabular::M_VALIGN_TOP:
1600 case LyXTabular::VALIGN_TOP:
1601 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1604 case LyXTabular::M_VALIGN_BOTTOM:
1605 case LyXTabular::VALIGN_BOTTOM:
1606 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1609 case LyXTabular::M_VALIGN_MIDDLE:
1610 case LyXTabular::VALIGN_MIDDLE:
1611 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1618 if (hasSelection()) {
1619 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1621 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1622 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1624 recordUndo(bv, Undo::ATOMIC);
1626 int row = tabular.row_of_cell(actcell);
1627 int column = tabular.column_of_cell(actcell);
1629 LyXTabular::ltType ltt;
1633 case LyXTabular::SET_PWIDTH:
1635 LyXLength const vallen(value);
1636 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1638 bool const update = (tmplen != vallen);
1639 tabular.setColumnPWidth(actcell, vallen);
1641 // We need this otherwise we won't resize
1642 // the insettext of the active cell (if any)
1643 // until later (see InsetText::do_resize)
1644 unlockInsetInInset(bv, the_locking_inset);
1649 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1650 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1651 else if (!vallen.zero()
1652 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1653 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1657 case LyXTabular::SET_MPWIDTH:
1659 LyXLength const vallen(value);
1660 LyXLength const & tmplen = tabular.getPWidth(actcell);
1662 bool const update = (tmplen != vallen);
1663 tabular.setMColumnPWidth(actcell, vallen);
1665 // We need this otherwise we won't resize
1666 // the insettext of the active cell (if any)
1667 // until later (see InsetText::do_resize)
1668 unlockInsetInInset(bv, the_locking_inset);
1673 case LyXTabular::SET_SPECIAL_COLUMN:
1674 case LyXTabular::SET_SPECIAL_MULTI:
1675 tabular.setAlignSpecial(actcell,value,feature);
1678 case LyXTabular::APPEND_ROW:
1679 // append the row into the tabular
1680 unlockInsetInInset(bv, the_locking_inset);
1681 tabular.appendRow(bv->buffer()->params(), actcell);
1684 case LyXTabular::APPEND_COLUMN:
1685 // append the column into the tabular
1686 unlockInsetInInset(bv, the_locking_inset);
1687 tabular.appendColumn(bv->buffer()->params(), actcell);
1688 actcell = tabular.getCellNumber(row, column);
1691 case LyXTabular::DELETE_ROW:
1692 unlockInsetInInset(bv, the_locking_inset);
1693 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1694 tabular.deleteRow(sel_row_start);
1696 if (sel_row_start >= tabular.rows())
1698 actcell = tabular.getCellNumber(sel_row_start, column);
1702 case LyXTabular::DELETE_COLUMN:
1703 unlockInsetInInset(bv, the_locking_inset);
1704 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1705 tabular.deleteColumn(sel_col_start);
1707 if (sel_col_start >= tabular.columns())
1709 actcell = tabular.getCellNumber(row, sel_col_start);
1713 case LyXTabular::M_TOGGLE_LINE_TOP:
1715 case LyXTabular::TOGGLE_LINE_TOP:
1717 bool lineSet = !tabular.topLine(actcell, flag);
1718 for (int i = sel_row_start; i <= sel_row_end; ++i)
1719 for (int j = sel_col_start; j <= sel_col_end; ++j)
1721 tabular.getCellNumber(i, j),
1727 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1729 case LyXTabular::TOGGLE_LINE_BOTTOM:
1731 bool lineSet = !tabular.bottomLine(actcell, flag);
1732 for (int i = sel_row_start; i <= sel_row_end; ++i)
1733 for (int j = sel_col_start; j <= sel_col_end; ++j)
1734 tabular.setBottomLine(
1735 tabular.getCellNumber(i, j),
1742 case LyXTabular::M_TOGGLE_LINE_LEFT:
1744 case LyXTabular::TOGGLE_LINE_LEFT:
1746 bool lineSet = !tabular.leftLine(actcell, flag);
1747 for (int i = sel_row_start; i <= sel_row_end; ++i)
1748 for (int j = sel_col_start; j <= sel_col_end; ++j)
1749 tabular.setLeftLine(
1750 tabular.getCellNumber(i,j),
1757 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1759 case LyXTabular::TOGGLE_LINE_RIGHT:
1761 bool lineSet = !tabular.rightLine(actcell, flag);
1762 for (int i = sel_row_start; i <= sel_row_end; ++i)
1763 for (int j = sel_col_start; j <= sel_col_end; ++j)
1764 tabular.setRightLine(
1765 tabular.getCellNumber(i,j),
1772 case LyXTabular::M_ALIGN_LEFT:
1773 case LyXTabular::M_ALIGN_RIGHT:
1774 case LyXTabular::M_ALIGN_CENTER:
1776 case LyXTabular::ALIGN_LEFT:
1777 case LyXTabular::ALIGN_RIGHT:
1778 case LyXTabular::ALIGN_CENTER:
1779 case LyXTabular::ALIGN_BLOCK:
1780 for (int i = sel_row_start; i <= sel_row_end; ++i)
1781 for (int j = sel_col_start; j <= sel_col_end; ++j)
1782 tabular.setAlignment(
1783 tabular.getCellNumber(i, j),
1789 case LyXTabular::M_VALIGN_TOP:
1790 case LyXTabular::M_VALIGN_BOTTOM:
1791 case LyXTabular::M_VALIGN_MIDDLE:
1793 case LyXTabular::VALIGN_TOP:
1794 case LyXTabular::VALIGN_BOTTOM:
1795 case LyXTabular::VALIGN_MIDDLE:
1796 for (int i = sel_row_start; i <= sel_row_end; ++i)
1797 for (int j = sel_col_start; j <= sel_col_end; ++j)
1798 tabular.setVAlignment(
1799 tabular.getCellNumber(i, j),
1804 case LyXTabular::MULTICOLUMN: {
1805 if (sel_row_start != sel_row_end) {
1806 #ifdef WITH_WARNINGS
1807 #warning Need I say it ? This is horrible.
1809 Alert::error(_("Error setting multicolumn"),
1810 _("You cannot set multicolumn vertically."));
1813 // just multicol for one Single Cell
1814 if (!hasSelection()) {
1815 // check wether we are completly in a multicol
1816 if (tabular.isMultiColumn(actcell))
1817 tabular.unsetMultiColumn(actcell);
1819 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1823 // we have a selection so this means we just add all this
1824 // cells to form a multicolumn cell
1828 if (sel_cell_start > sel_cell_end) {
1829 s_start = sel_cell_end;
1830 s_end = sel_cell_start;
1832 s_start = sel_cell_start;
1833 s_end = sel_cell_end;
1835 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1842 case LyXTabular::SET_ALL_LINES:
1844 case LyXTabular::UNSET_ALL_LINES:
1845 for (int i = sel_row_start; i <= sel_row_end; ++i)
1846 for (int j = sel_col_start; j <= sel_col_end; ++j)
1847 tabular.setAllLines(
1848 tabular.getCellNumber(i,j), setLines);
1852 case LyXTabular::SET_LONGTABULAR:
1853 tabular.setLongTabular(true);
1854 updateLocal(bv); // because this toggles displayed
1857 case LyXTabular::UNSET_LONGTABULAR:
1858 tabular.setLongTabular(false);
1859 updateLocal(bv); // because this toggles displayed
1862 case LyXTabular::SET_ROTATE_TABULAR:
1863 tabular.setRotateTabular(true);
1866 case LyXTabular::UNSET_ROTATE_TABULAR:
1867 tabular.setRotateTabular(false);
1870 case LyXTabular::SET_ROTATE_CELL:
1871 for (int i = sel_row_start; i <= sel_row_end; ++i)
1872 for (int j = sel_col_start; j<=sel_col_end; ++j)
1873 tabular.setRotateCell(
1874 tabular.getCellNumber(i, j), true);
1877 case LyXTabular::UNSET_ROTATE_CELL:
1878 for (int i = sel_row_start; i <= sel_row_end; ++i)
1879 for (int j = sel_col_start; j <= sel_col_end; ++j)
1880 tabular.setRotateCell(
1881 tabular.getCellNumber(i, j), false);
1884 case LyXTabular::SET_USEBOX: {
1885 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1886 if (val == tabular.getUsebox(actcell))
1887 val = LyXTabular::BOX_NONE;
1888 for (int i = sel_row_start; i <= sel_row_end; ++i)
1889 for (int j = sel_col_start; j <= sel_col_end; ++j)
1890 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1894 case LyXTabular::UNSET_LTFIRSTHEAD:
1896 case LyXTabular::SET_LTFIRSTHEAD:
1897 tabular.getRowOfLTFirstHead(row, ltt);
1898 checkLongtableSpecial(ltt, value, flag);
1899 tabular.setLTHead(row, flag, ltt, true);
1902 case LyXTabular::UNSET_LTHEAD:
1904 case LyXTabular::SET_LTHEAD:
1905 tabular.getRowOfLTHead(row, ltt);
1906 checkLongtableSpecial(ltt, value, flag);
1907 tabular.setLTHead(row, flag, ltt, false);
1910 case LyXTabular::UNSET_LTFOOT:
1912 case LyXTabular::SET_LTFOOT:
1913 tabular.getRowOfLTFoot(row, ltt);
1914 checkLongtableSpecial(ltt, value, flag);
1915 tabular.setLTFoot(row, flag, ltt, false);
1918 case LyXTabular::UNSET_LTLASTFOOT:
1920 case LyXTabular::SET_LTLASTFOOT:
1921 tabular.getRowOfLTLastFoot(row, ltt);
1922 checkLongtableSpecial(ltt, value, flag);
1923 tabular.setLTFoot(row, flag, ltt, true);
1926 case LyXTabular::SET_LTNEWPAGE: {
1927 bool what = !tabular.getLTNewPage(row);
1928 tabular.setLTNewPage(row, what);
1932 // dummy stuff just to avoid warnings
1933 case LyXTabular::LAST_ACTION:
1937 InsetTabularMailer(*this).updateDialog(bv);
1941 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1942 mouse_button::state button, bool behind)
1944 UpdatableInset & inset = tabular.getCellInset(actcell);
1947 x = inset.x() + inset.width();
1948 y = inset.descent();
1950 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1951 //inset_y = cursory_;
1952 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1953 if (!the_locking_inset)
1956 return the_locking_inset;
1960 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1961 mouse_button::state button)
1963 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1965 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1969 bool InsetTabular::insetHit(BufferView *, int x, int) const
1971 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1975 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1977 //resizeLyXText(bv, recursive);
1981 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1982 bool const recursive) const
1984 if (the_locking_inset)
1985 return the_locking_inset->getLyXText(bv, recursive);
1986 return InsetOld::getLyXText(bv, recursive);
1990 bool InsetTabular::showInsetDialog(BufferView * bv) const
1992 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1993 InsetTabularMailer(*this).showDialog(bv);
1998 void InsetTabular::openLayoutDialog(BufferView * bv) const
2000 if (the_locking_inset) {
2001 InsetTabular * inset = static_cast<InsetTabular *>
2002 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2004 inset->openLayoutDialog(bv);
2008 InsetTabularMailer(*this).showDialog(bv);
2013 // function returns an object as defined in func_status.h:
2014 // states OK, Unknown, Disabled, On, Off.
2016 FuncStatus InsetTabular::getStatus(string const & what) const
2018 int action = LyXTabular::LAST_ACTION;
2022 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2023 string const tmp = tabularFeature[i].feature;
2024 if (tmp == what.substr(0, tmp.length())) {
2025 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2026 // tabularFeatures[i].feature.length())) {
2027 action = tabularFeature[i].action;
2031 if (action == LyXTabular::LAST_ACTION) {
2033 return status.unknown(true);
2036 string const argument
2037 = ltrim(what.substr(tabularFeature[i].feature.length()));
2042 LyXTabular::ltType dummyltt;
2046 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2048 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2051 case LyXTabular::SET_PWIDTH:
2052 case LyXTabular::SET_MPWIDTH:
2053 case LyXTabular::SET_SPECIAL_COLUMN:
2054 case LyXTabular::SET_SPECIAL_MULTI:
2055 case LyXTabular::APPEND_ROW:
2056 case LyXTabular::APPEND_COLUMN:
2057 case LyXTabular::DELETE_ROW:
2058 case LyXTabular::DELETE_COLUMN:
2059 case LyXTabular::SET_ALL_LINES:
2060 case LyXTabular::UNSET_ALL_LINES:
2061 return status.clear();
2063 case LyXTabular::MULTICOLUMN:
2064 status.setOnOff(tabular.isMultiColumn(actcell));
2067 case LyXTabular::M_TOGGLE_LINE_TOP:
2069 case LyXTabular::TOGGLE_LINE_TOP:
2070 status.setOnOff(tabular.topLine(actcell, flag));
2073 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2075 case LyXTabular::TOGGLE_LINE_BOTTOM:
2076 status.setOnOff(tabular.bottomLine(actcell, flag));
2079 case LyXTabular::M_TOGGLE_LINE_LEFT:
2081 case LyXTabular::TOGGLE_LINE_LEFT:
2082 status.setOnOff(tabular.leftLine(actcell, flag));
2085 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2087 case LyXTabular::TOGGLE_LINE_RIGHT:
2088 status.setOnOff(tabular.rightLine(actcell, flag));
2091 case LyXTabular::M_ALIGN_LEFT:
2093 case LyXTabular::ALIGN_LEFT:
2094 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2097 case LyXTabular::M_ALIGN_RIGHT:
2099 case LyXTabular::ALIGN_RIGHT:
2100 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2103 case LyXTabular::M_ALIGN_CENTER:
2105 case LyXTabular::ALIGN_CENTER:
2106 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2109 case LyXTabular::ALIGN_BLOCK:
2110 status.disabled(tabular.getPWidth(actcell).zero());
2111 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2114 case LyXTabular::M_VALIGN_TOP:
2116 case LyXTabular::VALIGN_TOP:
2117 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2120 case LyXTabular::M_VALIGN_BOTTOM:
2122 case LyXTabular::VALIGN_BOTTOM:
2123 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2126 case LyXTabular::M_VALIGN_MIDDLE:
2128 case LyXTabular::VALIGN_MIDDLE:
2129 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2132 case LyXTabular::SET_LONGTABULAR:
2133 status.setOnOff(tabular.isLongTabular());
2136 case LyXTabular::UNSET_LONGTABULAR:
2137 status.setOnOff(!tabular.isLongTabular());
2140 case LyXTabular::SET_ROTATE_TABULAR:
2141 status.setOnOff(tabular.getRotateTabular());
2144 case LyXTabular::UNSET_ROTATE_TABULAR:
2145 status.setOnOff(!tabular.getRotateTabular());
2148 case LyXTabular::SET_ROTATE_CELL:
2149 status.setOnOff(tabular.getRotateCell(actcell));
2152 case LyXTabular::UNSET_ROTATE_CELL:
2153 status.setOnOff(!tabular.getRotateCell(actcell));
2156 case LyXTabular::SET_USEBOX:
2157 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2160 case LyXTabular::SET_LTFIRSTHEAD:
2161 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2164 case LyXTabular::SET_LTHEAD:
2165 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2168 case LyXTabular::SET_LTFOOT:
2169 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2172 case LyXTabular::SET_LTLASTFOOT:
2173 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2176 case LyXTabular::SET_LTNEWPAGE:
2177 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2182 status.disabled(true);
2189 void InsetTabular::getLabelList(Buffer const & buffer,
2190 std::vector<string> & list) const
2192 tabular.getLabelList(buffer, list);
2196 bool InsetTabular::copySelection(BufferView * bv)
2198 if (!hasSelection())
2201 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2202 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2203 if (sel_col_start > sel_col_end) {
2204 sel_col_start = sel_col_end;
2205 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2207 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2210 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2211 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2212 if (sel_row_start > sel_row_end)
2213 swap(sel_row_start, sel_row_end);
2215 delete paste_tabular;
2216 paste_tabular = new LyXTabular(tabular);
2217 paste_tabular->setOwner(this);
2219 for (int i = 0; i < sel_row_start; ++i)
2220 paste_tabular->deleteRow(0);
2222 int const rows = sel_row_end - sel_row_start + 1;
2223 while (paste_tabular->rows() > rows)
2224 paste_tabular->deleteRow(rows);
2226 paste_tabular->setTopLine(0, true, true);
2227 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2230 for (int i = 0; i < sel_col_start; ++i)
2231 paste_tabular->deleteColumn(0);
2233 int const columns = sel_col_end - sel_col_start + 1;
2234 while (paste_tabular->columns() > columns)
2235 paste_tabular->deleteColumn(columns);
2237 paste_tabular->setLeftLine(0, true, true);
2238 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2242 paste_tabular->ascii(*bv->buffer(), os,
2243 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2244 bv->stuffClipboard(os.str());
2249 bool InsetTabular::pasteSelection(BufferView * bv)
2254 for (int r1 = 0, r2 = actrow;
2255 r1 < paste_tabular->rows() && r2 < tabular.rows();
2257 for (int c1 = 0, c2 = actcol;
2258 c1 < paste_tabular->columns() && c2 < tabular.columns();
2260 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2261 tabular.isPartOfMultiColumn(r2, c2))
2263 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2267 if (tabular.isPartOfMultiColumn(r2, c2)) {
2271 InsetText & inset = tabular.getCellInset(r2, c2);
2272 inset = paste_tabular->getCellInset(r1, c1);
2273 inset.setOwner(this);
2274 inset.deleteLyXText(bv);
2282 bool InsetTabular::cutSelection(BufferParams const & bp)
2284 if (!hasSelection())
2287 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2288 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2289 if (sel_col_start > sel_col_end) {
2290 sel_col_start = sel_col_end;
2291 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2293 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2296 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2297 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2299 if (sel_row_start > sel_row_end)
2300 swap(sel_row_start, sel_row_end);
2302 if (sel_cell_start > sel_cell_end)
2303 swap(sel_cell_start, sel_cell_end);
2305 for (int i = sel_row_start; i <= sel_row_end; ++i)
2306 for (int j = sel_col_start; j <= sel_col_end; ++j)
2307 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2312 bool InsetTabular::isRightToLeft(BufferView * bv)
2314 return bv->getParentLanguage(this)->RightToLeft();
2318 int InsetTabular::scroll(bool recursive) const
2320 int sx = UpdatableInset::scroll(false);
2322 if (recursive && the_locking_inset)
2323 sx += the_locking_inset->scroll(recursive);
2329 void InsetTabular::getSelection(int & srow, int & erow,
2330 int & scol, int & ecol) const
2332 int const start = hasSelection() ? sel_cell_start : actcell;
2333 int const end = hasSelection() ? sel_cell_end : actcell;
2335 srow = tabular.row_of_cell(start);
2336 erow = tabular.row_of_cell(end);
2340 scol = tabular.column_of_cell(start);
2341 ecol = tabular.column_of_cell(end);
2345 ecol = tabular.right_column_of_cell(end);
2349 ParagraphList * InsetTabular::getParagraphs(int i) const
2351 return (i < tabular.getNumberOfCells())
2352 ? tabular.getCellInset(i).getParagraphs(0)
2357 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2359 if (the_locking_inset)
2360 return the_locking_inset->cursor(bv);
2361 return InsetOld::cursor(bv);
2365 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2368 return const_cast<InsetTabular *>(this);
2370 for (int i = 0; i < tabular.rows(); ++i) {
2371 for (int j = 0; j < tabular.columns(); ++j) {
2372 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2382 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2384 if (the_locking_inset) {
2385 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2386 if (!word.word().empty())
2388 if (tabular.isLastCell(actcell)) {
2389 bv->unlockInset(const_cast<InsetTabular *>(this));
2390 return WordLangTuple();
2394 // otherwise we have to lock the next inset and ask for it's selecttion
2395 tabular.getCellInset(actcell)
2396 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2397 WordLangTuple word(selectNextWordInt(bv, value));
2398 if (!word.word().empty())
2404 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2406 // when entering this function the inset should be ALWAYS locked!
2407 BOOST_ASSERT(the_locking_inset);
2409 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2410 if (!word.word().empty())
2413 if (tabular.isLastCell(actcell)) {
2414 bv->unlockInset(const_cast<InsetTabular *>(this));
2415 return WordLangTuple();
2418 // otherwise we have to lock the next inset and ask for it's selecttion
2420 tabular.getCellInset(actcell)
2421 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2422 return selectNextWordInt(bv, value);
2426 void InsetTabular::selectSelectedWord(BufferView * bv)
2428 if (the_locking_inset)
2429 the_locking_inset->selectSelectedWord(bv);
2433 void InsetTabular::markErased()
2435 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2436 tabular.getCellInset(cell).markErased();
2440 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2442 if (the_locking_inset) {
2443 if (the_locking_inset->nextChange(bv, length)) {
2447 if (tabular.isLastCell(actcell))
2451 InsetText & inset = tabular.getCellInset(actcell);
2452 if (inset.nextChange(bv, length)) {
2456 while (!tabular.isLastCell(actcell)) {
2458 InsetText & inset = tabular.getCellInset(actcell);
2459 if (inset.nextChange(bv, length)) {
2468 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2472 if (the_locking_inset) {
2473 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2477 if (tabular.isLastCell(actcell))
2481 InsetText & inset = tabular.getCellInset(cell);
2482 if (inset.searchForward(bv, str, cs, mw)) {
2486 while (!tabular.isLastCell(cell)) {
2488 InsetText & inset = tabular.getCellInset(cell);
2489 if (inset.searchForward(bv, str, cs, mw)) {
2498 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2501 int cell = tabular.getNumberOfCells();
2502 if (the_locking_inset) {
2503 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2512 InsetText & inset = tabular.getCellInset(cell);
2513 if (inset.searchBackward(bv, str, cs, mw)) {
2522 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2524 if (the_locking_inset)
2525 return the_locking_inset->insetAllowed(code);
2526 // we return true here because if the inset is not locked someone
2527 // wants to insert something in one of our insettexts and we generally
2533 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2535 const int cell = tabular.getCellFromInset(in);
2538 return tabular.getPWidth(cell).zero();
2540 // well we didn't obviously find it so maybe our owner knows more
2541 BOOST_ASSERT(owner());
2542 return owner()->forceDefaultParagraphs(in);
2546 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2549 if (buf.length() <= 0)
2555 string::size_type len = buf.length();
2556 string::size_type p = 0;
2558 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2566 maxCols = max(cols, maxCols);
2572 maxCols = max(cols, maxCols);
2573 LyXTabular * loctab;
2578 delete paste_tabular;
2579 paste_tabular = new LyXTabular(bv->buffer()->params(),
2581 paste_tabular->setOwner(this);
2582 loctab = paste_tabular;
2591 string::size_type op = 0;
2592 int cells = loctab->getNumberOfCells();
2595 rows = loctab->rows();
2596 int const columns = loctab->columns();
2598 while (cell < cells && p < len && row < rows &&
2599 (p = buf.find_first_of("\t\n", p)) != string::npos)
2605 // we can only set this if we are not too far right
2606 if (cols < columns) {
2607 InsetText & inset = loctab->getCellInset(cell);
2608 LyXFont const font = inset.getLyXText(bv)->
2609 getFont(inset.paragraphs.begin(), 0);
2610 inset.setText(buf.substr(op, p - op), font);
2616 // we can only set this if we are not too far right
2617 if (cols < columns) {
2618 InsetText & inset = tabular.getCellInset(cell);
2619 LyXFont const font = inset.getLyXText(bv)->
2620 getFont(inset.paragraphs.begin(), 0);
2621 inset.setText(buf.substr(op, p - op), font);
2626 cell = loctab->getCellNumber(row, cols);
2632 // check for the last cell if there is no trailing '\n'
2633 if (cell < cells && op < len) {
2634 InsetText & inset = loctab->getCellInset(cell);
2635 LyXFont const font = inset.getLyXText(bv)->
2636 getFont(inset.paragraphs.begin(), 0);
2637 inset.setText(buf.substr(op, len - op), font);
2644 void InsetTabular::addPreview(PreviewLoader & loader) const
2646 int const rows = tabular.rows();
2647 int const columns = tabular.columns();
2648 for (int i = 0; i < rows; ++i)
2649 for (int j = 0; j < columns; ++j)
2650 tabular.getCellInset(i, j).addPreview(loader);
2654 string const InsetTabularMailer::name_("tabular");
2656 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2657 : inset_(const_cast<InsetTabular &>(inset))
2661 string const InsetTabularMailer::inset2string(Buffer const &) const
2663 return params2string(inset_);
2667 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2669 istringstream data(in);
2671 lex.setStream(data);
2673 #warning CHECK verify that this is a sane value to return.
2679 string const token = lex.getString();
2687 string const token = lex.getString();
2688 if (token != "\\active_cell")
2691 cell = lex.getInteger();
2694 // This is part of the inset proper that is usually swallowed
2695 // by Buffer::readInset
2698 string const token = lex.getString();
2699 if (token != "Tabular")
2706 Buffer const & buffer = inset.buffer();
2707 inset.read(buffer, lex);
2709 // We can't set the active cell, but we can tell the frontend
2715 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2717 Buffer const & buffer = inset.buffer();
2720 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2721 inset.write(buffer, data);
2722 data << "\\end_inset\n";