3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
13 #include "insettabular.h"
14 #include "insettext.h"
17 #include "BufferView.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
28 #include "lyxlength.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "metricsinfo.h"
36 #include "frontends/Alert.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/Painter.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
51 using namespace lyx::support;
52 using namespace lyx::graphics;
66 int const ADD_TO_HEIGHT = 2;
67 int const ADD_TO_TABULAR_WIDTH = 2;
70 LyXTabular * paste_tabular = 0;
73 struct TabularFeature {
74 LyXTabular::Feature action;
79 TabularFeature tabularFeature[] =
81 { LyXTabular::APPEND_ROW, "append-row" },
82 { LyXTabular::APPEND_COLUMN, "append-column" },
83 { LyXTabular::DELETE_ROW, "delete-row" },
84 { LyXTabular::DELETE_COLUMN, "delete-column" },
85 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
86 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
87 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
88 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
89 { LyXTabular::ALIGN_LEFT, "align-left" },
90 { LyXTabular::ALIGN_RIGHT, "align-right" },
91 { LyXTabular::ALIGN_CENTER, "align-center" },
92 { LyXTabular::ALIGN_BLOCK, "align-block" },
93 { LyXTabular::VALIGN_TOP, "valign-top" },
94 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
95 { LyXTabular::VALIGN_CENTER, "valign-center" },
96 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
97 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
98 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
99 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
100 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
101 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
102 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
103 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
104 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
105 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
106 { LyXTabular::MULTICOLUMN, "multicolumn" },
107 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
108 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
109 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
110 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
111 { LyXTabular::SET_PWIDTH, "set-pwidth" },
112 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
113 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
114 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
115 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
116 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
117 { LyXTabular::SET_USEBOX, "set-usebox" },
118 { LyXTabular::SET_LTHEAD, "set-lthead" },
119 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
120 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
121 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
122 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
123 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
124 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
125 { LyXTabular::LAST_ACTION, "" }
129 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
130 bool operator()(TabularFeature & tf)
132 return tf.action == feature_;
135 LyXTabular::Feature feature_;
141 string const featureAsString(LyXTabular::Feature feature)
143 TabularFeature * it = tabularFeature;
144 TabularFeature * end = it +
145 sizeof(tabularFeature) / sizeof(TabularFeature);
146 it = std::find_if(it, end, FindFeature(feature));
147 return (it == end) ? string() : it->feature;
151 bool InsetTabular::hasPasteBuffer() const
153 return (paste_tabular != 0);
157 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
158 : tabular(buf.params, this, max(rows, 1), max(columns, 1)),
159 buffer_(&buf), cursorx_(0), cursory_(0)
161 // for now make it always display as display() inset
163 the_locking_inset = 0;
164 old_locking_inset = 0;
167 actrow = actcell = 0;
175 InsetTabular::InsetTabular(InsetTabular const & tab)
176 : UpdatableInset(tab),
177 tabular(tab.buffer_->params, this, tab.tabular),
178 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
180 the_locking_inset = 0;
181 old_locking_inset = 0;
184 actrow = actcell = 0;
192 InsetTabular::~InsetTabular()
194 InsetTabularMailer(*this).hideDialog();
198 auto_ptr<InsetBase> InsetTabular::clone() const
200 return auto_ptr<InsetBase>(new InsetTabular(*this));
204 Buffer const * InsetTabular::buffer() const
210 BufferView * InsetTabular::view() const
217 void InsetTabular::buffer(Buffer * b)
223 void InsetTabular::write(Buffer const * buf, ostream & os) const
225 os << "Tabular" << endl;
226 tabular.write(buf, os);
230 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
232 bool const old_format = (lex.getString() == "\\LyXTable");
234 tabular.read(buf, lex);
240 string token = lex.getString();
241 while (lex.isOK() && (token != "\\end_inset")) {
243 token = lex.getString();
245 if (token != "\\end_inset") {
246 lex.printError("Missing \\end_inset at this point. "
252 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
254 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
255 // mi.base.textwidth << "\n";
257 lyxerr << "InsetTabular::metrics: need bv" << endl;
261 calculate_dimensions_of_cells(mi);
263 dim.asc = tabular.getAscentOfRow(0);
264 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
265 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
270 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
272 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
274 BufferView * bv = pi.base.bv;
277 UpdatableInset::draw(pi, x, y);
285 x += ADD_TO_TABULAR_WIDTH;
288 first_visible_cell = -1;
289 for (int i = 0; i < tabular.rows(); ++i) {
291 cell = tabular.getCellNumber(i, 0);
292 if (y + tabular.getDescentOfRow(i) <= 0 &&
293 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
295 y += tabular.getDescentOfRow(i) +
296 tabular.getAscentOfRow(i + 1) +
297 tabular.getAdditionalHeight(i + 1);
300 for (int j = 0; j < tabular.columns(); ++j) {
301 if (nx > bv->workWidth())
303 if (tabular.isPartOfMultiColumn(i, j))
305 int cx = nx + tabular.getBeginningOfTextInCell(cell);
306 if (first_visible_cell < 0)
307 first_visible_cell = cell;
308 if (hasSelection()) {
309 drawCellSelection(pi.pain, nx, y, i, j, cell);
312 tabular.getCellInset(cell).draw(pi, cx, y);
313 drawCellLines(pi.pain, nx, y, i, cell);
314 nx += tabular.getWidthOfColumn(cell);
318 // Would be nice, but for some completely unfathomable reason,
319 // on a col resize to a new fixed width, even though the insettexts
320 // are resized, the cell isn't, but drawing all cells in a tall table
321 // has the desired effect somehow. Complete dark magic.
323 // avoiding drawing the rest of a long table is
324 // a pretty big speedup
325 if (y > bv->workHeight())
329 y += tabular.getDescentOfRow(i) +
330 tabular.getAscentOfRow(i + 1) +
331 tabular.getAdditionalHeight(i + 1);
336 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
337 int row, int cell) const
339 int x2 = x + tabular.getWidthOfColumn(cell);
342 if (!tabular.topAlreadyDrawn(cell)) {
343 on_off = !tabular.topLine(cell);
344 pain.line(x, y - tabular.getAscentOfRow(row),
345 x2, y - tabular.getAscentOfRow(row),
346 on_off ? LColor::tabularonoffline : LColor::tabularline,
347 on_off ? Painter::line_onoffdash : Painter::line_solid);
349 on_off = !tabular.bottomLine(cell);
350 pain.line(x, y + tabular.getDescentOfRow(row),
351 x2, y + tabular.getDescentOfRow(row),
352 on_off ? LColor::tabularonoffline : LColor::tabularline,
353 on_off ? Painter::line_onoffdash : Painter::line_solid);
354 if (!tabular.leftAlreadyDrawn(cell)) {
355 on_off = !tabular.leftLine(cell);
356 pain.line(x, y - tabular.getAscentOfRow(row),
357 x, y + tabular.getDescentOfRow(row),
358 on_off ? LColor::tabularonoffline : LColor::tabularline,
359 on_off ? Painter::line_onoffdash : Painter::line_solid);
361 on_off = !tabular.rightLine(cell);
362 pain.line(x2 - tabular.getAdditionalWidth(cell),
363 y - tabular.getAscentOfRow(row),
364 x2 - tabular.getAdditionalWidth(cell),
365 y + tabular.getDescentOfRow(row),
366 on_off ? LColor::tabularonoffline : LColor::tabularline,
367 on_off ? Painter::line_onoffdash : Painter::line_solid);
371 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
372 int row, int column, int cell) const
374 Assert(hasSelection());
375 int cs = tabular.column_of_cell(sel_cell_start);
376 int ce = tabular.column_of_cell(sel_cell_end);
379 cs = tabular.column_of_cell(sel_cell_end);
381 ce = tabular.right_column_of_cell(sel_cell_end);
384 int rs = tabular.row_of_cell(sel_cell_start);
385 int re = tabular.row_of_cell(sel_cell_end);
389 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
390 int w = tabular.getWidthOfColumn(cell);
391 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
392 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
393 w, h, LColor::selection);
398 string const InsetTabular::editMessage() const
400 return _("Opened table");
404 void InsetTabular::insetUnlock(BufferView * bv)
406 if (the_locking_inset) {
407 the_locking_inset->insetUnlock(bv);
409 the_locking_inset = 0;
414 if (scroll(false) || hasSelection()) {
423 void InsetTabular::updateLocal(BufferView * bv) const
431 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
433 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
438 if (inset == &tabular.getCellInset(actcell)) {
439 lyxerr[Debug::INSETTEXT] << "OK" << endl;
440 the_locking_inset = &tabular.getCellInset(actcell);
443 } else if (!the_locking_inset) {
444 int const n = tabular.getNumberOfCells();
445 int const id = inset->id();
446 for (int i = 0; i < n; ++i) {
447 InsetText * in = &tabular.getCellInset(i);
450 the_locking_inset = in;
455 if (in->getInsetFromID(id)) {
457 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
458 return the_locking_inset->lockInsetInInset(bv, inset);
461 } else if (the_locking_inset && (the_locking_inset == inset)) {
462 lyxerr[Debug::INSETTEXT] << "OK" << endl;
464 } else if (the_locking_inset) {
465 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
466 return the_locking_inset->lockInsetInInset(bv, inset);
468 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
473 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
476 if (!the_locking_inset)
478 if (the_locking_inset == inset) {
479 the_locking_inset->insetUnlock(bv);
481 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
488 // this has to be here otherwise we don't redraw the cell!
489 the_locking_inset = 0;
492 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
493 if (inset->lyxCode() == TABULAR_CODE &&
494 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
495 InsetTabularMailer(*this).updateDialog(bv);
504 int InsetTabular::insetInInsetY() const
506 if (!the_locking_inset)
508 return inset_y + the_locking_inset->insetInInsetY();
512 UpdatableInset * InsetTabular::getLockingInset() const
514 return the_locking_inset ? the_locking_inset->getLockingInset() :
515 const_cast<InsetTabular *>(this);
519 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
523 if (the_locking_inset)
524 return the_locking_inset->getFirstLockingInsetOfType(c);
529 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
531 if (the_locking_inset)
532 return the_locking_inset->insertInset(bv, inset);
537 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
539 if (hasSelection() && cmd.button() == mouse_button::button3)
542 if (hasSelection()) {
544 updateLocal(cmd.view());
547 int const ocell = actcell;
548 BufferView * bv = cmd.view();
552 the_locking_inset = 0;
556 setPos(bv, cmd.x, cmd.y);
559 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
561 if ((ocell == actcell) && the_locking_inset && inset_hit) {
563 FuncRequest cmd1 = cmd;
566 the_locking_inset->localDispatch(cmd1);
570 if (the_locking_inset) {
571 the_locking_inset->insetUnlock(bv);
573 the_locking_inset = 0;
576 if (cmd.button() == mouse_button::button2) {
577 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
581 if (inset_hit && bv->theLockingInset()) {
582 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
583 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
586 FuncRequest cmd1 = cmd;
589 the_locking_inset->localDispatch(cmd1);
594 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
597 if (the_locking_inset) {
598 FuncRequest cmd1 = cmd;
601 ret = the_locking_inset->localDispatch(cmd1);
603 if (cmd.button() == mouse_button::button3 && !ret) {
604 InsetTabularMailer(*this).showDialog(cmd.view());
611 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
613 if (the_locking_inset) {
614 FuncRequest cmd1 = cmd;
617 the_locking_inset->localDispatch(cmd1);
621 BufferView * bv = cmd.view();
622 int const old_cell = actcell;
624 setPos(bv, cmd.x, cmd.y);
625 if (!hasSelection()) {
626 setSelection(actcell, actcell);
628 } else if (old_cell != actcell) {
629 setSelection(sel_cell_start, actcell);
635 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
637 // We need to save the value of the_locking_inset as the call to
638 // the_locking_inset->localDispatch might unlock it.
639 old_locking_inset = the_locking_inset;
640 RESULT result = UpdatableInset::localDispatch(cmd);
641 BufferView * bv = cmd.view();
643 if (cmd.action == LFUN_INSET_EDIT) {
645 if (!bv->lockInset(this)) {
646 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
652 the_locking_inset = 0;
656 if (cmd.argument.size()) {
657 if (cmd.argument == "left") {
658 if (isRightToLeft(bv))
659 actcell = tabular.getLastCellInRow(0);
663 if (isRightToLeft(bv))
664 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
666 actcell = tabular.getNumberOfCells() - 1;
674 setPos(bv, cmd.x, cmd.y);
677 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
678 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
684 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
689 if (cmd.action < 0 && cmd.argument.empty())
692 bool hs = hasSelection();
695 // this one have priority over the locked InsetText, if we're not already
696 // inside another tabular then that one get's priority!
697 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
698 switch (cmd.action) {
699 case LFUN_MOUSE_PRESS:
703 case LFUN_MOUSE_MOTION:
704 lfunMouseMotion(cmd);
707 case LFUN_MOUSE_RELEASE:
708 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
710 case LFUN_CELL_BACKWARD:
711 case LFUN_CELL_FORWARD:
712 unlockInsetInInset(bv, the_locking_inset);
713 if (cmd.action == LFUN_CELL_FORWARD)
714 moveNextCell(bv, old_locking_inset != 0);
716 movePrevCell(bv, old_locking_inset != 0);
720 if (!the_locking_inset)
721 return DISPATCHED_NOUPDATE;
723 // this to avoid compiler warnings.
729 kb_action action = cmd.action;
730 string arg = cmd.argument;
731 if (the_locking_inset) {
732 result = the_locking_inset->localDispatch(cmd);
733 if (result == DISPATCHED_NOUPDATE) {
736 if (sc != scroll()) { // inset has been scrolled
740 } else if (result == DISPATCHED) {
743 } else if (result == FINISHED_UP) {
745 // Make sure to reset status message after
746 // exiting, e.g. math inset
747 bv->owner()->clearMessage();
748 } else if (result == FINISHED_DOWN) {
750 bv->owner()->clearMessage();
751 } else if (result == FINISHED_RIGHT) {
753 bv->owner()->clearMessage();
754 } else if (result == FINISHED) {
755 bv->owner()->clearMessage();
761 // --- Cursor Movements ----------------------------------
762 case LFUN_RIGHTSEL: {
763 int const start = hasSelection() ? sel_cell_start : actcell;
764 if (tabular.isLastCellInRow(actcell)) {
765 setSelection(start, actcell);
770 // if we are starting a selection, only select
771 // the current cell at the beginning
772 if (hasSelection()) {
773 moveRight(bv, false);
776 setSelection(start, end);
781 result = moveRight(bv);
787 int const start = hasSelection() ? sel_cell_start : actcell;
788 if (tabular.isFirstCellInRow(actcell)) {
789 setSelection(start, actcell);
794 // if we are starting a selection, only select
795 // the current cell at the beginning
796 if (hasSelection()) {
800 setSelection(start, end);
805 result = moveLeft(bv);
811 int const start = hasSelection() ? sel_cell_start : actcell;
812 int const ocell = actcell;
813 // if we are starting a selection, only select
814 // the current cell at the beginning
815 if (hasSelection()) {
817 if (ocell == sel_cell_end ||
818 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
819 setSelection(start, tabular.getCellBelow(sel_cell_end));
821 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
823 setSelection(start, start);
829 result = moveDown(bv, old_locking_inset != 0);
835 int const start = hasSelection() ? sel_cell_start : actcell;
836 int const ocell = actcell;
837 // if we are starting a selection, only select
838 // the current cell at the beginning
839 if (hasSelection()) {
841 if ((ocell == sel_cell_end) ||
842 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
843 setSelection(start, tabular.getCellAbove(sel_cell_end));
845 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
847 setSelection(start, start);
853 result = moveUp(bv, old_locking_inset != 0);
862 unlockInsetInInset(bv, the_locking_inset);
863 if (bv->text->top_y() + bv->painter().paperHeight() <
864 top_baseline + tabular.getHeightOfTabular())
866 bv->scrollDocView(bv->text->top_y() + bv->painter().paperHeight());
867 actcell = tabular.getCellBelow(first_visible_cell) + column;
869 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
879 unlockInsetInInset(bv, the_locking_inset);
880 if (top_baseline < 0) {
881 bv->scrollDocView(bv->text->top_y() - bv->painter().paperHeight());
882 if (top_baseline > 0)
885 actcell = tabular.getCellBelow(first_visible_cell) + column;
893 // none of these make sense for insettabular,
894 // but we must catch them to prevent any
895 // selection from being confused
899 case LFUN_WORDLEFTSEL:
901 case LFUN_WORDRIGHTSEL:
903 case LFUN_DOWN_PARAGRAPH:
904 case LFUN_DOWN_PARAGRAPHSEL:
905 case LFUN_UP_PARAGRAPH:
906 case LFUN_UP_PARAGRAPHSEL:
912 case LFUN_BEGINNINGBUF:
913 case LFUN_BEGINNINGBUFSEL:
917 case LFUN_LAYOUT_TABULAR: {
918 InsetTabularMailer(*this).showDialog(bv);
921 case LFUN_INSET_DIALOG_UPDATE: {
922 InsetTabularMailer(*this).updateDialog(bv);
925 case LFUN_TABULAR_FEATURE:
926 if (!tabularFeatures(bv, arg))
927 result = UNDISPATCHED;
929 // insert file functions
930 case LFUN_FILE_INSERT_ASCII_PARA:
931 case LFUN_FILE_INSERT_ASCII:
933 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
936 if (insertAsciiString(bv, tmpstr, false))
939 result = UNDISPATCHED;
942 // cut and paste functions
944 if (!copySelection(bv))
948 recordUndo(bv, Undo::DELETE);
949 cutSelection(bv->buffer()->params);
958 case LFUN_PASTESELECTION:
960 string const clip(bv->getClipboard());
964 if (clip.find('\t') != string::npos) {
968 string::size_type len = clip.length();
969 string::size_type p = 0;
972 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
980 maxCols = max(cols, maxCols);
986 maxCols = max(cols, maxCols);
987 delete paste_tabular;
988 paste_tabular = new LyXTabular(bv->buffer()->params,
989 this, rows, maxCols);
990 string::size_type op = 0;
992 int cells = paste_tabular->getNumberOfCells();
994 while ((cell < cells) && (p < len) &&
995 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1000 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1005 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1006 while (cols++ < maxCols)
1014 // check for the last cell if there is no trailing '\n'
1015 if ((cell < cells) && (op < len))
1016 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1019 if (!insertAsciiString(bv, clip, true))
1022 // so that the clipboard is used and it goes on
1024 // and executes LFUN_PASTESELECTION in insettext!
1025 delete paste_tabular;
1030 if (hasPasteBuffer()) {
1031 recordUndo(bv, Undo::INSERT);
1036 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1038 // handle font changing stuff on selection before we lock the inset
1039 // in the default part!
1040 result = UNDISPATCHED;
1051 case LFUN_UNDERLINE:
1052 case LFUN_FONT_SIZE:
1053 if (bv->dispatch(FuncRequest(bv, action, arg)))
1054 result = DISPATCHED;
1060 // we try to activate the actual inset and put this event down to
1061 // the insets dispatch function.
1062 if (result == DISPATCHED || the_locking_inset)
1064 if (activateCellInset(bv)) {
1065 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1066 if (result == UNDISPATCHED || result >= FINISHED) {
1067 unlockInsetInInset(bv, the_locking_inset);
1068 // we need to update if this was requested before
1070 return UNDISPATCHED;
1079 if (result < FINISHED) {
1080 if (!the_locking_inset && bv->fitCursor())
1083 bv->unlockInset(this);
1088 int InsetTabular::latex(Buffer const * buf, ostream & os,
1089 LatexRunParams const & runparams) const
1091 return tabular.latex(buf, os, runparams);
1095 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1098 return tabular.ascii(buf, os, (int)parOwner()->params().depth(),
1100 return tabular.ascii(buf, os, 0, false, 0);
1104 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1106 return tabular.linuxdoc(buf,os);
1110 int InsetTabular::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1115 // if the table is inside a float it doesn't need the informaltable
1116 // wrapper. Search for it.
1117 for (master = owner();
1118 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1119 master = master->owner());
1122 os << "<informaltable>";
1127 ret += tabular.docbook(buf, os, mixcont);
1129 os << "</informaltable>";
1138 void InsetTabular::validate(LaTeXFeatures & features) const
1140 tabular.validate(features);
1144 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1147 // if we have a locking_inset we should have to check only this cell for
1148 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1149 // This is _really_ important (André)
1150 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1153 for (int j = 0; j < tabular.columns(); ++j) {
1157 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1158 tabular.getCellInset(actrow, j).metrics(m, dim);
1159 maxAsc = max(dim.asc, maxAsc);
1160 maxDesc = max(dim.des, maxDesc);
1162 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1163 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1164 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1170 for (int i = 0; i < tabular.rows(); ++i) {
1173 for (int j = 0; j < tabular.columns(); ++j) {
1174 if (tabular.isPartOfMultiColumn(i, j))
1180 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1181 tabular.getCellInset(cell).metrics(m, dim);
1182 maxAsc = max(maxAsc, dim.asc);
1183 maxDesc = max(maxDesc, dim.des);
1184 tabular.setWidthOfCell(cell, dim.wid);
1186 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1187 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1192 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1194 if (the_locking_inset) {
1195 the_locking_inset->getCursor(bv, x, y);
1200 y = cursory_ + InsetTabular::y();
1203 int desc = tabular.getDescentOfRow(actrow);
1205 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1207 y += ADD_TO_HEIGHT * 2;
1208 y += TEXT_TO_INSET_OFFSET;
1212 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1214 if (the_locking_inset) {
1215 the_locking_inset->getCursorPos(bv, x, y);
1218 x = cursorx_ - top_x;
1223 void InsetTabular::fitInsetCursor(BufferView * bv) const
1225 if (the_locking_inset) {
1226 the_locking_inset->fitInsetCursor(bv);
1231 int const asc = font_metrics::maxAscent(font);
1232 int const desc = font_metrics::maxDescent(font);
1235 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1239 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1242 actcell = actrow = actcol = 0;
1243 int ly = tabular.getDescentOfRow(actrow);
1245 // first search the right row
1246 while (ly < y && actrow + 1 < tabular.rows()) {
1247 cursory_ += tabular.getDescentOfRow(actrow) +
1248 tabular.getAscentOfRow(actrow + 1) +
1249 tabular.getAdditionalHeight(actrow + 1);
1251 ly = cursory_ + tabular.getDescentOfRow(actrow);
1253 actcell = tabular.getCellNumber(actrow, actcol);
1255 // now search the right column
1256 int lx = tabular.getWidthOfColumn(actcell) -
1257 tabular.getAdditionalWidth(actcell);
1259 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1260 lx += tabular.getWidthOfColumn(actcell + 1)
1261 + tabular.getAdditionalWidth(actcell);
1263 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1268 int InsetTabular::getCellXPos(int cell) const
1272 for (; !tabular.isFirstCellInRow(c); --c)
1274 int lx = tabular.getWidthOfColumn(cell);
1275 for (; c < cell; ++c)
1276 lx += tabular.getWidthOfColumn(c);
1278 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1282 void InsetTabular::resetPos(BufferView * bv) const
1284 #ifdef WITH_WARNINGS
1285 #warning This should be fixed in the right manner (20011128 Jug)
1287 // fast hack to fix infinite repaintings!
1288 if (in_reset_pos > 0)
1292 actcol = tabular.column_of_cell(actcell);
1295 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1296 if (tabular.isLastCellInRow(cell)) {
1297 cursory_ += tabular.getDescentOfRow(actrow) +
1298 tabular.getAscentOfRow(actrow + 1) +
1299 tabular.getAdditionalHeight(actrow + 1);
1304 if (the_locking_inset)
1308 // we need this only from here on!!!
1310 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1311 int new_x = getCellXPos(actcell);
1312 int old_x = cursorx_;
1315 // cursor.x(getCellXPos(actcell) + offset);
1316 if (actcol < tabular.columns() - 1 && scroll(false) &&
1317 tabular.getWidthOfTabular() < bv->workWidth()-20)
1321 } else if (the_locking_inset &&
1322 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1324 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1325 if (xx > bv->workWidth()-20) {
1326 scroll(bv, -(xx - bv->workWidth() + 60));
1328 } else if (xx < 20) {
1336 } else if (cursorx_ - offset > 20 &&
1337 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1338 > bv->workWidth() - 20) {
1339 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1341 } else if (cursorx_ - offset < 20) {
1342 scroll(bv, 20 - cursorx_ + offset);
1344 } else if (scroll() && top_x > 20 &&
1345 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1346 scroll(bv, old_x - cursorx_);
1349 if (the_locking_inset) {
1350 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1353 if ((!the_locking_inset ||
1354 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1355 actcell != oldcell) {
1356 InsetTabularMailer(*this).updateDialog(bv);
1363 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1365 if (lock && !old_locking_inset) {
1366 if (activateCellInset(bv))
1369 bool moved = isRightToLeft(bv)
1370 ? movePrevCell(bv) : moveNextCell(bv);
1372 return FINISHED_RIGHT;
1373 if (lock && activateCellInset(bv))
1377 return DISPATCHED_NOUPDATE;
1381 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1383 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1386 if (lock) { // behind the inset
1387 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1391 return DISPATCHED_NOUPDATE;
1395 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1397 int const ocell = actcell;
1398 actcell = tabular.getCellAbove(actcell);
1399 if (actcell == ocell) // we moved out of the inset
1405 if (old_locking_inset) {
1406 old_locking_inset->getCursorPos(bv, x, y);
1407 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1409 if (activateCellInset(bv, x, 0))
1412 return DISPATCHED_NOUPDATE;
1416 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1418 int const ocell = actcell;
1419 actcell = tabular.getCellBelow(actcell);
1420 if (actcell == ocell) // we moved out of the inset
1421 return FINISHED_DOWN;
1426 if (old_locking_inset) {
1427 old_locking_inset->getCursorPos(bv, x, y);
1428 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1430 if (activateCellInset(bv, x, 0))
1433 return DISPATCHED_NOUPDATE;
1437 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1439 if (isRightToLeft(bv)) {
1440 if (tabular.isFirstCellInRow(actcell)) {
1441 int row = tabular.row_of_cell(actcell);
1442 if (row == tabular.rows() - 1)
1444 actcell = tabular.getLastCellInRow(row);
1445 actcell = tabular.getCellBelow(actcell);
1452 if (tabular.isLastCell(actcell))
1457 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1458 isRightToLeftPar(bv->buffer()->params);
1459 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1466 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1468 if (isRightToLeft(bv)) {
1469 if (tabular.isLastCellInRow(actcell)) {
1470 int row = tabular.row_of_cell(actcell);
1473 actcell = tabular.getFirstCellInRow(row);
1474 actcell = tabular.getCellAbove(actcell);
1476 if (tabular.isLastCell(actcell))
1481 if (!actcell) // first cell
1486 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1487 isRightToLeftPar(bv->buffer()->params);
1488 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1495 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1499 setSelection(0, tabular.getNumberOfCells() - 1);
1501 if (hasSelection()) {
1502 recordUndo(bv, Undo::ATOMIC);
1503 bool const frozen = undo_frozen;
1506 // apply the fontchange on the whole selection
1511 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1512 for(int i = sel_row_start; i <= sel_row_end; ++i)
1513 for(int j = sel_col_start; j <= sel_col_end; ++j)
1514 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1522 if (the_locking_inset)
1523 the_locking_inset->setFont(bv, font, tall);
1527 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1529 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1532 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1533 string const tmp = tabularFeature[i].feature;
1535 if (tmp == what.substr(0, tmp.length())) {
1536 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1537 //tabularFeatures[i].feature.length())) {
1538 action = tabularFeature[i].action;
1542 if (action == LyXTabular::LAST_ACTION)
1546 ltrim(what.substr(tabularFeature[i].feature.length()));
1547 tabularFeatures(bv, action, val);
1553 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1554 string const & special, bool & flag)
1556 if (special == "dl_above") {
1559 } else if (special == "dl_below") {
1560 ltt.bottomDL = flag;
1562 } else if (special == "empty") {
1574 void InsetTabular::tabularFeatures(BufferView * bv,
1575 LyXTabular::Feature feature,
1576 string const & value)
1582 bool setLines = false;
1583 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1584 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1588 case LyXTabular::M_ALIGN_LEFT:
1589 case LyXTabular::ALIGN_LEFT:
1590 setAlign = LYX_ALIGN_LEFT;
1593 case LyXTabular::M_ALIGN_RIGHT:
1594 case LyXTabular::ALIGN_RIGHT:
1595 setAlign = LYX_ALIGN_RIGHT;
1598 case LyXTabular::M_ALIGN_CENTER:
1599 case LyXTabular::ALIGN_CENTER:
1600 setAlign = LYX_ALIGN_CENTER;
1603 case LyXTabular::ALIGN_BLOCK:
1604 setAlign = LYX_ALIGN_BLOCK;
1607 case LyXTabular::M_VALIGN_TOP:
1608 case LyXTabular::VALIGN_TOP:
1609 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1612 case LyXTabular::M_VALIGN_BOTTOM:
1613 case LyXTabular::VALIGN_BOTTOM:
1614 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1617 case LyXTabular::M_VALIGN_CENTER:
1618 case LyXTabular::VALIGN_CENTER:
1619 setVAlign = LyXTabular::LYX_VALIGN_CENTER;
1626 if (hasSelection()) {
1627 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1629 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1630 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1632 recordUndo(bv, Undo::ATOMIC);
1634 int row = tabular.row_of_cell(actcell);
1635 int column = tabular.column_of_cell(actcell);
1637 LyXTabular::ltType ltt;
1641 case LyXTabular::SET_PWIDTH:
1643 LyXLength const vallen(value);
1644 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1646 bool const update = (tmplen != vallen);
1647 tabular.setColumnPWidth(actcell, vallen);
1649 // We need this otherwise we won't resize
1650 // the insettext of the active cell (if any)
1651 // until later (see InsetText::do_resize)
1652 unlockInsetInInset(bv, the_locking_inset);
1654 for (int i = 0; i < tabular.rows(); ++i)
1655 tabular.getCellInset(i, column).resizeLyXText(bv);
1660 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1661 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1662 else if (!vallen.zero()
1663 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1664 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1668 case LyXTabular::SET_MPWIDTH:
1670 LyXLength const vallen(value);
1671 LyXLength const & tmplen = tabular.getPWidth(actcell);
1673 bool const update = (tmplen != vallen);
1674 tabular.setMColumnPWidth(actcell, vallen);
1676 // We need this otherwise we won't resize
1677 // the insettext of the active cell (if any)
1678 // until later (see InsetText::do_resize)
1679 unlockInsetInInset(bv, the_locking_inset);
1681 for (int i = 0; i < tabular.rows(); ++i)
1682 tabular.getCellInset(i, column).resizeLyXText(bv);
1688 case LyXTabular::SET_SPECIAL_COLUMN:
1689 case LyXTabular::SET_SPECIAL_MULTI:
1690 tabular.setAlignSpecial(actcell,value,feature);
1693 case LyXTabular::APPEND_ROW:
1694 // append the row into the tabular
1695 unlockInsetInInset(bv, the_locking_inset);
1696 tabular.appendRow(bv->buffer()->params, actcell);
1699 case LyXTabular::APPEND_COLUMN:
1700 // append the column into the tabular
1701 unlockInsetInInset(bv, the_locking_inset);
1702 tabular.appendColumn(bv->buffer()->params, actcell);
1703 actcell = tabular.getCellNumber(row, column);
1706 case LyXTabular::DELETE_ROW:
1707 unlockInsetInInset(bv, the_locking_inset);
1708 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1709 tabular.deleteRow(sel_row_start);
1711 if (sel_row_start >= tabular.rows())
1713 actcell = tabular.getCellNumber(sel_row_start, column);
1717 case LyXTabular::DELETE_COLUMN:
1718 unlockInsetInInset(bv, the_locking_inset);
1719 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1720 tabular.deleteColumn(sel_col_start);
1722 if (sel_col_start >= tabular.columns())
1724 actcell = tabular.getCellNumber(row, sel_col_start);
1728 case LyXTabular::M_TOGGLE_LINE_TOP:
1730 case LyXTabular::TOGGLE_LINE_TOP:
1732 bool lineSet = !tabular.topLine(actcell, flag);
1733 for (int i = sel_row_start; i <= sel_row_end; ++i)
1734 for (int j = sel_col_start; j <= sel_col_end; ++j)
1736 tabular.getCellNumber(i, j),
1742 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1744 case LyXTabular::TOGGLE_LINE_BOTTOM:
1746 bool lineSet = !tabular.bottomLine(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.setBottomLine(
1750 tabular.getCellNumber(i, j),
1757 case LyXTabular::M_TOGGLE_LINE_LEFT:
1759 case LyXTabular::TOGGLE_LINE_LEFT:
1761 bool lineSet = !tabular.leftLine(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.setLeftLine(
1765 tabular.getCellNumber(i,j),
1772 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1774 case LyXTabular::TOGGLE_LINE_RIGHT:
1776 bool lineSet = !tabular.rightLine(actcell, flag);
1777 for (int i = sel_row_start; i <= sel_row_end; ++i)
1778 for (int j = sel_col_start; j <= sel_col_end; ++j)
1779 tabular.setRightLine(
1780 tabular.getCellNumber(i,j),
1787 case LyXTabular::M_ALIGN_LEFT:
1788 case LyXTabular::M_ALIGN_RIGHT:
1789 case LyXTabular::M_ALIGN_CENTER:
1791 case LyXTabular::ALIGN_LEFT:
1792 case LyXTabular::ALIGN_RIGHT:
1793 case LyXTabular::ALIGN_CENTER:
1794 case LyXTabular::ALIGN_BLOCK:
1795 for (int i = sel_row_start; i <= sel_row_end; ++i)
1796 for (int j = sel_col_start; j <= sel_col_end; ++j)
1797 tabular.setAlignment(
1798 tabular.getCellNumber(i, j),
1804 case LyXTabular::M_VALIGN_TOP:
1805 case LyXTabular::M_VALIGN_BOTTOM:
1806 case LyXTabular::M_VALIGN_CENTER:
1808 case LyXTabular::VALIGN_TOP:
1809 case LyXTabular::VALIGN_BOTTOM:
1810 case LyXTabular::VALIGN_CENTER:
1811 for (int i = sel_row_start; i <= sel_row_end; ++i)
1812 for (int j = sel_col_start; j <= sel_col_end; ++j)
1813 tabular.setVAlignment(
1814 tabular.getCellNumber(i, j),
1819 case LyXTabular::MULTICOLUMN: {
1820 if (sel_row_start != sel_row_end) {
1821 #ifdef WITH_WARNINGS
1822 #warning Need I say it ? This is horrible.
1824 Alert::error(_("Error setting multicolumn"),
1825 _("You cannot set multicolumn vertically."));
1828 // just multicol for one Single Cell
1829 if (!hasSelection()) {
1830 // check wether we are completly in a multicol
1831 if (tabular.isMultiColumn(actcell))
1832 tabular.unsetMultiColumn(actcell);
1834 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1838 // we have a selection so this means we just add all this
1839 // cells to form a multicolumn cell
1843 if (sel_cell_start > sel_cell_end) {
1844 s_start = sel_cell_end;
1845 s_end = sel_cell_start;
1847 s_start = sel_cell_start;
1848 s_end = sel_cell_end;
1850 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1857 case LyXTabular::SET_ALL_LINES:
1859 case LyXTabular::UNSET_ALL_LINES:
1860 for (int i = sel_row_start; i <= sel_row_end; ++i)
1861 for (int j = sel_col_start; j <= sel_col_end; ++j)
1862 tabular.setAllLines(
1863 tabular.getCellNumber(i,j), setLines);
1867 case LyXTabular::SET_LONGTABULAR:
1868 tabular.setLongTabular(true);
1869 updateLocal(bv); // because this toggles displayed
1872 case LyXTabular::UNSET_LONGTABULAR:
1873 tabular.setLongTabular(false);
1874 updateLocal(bv); // because this toggles displayed
1877 case LyXTabular::SET_ROTATE_TABULAR:
1878 tabular.setRotateTabular(true);
1881 case LyXTabular::UNSET_ROTATE_TABULAR:
1882 tabular.setRotateTabular(false);
1885 case LyXTabular::SET_ROTATE_CELL:
1886 for (int i = sel_row_start; i <= sel_row_end; ++i)
1887 for (int j = sel_col_start; j<=sel_col_end; ++j)
1888 tabular.setRotateCell(
1889 tabular.getCellNumber(i, j), true);
1892 case LyXTabular::UNSET_ROTATE_CELL:
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.setRotateCell(
1896 tabular.getCellNumber(i, j), false);
1899 case LyXTabular::SET_USEBOX: {
1900 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1901 if (val == tabular.getUsebox(actcell))
1902 val = LyXTabular::BOX_NONE;
1903 for (int i = sel_row_start; i <= sel_row_end; ++i)
1904 for (int j = sel_col_start; j <= sel_col_end; ++j)
1905 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1909 case LyXTabular::UNSET_LTFIRSTHEAD:
1911 case LyXTabular::SET_LTFIRSTHEAD:
1912 tabular.getRowOfLTFirstHead(row, ltt);
1913 checkLongtableSpecial(ltt, value, flag);
1914 tabular.setLTHead(row, flag, ltt, true);
1917 case LyXTabular::UNSET_LTHEAD:
1919 case LyXTabular::SET_LTHEAD:
1920 tabular.getRowOfLTHead(row, ltt);
1921 checkLongtableSpecial(ltt, value, flag);
1922 tabular.setLTHead(row, flag, ltt, false);
1925 case LyXTabular::UNSET_LTFOOT:
1927 case LyXTabular::SET_LTFOOT:
1928 tabular.getRowOfLTFoot(row, ltt);
1929 checkLongtableSpecial(ltt, value, flag);
1930 tabular.setLTFoot(row, flag, ltt, false);
1933 case LyXTabular::UNSET_LTLASTFOOT:
1935 case LyXTabular::SET_LTLASTFOOT:
1936 tabular.getRowOfLTLastFoot(row, ltt);
1937 checkLongtableSpecial(ltt, value, flag);
1938 tabular.setLTFoot(row, flag, ltt, true);
1941 case LyXTabular::SET_LTNEWPAGE: {
1942 bool what = !tabular.getLTNewPage(row);
1943 tabular.setLTNewPage(row, what);
1947 // dummy stuff just to avoid warnings
1948 case LyXTabular::LAST_ACTION:
1952 InsetTabularMailer(*this).updateDialog(bv);
1956 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1957 mouse_button::state button, bool behind)
1959 UpdatableInset & inset = tabular.getCellInset(actcell);
1962 x = inset.x() + inset.width();
1963 y = inset.descent();
1965 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1966 //inset_y = cursory_;
1967 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1968 if (!the_locking_inset)
1971 return the_locking_inset;
1975 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1976 mouse_button::state button)
1978 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1980 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1984 bool InsetTabular::insetHit(BufferView *, int x, int) const
1986 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1990 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1992 resizeLyXText(bv, recursive);
1996 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
1999 for (int i = 0; i < tabular.rows(); ++i)
2000 for (int j = 0; j < tabular.columns(); ++j)
2001 tabular.getCellInset(i, j).resizeLyXText(bv, true);
2005 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2006 bool const recursive) const
2008 if (the_locking_inset)
2009 return the_locking_inset->getLyXText(bv, recursive);
2010 return InsetOld::getLyXText(bv, recursive);
2014 bool InsetTabular::showInsetDialog(BufferView * bv) const
2016 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2017 InsetTabularMailer(*this).showDialog(bv);
2022 void InsetTabular::openLayoutDialog(BufferView * bv) const
2024 if (the_locking_inset) {
2025 InsetTabular * inset = static_cast<InsetTabular *>
2026 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2028 inset->openLayoutDialog(bv);
2032 InsetTabularMailer(*this).showDialog(bv);
2037 // function returns an object as defined in func_status.h:
2038 // states OK, Unknown, Disabled, On, Off.
2040 FuncStatus InsetTabular::getStatus(string const & what) const
2042 int action = LyXTabular::LAST_ACTION;
2046 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2047 string const tmp = tabularFeature[i].feature;
2048 if (tmp == what.substr(0, tmp.length())) {
2049 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2050 // tabularFeatures[i].feature.length())) {
2051 action = tabularFeature[i].action;
2055 if (action == LyXTabular::LAST_ACTION) {
2057 return status.unknown(true);
2060 string const argument
2061 = ltrim(what.substr(tabularFeature[i].feature.length()));
2066 LyXTabular::ltType dummyltt;
2070 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2072 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2075 case LyXTabular::SET_PWIDTH:
2076 case LyXTabular::SET_MPWIDTH:
2077 case LyXTabular::SET_SPECIAL_COLUMN:
2078 case LyXTabular::SET_SPECIAL_MULTI:
2079 case LyXTabular::APPEND_ROW:
2080 case LyXTabular::APPEND_COLUMN:
2081 case LyXTabular::DELETE_ROW:
2082 case LyXTabular::DELETE_COLUMN:
2083 case LyXTabular::SET_ALL_LINES:
2084 case LyXTabular::UNSET_ALL_LINES:
2085 return status.clear();
2087 case LyXTabular::MULTICOLUMN:
2088 status.setOnOff(tabular.isMultiColumn(actcell));
2091 case LyXTabular::M_TOGGLE_LINE_TOP:
2093 case LyXTabular::TOGGLE_LINE_TOP:
2094 status.setOnOff(tabular.topLine(actcell, flag));
2097 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2099 case LyXTabular::TOGGLE_LINE_BOTTOM:
2100 status.setOnOff(tabular.bottomLine(actcell, flag));
2103 case LyXTabular::M_TOGGLE_LINE_LEFT:
2105 case LyXTabular::TOGGLE_LINE_LEFT:
2106 status.setOnOff(tabular.leftLine(actcell, flag));
2109 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2111 case LyXTabular::TOGGLE_LINE_RIGHT:
2112 status.setOnOff(tabular.rightLine(actcell, flag));
2115 case LyXTabular::M_ALIGN_LEFT:
2117 case LyXTabular::ALIGN_LEFT:
2118 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2121 case LyXTabular::M_ALIGN_RIGHT:
2123 case LyXTabular::ALIGN_RIGHT:
2124 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2127 case LyXTabular::M_ALIGN_CENTER:
2129 case LyXTabular::ALIGN_CENTER:
2130 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2133 case LyXTabular::ALIGN_BLOCK:
2134 status.disabled(tabular.getPWidth(actcell).zero());
2135 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2138 case LyXTabular::M_VALIGN_TOP:
2140 case LyXTabular::VALIGN_TOP:
2141 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2144 case LyXTabular::M_VALIGN_BOTTOM:
2146 case LyXTabular::VALIGN_BOTTOM:
2147 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2150 case LyXTabular::M_VALIGN_CENTER:
2152 case LyXTabular::VALIGN_CENTER:
2153 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2156 case LyXTabular::SET_LONGTABULAR:
2157 status.setOnOff(tabular.isLongTabular());
2160 case LyXTabular::UNSET_LONGTABULAR:
2161 status.setOnOff(!tabular.isLongTabular());
2164 case LyXTabular::SET_ROTATE_TABULAR:
2165 status.setOnOff(tabular.getRotateTabular());
2168 case LyXTabular::UNSET_ROTATE_TABULAR:
2169 status.setOnOff(!tabular.getRotateTabular());
2172 case LyXTabular::SET_ROTATE_CELL:
2173 status.setOnOff(tabular.getRotateCell(actcell));
2176 case LyXTabular::UNSET_ROTATE_CELL:
2177 status.setOnOff(!tabular.getRotateCell(actcell));
2180 case LyXTabular::SET_USEBOX:
2181 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2184 case LyXTabular::SET_LTFIRSTHEAD:
2185 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2188 case LyXTabular::SET_LTHEAD:
2189 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2192 case LyXTabular::SET_LTFOOT:
2193 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2196 case LyXTabular::SET_LTLASTFOOT:
2197 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2200 case LyXTabular::SET_LTNEWPAGE:
2201 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2206 status.disabled(true);
2213 void InsetTabular::getLabelList(std::vector<string> & list) const
2215 tabular.getLabelList(list);
2219 bool InsetTabular::copySelection(BufferView * bv)
2221 if (!hasSelection())
2224 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2225 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2226 if (sel_col_start > sel_col_end) {
2227 sel_col_start = sel_col_end;
2228 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2230 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2233 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2234 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2235 if (sel_row_start > sel_row_end)
2236 swap(sel_row_start, sel_row_end);
2238 delete paste_tabular;
2239 paste_tabular = new LyXTabular(bv->buffer()->params, this, tabular);
2241 for (int i = 0; i < sel_row_start; ++i)
2242 paste_tabular->deleteRow(0);
2244 int const rows = sel_row_end - sel_row_start + 1;
2245 while (paste_tabular->rows() > rows)
2246 paste_tabular->deleteRow(rows);
2248 paste_tabular->setTopLine(0, true, true);
2249 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2252 for (int i = 0; i < sel_col_start; ++i)
2253 paste_tabular->deleteColumn(0);
2255 int const columns = sel_col_end - sel_col_start + 1;
2256 while (paste_tabular->columns() > columns)
2257 paste_tabular->deleteColumn(columns);
2259 paste_tabular->setLeftLine(0, true, true);
2260 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2264 paste_tabular->ascii(bv->buffer(), os,
2265 (int)parOwner()->params().depth(), true, '\t');
2266 bv->stuffClipboard(STRCONV(os.str()));
2271 bool InsetTabular::pasteSelection(BufferView * bv)
2276 for (int r1 = 0, r2 = actrow;
2277 r1 < paste_tabular->rows() && r2 < tabular.rows();
2279 for (int c1 = 0, c2 = actcol;
2280 c1 < paste_tabular->columns() && c2 < tabular.columns();
2282 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2283 tabular.isPartOfMultiColumn(r2, c2))
2285 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2289 if (tabular.isPartOfMultiColumn(r2, c2)) {
2293 InsetText & inset = tabular.getCellInset(r2, c2);
2294 inset = paste_tabular->getCellInset(r1, c1);
2295 inset.setOwner(this);
2296 inset.deleteLyXText(bv);
2304 bool InsetTabular::cutSelection(BufferParams const & bp)
2306 if (!hasSelection())
2309 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2310 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2311 if (sel_col_start > sel_col_end) {
2312 sel_col_start = sel_col_end;
2313 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2315 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2318 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2319 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2321 if (sel_row_start > sel_row_end)
2322 swap(sel_row_start, sel_row_end);
2324 if (sel_cell_start > sel_cell_end)
2325 swap(sel_cell_start, sel_cell_end);
2327 for (int i = sel_row_start; i <= sel_row_end; ++i)
2328 for (int j = sel_col_start; j <= sel_col_end; ++j)
2329 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2334 bool InsetTabular::isRightToLeft(BufferView * bv)
2336 return bv->getParentLanguage(this)->RightToLeft();
2340 int InsetTabular::scroll(bool recursive) const
2342 int sx = UpdatableInset::scroll(false);
2344 if (recursive && the_locking_inset)
2345 sx += the_locking_inset->scroll(recursive);
2351 void InsetTabular::getSelection(int & srow, int & erow,
2352 int & scol, int & ecol) const
2354 int const start = hasSelection() ? sel_cell_start : actcell;
2355 int const end = hasSelection() ? sel_cell_end : actcell;
2357 srow = tabular.row_of_cell(start);
2358 erow = tabular.row_of_cell(end);
2362 scol = tabular.column_of_cell(start);
2363 ecol = tabular.column_of_cell(end);
2367 ecol = tabular.right_column_of_cell(end);
2371 ParagraphList * InsetTabular::getParagraphs(int i) const
2373 return (i < tabular.getNumberOfCells())
2374 ? tabular.getCellInset(i).getParagraphs(0)
2379 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2381 if (the_locking_inset)
2382 return the_locking_inset->cursor(bv);
2383 return InsetOld::cursor(bv);
2387 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2390 return const_cast<InsetTabular *>(this);
2392 for (int i = 0; i < tabular.rows(); ++i) {
2393 for (int j = 0; j < tabular.columns(); ++j) {
2394 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2404 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2406 if (the_locking_inset) {
2407 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2408 if (!word.word().empty())
2410 if (tabular.isLastCell(actcell)) {
2411 bv->unlockInset(const_cast<InsetTabular *>(this));
2412 return WordLangTuple();
2416 // otherwise we have to lock the next inset and ask for it's selecttion
2417 tabular.getCellInset(actcell)
2418 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2419 WordLangTuple word(selectNextWordInt(bv, value));
2420 if (!word.word().empty())
2426 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2428 // when entering this function the inset should be ALWAYS locked!
2429 Assert(the_locking_inset);
2431 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2432 if (!word.word().empty())
2435 if (tabular.isLastCell(actcell)) {
2436 bv->unlockInset(const_cast<InsetTabular *>(this));
2437 return WordLangTuple();
2440 // otherwise we have to lock the next inset and ask for it's selecttion
2442 tabular.getCellInset(actcell)
2443 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2444 return selectNextWordInt(bv, value);
2448 void InsetTabular::selectSelectedWord(BufferView * bv)
2450 if (the_locking_inset)
2451 the_locking_inset->selectSelectedWord(bv);
2455 void InsetTabular::markErased()
2457 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2458 tabular.getCellInset(cell).markErased();
2462 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2464 if (the_locking_inset) {
2465 if (the_locking_inset->nextChange(bv, length)) {
2469 if (tabular.isLastCell(actcell))
2473 InsetText & inset = tabular.getCellInset(actcell);
2474 if (inset.nextChange(bv, length)) {
2478 while (!tabular.isLastCell(actcell)) {
2480 InsetText & inset = tabular.getCellInset(actcell);
2481 if (inset.nextChange(bv, length)) {
2490 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2494 if (the_locking_inset) {
2495 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2499 if (tabular.isLastCell(actcell))
2503 InsetText & inset = tabular.getCellInset(cell);
2504 if (inset.searchForward(bv, str, cs, mw)) {
2508 while (!tabular.isLastCell(cell)) {
2510 InsetText & inset = tabular.getCellInset(cell);
2511 if (inset.searchForward(bv, str, cs, mw)) {
2520 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2523 int cell = tabular.getNumberOfCells();
2524 if (the_locking_inset) {
2525 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2534 InsetText & inset = tabular.getCellInset(cell);
2535 if (inset.searchBackward(bv, str, cs, mw)) {
2544 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2546 if (the_locking_inset)
2547 return the_locking_inset->insetAllowed(code);
2548 // we return true here because if the inset is not locked someone
2549 // wants to insert something in one of our insettexts and we generally
2555 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2557 const int cell = tabular.getCellFromInset(in, actcell);
2560 return tabular.getPWidth(cell).zero();
2562 // well we didn't obviously find it so maybe our owner knows more
2564 return owner()->forceDefaultParagraphs(in);
2566 lyxerr << "If we're here there is really something strange going on!"
2572 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2575 if (buf.length() <= 0)
2581 string::size_type len = buf.length();
2582 string::size_type p = 0;
2584 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2592 maxCols = max(cols, maxCols);
2598 maxCols = max(cols, maxCols);
2599 LyXTabular * loctab;
2604 delete paste_tabular;
2605 paste_tabular = new LyXTabular(bv->buffer()->params,
2606 this, rows, maxCols);
2607 loctab = paste_tabular;
2616 string::size_type op = 0;
2617 int cells = loctab->getNumberOfCells();
2620 rows = loctab->rows();
2621 int const columns = loctab->columns();
2623 while (cell < cells && p < len && row < rows &&
2624 (p = buf.find_first_of("\t\n", p)) != string::npos)
2630 // we can only set this if we are not too far right
2631 if (cols < columns) {
2632 InsetText & inset = loctab->getCellInset(cell);
2633 LyXFont const font = inset.getLyXText(bv)->
2634 getFont(inset.paragraphs.begin(), 0);
2635 inset.setText(buf.substr(op, p - op), font);
2641 // we can only set this if we are not too far right
2642 if (cols < columns) {
2643 InsetText & inset = tabular.getCellInset(cell);
2644 LyXFont const font = inset.getLyXText(bv)->
2645 getFont(inset.paragraphs.begin(), 0);
2646 inset.setText(buf.substr(op, p - op), font);
2651 cell = loctab->getCellNumber(row, cols);
2657 // check for the last cell if there is no trailing '\n'
2658 if (cell < cells && op < len) {
2659 InsetText & inset = loctab->getCellInset(cell);
2660 LyXFont const font = inset.getLyXText(bv)->
2661 getFont(inset.paragraphs.begin(), 0);
2662 inset.setText(buf.substr(op, len - op), font);
2669 void InsetTabular::addPreview(PreviewLoader & loader) const
2671 int const rows = tabular.rows();
2672 int const columns = tabular.columns();
2673 for (int i = 0; i < rows; ++i)
2674 for (int j = 0; j < columns; ++j)
2675 tabular.getCellInset(i, j).addPreview(loader);
2679 string const InsetTabularMailer::name_("tabular");
2681 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2682 : inset_(const_cast<InsetTabular &>(inset))
2686 string const InsetTabularMailer::inset2string(Buffer const &) const
2688 return params2string(inset_);
2692 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2694 istringstream data(STRCONV(in));
2696 lex.setStream(data);
2698 #warning CHECK verify that this is a sane value to return.
2704 string const token = lex.getString();
2712 string const token = lex.getString();
2713 if (token != "\\active_cell")
2716 cell = lex.getInteger();
2719 // This is part of the inset proper that is usually swallowed
2720 // by Buffer::readInset
2723 string const token = lex.getString();
2724 if (token != "Tabular")
2731 Buffer const * const buffer = inset.buffer();
2733 inset.read(buffer, lex);
2735 // We can't set the active cell, but we can tell the frontend
2741 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2743 Buffer const * const buffer = inset.buffer();
2748 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2749 inset.write(buffer, data);
2750 data << "\\end_inset\n";
2751 return STRCONV(data.str());