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"
41 using lyx::graphics::PreviewLoader;
43 using lyx::support::ltrim;
44 using lyx::support::strToInt;
51 using std::istringstream;
53 using std::ostringstream;
58 int const ADD_TO_HEIGHT = 2;
59 int const ADD_TO_TABULAR_WIDTH = 2;
62 LyXTabular * paste_tabular = 0;
65 struct TabularFeature {
66 LyXTabular::Feature action;
71 TabularFeature tabularFeature[] =
73 { LyXTabular::APPEND_ROW, "append-row" },
74 { LyXTabular::APPEND_COLUMN, "append-column" },
75 { LyXTabular::DELETE_ROW, "delete-row" },
76 { LyXTabular::DELETE_COLUMN, "delete-column" },
77 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
78 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
79 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
80 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
81 { LyXTabular::ALIGN_LEFT, "align-left" },
82 { LyXTabular::ALIGN_RIGHT, "align-right" },
83 { LyXTabular::ALIGN_CENTER, "align-center" },
84 { LyXTabular::ALIGN_BLOCK, "align-block" },
85 { LyXTabular::VALIGN_TOP, "valign-top" },
86 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
87 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
88 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
89 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
90 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
91 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
92 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
93 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
94 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
95 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
96 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
97 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
98 { LyXTabular::MULTICOLUMN, "multicolumn" },
99 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
100 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
101 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
102 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
103 { LyXTabular::SET_PWIDTH, "set-pwidth" },
104 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
105 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
106 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
107 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
108 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
109 { LyXTabular::SET_USEBOX, "set-usebox" },
110 { LyXTabular::SET_LTHEAD, "set-lthead" },
111 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
112 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
113 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
114 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
115 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
116 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
117 { LyXTabular::LAST_ACTION, "" }
121 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
122 bool operator()(TabularFeature & tf)
124 return tf.action == feature_;
127 LyXTabular::Feature feature_;
133 string const featureAsString(LyXTabular::Feature feature)
135 TabularFeature * it = tabularFeature;
136 TabularFeature * end = it +
137 sizeof(tabularFeature) / sizeof(TabularFeature);
138 it = std::find_if(it, end, FindFeature(feature));
139 return (it == end) ? string() : it->feature;
143 bool InsetTabular::hasPasteBuffer() const
145 return (paste_tabular != 0);
149 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
150 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
151 buffer_(&buf), cursorx_(0), cursory_(0)
153 tabular.setOwner(this);
154 // for now make it always display as display() inset
156 the_locking_inset = 0;
157 old_locking_inset = 0;
160 actrow = actcell = 0;
168 InsetTabular::InsetTabular(InsetTabular const & tab)
169 : UpdatableInset(tab), tabular(tab.tabular),
170 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
172 tabular.setOwner(this);
173 the_locking_inset = 0;
174 old_locking_inset = 0;
177 actrow = actcell = 0;
185 InsetTabular::~InsetTabular()
187 InsetTabularMailer(*this).hideDialog();
191 auto_ptr<InsetBase> InsetTabular::clone() const
193 return auto_ptr<InsetBase>(new InsetTabular(*this));
197 Buffer const & InsetTabular::buffer() const
203 BufferView * InsetTabular::view() const
210 void InsetTabular::buffer(Buffer * b)
216 void InsetTabular::write(Buffer const & buf, ostream & os) const
218 os << "Tabular" << endl;
219 tabular.write(buf, os);
223 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
225 bool const old_format = (lex.getString() == "\\LyXTable");
227 tabular.read(buf, lex);
233 string token = lex.getString();
234 while (lex.isOK() && (token != "\\end_inset")) {
236 token = lex.getString();
238 if (token != "\\end_inset") {
239 lex.printError("Missing \\end_inset at this point. "
245 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
247 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
248 // mi.base.textwidth << "\n";
250 lyxerr << "InsetTabular::metrics: need bv" << endl;
254 calculate_dimensions_of_cells(mi);
256 dim.asc = tabular.getAscentOfRow(0);
257 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
258 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
263 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
265 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
267 BufferView * bv = pi.base.bv;
270 UpdatableInset::draw(pi, x, y);
278 x += ADD_TO_TABULAR_WIDTH;
281 first_visible_cell = -1;
282 for (int i = 0; i < tabular.rows(); ++i) {
284 cell = tabular.getCellNumber(i, 0);
285 if (y + tabular.getDescentOfRow(i) <= 0 &&
286 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
288 y += tabular.getDescentOfRow(i) +
289 tabular.getAscentOfRow(i + 1) +
290 tabular.getAdditionalHeight(i + 1);
293 for (int j = 0; j < tabular.columns(); ++j) {
294 if (nx > bv->workWidth())
296 if (tabular.isPartOfMultiColumn(i, j))
298 int cx = nx + tabular.getBeginningOfTextInCell(cell);
299 if (first_visible_cell < 0)
300 first_visible_cell = cell;
301 if (hasSelection()) {
302 drawCellSelection(pi.pain, nx, y, i, j, cell);
305 tabular.getCellInset(cell).draw(pi, cx, y);
306 drawCellLines(pi.pain, nx, y, i, cell);
307 nx += tabular.getWidthOfColumn(cell);
311 // Would be nice, but for some completely unfathomable reason,
312 // on a col resize to a new fixed width, even though the insettexts
313 // are resized, the cell isn't, but drawing all cells in a tall table
314 // has the desired effect somehow. Complete dark magic.
316 // avoiding drawing the rest of a long table is
317 // a pretty big speedup
318 if (y > bv->workHeight())
322 y += tabular.getDescentOfRow(i) +
323 tabular.getAscentOfRow(i + 1) +
324 tabular.getAdditionalHeight(i + 1);
329 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
330 int row, int cell) const
332 int x2 = x + tabular.getWidthOfColumn(cell);
335 if (!tabular.topAlreadyDrawn(cell)) {
336 on_off = !tabular.topLine(cell);
337 pain.line(x, y - tabular.getAscentOfRow(row),
338 x2, y - tabular.getAscentOfRow(row),
339 on_off ? LColor::tabularonoffline : LColor::tabularline,
340 on_off ? Painter::line_onoffdash : Painter::line_solid);
342 on_off = !tabular.bottomLine(cell);
343 pain.line(x, y + tabular.getDescentOfRow(row),
344 x2, y + tabular.getDescentOfRow(row),
345 on_off ? LColor::tabularonoffline : LColor::tabularline,
346 on_off ? Painter::line_onoffdash : Painter::line_solid);
347 if (!tabular.leftAlreadyDrawn(cell)) {
348 on_off = !tabular.leftLine(cell);
349 pain.line(x, y - tabular.getAscentOfRow(row),
350 x, y + tabular.getDescentOfRow(row),
351 on_off ? LColor::tabularonoffline : LColor::tabularline,
352 on_off ? Painter::line_onoffdash : Painter::line_solid);
354 on_off = !tabular.rightLine(cell);
355 pain.line(x2 - tabular.getAdditionalWidth(cell),
356 y - tabular.getAscentOfRow(row),
357 x2 - tabular.getAdditionalWidth(cell),
358 y + tabular.getDescentOfRow(row),
359 on_off ? LColor::tabularonoffline : LColor::tabularline,
360 on_off ? Painter::line_onoffdash : Painter::line_solid);
364 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
365 int row, int column, int cell) const
367 BOOST_ASSERT(hasSelection());
368 int cs = tabular.column_of_cell(sel_cell_start);
369 int ce = tabular.column_of_cell(sel_cell_end);
372 cs = tabular.column_of_cell(sel_cell_end);
374 ce = tabular.right_column_of_cell(sel_cell_end);
377 int rs = tabular.row_of_cell(sel_cell_start);
378 int re = tabular.row_of_cell(sel_cell_end);
382 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
383 int w = tabular.getWidthOfColumn(cell);
384 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
385 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
386 w, h, LColor::selection);
391 string const InsetTabular::editMessage() const
393 return _("Opened table");
397 void InsetTabular::insetUnlock(BufferView * bv)
399 if (the_locking_inset) {
400 the_locking_inset->insetUnlock(bv);
402 the_locking_inset = 0;
407 if (scroll(false) || hasSelection()) {
416 void InsetTabular::updateLocal(BufferView * bv) const
418 bv->updateInset(this);
424 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
426 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
431 if (inset == &tabular.getCellInset(actcell)) {
432 lyxerr[Debug::INSETTEXT] << "OK" << endl;
433 the_locking_inset = &tabular.getCellInset(actcell);
436 } else if (!the_locking_inset) {
437 int const n = tabular.getNumberOfCells();
438 int const id = inset->id();
439 for (int i = 0; i < n; ++i) {
440 InsetText * in = &tabular.getCellInset(i);
443 the_locking_inset = in;
448 if (in->getInsetFromID(id)) {
450 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
451 return the_locking_inset->lockInsetInInset(bv, inset);
454 } else if (the_locking_inset && (the_locking_inset == inset)) {
455 lyxerr[Debug::INSETTEXT] << "OK" << endl;
457 } else if (the_locking_inset) {
458 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
459 return the_locking_inset->lockInsetInInset(bv, inset);
461 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
466 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
469 if (!the_locking_inset)
471 if (the_locking_inset == inset) {
472 the_locking_inset->insetUnlock(bv);
474 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
481 // this has to be here otherwise we don't redraw the cell!
482 the_locking_inset = 0;
485 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
486 if (inset->lyxCode() == TABULAR_CODE &&
487 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
488 InsetTabularMailer(*this).updateDialog(bv);
497 int InsetTabular::insetInInsetY() const
499 if (!the_locking_inset)
501 return inset_y + the_locking_inset->insetInInsetY();
505 UpdatableInset * InsetTabular::getLockingInset() const
507 return the_locking_inset ? the_locking_inset->getLockingInset() :
508 const_cast<InsetTabular *>(this);
512 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
516 if (the_locking_inset)
517 return the_locking_inset->getFirstLockingInsetOfType(c);
522 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
524 if (the_locking_inset)
525 return the_locking_inset->insertInset(bv, inset);
530 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
532 if (hasSelection() && cmd.button() == mouse_button::button3)
535 if (hasSelection()) {
537 updateLocal(cmd.view());
540 int const ocell = actcell;
541 BufferView * bv = cmd.view();
545 the_locking_inset = 0;
549 setPos(bv, cmd.x, cmd.y);
552 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
554 if ((ocell == actcell) && the_locking_inset && inset_hit) {
556 FuncRequest cmd1 = cmd;
559 the_locking_inset->localDispatch(cmd1);
563 if (the_locking_inset) {
564 the_locking_inset->insetUnlock(bv);
566 the_locking_inset = 0;
569 if (cmd.button() == mouse_button::button2) {
570 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
574 if (inset_hit && bv->theLockingInset()) {
575 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
576 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
579 FuncRequest cmd1 = cmd;
582 the_locking_inset->localDispatch(cmd1);
587 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
590 if (the_locking_inset) {
591 FuncRequest cmd1 = cmd;
594 ret = the_locking_inset->localDispatch(cmd1);
596 if (cmd.button() == mouse_button::button3 && !ret) {
597 InsetTabularMailer(*this).showDialog(cmd.view());
604 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
606 if (the_locking_inset) {
607 FuncRequest cmd1 = cmd;
610 the_locking_inset->localDispatch(cmd1);
614 BufferView * bv = cmd.view();
615 int const old_cell = actcell;
617 setPos(bv, cmd.x, cmd.y);
618 if (!hasSelection()) {
619 setSelection(actcell, actcell);
621 } else if (old_cell != actcell) {
622 setSelection(sel_cell_start, actcell);
628 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
630 // We need to save the value of the_locking_inset as the call to
631 // the_locking_inset->localDispatch might unlock it.
632 old_locking_inset = the_locking_inset;
633 RESULT result = UpdatableInset::localDispatch(cmd);
634 BufferView * bv = cmd.view();
636 if (cmd.action == LFUN_INSET_EDIT) {
638 if (!bv->lockInset(this)) {
639 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
645 the_locking_inset = 0;
649 if (cmd.argument.size()) {
650 if (cmd.argument == "left") {
651 if (isRightToLeft(bv))
652 actcell = tabular.getLastCellInRow(0);
656 if (isRightToLeft(bv))
657 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
659 actcell = tabular.getNumberOfCells() - 1;
667 setPos(bv, cmd.x, cmd.y);
670 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
671 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
677 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
682 if (cmd.action < 0 && cmd.argument.empty())
685 bool hs = hasSelection();
688 // this one have priority over the locked InsetText, if we're not already
689 // inside another tabular then that one get's priority!
690 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
691 switch (cmd.action) {
692 case LFUN_MOUSE_PRESS:
696 case LFUN_MOUSE_MOTION:
697 lfunMouseMotion(cmd);
700 case LFUN_MOUSE_RELEASE:
701 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
703 case LFUN_CELL_BACKWARD:
704 case LFUN_CELL_FORWARD:
705 unlockInsetInInset(bv, the_locking_inset);
706 if (cmd.action == LFUN_CELL_FORWARD)
707 moveNextCell(bv, old_locking_inset != 0);
709 movePrevCell(bv, old_locking_inset != 0);
713 if (!the_locking_inset)
714 return DISPATCHED_NOUPDATE;
716 // this to avoid compiler warnings.
722 kb_action action = cmd.action;
723 string arg = cmd.argument;
724 if (the_locking_inset) {
725 result = the_locking_inset->localDispatch(cmd);
726 if (result == DISPATCHED_NOUPDATE) {
729 if (sc != scroll()) { // inset has been scrolled
733 } else if (result == DISPATCHED) {
736 } else if (result == FINISHED_UP) {
738 // Make sure to reset status message after
739 // exiting, e.g. math inset
740 bv->owner()->clearMessage();
741 } else if (result == FINISHED_DOWN) {
743 bv->owner()->clearMessage();
744 } else if (result == FINISHED_RIGHT) {
746 bv->owner()->clearMessage();
747 } else if (result == FINISHED) {
748 bv->owner()->clearMessage();
754 // --- Cursor Movements ----------------------------------
755 case LFUN_RIGHTSEL: {
756 int const start = hasSelection() ? sel_cell_start : actcell;
757 if (tabular.isLastCellInRow(actcell)) {
758 setSelection(start, actcell);
763 // if we are starting a selection, only select
764 // the current cell at the beginning
765 if (hasSelection()) {
766 moveRight(bv, false);
769 setSelection(start, end);
774 result = moveRight(bv);
780 int const start = hasSelection() ? sel_cell_start : actcell;
781 if (tabular.isFirstCellInRow(actcell)) {
782 setSelection(start, actcell);
787 // if we are starting a selection, only select
788 // the current cell at the beginning
789 if (hasSelection()) {
793 setSelection(start, end);
798 result = moveLeft(bv);
804 int const start = hasSelection() ? sel_cell_start : actcell;
805 int const ocell = actcell;
806 // if we are starting a selection, only select
807 // the current cell at the beginning
808 if (hasSelection()) {
810 if (ocell == sel_cell_end ||
811 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
812 setSelection(start, tabular.getCellBelow(sel_cell_end));
814 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
816 setSelection(start, start);
822 result = moveDown(bv, old_locking_inset != 0);
828 int const start = hasSelection() ? sel_cell_start : actcell;
829 int const ocell = actcell;
830 // if we are starting a selection, only select
831 // the current cell at the beginning
832 if (hasSelection()) {
834 if ((ocell == sel_cell_end) ||
835 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
836 setSelection(start, tabular.getCellAbove(sel_cell_end));
838 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
840 setSelection(start, start);
846 result = moveUp(bv, old_locking_inset != 0);
855 unlockInsetInInset(bv, the_locking_inset);
856 if (bv->top_y() + bv->painter().paperHeight() <
857 top_baseline + tabular.getHeightOfTabular())
859 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
860 actcell = tabular.getCellBelow(first_visible_cell) + column;
862 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
872 unlockInsetInInset(bv, the_locking_inset);
873 if (top_baseline < 0) {
874 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
875 if (top_baseline > 0)
878 actcell = tabular.getCellBelow(first_visible_cell) + column;
886 // none of these make sense for insettabular,
887 // but we must catch them to prevent any
888 // selection from being confused
892 case LFUN_WORDLEFTSEL:
894 case LFUN_WORDRIGHTSEL:
896 case LFUN_DOWN_PARAGRAPH:
897 case LFUN_DOWN_PARAGRAPHSEL:
898 case LFUN_UP_PARAGRAPH:
899 case LFUN_UP_PARAGRAPHSEL:
905 case LFUN_BEGINNINGBUF:
906 case LFUN_BEGINNINGBUFSEL:
910 case LFUN_LAYOUT_TABULAR: {
911 InsetTabularMailer(*this).showDialog(bv);
914 case LFUN_INSET_DIALOG_UPDATE: {
915 InsetTabularMailer(*this).updateDialog(bv);
918 case LFUN_TABULAR_FEATURE:
919 if (!tabularFeatures(bv, arg))
920 result = UNDISPATCHED;
922 // insert file functions
923 case LFUN_FILE_INSERT_ASCII_PARA:
924 case LFUN_FILE_INSERT_ASCII:
926 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
929 if (insertAsciiString(bv, tmpstr, false))
932 result = UNDISPATCHED;
935 // cut and paste functions
937 if (!copySelection(bv))
941 recordUndo(bv, Undo::DELETE);
942 cutSelection(bv->buffer()->params());
951 case LFUN_PASTESELECTION:
953 string const clip(bv->getClipboard());
957 if (clip.find('\t') != string::npos) {
961 string::size_type len = clip.length();
962 string::size_type p = 0;
965 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
973 maxCols = max(cols, maxCols);
979 maxCols = max(cols, maxCols);
980 delete paste_tabular;
981 paste_tabular = new LyXTabular(bv->buffer()->params(),
982 this, rows, maxCols);
983 string::size_type op = 0;
985 int cells = paste_tabular->getNumberOfCells();
987 while ((cell < cells) && (p < len) &&
988 (p = clip.find_first_of("\t\n", p)) != string::npos) {
993 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
998 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
999 while (cols++ < maxCols)
1007 // check for the last cell if there is no trailing '\n'
1008 if ((cell < cells) && (op < len))
1009 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1012 if (!insertAsciiString(bv, clip, true))
1015 // so that the clipboard is used and it goes on
1017 // and executes LFUN_PASTESELECTION in insettext!
1018 delete paste_tabular;
1023 if (hasPasteBuffer()) {
1024 recordUndo(bv, Undo::INSERT);
1029 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1031 // handle font changing stuff on selection before we lock the inset
1032 // in the default part!
1033 result = UNDISPATCHED;
1044 case LFUN_UNDERLINE:
1045 case LFUN_FONT_SIZE:
1046 if (bv->dispatch(FuncRequest(bv, action, arg)))
1047 result = DISPATCHED;
1053 // we try to activate the actual inset and put this event down to
1054 // the insets dispatch function.
1055 if (result == DISPATCHED || the_locking_inset)
1057 if (activateCellInset(bv)) {
1058 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1059 if (result == UNDISPATCHED || result >= FINISHED) {
1060 unlockInsetInInset(bv, the_locking_inset);
1061 // we need to update if this was requested before
1063 return UNDISPATCHED;
1072 if (result < FINISHED) {
1073 if (!the_locking_inset && bv->fitCursor())
1076 bv->unlockInset(this);
1081 int InsetTabular::latex(Buffer const & buf, ostream & os,
1082 LatexRunParams const & runparams) const
1084 return tabular.latex(buf, os, runparams);
1088 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1091 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1093 return tabular.ascii(buf, os, 0, false, 0);
1097 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1099 return tabular.linuxdoc(buf,os);
1103 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1108 // if the table is inside a float it doesn't need the informaltable
1109 // wrapper. Search for it.
1110 for (master = owner();
1111 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1112 master = master->owner());
1115 os << "<informaltable>";
1120 ret += tabular.docbook(buf, os, mixcont);
1122 os << "</informaltable>";
1131 void InsetTabular::validate(LaTeXFeatures & features) const
1133 tabular.validate(features);
1137 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1140 // if we have a locking_inset we should have to check only this cell for
1141 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1142 // This is _really_ important (André)
1143 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1146 for (int j = 0; j < tabular.columns(); ++j) {
1150 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1151 tabular.getCellInset(actrow, j).metrics(m, dim);
1152 maxAsc = max(dim.asc, maxAsc);
1153 maxDesc = max(dim.des, maxDesc);
1155 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1156 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1157 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1163 for (int i = 0; i < tabular.rows(); ++i) {
1166 for (int j = 0; j < tabular.columns(); ++j) {
1167 if (tabular.isPartOfMultiColumn(i, j))
1173 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1174 tabular.getCellInset(cell).metrics(m, dim);
1175 maxAsc = max(maxAsc, dim.asc);
1176 maxDesc = max(maxDesc, dim.des);
1177 tabular.setWidthOfCell(cell, dim.wid);
1179 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1180 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1185 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1187 if (the_locking_inset) {
1188 the_locking_inset->getCursor(bv, x, y);
1193 y = cursory_ + InsetTabular::y();
1196 int desc = tabular.getDescentOfRow(actrow);
1198 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1200 y += ADD_TO_HEIGHT * 2;
1201 y += TEXT_TO_INSET_OFFSET;
1205 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1207 if (the_locking_inset) {
1208 the_locking_inset->getCursorPos(bv, x, y);
1211 x = cursorx_ - top_x;
1216 void InsetTabular::fitInsetCursor(BufferView * bv) const
1218 if (the_locking_inset) {
1219 the_locking_inset->fitInsetCursor(bv);
1224 int const asc = font_metrics::maxAscent(font);
1225 int const desc = font_metrics::maxDescent(font);
1228 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1232 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1235 actcell = actrow = actcol = 0;
1236 int ly = tabular.getDescentOfRow(actrow);
1238 // first search the right row
1239 while (ly < y && actrow + 1 < tabular.rows()) {
1240 cursory_ += tabular.getDescentOfRow(actrow) +
1241 tabular.getAscentOfRow(actrow + 1) +
1242 tabular.getAdditionalHeight(actrow + 1);
1244 ly = cursory_ + tabular.getDescentOfRow(actrow);
1246 actcell = tabular.getCellNumber(actrow, actcol);
1248 // now search the right column
1249 int lx = tabular.getWidthOfColumn(actcell) -
1250 tabular.getAdditionalWidth(actcell);
1252 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1253 lx += tabular.getWidthOfColumn(actcell + 1)
1254 + tabular.getAdditionalWidth(actcell);
1256 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1261 int InsetTabular::getCellXPos(int cell) const
1265 for (; !tabular.isFirstCellInRow(c); --c)
1267 int lx = tabular.getWidthOfColumn(cell);
1268 for (; c < cell; ++c)
1269 lx += tabular.getWidthOfColumn(c);
1271 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1275 void InsetTabular::resetPos(BufferView * bv) const
1277 #ifdef WITH_WARNINGS
1278 #warning This should be fixed in the right manner (20011128 Jug)
1280 // fast hack to fix infinite repaintings!
1281 if (in_reset_pos > 0)
1285 actcol = tabular.column_of_cell(actcell);
1288 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1289 if (tabular.isLastCellInRow(cell)) {
1290 cursory_ += tabular.getDescentOfRow(actrow) +
1291 tabular.getAscentOfRow(actrow + 1) +
1292 tabular.getAdditionalHeight(actrow + 1);
1297 if (the_locking_inset)
1301 // we need this only from here on!!!
1303 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1304 int new_x = getCellXPos(actcell);
1305 int old_x = cursorx_;
1308 // cursor.x(getCellXPos(actcell) + offset);
1309 if (actcol < tabular.columns() - 1 && scroll(false) &&
1310 tabular.getWidthOfTabular() < bv->workWidth()-20)
1314 } else if (the_locking_inset &&
1315 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1317 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1318 if (xx > bv->workWidth()-20) {
1319 scroll(bv, -(xx - bv->workWidth() + 60));
1321 } else if (xx < 20) {
1329 } else if (cursorx_ - offset > 20 &&
1330 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1331 > bv->workWidth() - 20) {
1332 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1334 } else if (cursorx_ - offset < 20) {
1335 scroll(bv, 20 - cursorx_ + offset);
1337 } else if (scroll() && top_x > 20 &&
1338 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1339 scroll(bv, old_x - cursorx_);
1342 if (the_locking_inset) {
1343 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1346 if ((!the_locking_inset ||
1347 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1348 actcell != oldcell) {
1349 InsetTabularMailer(*this).updateDialog(bv);
1356 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1358 if (lock && !old_locking_inset) {
1359 if (activateCellInset(bv))
1362 bool moved = isRightToLeft(bv)
1363 ? movePrevCell(bv) : moveNextCell(bv);
1365 return FINISHED_RIGHT;
1366 if (lock && activateCellInset(bv))
1370 return DISPATCHED_NOUPDATE;
1374 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1376 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1379 if (lock) { // behind the inset
1380 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1384 return DISPATCHED_NOUPDATE;
1388 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1390 int const ocell = actcell;
1391 actcell = tabular.getCellAbove(actcell);
1392 if (actcell == ocell) // we moved out of the inset
1398 if (old_locking_inset) {
1399 old_locking_inset->getCursorPos(bv, x, y);
1400 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1402 if (activateCellInset(bv, x, 0))
1405 return DISPATCHED_NOUPDATE;
1409 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1411 int const ocell = actcell;
1412 actcell = tabular.getCellBelow(actcell);
1413 if (actcell == ocell) // we moved out of the inset
1414 return FINISHED_DOWN;
1419 if (old_locking_inset) {
1420 old_locking_inset->getCursorPos(bv, x, y);
1421 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1423 if (activateCellInset(bv, x, 0))
1426 return DISPATCHED_NOUPDATE;
1430 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1432 if (isRightToLeft(bv)) {
1433 if (tabular.isFirstCellInRow(actcell)) {
1434 int row = tabular.row_of_cell(actcell);
1435 if (row == tabular.rows() - 1)
1437 actcell = tabular.getLastCellInRow(row);
1438 actcell = tabular.getCellBelow(actcell);
1445 if (tabular.isLastCell(actcell))
1450 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1451 isRightToLeftPar(bv->buffer()->params());
1452 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1459 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1461 if (isRightToLeft(bv)) {
1462 if (tabular.isLastCellInRow(actcell)) {
1463 int row = tabular.row_of_cell(actcell);
1466 actcell = tabular.getFirstCellInRow(row);
1467 actcell = tabular.getCellAbove(actcell);
1469 if (tabular.isLastCell(actcell))
1474 if (!actcell) // first cell
1479 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1480 isRightToLeftPar(bv->buffer()->params());
1481 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1488 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1492 setSelection(0, tabular.getNumberOfCells() - 1);
1494 if (hasSelection()) {
1495 recordUndo(bv, Undo::ATOMIC);
1496 bool const frozen = undo_frozen;
1499 // apply the fontchange on the whole selection
1504 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1505 for(int i = sel_row_start; i <= sel_row_end; ++i)
1506 for(int j = sel_col_start; j <= sel_col_end; ++j)
1507 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1515 if (the_locking_inset)
1516 the_locking_inset->setFont(bv, font, tall);
1520 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1522 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1525 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1526 string const tmp = tabularFeature[i].feature;
1528 if (tmp == what.substr(0, tmp.length())) {
1529 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1530 //tabularFeatures[i].feature.length())) {
1531 action = tabularFeature[i].action;
1535 if (action == LyXTabular::LAST_ACTION)
1539 ltrim(what.substr(tabularFeature[i].feature.length()));
1540 tabularFeatures(bv, action, val);
1546 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1547 string const & special, bool & flag)
1549 if (special == "dl_above") {
1552 } else if (special == "dl_below") {
1553 ltt.bottomDL = flag;
1555 } else if (special == "empty") {
1567 void InsetTabular::tabularFeatures(BufferView * bv,
1568 LyXTabular::Feature feature,
1569 string const & value)
1575 bool setLines = false;
1576 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1577 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1581 case LyXTabular::M_ALIGN_LEFT:
1582 case LyXTabular::ALIGN_LEFT:
1583 setAlign = LYX_ALIGN_LEFT;
1586 case LyXTabular::M_ALIGN_RIGHT:
1587 case LyXTabular::ALIGN_RIGHT:
1588 setAlign = LYX_ALIGN_RIGHT;
1591 case LyXTabular::M_ALIGN_CENTER:
1592 case LyXTabular::ALIGN_CENTER:
1593 setAlign = LYX_ALIGN_CENTER;
1596 case LyXTabular::ALIGN_BLOCK:
1597 setAlign = LYX_ALIGN_BLOCK;
1600 case LyXTabular::M_VALIGN_TOP:
1601 case LyXTabular::VALIGN_TOP:
1602 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1605 case LyXTabular::M_VALIGN_BOTTOM:
1606 case LyXTabular::VALIGN_BOTTOM:
1607 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1610 case LyXTabular::M_VALIGN_MIDDLE:
1611 case LyXTabular::VALIGN_MIDDLE:
1612 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1619 if (hasSelection()) {
1620 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1622 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1623 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1625 recordUndo(bv, Undo::ATOMIC);
1627 int row = tabular.row_of_cell(actcell);
1628 int column = tabular.column_of_cell(actcell);
1630 LyXTabular::ltType ltt;
1634 case LyXTabular::SET_PWIDTH:
1636 LyXLength const vallen(value);
1637 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1639 bool const update = (tmplen != vallen);
1640 tabular.setColumnPWidth(actcell, vallen);
1642 // We need this otherwise we won't resize
1643 // the insettext of the active cell (if any)
1644 // until later (see InsetText::do_resize)
1645 unlockInsetInInset(bv, the_locking_inset);
1650 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1651 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1652 else if (!vallen.zero()
1653 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1654 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1658 case LyXTabular::SET_MPWIDTH:
1660 LyXLength const vallen(value);
1661 LyXLength const & tmplen = tabular.getPWidth(actcell);
1663 bool const update = (tmplen != vallen);
1664 tabular.setMColumnPWidth(actcell, vallen);
1666 // We need this otherwise we won't resize
1667 // the insettext of the active cell (if any)
1668 // until later (see InsetText::do_resize)
1669 unlockInsetInInset(bv, the_locking_inset);
1674 case LyXTabular::SET_SPECIAL_COLUMN:
1675 case LyXTabular::SET_SPECIAL_MULTI:
1676 tabular.setAlignSpecial(actcell,value,feature);
1679 case LyXTabular::APPEND_ROW:
1680 // append the row into the tabular
1681 unlockInsetInInset(bv, the_locking_inset);
1682 tabular.appendRow(bv->buffer()->params(), actcell);
1683 tabular.setOwner(this);
1684 //tabular.init(bv->buffer()->params(), tabular.rows(), tabular.columns());
1687 case LyXTabular::APPEND_COLUMN:
1688 // append the column into the tabular
1689 unlockInsetInInset(bv, the_locking_inset);
1690 tabular.appendColumn(bv->buffer()->params(), actcell);
1691 tabular.setOwner(this);
1692 //tabular.init(bv->buffer()->params(), tabular.rows(), tabular.columns());
1693 actcell = tabular.getCellNumber(row, column);
1696 case LyXTabular::DELETE_ROW:
1697 unlockInsetInInset(bv, the_locking_inset);
1698 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1699 tabular.deleteRow(sel_row_start);
1701 if (sel_row_start >= tabular.rows())
1703 actcell = tabular.getCellNumber(sel_row_start, column);
1707 case LyXTabular::DELETE_COLUMN:
1708 unlockInsetInInset(bv, the_locking_inset);
1709 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1710 tabular.deleteColumn(sel_col_start);
1712 if (sel_col_start >= tabular.columns())
1714 actcell = tabular.getCellNumber(row, sel_col_start);
1718 case LyXTabular::M_TOGGLE_LINE_TOP:
1720 case LyXTabular::TOGGLE_LINE_TOP:
1722 bool lineSet = !tabular.topLine(actcell, flag);
1723 for (int i = sel_row_start; i <= sel_row_end; ++i)
1724 for (int j = sel_col_start; j <= sel_col_end; ++j)
1726 tabular.getCellNumber(i, j),
1732 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1734 case LyXTabular::TOGGLE_LINE_BOTTOM:
1736 bool lineSet = !tabular.bottomLine(actcell, flag);
1737 for (int i = sel_row_start; i <= sel_row_end; ++i)
1738 for (int j = sel_col_start; j <= sel_col_end; ++j)
1739 tabular.setBottomLine(
1740 tabular.getCellNumber(i, j),
1747 case LyXTabular::M_TOGGLE_LINE_LEFT:
1749 case LyXTabular::TOGGLE_LINE_LEFT:
1751 bool lineSet = !tabular.leftLine(actcell, flag);
1752 for (int i = sel_row_start; i <= sel_row_end; ++i)
1753 for (int j = sel_col_start; j <= sel_col_end; ++j)
1754 tabular.setLeftLine(
1755 tabular.getCellNumber(i,j),
1762 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1764 case LyXTabular::TOGGLE_LINE_RIGHT:
1766 bool lineSet = !tabular.rightLine(actcell, flag);
1767 for (int i = sel_row_start; i <= sel_row_end; ++i)
1768 for (int j = sel_col_start; j <= sel_col_end; ++j)
1769 tabular.setRightLine(
1770 tabular.getCellNumber(i,j),
1777 case LyXTabular::M_ALIGN_LEFT:
1778 case LyXTabular::M_ALIGN_RIGHT:
1779 case LyXTabular::M_ALIGN_CENTER:
1781 case LyXTabular::ALIGN_LEFT:
1782 case LyXTabular::ALIGN_RIGHT:
1783 case LyXTabular::ALIGN_CENTER:
1784 case LyXTabular::ALIGN_BLOCK:
1785 for (int i = sel_row_start; i <= sel_row_end; ++i)
1786 for (int j = sel_col_start; j <= sel_col_end; ++j)
1787 tabular.setAlignment(
1788 tabular.getCellNumber(i, j),
1794 case LyXTabular::M_VALIGN_TOP:
1795 case LyXTabular::M_VALIGN_BOTTOM:
1796 case LyXTabular::M_VALIGN_MIDDLE:
1798 case LyXTabular::VALIGN_TOP:
1799 case LyXTabular::VALIGN_BOTTOM:
1800 case LyXTabular::VALIGN_MIDDLE:
1801 for (int i = sel_row_start; i <= sel_row_end; ++i)
1802 for (int j = sel_col_start; j <= sel_col_end; ++j)
1803 tabular.setVAlignment(
1804 tabular.getCellNumber(i, j),
1809 case LyXTabular::MULTICOLUMN: {
1810 if (sel_row_start != sel_row_end) {
1811 #ifdef WITH_WARNINGS
1812 #warning Need I say it ? This is horrible.
1814 Alert::error(_("Error setting multicolumn"),
1815 _("You cannot set multicolumn vertically."));
1818 // just multicol for one Single Cell
1819 if (!hasSelection()) {
1820 // check wether we are completly in a multicol
1821 if (tabular.isMultiColumn(actcell))
1822 tabular.unsetMultiColumn(actcell);
1824 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1828 // we have a selection so this means we just add all this
1829 // cells to form a multicolumn cell
1833 if (sel_cell_start > sel_cell_end) {
1834 s_start = sel_cell_end;
1835 s_end = sel_cell_start;
1837 s_start = sel_cell_start;
1838 s_end = sel_cell_end;
1840 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1847 case LyXTabular::SET_ALL_LINES:
1849 case LyXTabular::UNSET_ALL_LINES:
1850 for (int i = sel_row_start; i <= sel_row_end; ++i)
1851 for (int j = sel_col_start; j <= sel_col_end; ++j)
1852 tabular.setAllLines(
1853 tabular.getCellNumber(i,j), setLines);
1857 case LyXTabular::SET_LONGTABULAR:
1858 tabular.setLongTabular(true);
1859 updateLocal(bv); // because this toggles displayed
1862 case LyXTabular::UNSET_LONGTABULAR:
1863 tabular.setLongTabular(false);
1864 updateLocal(bv); // because this toggles displayed
1867 case LyXTabular::SET_ROTATE_TABULAR:
1868 tabular.setRotateTabular(true);
1871 case LyXTabular::UNSET_ROTATE_TABULAR:
1872 tabular.setRotateTabular(false);
1875 case LyXTabular::SET_ROTATE_CELL:
1876 for (int i = sel_row_start; i <= sel_row_end; ++i)
1877 for (int j = sel_col_start; j<=sel_col_end; ++j)
1878 tabular.setRotateCell(
1879 tabular.getCellNumber(i, j), true);
1882 case LyXTabular::UNSET_ROTATE_CELL:
1883 for (int i = sel_row_start; i <= sel_row_end; ++i)
1884 for (int j = sel_col_start; j <= sel_col_end; ++j)
1885 tabular.setRotateCell(
1886 tabular.getCellNumber(i, j), false);
1889 case LyXTabular::SET_USEBOX: {
1890 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1891 if (val == tabular.getUsebox(actcell))
1892 val = LyXTabular::BOX_NONE;
1893 for (int i = sel_row_start; i <= sel_row_end; ++i)
1894 for (int j = sel_col_start; j <= sel_col_end; ++j)
1895 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1899 case LyXTabular::UNSET_LTFIRSTHEAD:
1901 case LyXTabular::SET_LTFIRSTHEAD:
1902 tabular.getRowOfLTFirstHead(row, ltt);
1903 checkLongtableSpecial(ltt, value, flag);
1904 tabular.setLTHead(row, flag, ltt, true);
1907 case LyXTabular::UNSET_LTHEAD:
1909 case LyXTabular::SET_LTHEAD:
1910 tabular.getRowOfLTHead(row, ltt);
1911 checkLongtableSpecial(ltt, value, flag);
1912 tabular.setLTHead(row, flag, ltt, false);
1915 case LyXTabular::UNSET_LTFOOT:
1917 case LyXTabular::SET_LTFOOT:
1918 tabular.getRowOfLTFoot(row, ltt);
1919 checkLongtableSpecial(ltt, value, flag);
1920 tabular.setLTFoot(row, flag, ltt, false);
1923 case LyXTabular::UNSET_LTLASTFOOT:
1925 case LyXTabular::SET_LTLASTFOOT:
1926 tabular.getRowOfLTLastFoot(row, ltt);
1927 checkLongtableSpecial(ltt, value, flag);
1928 tabular.setLTFoot(row, flag, ltt, true);
1931 case LyXTabular::SET_LTNEWPAGE: {
1932 bool what = !tabular.getLTNewPage(row);
1933 tabular.setLTNewPage(row, what);
1937 // dummy stuff just to avoid warnings
1938 case LyXTabular::LAST_ACTION:
1942 InsetTabularMailer(*this).updateDialog(bv);
1946 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1947 mouse_button::state button, bool behind)
1949 UpdatableInset & inset = tabular.getCellInset(actcell);
1952 x = inset.x() + inset.width();
1953 y = inset.descent();
1955 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1956 //inset_y = cursory_;
1957 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1958 if (!the_locking_inset)
1961 return the_locking_inset;
1965 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1966 mouse_button::state button)
1968 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1970 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1974 bool InsetTabular::insetHit(BufferView *, int x, int) const
1976 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1980 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1982 //resizeLyXText(bv, recursive);
1986 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1987 bool const recursive) const
1989 if (the_locking_inset)
1990 return the_locking_inset->getLyXText(bv, recursive);
1991 return InsetOld::getLyXText(bv, recursive);
1995 bool InsetTabular::showInsetDialog(BufferView * bv) const
1997 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1998 InsetTabularMailer(*this).showDialog(bv);
2003 void InsetTabular::openLayoutDialog(BufferView * bv) const
2005 if (the_locking_inset) {
2006 InsetTabular * inset = static_cast<InsetTabular *>
2007 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2009 inset->openLayoutDialog(bv);
2013 InsetTabularMailer(*this).showDialog(bv);
2018 // function returns an object as defined in func_status.h:
2019 // states OK, Unknown, Disabled, On, Off.
2021 FuncStatus InsetTabular::getStatus(string const & what) const
2023 int action = LyXTabular::LAST_ACTION;
2027 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2028 string const tmp = tabularFeature[i].feature;
2029 if (tmp == what.substr(0, tmp.length())) {
2030 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2031 // tabularFeatures[i].feature.length())) {
2032 action = tabularFeature[i].action;
2036 if (action == LyXTabular::LAST_ACTION) {
2038 return status.unknown(true);
2041 string const argument
2042 = ltrim(what.substr(tabularFeature[i].feature.length()));
2047 LyXTabular::ltType dummyltt;
2051 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2053 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2056 case LyXTabular::SET_PWIDTH:
2057 case LyXTabular::SET_MPWIDTH:
2058 case LyXTabular::SET_SPECIAL_COLUMN:
2059 case LyXTabular::SET_SPECIAL_MULTI:
2060 case LyXTabular::APPEND_ROW:
2061 case LyXTabular::APPEND_COLUMN:
2062 case LyXTabular::DELETE_ROW:
2063 case LyXTabular::DELETE_COLUMN:
2064 case LyXTabular::SET_ALL_LINES:
2065 case LyXTabular::UNSET_ALL_LINES:
2066 return status.clear();
2068 case LyXTabular::MULTICOLUMN:
2069 status.setOnOff(tabular.isMultiColumn(actcell));
2072 case LyXTabular::M_TOGGLE_LINE_TOP:
2074 case LyXTabular::TOGGLE_LINE_TOP:
2075 status.setOnOff(tabular.topLine(actcell, flag));
2078 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2080 case LyXTabular::TOGGLE_LINE_BOTTOM:
2081 status.setOnOff(tabular.bottomLine(actcell, flag));
2084 case LyXTabular::M_TOGGLE_LINE_LEFT:
2086 case LyXTabular::TOGGLE_LINE_LEFT:
2087 status.setOnOff(tabular.leftLine(actcell, flag));
2090 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2092 case LyXTabular::TOGGLE_LINE_RIGHT:
2093 status.setOnOff(tabular.rightLine(actcell, flag));
2096 case LyXTabular::M_ALIGN_LEFT:
2098 case LyXTabular::ALIGN_LEFT:
2099 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2102 case LyXTabular::M_ALIGN_RIGHT:
2104 case LyXTabular::ALIGN_RIGHT:
2105 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2108 case LyXTabular::M_ALIGN_CENTER:
2110 case LyXTabular::ALIGN_CENTER:
2111 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2114 case LyXTabular::ALIGN_BLOCK:
2115 status.disabled(tabular.getPWidth(actcell).zero());
2116 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2119 case LyXTabular::M_VALIGN_TOP:
2121 case LyXTabular::VALIGN_TOP:
2122 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2125 case LyXTabular::M_VALIGN_BOTTOM:
2127 case LyXTabular::VALIGN_BOTTOM:
2128 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2131 case LyXTabular::M_VALIGN_MIDDLE:
2133 case LyXTabular::VALIGN_MIDDLE:
2134 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2137 case LyXTabular::SET_LONGTABULAR:
2138 status.setOnOff(tabular.isLongTabular());
2141 case LyXTabular::UNSET_LONGTABULAR:
2142 status.setOnOff(!tabular.isLongTabular());
2145 case LyXTabular::SET_ROTATE_TABULAR:
2146 status.setOnOff(tabular.getRotateTabular());
2149 case LyXTabular::UNSET_ROTATE_TABULAR:
2150 status.setOnOff(!tabular.getRotateTabular());
2153 case LyXTabular::SET_ROTATE_CELL:
2154 status.setOnOff(tabular.getRotateCell(actcell));
2157 case LyXTabular::UNSET_ROTATE_CELL:
2158 status.setOnOff(!tabular.getRotateCell(actcell));
2161 case LyXTabular::SET_USEBOX:
2162 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2165 case LyXTabular::SET_LTFIRSTHEAD:
2166 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2169 case LyXTabular::SET_LTHEAD:
2170 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2173 case LyXTabular::SET_LTFOOT:
2174 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2177 case LyXTabular::SET_LTLASTFOOT:
2178 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2181 case LyXTabular::SET_LTNEWPAGE:
2182 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2187 status.disabled(true);
2194 void InsetTabular::getLabelList(Buffer const & buffer,
2195 std::vector<string> & list) const
2197 tabular.getLabelList(buffer, list);
2201 bool InsetTabular::copySelection(BufferView * bv)
2203 if (!hasSelection())
2206 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2207 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2208 if (sel_col_start > sel_col_end) {
2209 sel_col_start = sel_col_end;
2210 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2212 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2215 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2216 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2217 if (sel_row_start > sel_row_end)
2218 swap(sel_row_start, sel_row_end);
2220 delete paste_tabular;
2221 paste_tabular = new LyXTabular(tabular);
2222 paste_tabular->setOwner(this);
2224 for (int i = 0; i < sel_row_start; ++i)
2225 paste_tabular->deleteRow(0);
2227 int const rows = sel_row_end - sel_row_start + 1;
2228 while (paste_tabular->rows() > rows)
2229 paste_tabular->deleteRow(rows);
2231 paste_tabular->setTopLine(0, true, true);
2232 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2235 for (int i = 0; i < sel_col_start; ++i)
2236 paste_tabular->deleteColumn(0);
2238 int const columns = sel_col_end - sel_col_start + 1;
2239 while (paste_tabular->columns() > columns)
2240 paste_tabular->deleteColumn(columns);
2242 paste_tabular->setLeftLine(0, true, true);
2243 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2247 paste_tabular->ascii(*bv->buffer(), os,
2248 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2249 bv->stuffClipboard(os.str());
2254 bool InsetTabular::pasteSelection(BufferView * bv)
2259 for (int r1 = 0, r2 = actrow;
2260 r1 < paste_tabular->rows() && r2 < tabular.rows();
2262 for (int c1 = 0, c2 = actcol;
2263 c1 < paste_tabular->columns() && c2 < tabular.columns();
2265 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2266 tabular.isPartOfMultiColumn(r2, c2))
2268 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2272 if (tabular.isPartOfMultiColumn(r2, c2)) {
2276 InsetText & inset = tabular.getCellInset(r2, c2);
2277 inset = paste_tabular->getCellInset(r1, c1);
2278 inset.setOwner(this);
2279 inset.deleteLyXText(bv);
2287 bool InsetTabular::cutSelection(BufferParams const & bp)
2289 if (!hasSelection())
2292 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2293 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2294 if (sel_col_start > sel_col_end) {
2295 sel_col_start = sel_col_end;
2296 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2298 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2301 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2302 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2304 if (sel_row_start > sel_row_end)
2305 swap(sel_row_start, sel_row_end);
2307 if (sel_cell_start > sel_cell_end)
2308 swap(sel_cell_start, sel_cell_end);
2310 for (int i = sel_row_start; i <= sel_row_end; ++i)
2311 for (int j = sel_col_start; j <= sel_col_end; ++j)
2312 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2317 bool InsetTabular::isRightToLeft(BufferView * bv)
2319 return bv->getParentLanguage(this)->RightToLeft();
2323 int InsetTabular::scroll(bool recursive) const
2325 int sx = UpdatableInset::scroll(false);
2327 if (recursive && the_locking_inset)
2328 sx += the_locking_inset->scroll(recursive);
2334 void InsetTabular::getSelection(int & srow, int & erow,
2335 int & scol, int & ecol) const
2337 int const start = hasSelection() ? sel_cell_start : actcell;
2338 int const end = hasSelection() ? sel_cell_end : actcell;
2340 srow = tabular.row_of_cell(start);
2341 erow = tabular.row_of_cell(end);
2345 scol = tabular.column_of_cell(start);
2346 ecol = tabular.column_of_cell(end);
2350 ecol = tabular.right_column_of_cell(end);
2354 ParagraphList * InsetTabular::getParagraphs(int i) const
2356 return (i < tabular.getNumberOfCells())
2357 ? tabular.getCellInset(i).getParagraphs(0)
2362 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2364 if (the_locking_inset)
2365 return the_locking_inset->cursor(bv);
2366 return InsetOld::cursor(bv);
2370 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2373 return const_cast<InsetTabular *>(this);
2375 for (int i = 0; i < tabular.rows(); ++i) {
2376 for (int j = 0; j < tabular.columns(); ++j) {
2377 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2387 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2389 if (the_locking_inset) {
2390 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2391 if (!word.word().empty())
2393 if (tabular.isLastCell(actcell)) {
2394 bv->unlockInset(const_cast<InsetTabular *>(this));
2395 return WordLangTuple();
2399 // otherwise we have to lock the next inset and ask for it's selecttion
2400 tabular.getCellInset(actcell)
2401 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2402 WordLangTuple word(selectNextWordInt(bv, value));
2403 if (!word.word().empty())
2409 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2411 // when entering this function the inset should be ALWAYS locked!
2412 BOOST_ASSERT(the_locking_inset);
2414 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2415 if (!word.word().empty())
2418 if (tabular.isLastCell(actcell)) {
2419 bv->unlockInset(const_cast<InsetTabular *>(this));
2420 return WordLangTuple();
2423 // otherwise we have to lock the next inset and ask for it's selecttion
2425 tabular.getCellInset(actcell)
2426 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2427 return selectNextWordInt(bv, value);
2431 void InsetTabular::selectSelectedWord(BufferView * bv)
2433 if (the_locking_inset)
2434 the_locking_inset->selectSelectedWord(bv);
2438 void InsetTabular::markErased()
2440 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2441 tabular.getCellInset(cell).markErased();
2445 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2447 if (the_locking_inset) {
2448 if (the_locking_inset->nextChange(bv, length)) {
2452 if (tabular.isLastCell(actcell))
2456 InsetText & inset = tabular.getCellInset(actcell);
2457 if (inset.nextChange(bv, length)) {
2461 while (!tabular.isLastCell(actcell)) {
2463 InsetText & inset = tabular.getCellInset(actcell);
2464 if (inset.nextChange(bv, length)) {
2473 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2477 if (the_locking_inset) {
2478 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2482 if (tabular.isLastCell(actcell))
2486 InsetText & inset = tabular.getCellInset(cell);
2487 if (inset.searchForward(bv, str, cs, mw)) {
2491 while (!tabular.isLastCell(cell)) {
2493 InsetText & inset = tabular.getCellInset(cell);
2494 if (inset.searchForward(bv, str, cs, mw)) {
2503 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2506 int cell = tabular.getNumberOfCells();
2507 if (the_locking_inset) {
2508 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2517 InsetText & inset = tabular.getCellInset(cell);
2518 if (inset.searchBackward(bv, str, cs, mw)) {
2527 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2529 if (the_locking_inset)
2530 return the_locking_inset->insetAllowed(code);
2531 // we return true here because if the inset is not locked someone
2532 // wants to insert something in one of our insettexts and we generally
2538 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2540 const int cell = tabular.getCellFromInset(in);
2543 return tabular.getPWidth(cell).zero();
2545 // well we didn't obviously find it so maybe our owner knows more
2546 BOOST_ASSERT(owner());
2547 return owner()->forceDefaultParagraphs(in);
2551 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2554 if (buf.length() <= 0)
2560 string::size_type len = buf.length();
2561 string::size_type p = 0;
2563 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2571 maxCols = max(cols, maxCols);
2577 maxCols = max(cols, maxCols);
2578 LyXTabular * loctab;
2583 delete paste_tabular;
2584 paste_tabular = new LyXTabular(bv->buffer()->params(),
2586 paste_tabular->setOwner(this);
2587 loctab = paste_tabular;
2596 string::size_type op = 0;
2597 int cells = loctab->getNumberOfCells();
2600 rows = loctab->rows();
2601 int const columns = loctab->columns();
2603 while (cell < cells && p < len && row < rows &&
2604 (p = buf.find_first_of("\t\n", p)) != string::npos)
2610 // we can only set this if we are not too far right
2611 if (cols < columns) {
2612 InsetText & inset = loctab->getCellInset(cell);
2613 LyXFont const font = inset.getLyXText(bv)->
2614 getFont(inset.paragraphs.begin(), 0);
2615 inset.setText(buf.substr(op, p - op), font);
2621 // we can only set this if we are not too far right
2622 if (cols < columns) {
2623 InsetText & inset = tabular.getCellInset(cell);
2624 LyXFont const font = inset.getLyXText(bv)->
2625 getFont(inset.paragraphs.begin(), 0);
2626 inset.setText(buf.substr(op, p - op), font);
2631 cell = loctab->getCellNumber(row, cols);
2637 // check for the last cell if there is no trailing '\n'
2638 if (cell < cells && op < len) {
2639 InsetText & inset = loctab->getCellInset(cell);
2640 LyXFont const font = inset.getLyXText(bv)->
2641 getFont(inset.paragraphs.begin(), 0);
2642 inset.setText(buf.substr(op, len - op), font);
2649 void InsetTabular::addPreview(PreviewLoader & loader) const
2651 int const rows = tabular.rows();
2652 int const columns = tabular.columns();
2653 for (int i = 0; i < rows; ++i)
2654 for (int j = 0; j < columns; ++j)
2655 tabular.getCellInset(i, j).addPreview(loader);
2659 string const InsetTabularMailer::name_("tabular");
2661 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2662 : inset_(const_cast<InsetTabular &>(inset))
2666 string const InsetTabularMailer::inset2string(Buffer const &) const
2668 return params2string(inset_);
2672 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2674 istringstream data(in);
2676 lex.setStream(data);
2678 #warning CHECK verify that this is a sane value to return.
2684 string const token = lex.getString();
2692 string const token = lex.getString();
2693 if (token != "\\active_cell")
2696 cell = lex.getInteger();
2699 // This is part of the inset proper that is usually swallowed
2700 // by Buffer::readInset
2703 string const token = lex.getString();
2704 if (token != "Tabular")
2711 Buffer const & buffer = inset.buffer();
2712 inset.read(buffer, lex);
2714 // We can't set the active cell, but we can tell the frontend
2720 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2722 Buffer const & buffer = inset.buffer();
2725 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2726 inset.write(buffer, data);
2727 data << "\\end_inset\n";