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 "BufferView.h"
18 #include "funcrequest.h"
19 #include "FuncStatus.h"
24 #include "metricsinfo.h"
25 #include "paragraph_funcs.h"
26 #include "ParagraphParameters.h"
27 #include "undo_funcs.h"
28 #include "WordLangTuple.h"
30 #include "frontends/Alert.h"
31 #include "frontends/font_metrics.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/Painter.h"
35 #include "support/LAssert.h"
37 #include "support/std_sstream.h"
39 using namespace lyx::support;
40 using namespace lyx::graphics;
47 using std::istringstream;
49 using std::ostringstream;
55 int const ADD_TO_HEIGHT = 2;
56 int const ADD_TO_TABULAR_WIDTH = 2;
59 LyXTabular * paste_tabular = 0;
62 struct TabularFeature {
63 LyXTabular::Feature action;
68 TabularFeature tabularFeature[] =
70 { LyXTabular::APPEND_ROW, "append-row" },
71 { LyXTabular::APPEND_COLUMN, "append-column" },
72 { LyXTabular::DELETE_ROW, "delete-row" },
73 { LyXTabular::DELETE_COLUMN, "delete-column" },
74 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
75 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
76 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
77 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
78 { LyXTabular::ALIGN_LEFT, "align-left" },
79 { LyXTabular::ALIGN_RIGHT, "align-right" },
80 { LyXTabular::ALIGN_CENTER, "align-center" },
81 { LyXTabular::ALIGN_BLOCK, "align-block" },
82 { LyXTabular::VALIGN_TOP, "valign-top" },
83 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
84 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
85 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
86 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
87 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
88 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
89 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
90 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
91 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
92 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
93 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
94 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
95 { LyXTabular::MULTICOLUMN, "multicolumn" },
96 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
97 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
98 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
99 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
100 { LyXTabular::SET_PWIDTH, "set-pwidth" },
101 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
102 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
103 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
104 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
105 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
106 { LyXTabular::SET_USEBOX, "set-usebox" },
107 { LyXTabular::SET_LTHEAD, "set-lthead" },
108 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
109 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
110 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
111 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
112 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
113 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
114 { LyXTabular::LAST_ACTION, "" }
118 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
119 bool operator()(TabularFeature & tf)
121 return tf.action == feature_;
124 LyXTabular::Feature feature_;
130 string const featureAsString(LyXTabular::Feature feature)
132 TabularFeature * it = tabularFeature;
133 TabularFeature * end = it +
134 sizeof(tabularFeature) / sizeof(TabularFeature);
135 it = std::find_if(it, end, FindFeature(feature));
136 return (it == end) ? string() : it->feature;
140 bool InsetTabular::hasPasteBuffer() const
142 return (paste_tabular != 0);
146 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
147 : tabular(buf.params, this, max(rows, 1), max(columns, 1)),
148 buffer_(&buf), cursorx_(0), cursory_(0)
150 // for now make it always display as display() inset
152 the_locking_inset = 0;
153 old_locking_inset = 0;
156 actrow = actcell = 0;
164 InsetTabular::InsetTabular(InsetTabular const & tab)
165 : UpdatableInset(tab),
166 tabular(tab.buffer_->params, this, tab.tabular),
167 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
169 the_locking_inset = 0;
170 old_locking_inset = 0;
173 actrow = actcell = 0;
181 InsetTabular::~InsetTabular()
183 InsetTabularMailer(*this).hideDialog();
187 auto_ptr<InsetBase> InsetTabular::clone() const
189 return auto_ptr<InsetBase>(new InsetTabular(*this));
193 Buffer const & InsetTabular::buffer() const
199 BufferView * InsetTabular::view() const
206 void InsetTabular::buffer(Buffer * b)
212 void InsetTabular::write(Buffer const & buf, ostream & os) const
214 os << "Tabular" << endl;
215 tabular.write(buf, os);
219 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
221 bool const old_format = (lex.getString() == "\\LyXTable");
223 tabular.read(buf, lex);
229 string token = lex.getString();
230 while (lex.isOK() && (token != "\\end_inset")) {
232 token = lex.getString();
234 if (token != "\\end_inset") {
235 lex.printError("Missing \\end_inset at this point. "
241 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
243 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
244 // mi.base.textwidth << "\n";
246 lyxerr << "InsetTabular::metrics: need bv" << endl;
250 calculate_dimensions_of_cells(mi);
252 dim.asc = tabular.getAscentOfRow(0);
253 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
254 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
259 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
261 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
263 BufferView * bv = pi.base.bv;
266 UpdatableInset::draw(pi, x, y);
274 x += ADD_TO_TABULAR_WIDTH;
277 first_visible_cell = -1;
278 for (int i = 0; i < tabular.rows(); ++i) {
280 cell = tabular.getCellNumber(i, 0);
281 if (y + tabular.getDescentOfRow(i) <= 0 &&
282 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
284 y += tabular.getDescentOfRow(i) +
285 tabular.getAscentOfRow(i + 1) +
286 tabular.getAdditionalHeight(i + 1);
289 for (int j = 0; j < tabular.columns(); ++j) {
290 if (nx > bv->workWidth())
292 if (tabular.isPartOfMultiColumn(i, j))
294 int cx = nx + tabular.getBeginningOfTextInCell(cell);
295 if (first_visible_cell < 0)
296 first_visible_cell = cell;
297 if (hasSelection()) {
298 drawCellSelection(pi.pain, nx, y, i, j, cell);
301 tabular.getCellInset(cell).draw(pi, cx, y);
302 drawCellLines(pi.pain, nx, y, i, cell);
303 nx += tabular.getWidthOfColumn(cell);
307 // Would be nice, but for some completely unfathomable reason,
308 // on a col resize to a new fixed width, even though the insettexts
309 // are resized, the cell isn't, but drawing all cells in a tall table
310 // has the desired effect somehow. Complete dark magic.
312 // avoiding drawing the rest of a long table is
313 // a pretty big speedup
314 if (y > bv->workHeight())
318 y += tabular.getDescentOfRow(i) +
319 tabular.getAscentOfRow(i + 1) +
320 tabular.getAdditionalHeight(i + 1);
325 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
326 int row, int cell) const
328 int x2 = x + tabular.getWidthOfColumn(cell);
331 if (!tabular.topAlreadyDrawn(cell)) {
332 on_off = !tabular.topLine(cell);
333 pain.line(x, y - tabular.getAscentOfRow(row),
334 x2, y - tabular.getAscentOfRow(row),
335 on_off ? LColor::tabularonoffline : LColor::tabularline,
336 on_off ? Painter::line_onoffdash : Painter::line_solid);
338 on_off = !tabular.bottomLine(cell);
339 pain.line(x, y + tabular.getDescentOfRow(row),
340 x2, y + tabular.getDescentOfRow(row),
341 on_off ? LColor::tabularonoffline : LColor::tabularline,
342 on_off ? Painter::line_onoffdash : Painter::line_solid);
343 if (!tabular.leftAlreadyDrawn(cell)) {
344 on_off = !tabular.leftLine(cell);
345 pain.line(x, y - tabular.getAscentOfRow(row),
346 x, y + tabular.getDescentOfRow(row),
347 on_off ? LColor::tabularonoffline : LColor::tabularline,
348 on_off ? Painter::line_onoffdash : Painter::line_solid);
350 on_off = !tabular.rightLine(cell);
351 pain.line(x2 - tabular.getAdditionalWidth(cell),
352 y - tabular.getAscentOfRow(row),
353 x2 - tabular.getAdditionalWidth(cell),
354 y + tabular.getDescentOfRow(row),
355 on_off ? LColor::tabularonoffline : LColor::tabularline,
356 on_off ? Painter::line_onoffdash : Painter::line_solid);
360 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
361 int row, int column, int cell) const
363 Assert(hasSelection());
364 int cs = tabular.column_of_cell(sel_cell_start);
365 int ce = tabular.column_of_cell(sel_cell_end);
368 cs = tabular.column_of_cell(sel_cell_end);
370 ce = tabular.right_column_of_cell(sel_cell_end);
373 int rs = tabular.row_of_cell(sel_cell_start);
374 int re = tabular.row_of_cell(sel_cell_end);
378 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
379 int w = tabular.getWidthOfColumn(cell);
380 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
381 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
382 w, h, LColor::selection);
387 string const InsetTabular::editMessage() const
389 return _("Opened table");
393 void InsetTabular::insetUnlock(BufferView * bv)
395 if (the_locking_inset) {
396 the_locking_inset->insetUnlock(bv);
398 the_locking_inset = 0;
403 if (scroll(false) || hasSelection()) {
412 void InsetTabular::updateLocal(BufferView * bv) const
414 bv->updateInset(this);
420 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
422 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
427 if (inset == &tabular.getCellInset(actcell)) {
428 lyxerr[Debug::INSETTEXT] << "OK" << endl;
429 the_locking_inset = &tabular.getCellInset(actcell);
432 } else if (!the_locking_inset) {
433 int const n = tabular.getNumberOfCells();
434 int const id = inset->id();
435 for (int i = 0; i < n; ++i) {
436 InsetText * in = &tabular.getCellInset(i);
439 the_locking_inset = in;
444 if (in->getInsetFromID(id)) {
446 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
447 return the_locking_inset->lockInsetInInset(bv, inset);
450 } else if (the_locking_inset && (the_locking_inset == inset)) {
451 lyxerr[Debug::INSETTEXT] << "OK" << endl;
453 } else if (the_locking_inset) {
454 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
455 return the_locking_inset->lockInsetInInset(bv, inset);
457 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
462 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
465 if (!the_locking_inset)
467 if (the_locking_inset == inset) {
468 the_locking_inset->insetUnlock(bv);
470 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
477 // this has to be here otherwise we don't redraw the cell!
478 the_locking_inset = 0;
481 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
482 if (inset->lyxCode() == TABULAR_CODE &&
483 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
484 InsetTabularMailer(*this).updateDialog(bv);
493 int InsetTabular::insetInInsetY() const
495 if (!the_locking_inset)
497 return inset_y + the_locking_inset->insetInInsetY();
501 UpdatableInset * InsetTabular::getLockingInset() const
503 return the_locking_inset ? the_locking_inset->getLockingInset() :
504 const_cast<InsetTabular *>(this);
508 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
512 if (the_locking_inset)
513 return the_locking_inset->getFirstLockingInsetOfType(c);
518 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
520 if (the_locking_inset)
521 return the_locking_inset->insertInset(bv, inset);
526 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
528 if (hasSelection() && cmd.button() == mouse_button::button3)
531 if (hasSelection()) {
533 updateLocal(cmd.view());
536 int const ocell = actcell;
537 BufferView * bv = cmd.view();
541 the_locking_inset = 0;
545 setPos(bv, cmd.x, cmd.y);
548 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
550 if ((ocell == actcell) && the_locking_inset && inset_hit) {
552 FuncRequest cmd1 = cmd;
555 the_locking_inset->localDispatch(cmd1);
559 if (the_locking_inset) {
560 the_locking_inset->insetUnlock(bv);
562 the_locking_inset = 0;
565 if (cmd.button() == mouse_button::button2) {
566 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
570 if (inset_hit && bv->theLockingInset()) {
571 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
572 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
575 FuncRequest cmd1 = cmd;
578 the_locking_inset->localDispatch(cmd1);
583 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
586 if (the_locking_inset) {
587 FuncRequest cmd1 = cmd;
590 ret = the_locking_inset->localDispatch(cmd1);
592 if (cmd.button() == mouse_button::button3 && !ret) {
593 InsetTabularMailer(*this).showDialog(cmd.view());
600 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
602 if (the_locking_inset) {
603 FuncRequest cmd1 = cmd;
606 the_locking_inset->localDispatch(cmd1);
610 BufferView * bv = cmd.view();
611 int const old_cell = actcell;
613 setPos(bv, cmd.x, cmd.y);
614 if (!hasSelection()) {
615 setSelection(actcell, actcell);
617 } else if (old_cell != actcell) {
618 setSelection(sel_cell_start, actcell);
624 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
626 // We need to save the value of the_locking_inset as the call to
627 // the_locking_inset->localDispatch might unlock it.
628 old_locking_inset = the_locking_inset;
629 RESULT result = UpdatableInset::localDispatch(cmd);
630 BufferView * bv = cmd.view();
632 if (cmd.action == LFUN_INSET_EDIT) {
634 if (!bv->lockInset(this)) {
635 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
641 the_locking_inset = 0;
645 if (cmd.argument.size()) {
646 if (cmd.argument == "left") {
647 if (isRightToLeft(bv))
648 actcell = tabular.getLastCellInRow(0);
652 if (isRightToLeft(bv))
653 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
655 actcell = tabular.getNumberOfCells() - 1;
663 setPos(bv, cmd.x, cmd.y);
666 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
667 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
673 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
678 if (cmd.action < 0 && cmd.argument.empty())
681 bool hs = hasSelection();
684 // this one have priority over the locked InsetText, if we're not already
685 // inside another tabular then that one get's priority!
686 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
687 switch (cmd.action) {
688 case LFUN_MOUSE_PRESS:
692 case LFUN_MOUSE_MOTION:
693 lfunMouseMotion(cmd);
696 case LFUN_MOUSE_RELEASE:
697 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
699 case LFUN_CELL_BACKWARD:
700 case LFUN_CELL_FORWARD:
701 unlockInsetInInset(bv, the_locking_inset);
702 if (cmd.action == LFUN_CELL_FORWARD)
703 moveNextCell(bv, old_locking_inset != 0);
705 movePrevCell(bv, old_locking_inset != 0);
709 if (!the_locking_inset)
710 return DISPATCHED_NOUPDATE;
712 // this to avoid compiler warnings.
718 kb_action action = cmd.action;
719 string arg = cmd.argument;
720 if (the_locking_inset) {
721 result = the_locking_inset->localDispatch(cmd);
722 if (result == DISPATCHED_NOUPDATE) {
725 if (sc != scroll()) { // inset has been scrolled
729 } else if (result == DISPATCHED) {
732 } else if (result == FINISHED_UP) {
734 // Make sure to reset status message after
735 // exiting, e.g. math inset
736 bv->owner()->clearMessage();
737 } else if (result == FINISHED_DOWN) {
739 bv->owner()->clearMessage();
740 } else if (result == FINISHED_RIGHT) {
742 bv->owner()->clearMessage();
743 } else if (result == FINISHED) {
744 bv->owner()->clearMessage();
750 // --- Cursor Movements ----------------------------------
751 case LFUN_RIGHTSEL: {
752 int const start = hasSelection() ? sel_cell_start : actcell;
753 if (tabular.isLastCellInRow(actcell)) {
754 setSelection(start, actcell);
759 // if we are starting a selection, only select
760 // the current cell at the beginning
761 if (hasSelection()) {
762 moveRight(bv, false);
765 setSelection(start, end);
770 result = moveRight(bv);
776 int const start = hasSelection() ? sel_cell_start : actcell;
777 if (tabular.isFirstCellInRow(actcell)) {
778 setSelection(start, actcell);
783 // if we are starting a selection, only select
784 // the current cell at the beginning
785 if (hasSelection()) {
789 setSelection(start, end);
794 result = moveLeft(bv);
800 int const start = hasSelection() ? sel_cell_start : actcell;
801 int const ocell = actcell;
802 // if we are starting a selection, only select
803 // the current cell at the beginning
804 if (hasSelection()) {
806 if (ocell == sel_cell_end ||
807 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
808 setSelection(start, tabular.getCellBelow(sel_cell_end));
810 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
812 setSelection(start, start);
818 result = moveDown(bv, old_locking_inset != 0);
824 int const start = hasSelection() ? sel_cell_start : actcell;
825 int const ocell = actcell;
826 // if we are starting a selection, only select
827 // the current cell at the beginning
828 if (hasSelection()) {
830 if ((ocell == sel_cell_end) ||
831 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
832 setSelection(start, tabular.getCellAbove(sel_cell_end));
834 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
836 setSelection(start, start);
842 result = moveUp(bv, old_locking_inset != 0);
851 unlockInsetInInset(bv, the_locking_inset);
852 if (bv->top_y() + bv->painter().paperHeight() <
853 top_baseline + tabular.getHeightOfTabular())
855 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
856 actcell = tabular.getCellBelow(first_visible_cell) + column;
858 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
868 unlockInsetInInset(bv, the_locking_inset);
869 if (top_baseline < 0) {
870 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
871 if (top_baseline > 0)
874 actcell = tabular.getCellBelow(first_visible_cell) + column;
882 // none of these make sense for insettabular,
883 // but we must catch them to prevent any
884 // selection from being confused
888 case LFUN_WORDLEFTSEL:
890 case LFUN_WORDRIGHTSEL:
892 case LFUN_DOWN_PARAGRAPH:
893 case LFUN_DOWN_PARAGRAPHSEL:
894 case LFUN_UP_PARAGRAPH:
895 case LFUN_UP_PARAGRAPHSEL:
901 case LFUN_BEGINNINGBUF:
902 case LFUN_BEGINNINGBUFSEL:
906 case LFUN_LAYOUT_TABULAR: {
907 InsetTabularMailer(*this).showDialog(bv);
910 case LFUN_INSET_DIALOG_UPDATE: {
911 InsetTabularMailer(*this).updateDialog(bv);
914 case LFUN_TABULAR_FEATURE:
915 if (!tabularFeatures(bv, arg))
916 result = UNDISPATCHED;
918 // insert file functions
919 case LFUN_FILE_INSERT_ASCII_PARA:
920 case LFUN_FILE_INSERT_ASCII:
922 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
925 if (insertAsciiString(bv, tmpstr, false))
928 result = UNDISPATCHED;
931 // cut and paste functions
933 if (!copySelection(bv))
937 recordUndo(bv, Undo::DELETE);
938 cutSelection(bv->buffer()->params);
947 case LFUN_PASTESELECTION:
949 string const clip(bv->getClipboard());
953 if (clip.find('\t') != string::npos) {
957 string::size_type len = clip.length();
958 string::size_type p = 0;
961 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
969 maxCols = max(cols, maxCols);
975 maxCols = max(cols, maxCols);
976 delete paste_tabular;
977 paste_tabular = new LyXTabular(bv->buffer()->params,
978 this, rows, maxCols);
979 string::size_type op = 0;
981 int cells = paste_tabular->getNumberOfCells();
983 while ((cell < cells) && (p < len) &&
984 (p = clip.find_first_of("\t\n", p)) != string::npos) {
989 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
994 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
995 while (cols++ < maxCols)
1003 // check for the last cell if there is no trailing '\n'
1004 if ((cell < cells) && (op < len))
1005 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1008 if (!insertAsciiString(bv, clip, true))
1011 // so that the clipboard is used and it goes on
1013 // and executes LFUN_PASTESELECTION in insettext!
1014 delete paste_tabular;
1019 if (hasPasteBuffer()) {
1020 recordUndo(bv, Undo::INSERT);
1025 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1027 // handle font changing stuff on selection before we lock the inset
1028 // in the default part!
1029 result = UNDISPATCHED;
1040 case LFUN_UNDERLINE:
1041 case LFUN_FONT_SIZE:
1042 if (bv->dispatch(FuncRequest(bv, action, arg)))
1043 result = DISPATCHED;
1049 // we try to activate the actual inset and put this event down to
1050 // the insets dispatch function.
1051 if (result == DISPATCHED || the_locking_inset)
1053 if (activateCellInset(bv)) {
1054 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1055 if (result == UNDISPATCHED || result >= FINISHED) {
1056 unlockInsetInInset(bv, the_locking_inset);
1057 // we need to update if this was requested before
1059 return UNDISPATCHED;
1068 if (result < FINISHED) {
1069 if (!the_locking_inset && bv->fitCursor())
1072 bv->unlockInset(this);
1077 int InsetTabular::latex(Buffer const & buf, ostream & os,
1078 LatexRunParams const & runparams) const
1080 return tabular.latex(buf, os, runparams);
1084 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1087 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1089 return tabular.ascii(buf, os, 0, false, 0);
1093 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1095 return tabular.linuxdoc(buf,os);
1099 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1104 // if the table is inside a float it doesn't need the informaltable
1105 // wrapper. Search for it.
1106 for (master = owner();
1107 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1108 master = master->owner());
1111 os << "<informaltable>";
1116 ret += tabular.docbook(buf, os, mixcont);
1118 os << "</informaltable>";
1127 void InsetTabular::validate(LaTeXFeatures & features) const
1129 tabular.validate(features);
1133 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1136 // if we have a locking_inset we should have to check only this cell for
1137 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1138 // This is _really_ important (André)
1139 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1142 for (int j = 0; j < tabular.columns(); ++j) {
1146 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1147 tabular.getCellInset(actrow, j).metrics(m, dim);
1148 maxAsc = max(dim.asc, maxAsc);
1149 maxDesc = max(dim.des, maxDesc);
1151 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1152 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1153 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1159 for (int i = 0; i < tabular.rows(); ++i) {
1162 for (int j = 0; j < tabular.columns(); ++j) {
1163 if (tabular.isPartOfMultiColumn(i, j))
1169 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1170 tabular.getCellInset(cell).metrics(m, dim);
1171 maxAsc = max(maxAsc, dim.asc);
1172 maxDesc = max(maxDesc, dim.des);
1173 tabular.setWidthOfCell(cell, dim.wid);
1175 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1176 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1181 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1183 if (the_locking_inset) {
1184 the_locking_inset->getCursor(bv, x, y);
1189 y = cursory_ + InsetTabular::y();
1192 int desc = tabular.getDescentOfRow(actrow);
1194 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1196 y += ADD_TO_HEIGHT * 2;
1197 y += TEXT_TO_INSET_OFFSET;
1201 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1203 if (the_locking_inset) {
1204 the_locking_inset->getCursorPos(bv, x, y);
1207 x = cursorx_ - top_x;
1212 void InsetTabular::fitInsetCursor(BufferView * bv) const
1214 if (the_locking_inset) {
1215 the_locking_inset->fitInsetCursor(bv);
1220 int const asc = font_metrics::maxAscent(font);
1221 int const desc = font_metrics::maxDescent(font);
1224 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1228 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1231 actcell = actrow = actcol = 0;
1232 int ly = tabular.getDescentOfRow(actrow);
1234 // first search the right row
1235 while (ly < y && actrow + 1 < tabular.rows()) {
1236 cursory_ += tabular.getDescentOfRow(actrow) +
1237 tabular.getAscentOfRow(actrow + 1) +
1238 tabular.getAdditionalHeight(actrow + 1);
1240 ly = cursory_ + tabular.getDescentOfRow(actrow);
1242 actcell = tabular.getCellNumber(actrow, actcol);
1244 // now search the right column
1245 int lx = tabular.getWidthOfColumn(actcell) -
1246 tabular.getAdditionalWidth(actcell);
1248 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1249 lx += tabular.getWidthOfColumn(actcell + 1)
1250 + tabular.getAdditionalWidth(actcell);
1252 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1257 int InsetTabular::getCellXPos(int cell) const
1261 for (; !tabular.isFirstCellInRow(c); --c)
1263 int lx = tabular.getWidthOfColumn(cell);
1264 for (; c < cell; ++c)
1265 lx += tabular.getWidthOfColumn(c);
1267 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1271 void InsetTabular::resetPos(BufferView * bv) const
1273 #ifdef WITH_WARNINGS
1274 #warning This should be fixed in the right manner (20011128 Jug)
1276 // fast hack to fix infinite repaintings!
1277 if (in_reset_pos > 0)
1281 actcol = tabular.column_of_cell(actcell);
1284 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1285 if (tabular.isLastCellInRow(cell)) {
1286 cursory_ += tabular.getDescentOfRow(actrow) +
1287 tabular.getAscentOfRow(actrow + 1) +
1288 tabular.getAdditionalHeight(actrow + 1);
1293 if (the_locking_inset)
1297 // we need this only from here on!!!
1299 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1300 int new_x = getCellXPos(actcell);
1301 int old_x = cursorx_;
1304 // cursor.x(getCellXPos(actcell) + offset);
1305 if (actcol < tabular.columns() - 1 && scroll(false) &&
1306 tabular.getWidthOfTabular() < bv->workWidth()-20)
1310 } else if (the_locking_inset &&
1311 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1313 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1314 if (xx > bv->workWidth()-20) {
1315 scroll(bv, -(xx - bv->workWidth() + 60));
1317 } else if (xx < 20) {
1325 } else if (cursorx_ - offset > 20 &&
1326 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1327 > bv->workWidth() - 20) {
1328 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1330 } else if (cursorx_ - offset < 20) {
1331 scroll(bv, 20 - cursorx_ + offset);
1333 } else if (scroll() && top_x > 20 &&
1334 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1335 scroll(bv, old_x - cursorx_);
1338 if (the_locking_inset) {
1339 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1342 if ((!the_locking_inset ||
1343 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1344 actcell != oldcell) {
1345 InsetTabularMailer(*this).updateDialog(bv);
1352 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1354 if (lock && !old_locking_inset) {
1355 if (activateCellInset(bv))
1358 bool moved = isRightToLeft(bv)
1359 ? movePrevCell(bv) : moveNextCell(bv);
1361 return FINISHED_RIGHT;
1362 if (lock && activateCellInset(bv))
1366 return DISPATCHED_NOUPDATE;
1370 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1372 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1375 if (lock) { // behind the inset
1376 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1380 return DISPATCHED_NOUPDATE;
1384 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1386 int const ocell = actcell;
1387 actcell = tabular.getCellAbove(actcell);
1388 if (actcell == ocell) // we moved out of the inset
1394 if (old_locking_inset) {
1395 old_locking_inset->getCursorPos(bv, x, y);
1396 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1398 if (activateCellInset(bv, x, 0))
1401 return DISPATCHED_NOUPDATE;
1405 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1407 int const ocell = actcell;
1408 actcell = tabular.getCellBelow(actcell);
1409 if (actcell == ocell) // we moved out of the inset
1410 return FINISHED_DOWN;
1415 if (old_locking_inset) {
1416 old_locking_inset->getCursorPos(bv, x, y);
1417 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1419 if (activateCellInset(bv, x, 0))
1422 return DISPATCHED_NOUPDATE;
1426 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1428 if (isRightToLeft(bv)) {
1429 if (tabular.isFirstCellInRow(actcell)) {
1430 int row = tabular.row_of_cell(actcell);
1431 if (row == tabular.rows() - 1)
1433 actcell = tabular.getLastCellInRow(row);
1434 actcell = tabular.getCellBelow(actcell);
1441 if (tabular.isLastCell(actcell))
1446 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1447 isRightToLeftPar(bv->buffer()->params);
1448 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1455 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1457 if (isRightToLeft(bv)) {
1458 if (tabular.isLastCellInRow(actcell)) {
1459 int row = tabular.row_of_cell(actcell);
1462 actcell = tabular.getFirstCellInRow(row);
1463 actcell = tabular.getCellAbove(actcell);
1465 if (tabular.isLastCell(actcell))
1470 if (!actcell) // first cell
1475 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1476 isRightToLeftPar(bv->buffer()->params);
1477 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1484 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1488 setSelection(0, tabular.getNumberOfCells() - 1);
1490 if (hasSelection()) {
1491 recordUndo(bv, Undo::ATOMIC);
1492 bool const frozen = undo_frozen;
1495 // apply the fontchange on the whole selection
1500 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1501 for(int i = sel_row_start; i <= sel_row_end; ++i)
1502 for(int j = sel_col_start; j <= sel_col_end; ++j)
1503 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1511 if (the_locking_inset)
1512 the_locking_inset->setFont(bv, font, tall);
1516 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1518 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1521 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1522 string const tmp = tabularFeature[i].feature;
1524 if (tmp == what.substr(0, tmp.length())) {
1525 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1526 //tabularFeatures[i].feature.length())) {
1527 action = tabularFeature[i].action;
1531 if (action == LyXTabular::LAST_ACTION)
1535 ltrim(what.substr(tabularFeature[i].feature.length()));
1536 tabularFeatures(bv, action, val);
1542 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1543 string const & special, bool & flag)
1545 if (special == "dl_above") {
1548 } else if (special == "dl_below") {
1549 ltt.bottomDL = flag;
1551 } else if (special == "empty") {
1563 void InsetTabular::tabularFeatures(BufferView * bv,
1564 LyXTabular::Feature feature,
1565 string const & value)
1571 bool setLines = false;
1572 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1573 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1577 case LyXTabular::M_ALIGN_LEFT:
1578 case LyXTabular::ALIGN_LEFT:
1579 setAlign = LYX_ALIGN_LEFT;
1582 case LyXTabular::M_ALIGN_RIGHT:
1583 case LyXTabular::ALIGN_RIGHT:
1584 setAlign = LYX_ALIGN_RIGHT;
1587 case LyXTabular::M_ALIGN_CENTER:
1588 case LyXTabular::ALIGN_CENTER:
1589 setAlign = LYX_ALIGN_CENTER;
1592 case LyXTabular::ALIGN_BLOCK:
1593 setAlign = LYX_ALIGN_BLOCK;
1596 case LyXTabular::M_VALIGN_TOP:
1597 case LyXTabular::VALIGN_TOP:
1598 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1601 case LyXTabular::M_VALIGN_BOTTOM:
1602 case LyXTabular::VALIGN_BOTTOM:
1603 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1606 case LyXTabular::M_VALIGN_MIDDLE:
1607 case LyXTabular::VALIGN_MIDDLE:
1608 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1615 if (hasSelection()) {
1616 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1618 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1619 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1621 recordUndo(bv, Undo::ATOMIC);
1623 int row = tabular.row_of_cell(actcell);
1624 int column = tabular.column_of_cell(actcell);
1626 LyXTabular::ltType ltt;
1630 case LyXTabular::SET_PWIDTH:
1632 LyXLength const vallen(value);
1633 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1635 bool const update = (tmplen != vallen);
1636 tabular.setColumnPWidth(actcell, vallen);
1638 // We need this otherwise we won't resize
1639 // the insettext of the active cell (if any)
1640 // until later (see InsetText::do_resize)
1641 unlockInsetInInset(bv, the_locking_inset);
1646 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1647 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1648 else if (!vallen.zero()
1649 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1650 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1654 case LyXTabular::SET_MPWIDTH:
1656 LyXLength const vallen(value);
1657 LyXLength const & tmplen = tabular.getPWidth(actcell);
1659 bool const update = (tmplen != vallen);
1660 tabular.setMColumnPWidth(actcell, vallen);
1662 // We need this otherwise we won't resize
1663 // the insettext of the active cell (if any)
1664 // until later (see InsetText::do_resize)
1665 unlockInsetInInset(bv, the_locking_inset);
1670 case LyXTabular::SET_SPECIAL_COLUMN:
1671 case LyXTabular::SET_SPECIAL_MULTI:
1672 tabular.setAlignSpecial(actcell,value,feature);
1675 case LyXTabular::APPEND_ROW:
1676 // append the row into the tabular
1677 unlockInsetInInset(bv, the_locking_inset);
1678 tabular.appendRow(bv->buffer()->params, actcell);
1681 case LyXTabular::APPEND_COLUMN:
1682 // append the column into the tabular
1683 unlockInsetInInset(bv, the_locking_inset);
1684 tabular.appendColumn(bv->buffer()->params, actcell);
1685 actcell = tabular.getCellNumber(row, column);
1688 case LyXTabular::DELETE_ROW:
1689 unlockInsetInInset(bv, the_locking_inset);
1690 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1691 tabular.deleteRow(sel_row_start);
1693 if (sel_row_start >= tabular.rows())
1695 actcell = tabular.getCellNumber(sel_row_start, column);
1699 case LyXTabular::DELETE_COLUMN:
1700 unlockInsetInInset(bv, the_locking_inset);
1701 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1702 tabular.deleteColumn(sel_col_start);
1704 if (sel_col_start >= tabular.columns())
1706 actcell = tabular.getCellNumber(row, sel_col_start);
1710 case LyXTabular::M_TOGGLE_LINE_TOP:
1712 case LyXTabular::TOGGLE_LINE_TOP:
1714 bool lineSet = !tabular.topLine(actcell, flag);
1715 for (int i = sel_row_start; i <= sel_row_end; ++i)
1716 for (int j = sel_col_start; j <= sel_col_end; ++j)
1718 tabular.getCellNumber(i, j),
1724 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1726 case LyXTabular::TOGGLE_LINE_BOTTOM:
1728 bool lineSet = !tabular.bottomLine(actcell, flag);
1729 for (int i = sel_row_start; i <= sel_row_end; ++i)
1730 for (int j = sel_col_start; j <= sel_col_end; ++j)
1731 tabular.setBottomLine(
1732 tabular.getCellNumber(i, j),
1739 case LyXTabular::M_TOGGLE_LINE_LEFT:
1741 case LyXTabular::TOGGLE_LINE_LEFT:
1743 bool lineSet = !tabular.leftLine(actcell, flag);
1744 for (int i = sel_row_start; i <= sel_row_end; ++i)
1745 for (int j = sel_col_start; j <= sel_col_end; ++j)
1746 tabular.setLeftLine(
1747 tabular.getCellNumber(i,j),
1754 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1756 case LyXTabular::TOGGLE_LINE_RIGHT:
1758 bool lineSet = !tabular.rightLine(actcell, flag);
1759 for (int i = sel_row_start; i <= sel_row_end; ++i)
1760 for (int j = sel_col_start; j <= sel_col_end; ++j)
1761 tabular.setRightLine(
1762 tabular.getCellNumber(i,j),
1769 case LyXTabular::M_ALIGN_LEFT:
1770 case LyXTabular::M_ALIGN_RIGHT:
1771 case LyXTabular::M_ALIGN_CENTER:
1773 case LyXTabular::ALIGN_LEFT:
1774 case LyXTabular::ALIGN_RIGHT:
1775 case LyXTabular::ALIGN_CENTER:
1776 case LyXTabular::ALIGN_BLOCK:
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.setAlignment(
1780 tabular.getCellNumber(i, j),
1786 case LyXTabular::M_VALIGN_TOP:
1787 case LyXTabular::M_VALIGN_BOTTOM:
1788 case LyXTabular::M_VALIGN_MIDDLE:
1790 case LyXTabular::VALIGN_TOP:
1791 case LyXTabular::VALIGN_BOTTOM:
1792 case LyXTabular::VALIGN_MIDDLE:
1793 for (int i = sel_row_start; i <= sel_row_end; ++i)
1794 for (int j = sel_col_start; j <= sel_col_end; ++j)
1795 tabular.setVAlignment(
1796 tabular.getCellNumber(i, j),
1801 case LyXTabular::MULTICOLUMN: {
1802 if (sel_row_start != sel_row_end) {
1803 #ifdef WITH_WARNINGS
1804 #warning Need I say it ? This is horrible.
1806 Alert::error(_("Error setting multicolumn"),
1807 _("You cannot set multicolumn vertically."));
1810 // just multicol for one Single Cell
1811 if (!hasSelection()) {
1812 // check wether we are completly in a multicol
1813 if (tabular.isMultiColumn(actcell))
1814 tabular.unsetMultiColumn(actcell);
1816 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1820 // we have a selection so this means we just add all this
1821 // cells to form a multicolumn cell
1825 if (sel_cell_start > sel_cell_end) {
1826 s_start = sel_cell_end;
1827 s_end = sel_cell_start;
1829 s_start = sel_cell_start;
1830 s_end = sel_cell_end;
1832 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1839 case LyXTabular::SET_ALL_LINES:
1841 case LyXTabular::UNSET_ALL_LINES:
1842 for (int i = sel_row_start; i <= sel_row_end; ++i)
1843 for (int j = sel_col_start; j <= sel_col_end; ++j)
1844 tabular.setAllLines(
1845 tabular.getCellNumber(i,j), setLines);
1849 case LyXTabular::SET_LONGTABULAR:
1850 tabular.setLongTabular(true);
1851 updateLocal(bv); // because this toggles displayed
1854 case LyXTabular::UNSET_LONGTABULAR:
1855 tabular.setLongTabular(false);
1856 updateLocal(bv); // because this toggles displayed
1859 case LyXTabular::SET_ROTATE_TABULAR:
1860 tabular.setRotateTabular(true);
1863 case LyXTabular::UNSET_ROTATE_TABULAR:
1864 tabular.setRotateTabular(false);
1867 case LyXTabular::SET_ROTATE_CELL:
1868 for (int i = sel_row_start; i <= sel_row_end; ++i)
1869 for (int j = sel_col_start; j<=sel_col_end; ++j)
1870 tabular.setRotateCell(
1871 tabular.getCellNumber(i, j), true);
1874 case LyXTabular::UNSET_ROTATE_CELL:
1875 for (int i = sel_row_start; i <= sel_row_end; ++i)
1876 for (int j = sel_col_start; j <= sel_col_end; ++j)
1877 tabular.setRotateCell(
1878 tabular.getCellNumber(i, j), false);
1881 case LyXTabular::SET_USEBOX: {
1882 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1883 if (val == tabular.getUsebox(actcell))
1884 val = LyXTabular::BOX_NONE;
1885 for (int i = sel_row_start; i <= sel_row_end; ++i)
1886 for (int j = sel_col_start; j <= sel_col_end; ++j)
1887 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1891 case LyXTabular::UNSET_LTFIRSTHEAD:
1893 case LyXTabular::SET_LTFIRSTHEAD:
1894 tabular.getRowOfLTFirstHead(row, ltt);
1895 checkLongtableSpecial(ltt, value, flag);
1896 tabular.setLTHead(row, flag, ltt, true);
1899 case LyXTabular::UNSET_LTHEAD:
1901 case LyXTabular::SET_LTHEAD:
1902 tabular.getRowOfLTHead(row, ltt);
1903 checkLongtableSpecial(ltt, value, flag);
1904 tabular.setLTHead(row, flag, ltt, false);
1907 case LyXTabular::UNSET_LTFOOT:
1909 case LyXTabular::SET_LTFOOT:
1910 tabular.getRowOfLTFoot(row, ltt);
1911 checkLongtableSpecial(ltt, value, flag);
1912 tabular.setLTFoot(row, flag, ltt, false);
1915 case LyXTabular::UNSET_LTLASTFOOT:
1917 case LyXTabular::SET_LTLASTFOOT:
1918 tabular.getRowOfLTLastFoot(row, ltt);
1919 checkLongtableSpecial(ltt, value, flag);
1920 tabular.setLTFoot(row, flag, ltt, true);
1923 case LyXTabular::SET_LTNEWPAGE: {
1924 bool what = !tabular.getLTNewPage(row);
1925 tabular.setLTNewPage(row, what);
1929 // dummy stuff just to avoid warnings
1930 case LyXTabular::LAST_ACTION:
1934 InsetTabularMailer(*this).updateDialog(bv);
1938 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1939 mouse_button::state button, bool behind)
1941 UpdatableInset & inset = tabular.getCellInset(actcell);
1944 x = inset.x() + inset.width();
1945 y = inset.descent();
1947 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1948 //inset_y = cursory_;
1949 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1950 if (!the_locking_inset)
1953 return the_locking_inset;
1957 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1958 mouse_button::state button)
1960 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1962 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1966 bool InsetTabular::insetHit(BufferView *, int x, int) const
1968 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1972 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1974 //resizeLyXText(bv, recursive);
1978 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1979 bool const recursive) const
1981 if (the_locking_inset)
1982 return the_locking_inset->getLyXText(bv, recursive);
1983 return InsetOld::getLyXText(bv, recursive);
1987 bool InsetTabular::showInsetDialog(BufferView * bv) const
1989 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1990 InsetTabularMailer(*this).showDialog(bv);
1995 void InsetTabular::openLayoutDialog(BufferView * bv) const
1997 if (the_locking_inset) {
1998 InsetTabular * inset = static_cast<InsetTabular *>
1999 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2001 inset->openLayoutDialog(bv);
2005 InsetTabularMailer(*this).showDialog(bv);
2010 // function returns an object as defined in func_status.h:
2011 // states OK, Unknown, Disabled, On, Off.
2013 FuncStatus InsetTabular::getStatus(string const & what) const
2015 int action = LyXTabular::LAST_ACTION;
2019 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2020 string const tmp = tabularFeature[i].feature;
2021 if (tmp == what.substr(0, tmp.length())) {
2022 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2023 // tabularFeatures[i].feature.length())) {
2024 action = tabularFeature[i].action;
2028 if (action == LyXTabular::LAST_ACTION) {
2030 return status.unknown(true);
2033 string const argument
2034 = ltrim(what.substr(tabularFeature[i].feature.length()));
2039 LyXTabular::ltType dummyltt;
2043 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2045 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2048 case LyXTabular::SET_PWIDTH:
2049 case LyXTabular::SET_MPWIDTH:
2050 case LyXTabular::SET_SPECIAL_COLUMN:
2051 case LyXTabular::SET_SPECIAL_MULTI:
2052 case LyXTabular::APPEND_ROW:
2053 case LyXTabular::APPEND_COLUMN:
2054 case LyXTabular::DELETE_ROW:
2055 case LyXTabular::DELETE_COLUMN:
2056 case LyXTabular::SET_ALL_LINES:
2057 case LyXTabular::UNSET_ALL_LINES:
2058 return status.clear();
2060 case LyXTabular::MULTICOLUMN:
2061 status.setOnOff(tabular.isMultiColumn(actcell));
2064 case LyXTabular::M_TOGGLE_LINE_TOP:
2066 case LyXTabular::TOGGLE_LINE_TOP:
2067 status.setOnOff(tabular.topLine(actcell, flag));
2070 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2072 case LyXTabular::TOGGLE_LINE_BOTTOM:
2073 status.setOnOff(tabular.bottomLine(actcell, flag));
2076 case LyXTabular::M_TOGGLE_LINE_LEFT:
2078 case LyXTabular::TOGGLE_LINE_LEFT:
2079 status.setOnOff(tabular.leftLine(actcell, flag));
2082 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2084 case LyXTabular::TOGGLE_LINE_RIGHT:
2085 status.setOnOff(tabular.rightLine(actcell, flag));
2088 case LyXTabular::M_ALIGN_LEFT:
2090 case LyXTabular::ALIGN_LEFT:
2091 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2094 case LyXTabular::M_ALIGN_RIGHT:
2096 case LyXTabular::ALIGN_RIGHT:
2097 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2100 case LyXTabular::M_ALIGN_CENTER:
2102 case LyXTabular::ALIGN_CENTER:
2103 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2106 case LyXTabular::ALIGN_BLOCK:
2107 status.disabled(tabular.getPWidth(actcell).zero());
2108 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2111 case LyXTabular::M_VALIGN_TOP:
2113 case LyXTabular::VALIGN_TOP:
2114 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2117 case LyXTabular::M_VALIGN_BOTTOM:
2119 case LyXTabular::VALIGN_BOTTOM:
2120 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2123 case LyXTabular::M_VALIGN_MIDDLE:
2125 case LyXTabular::VALIGN_MIDDLE:
2126 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2129 case LyXTabular::SET_LONGTABULAR:
2130 status.setOnOff(tabular.isLongTabular());
2133 case LyXTabular::UNSET_LONGTABULAR:
2134 status.setOnOff(!tabular.isLongTabular());
2137 case LyXTabular::SET_ROTATE_TABULAR:
2138 status.setOnOff(tabular.getRotateTabular());
2141 case LyXTabular::UNSET_ROTATE_TABULAR:
2142 status.setOnOff(!tabular.getRotateTabular());
2145 case LyXTabular::SET_ROTATE_CELL:
2146 status.setOnOff(tabular.getRotateCell(actcell));
2149 case LyXTabular::UNSET_ROTATE_CELL:
2150 status.setOnOff(!tabular.getRotateCell(actcell));
2153 case LyXTabular::SET_USEBOX:
2154 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2157 case LyXTabular::SET_LTFIRSTHEAD:
2158 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2161 case LyXTabular::SET_LTHEAD:
2162 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2165 case LyXTabular::SET_LTFOOT:
2166 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2169 case LyXTabular::SET_LTLASTFOOT:
2170 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2173 case LyXTabular::SET_LTNEWPAGE:
2174 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2179 status.disabled(true);
2186 void InsetTabular::getLabelList(std::vector<string> & list) const
2188 tabular.getLabelList(list);
2192 bool InsetTabular::copySelection(BufferView * bv)
2194 if (!hasSelection())
2197 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2198 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2199 if (sel_col_start > sel_col_end) {
2200 sel_col_start = sel_col_end;
2201 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2203 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2206 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2207 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2208 if (sel_row_start > sel_row_end)
2209 swap(sel_row_start, sel_row_end);
2211 delete paste_tabular;
2212 paste_tabular = new LyXTabular(bv->buffer()->params, this, tabular);
2214 for (int i = 0; i < sel_row_start; ++i)
2215 paste_tabular->deleteRow(0);
2217 int const rows = sel_row_end - sel_row_start + 1;
2218 while (paste_tabular->rows() > rows)
2219 paste_tabular->deleteRow(rows);
2221 paste_tabular->setTopLine(0, true, true);
2222 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2225 for (int i = 0; i < sel_col_start; ++i)
2226 paste_tabular->deleteColumn(0);
2228 int const columns = sel_col_end - sel_col_start + 1;
2229 while (paste_tabular->columns() > columns)
2230 paste_tabular->deleteColumn(columns);
2232 paste_tabular->setLeftLine(0, true, true);
2233 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2237 paste_tabular->ascii(*bv->buffer(), os,
2238 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2239 bv->stuffClipboard(STRCONV(os.str()));
2244 bool InsetTabular::pasteSelection(BufferView * bv)
2249 for (int r1 = 0, r2 = actrow;
2250 r1 < paste_tabular->rows() && r2 < tabular.rows();
2252 for (int c1 = 0, c2 = actcol;
2253 c1 < paste_tabular->columns() && c2 < tabular.columns();
2255 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2256 tabular.isPartOfMultiColumn(r2, c2))
2258 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2262 if (tabular.isPartOfMultiColumn(r2, c2)) {
2266 InsetText & inset = tabular.getCellInset(r2, c2);
2267 inset = paste_tabular->getCellInset(r1, c1);
2268 inset.setOwner(this);
2269 inset.deleteLyXText(bv);
2277 bool InsetTabular::cutSelection(BufferParams const & bp)
2279 if (!hasSelection())
2282 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2283 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2284 if (sel_col_start > sel_col_end) {
2285 sel_col_start = sel_col_end;
2286 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2288 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2291 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2292 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2294 if (sel_row_start > sel_row_end)
2295 swap(sel_row_start, sel_row_end);
2297 if (sel_cell_start > sel_cell_end)
2298 swap(sel_cell_start, sel_cell_end);
2300 for (int i = sel_row_start; i <= sel_row_end; ++i)
2301 for (int j = sel_col_start; j <= sel_col_end; ++j)
2302 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2307 bool InsetTabular::isRightToLeft(BufferView * bv)
2309 return bv->getParentLanguage(this)->RightToLeft();
2313 int InsetTabular::scroll(bool recursive) const
2315 int sx = UpdatableInset::scroll(false);
2317 if (recursive && the_locking_inset)
2318 sx += the_locking_inset->scroll(recursive);
2324 void InsetTabular::getSelection(int & srow, int & erow,
2325 int & scol, int & ecol) const
2327 int const start = hasSelection() ? sel_cell_start : actcell;
2328 int const end = hasSelection() ? sel_cell_end : actcell;
2330 srow = tabular.row_of_cell(start);
2331 erow = tabular.row_of_cell(end);
2335 scol = tabular.column_of_cell(start);
2336 ecol = tabular.column_of_cell(end);
2340 ecol = tabular.right_column_of_cell(end);
2344 ParagraphList * InsetTabular::getParagraphs(int i) const
2346 return (i < tabular.getNumberOfCells())
2347 ? tabular.getCellInset(i).getParagraphs(0)
2352 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2354 if (the_locking_inset)
2355 return the_locking_inset->cursor(bv);
2356 return InsetOld::cursor(bv);
2360 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2363 return const_cast<InsetTabular *>(this);
2365 for (int i = 0; i < tabular.rows(); ++i) {
2366 for (int j = 0; j < tabular.columns(); ++j) {
2367 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2377 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2379 if (the_locking_inset) {
2380 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2381 if (!word.word().empty())
2383 if (tabular.isLastCell(actcell)) {
2384 bv->unlockInset(const_cast<InsetTabular *>(this));
2385 return WordLangTuple();
2389 // otherwise we have to lock the next inset and ask for it's selecttion
2390 tabular.getCellInset(actcell)
2391 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2392 WordLangTuple word(selectNextWordInt(bv, value));
2393 if (!word.word().empty())
2399 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2401 // when entering this function the inset should be ALWAYS locked!
2402 Assert(the_locking_inset);
2404 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2405 if (!word.word().empty())
2408 if (tabular.isLastCell(actcell)) {
2409 bv->unlockInset(const_cast<InsetTabular *>(this));
2410 return WordLangTuple();
2413 // otherwise we have to lock the next inset and ask for it's selecttion
2415 tabular.getCellInset(actcell)
2416 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2417 return selectNextWordInt(bv, value);
2421 void InsetTabular::selectSelectedWord(BufferView * bv)
2423 if (the_locking_inset)
2424 the_locking_inset->selectSelectedWord(bv);
2428 void InsetTabular::markErased()
2430 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2431 tabular.getCellInset(cell).markErased();
2435 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2437 if (the_locking_inset) {
2438 if (the_locking_inset->nextChange(bv, length)) {
2442 if (tabular.isLastCell(actcell))
2446 InsetText & inset = tabular.getCellInset(actcell);
2447 if (inset.nextChange(bv, length)) {
2451 while (!tabular.isLastCell(actcell)) {
2453 InsetText & inset = tabular.getCellInset(actcell);
2454 if (inset.nextChange(bv, length)) {
2463 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2467 if (the_locking_inset) {
2468 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2472 if (tabular.isLastCell(actcell))
2476 InsetText & inset = tabular.getCellInset(cell);
2477 if (inset.searchForward(bv, str, cs, mw)) {
2481 while (!tabular.isLastCell(cell)) {
2483 InsetText & inset = tabular.getCellInset(cell);
2484 if (inset.searchForward(bv, str, cs, mw)) {
2493 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2496 int cell = tabular.getNumberOfCells();
2497 if (the_locking_inset) {
2498 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2507 InsetText & inset = tabular.getCellInset(cell);
2508 if (inset.searchBackward(bv, str, cs, mw)) {
2517 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2519 if (the_locking_inset)
2520 return the_locking_inset->insetAllowed(code);
2521 // we return true here because if the inset is not locked someone
2522 // wants to insert something in one of our insettexts and we generally
2528 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2530 const int cell = tabular.getCellFromInset(in, actcell);
2533 return tabular.getPWidth(cell).zero();
2535 // well we didn't obviously find it so maybe our owner knows more
2537 return owner()->forceDefaultParagraphs(in);
2539 lyxerr << "If we're here there is really something strange going on!"
2545 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2548 if (buf.length() <= 0)
2554 string::size_type len = buf.length();
2555 string::size_type p = 0;
2557 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2565 maxCols = max(cols, maxCols);
2571 maxCols = max(cols, maxCols);
2572 LyXTabular * loctab;
2577 delete paste_tabular;
2578 paste_tabular = new LyXTabular(bv->buffer()->params,
2579 this, rows, maxCols);
2580 loctab = paste_tabular;
2589 string::size_type op = 0;
2590 int cells = loctab->getNumberOfCells();
2593 rows = loctab->rows();
2594 int const columns = loctab->columns();
2596 while (cell < cells && p < len && row < rows &&
2597 (p = buf.find_first_of("\t\n", p)) != string::npos)
2603 // we can only set this if we are not too far right
2604 if (cols < columns) {
2605 InsetText & inset = loctab->getCellInset(cell);
2606 LyXFont const font = inset.getLyXText(bv)->
2607 getFont(inset.paragraphs.begin(), 0);
2608 inset.setText(buf.substr(op, p - op), font);
2614 // we can only set this if we are not too far right
2615 if (cols < columns) {
2616 InsetText & inset = tabular.getCellInset(cell);
2617 LyXFont const font = inset.getLyXText(bv)->
2618 getFont(inset.paragraphs.begin(), 0);
2619 inset.setText(buf.substr(op, p - op), font);
2624 cell = loctab->getCellNumber(row, cols);
2630 // check for the last cell if there is no trailing '\n'
2631 if (cell < cells && op < len) {
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, len - op), font);
2642 void InsetTabular::addPreview(PreviewLoader & loader) const
2644 int const rows = tabular.rows();
2645 int const columns = tabular.columns();
2646 for (int i = 0; i < rows; ++i)
2647 for (int j = 0; j < columns; ++j)
2648 tabular.getCellInset(i, j).addPreview(loader);
2652 string const InsetTabularMailer::name_("tabular");
2654 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2655 : inset_(const_cast<InsetTabular &>(inset))
2659 string const InsetTabularMailer::inset2string(Buffer const &) const
2661 return params2string(inset_);
2665 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2667 istringstream data(STRCONV(in));
2669 lex.setStream(data);
2671 #warning CHECK verify that this is a sane value to return.
2677 string const token = lex.getString();
2685 string const token = lex.getString();
2686 if (token != "\\active_cell")
2689 cell = lex.getInteger();
2692 // This is part of the inset proper that is usually swallowed
2693 // by Buffer::readInset
2696 string const token = lex.getString();
2697 if (token != "Tabular")
2704 Buffer const & buffer = inset.buffer();
2705 inset.read(buffer, lex);
2707 // We can't set the active cell, but we can tell the frontend
2713 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2715 Buffer const & buffer = inset.buffer();
2718 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2719 inset.write(buffer, data);
2720 data << "\\end_inset\n";
2721 return STRCONV(data.str());