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 dispatch_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 dispatch_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 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 dispatch_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 dispatch_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 dispatch_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 dispatch_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 len(value);
1637 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1639 tabular.setColumnPWidth(actcell, len);
1640 if (oldlen != len) {
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 (!len.zero()
1652 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1653 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1657 case LyXTabular::SET_MPWIDTH:
1659 LyXLength const len(value);
1660 LyXLength const & oldlen = tabular.getPWidth(actcell);
1661 tabular.setMColumnPWidth(actcell, len);
1662 if (oldlen != len) {
1663 // We need this otherwise we won't resize
1664 // the insettext of the active cell (if any)
1665 // until later (see InsetText::do_resize)
1666 unlockInsetInInset(bv, the_locking_inset);
1671 case LyXTabular::SET_SPECIAL_COLUMN:
1672 case LyXTabular::SET_SPECIAL_MULTI:
1673 tabular.setAlignSpecial(actcell,value,feature);
1676 case LyXTabular::APPEND_ROW:
1677 // append the row into the tabular
1678 unlockInsetInInset(bv, the_locking_inset);
1679 tabular.appendRow(bv->buffer()->params(), actcell);
1680 tabular.setOwner(this);
1683 case LyXTabular::APPEND_COLUMN:
1684 // append the column into the tabular
1685 unlockInsetInInset(bv, the_locking_inset);
1686 tabular.appendColumn(bv->buffer()->params(), actcell);
1687 tabular.setOwner(this);
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))
2308 .clear(bp.tracking_changes);
2313 bool InsetTabular::isRightToLeft(BufferView * bv)
2315 return bv->getParentLanguage(this)->RightToLeft();
2319 int InsetTabular::scroll(bool recursive) const
2321 int sx = UpdatableInset::scroll(false);
2323 if (recursive && the_locking_inset)
2324 sx += the_locking_inset->scroll(recursive);
2330 void InsetTabular::getSelection(int & srow, int & erow,
2331 int & scol, int & ecol) const
2333 int const start = hasSelection() ? sel_cell_start : actcell;
2334 int const end = hasSelection() ? sel_cell_end : actcell;
2336 srow = tabular.row_of_cell(start);
2337 erow = tabular.row_of_cell(end);
2341 scol = tabular.column_of_cell(start);
2342 ecol = tabular.column_of_cell(end);
2346 ecol = tabular.right_column_of_cell(end);
2350 ParagraphList * InsetTabular::getParagraphs(int i) const
2352 return (i < tabular.getNumberOfCells())
2353 ? tabular.getCellInset(i).getParagraphs(0)
2358 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2360 if (the_locking_inset)
2361 return the_locking_inset->cursor(bv);
2362 return InsetOld::cursor(bv);
2366 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2369 return const_cast<InsetTabular *>(this);
2371 for (int i = 0; i < tabular.rows(); ++i) {
2372 for (int j = 0; j < tabular.columns(); ++j) {
2373 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2383 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2385 if (the_locking_inset) {
2386 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2387 if (!word.word().empty())
2389 if (tabular.isLastCell(actcell)) {
2390 bv->unlockInset(const_cast<InsetTabular *>(this));
2391 return WordLangTuple();
2395 // otherwise we have to lock the next inset and ask for it's selecttion
2396 tabular.getCellInset(actcell)
2397 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2398 WordLangTuple word(selectNextWordInt(bv, value));
2399 if (!word.word().empty())
2405 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2407 // when entering this function the inset should be ALWAYS locked!
2408 BOOST_ASSERT(the_locking_inset);
2410 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2411 if (!word.word().empty())
2414 if (tabular.isLastCell(actcell)) {
2415 bv->unlockInset(const_cast<InsetTabular *>(this));
2416 return WordLangTuple();
2419 // otherwise we have to lock the next inset and ask for it's selecttion
2421 tabular.getCellInset(actcell)
2422 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2423 return selectNextWordInt(bv, value);
2427 void InsetTabular::selectSelectedWord(BufferView * bv)
2429 if (the_locking_inset)
2430 the_locking_inset->selectSelectedWord(bv);
2434 void InsetTabular::markErased()
2436 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2437 tabular.getCellInset(cell).markErased();
2441 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2443 if (the_locking_inset) {
2444 if (the_locking_inset->nextChange(bv, length)) {
2448 if (tabular.isLastCell(actcell))
2452 InsetText & inset = tabular.getCellInset(actcell);
2453 if (inset.nextChange(bv, length)) {
2457 while (!tabular.isLastCell(actcell)) {
2459 InsetText & inset = tabular.getCellInset(actcell);
2460 if (inset.nextChange(bv, length)) {
2469 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2473 if (the_locking_inset) {
2474 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2478 if (tabular.isLastCell(actcell))
2482 InsetText & inset = tabular.getCellInset(cell);
2483 if (inset.searchForward(bv, str, cs, mw)) {
2487 while (!tabular.isLastCell(cell)) {
2489 InsetText & inset = tabular.getCellInset(cell);
2490 if (inset.searchForward(bv, str, cs, mw)) {
2499 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2502 int cell = tabular.getNumberOfCells();
2503 if (the_locking_inset) {
2504 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2513 InsetText & inset = tabular.getCellInset(cell);
2514 if (inset.searchBackward(bv, str, cs, mw)) {
2523 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2525 if (the_locking_inset)
2526 return the_locking_inset->insetAllowed(code);
2527 // we return true here because if the inset is not locked someone
2528 // wants to insert something in one of our insettexts and we generally
2534 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2536 const int cell = tabular.getCellFromInset(in);
2539 return tabular.getPWidth(cell).zero();
2541 // well we didn't obviously find it so maybe our owner knows more
2542 BOOST_ASSERT(owner());
2543 return owner()->forceDefaultParagraphs(in);
2547 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2550 if (buf.length() <= 0)
2556 string::size_type len = buf.length();
2557 string::size_type p = 0;
2559 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2567 maxCols = max(cols, maxCols);
2573 maxCols = max(cols, maxCols);
2574 LyXTabular * loctab;
2579 delete paste_tabular;
2580 paste_tabular = new LyXTabular(bv->buffer()->params(),
2582 paste_tabular->setOwner(this);
2583 loctab = paste_tabular;
2592 string::size_type op = 0;
2593 int cells = loctab->getNumberOfCells();
2596 rows = loctab->rows();
2597 int const columns = loctab->columns();
2599 while (cell < cells && p < len && row < rows &&
2600 (p = buf.find_first_of("\t\n", p)) != string::npos)
2606 // we can only set this if we are not too far right
2607 if (cols < columns) {
2608 InsetText & inset = loctab->getCellInset(cell);
2609 LyXFont const font = inset.getLyXText(bv)->
2610 getFont(inset.paragraphs.begin(), 0);
2611 inset.setText(buf.substr(op, p - op), font);
2617 // we can only set this if we are not too far right
2618 if (cols < columns) {
2619 InsetText & inset = tabular.getCellInset(cell);
2620 LyXFont const font = inset.getLyXText(bv)->
2621 getFont(inset.paragraphs.begin(), 0);
2622 inset.setText(buf.substr(op, p - op), font);
2627 cell = loctab->getCellNumber(row, cols);
2633 // check for the last cell if there is no trailing '\n'
2634 if (cell < cells && op < len) {
2635 InsetText & inset = loctab->getCellInset(cell);
2636 LyXFont const font = inset.getLyXText(bv)->
2637 getFont(inset.paragraphs.begin(), 0);
2638 inset.setText(buf.substr(op, len - op), font);
2645 void InsetTabular::addPreview(PreviewLoader & loader) const
2647 int const rows = tabular.rows();
2648 int const columns = tabular.columns();
2649 for (int i = 0; i < rows; ++i)
2650 for (int j = 0; j < columns; ++j)
2651 tabular.getCellInset(i, j).addPreview(loader);
2655 string const InsetTabularMailer::name_("tabular");
2657 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2658 : inset_(const_cast<InsetTabular &>(inset))
2662 string const InsetTabularMailer::inset2string(Buffer const &) const
2664 return params2string(inset_);
2668 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2670 istringstream data(in);
2672 lex.setStream(data);
2674 #warning CHECK verify that this is a sane value to return.
2680 string const token = lex.getString();
2688 string const token = lex.getString();
2689 if (token != "\\active_cell")
2692 cell = lex.getInteger();
2695 // This is part of the inset proper that is usually swallowed
2696 // by Buffer::readInset
2699 string const token = lex.getString();
2700 if (token != "Tabular")
2707 Buffer const & buffer = inset.buffer();
2708 inset.read(buffer, lex);
2710 // We can't set the active cell, but we can tell the frontend
2716 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2718 Buffer const & buffer = inset.buffer();
2721 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2722 inset.write(buffer, data);
2723 data << "\\end_inset\n";