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"
39 using namespace lyx::support;
40 using namespace lyx::graphics;
53 int const ADD_TO_HEIGHT = 2;
54 int const ADD_TO_TABULAR_WIDTH = 2;
57 LyXTabular * paste_tabular = 0;
60 struct TabularFeature {
61 LyXTabular::Feature action;
66 TabularFeature tabularFeature[] =
68 { LyXTabular::APPEND_ROW, "append-row" },
69 { LyXTabular::APPEND_COLUMN, "append-column" },
70 { LyXTabular::DELETE_ROW, "delete-row" },
71 { LyXTabular::DELETE_COLUMN, "delete-column" },
72 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
73 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
74 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
75 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
76 { LyXTabular::ALIGN_LEFT, "align-left" },
77 { LyXTabular::ALIGN_RIGHT, "align-right" },
78 { LyXTabular::ALIGN_CENTER, "align-center" },
79 { LyXTabular::ALIGN_BLOCK, "align-block" },
80 { LyXTabular::VALIGN_TOP, "valign-top" },
81 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
82 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
83 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
84 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
85 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
86 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
87 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
88 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
89 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
90 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
91 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
92 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
93 { LyXTabular::MULTICOLUMN, "multicolumn" },
94 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
95 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
96 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
97 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
98 { LyXTabular::SET_PWIDTH, "set-pwidth" },
99 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
100 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
101 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
102 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
103 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
104 { LyXTabular::SET_USEBOX, "set-usebox" },
105 { LyXTabular::SET_LTHEAD, "set-lthead" },
106 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
107 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
108 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
109 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
110 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
111 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
112 { LyXTabular::LAST_ACTION, "" }
116 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
117 bool operator()(TabularFeature & tf)
119 return tf.action == feature_;
122 LyXTabular::Feature feature_;
128 string const featureAsString(LyXTabular::Feature feature)
130 TabularFeature * it = tabularFeature;
131 TabularFeature * end = it +
132 sizeof(tabularFeature) / sizeof(TabularFeature);
133 it = std::find_if(it, end, FindFeature(feature));
134 return (it == end) ? string() : it->feature;
138 bool InsetTabular::hasPasteBuffer() const
140 return (paste_tabular != 0);
144 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
145 : tabular(buf.params, this, max(rows, 1), max(columns, 1)),
146 buffer_(&buf), cursorx_(0), cursory_(0)
148 // for now make it always display as display() inset
150 the_locking_inset = 0;
151 old_locking_inset = 0;
154 actrow = actcell = 0;
162 InsetTabular::InsetTabular(InsetTabular const & tab)
163 : UpdatableInset(tab),
164 tabular(tab.buffer_->params, this, tab.tabular),
165 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
167 the_locking_inset = 0;
168 old_locking_inset = 0;
171 actrow = actcell = 0;
179 InsetTabular::~InsetTabular()
181 InsetTabularMailer(*this).hideDialog();
185 auto_ptr<InsetBase> InsetTabular::clone() const
187 return auto_ptr<InsetBase>(new InsetTabular(*this));
191 Buffer const & InsetTabular::buffer() const
197 BufferView * InsetTabular::view() const
204 void InsetTabular::buffer(Buffer * b)
210 void InsetTabular::write(Buffer const & buf, ostream & os) const
212 os << "Tabular" << endl;
213 tabular.write(buf, os);
217 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
219 bool const old_format = (lex.getString() == "\\LyXTable");
221 tabular.read(buf, lex);
227 string token = lex.getString();
228 while (lex.isOK() && (token != "\\end_inset")) {
230 token = lex.getString();
232 if (token != "\\end_inset") {
233 lex.printError("Missing \\end_inset at this point. "
239 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
241 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
242 // mi.base.textwidth << "\n";
244 lyxerr << "InsetTabular::metrics: need bv" << endl;
248 calculate_dimensions_of_cells(mi);
250 dim.asc = tabular.getAscentOfRow(0);
251 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
252 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
257 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
259 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
261 BufferView * bv = pi.base.bv;
264 UpdatableInset::draw(pi, x, y);
272 x += ADD_TO_TABULAR_WIDTH;
275 first_visible_cell = -1;
276 for (int i = 0; i < tabular.rows(); ++i) {
278 cell = tabular.getCellNumber(i, 0);
279 if (y + tabular.getDescentOfRow(i) <= 0 &&
280 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
282 y += tabular.getDescentOfRow(i) +
283 tabular.getAscentOfRow(i + 1) +
284 tabular.getAdditionalHeight(i + 1);
287 for (int j = 0; j < tabular.columns(); ++j) {
288 if (nx > bv->workWidth())
290 if (tabular.isPartOfMultiColumn(i, j))
292 int cx = nx + tabular.getBeginningOfTextInCell(cell);
293 if (first_visible_cell < 0)
294 first_visible_cell = cell;
295 if (hasSelection()) {
296 drawCellSelection(pi.pain, nx, y, i, j, cell);
299 tabular.getCellInset(cell).draw(pi, cx, y);
300 drawCellLines(pi.pain, nx, y, i, cell);
301 nx += tabular.getWidthOfColumn(cell);
305 // Would be nice, but for some completely unfathomable reason,
306 // on a col resize to a new fixed width, even though the insettexts
307 // are resized, the cell isn't, but drawing all cells in a tall table
308 // has the desired effect somehow. Complete dark magic.
310 // avoiding drawing the rest of a long table is
311 // a pretty big speedup
312 if (y > bv->workHeight())
316 y += tabular.getDescentOfRow(i) +
317 tabular.getAscentOfRow(i + 1) +
318 tabular.getAdditionalHeight(i + 1);
323 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
324 int row, int cell) const
326 int x2 = x + tabular.getWidthOfColumn(cell);
329 if (!tabular.topAlreadyDrawn(cell)) {
330 on_off = !tabular.topLine(cell);
331 pain.line(x, y - tabular.getAscentOfRow(row),
332 x2, y - tabular.getAscentOfRow(row),
333 on_off ? LColor::tabularonoffline : LColor::tabularline,
334 on_off ? Painter::line_onoffdash : Painter::line_solid);
336 on_off = !tabular.bottomLine(cell);
337 pain.line(x, y + tabular.getDescentOfRow(row),
338 x2, y + tabular.getDescentOfRow(row),
339 on_off ? LColor::tabularonoffline : LColor::tabularline,
340 on_off ? Painter::line_onoffdash : Painter::line_solid);
341 if (!tabular.leftAlreadyDrawn(cell)) {
342 on_off = !tabular.leftLine(cell);
343 pain.line(x, y - tabular.getAscentOfRow(row),
344 x, y + tabular.getDescentOfRow(row),
345 on_off ? LColor::tabularonoffline : LColor::tabularline,
346 on_off ? Painter::line_onoffdash : Painter::line_solid);
348 on_off = !tabular.rightLine(cell);
349 pain.line(x2 - tabular.getAdditionalWidth(cell),
350 y - tabular.getAscentOfRow(row),
351 x2 - tabular.getAdditionalWidth(cell),
352 y + tabular.getDescentOfRow(row),
353 on_off ? LColor::tabularonoffline : LColor::tabularline,
354 on_off ? Painter::line_onoffdash : Painter::line_solid);
358 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
359 int row, int column, int cell) const
361 Assert(hasSelection());
362 int cs = tabular.column_of_cell(sel_cell_start);
363 int ce = tabular.column_of_cell(sel_cell_end);
366 cs = tabular.column_of_cell(sel_cell_end);
368 ce = tabular.right_column_of_cell(sel_cell_end);
371 int rs = tabular.row_of_cell(sel_cell_start);
372 int re = tabular.row_of_cell(sel_cell_end);
376 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
377 int w = tabular.getWidthOfColumn(cell);
378 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
379 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
380 w, h, LColor::selection);
385 string const InsetTabular::editMessage() const
387 return _("Opened table");
391 void InsetTabular::insetUnlock(BufferView * bv)
393 if (the_locking_inset) {
394 the_locking_inset->insetUnlock(bv);
396 the_locking_inset = 0;
401 if (scroll(false) || hasSelection()) {
410 void InsetTabular::updateLocal(BufferView * bv) const
412 bv->updateInset(this);
418 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
420 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
425 if (inset == &tabular.getCellInset(actcell)) {
426 lyxerr[Debug::INSETTEXT] << "OK" << endl;
427 the_locking_inset = &tabular.getCellInset(actcell);
430 } else if (!the_locking_inset) {
431 int const n = tabular.getNumberOfCells();
432 int const id = inset->id();
433 for (int i = 0; i < n; ++i) {
434 InsetText * in = &tabular.getCellInset(i);
437 the_locking_inset = in;
442 if (in->getInsetFromID(id)) {
444 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
445 return the_locking_inset->lockInsetInInset(bv, inset);
448 } else if (the_locking_inset && (the_locking_inset == inset)) {
449 lyxerr[Debug::INSETTEXT] << "OK" << endl;
451 } else if (the_locking_inset) {
452 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
453 return the_locking_inset->lockInsetInInset(bv, inset);
455 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
460 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
463 if (!the_locking_inset)
465 if (the_locking_inset == inset) {
466 the_locking_inset->insetUnlock(bv);
468 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
475 // this has to be here otherwise we don't redraw the cell!
476 the_locking_inset = 0;
479 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
480 if (inset->lyxCode() == TABULAR_CODE &&
481 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
482 InsetTabularMailer(*this).updateDialog(bv);
491 int InsetTabular::insetInInsetY() const
493 if (!the_locking_inset)
495 return inset_y + the_locking_inset->insetInInsetY();
499 UpdatableInset * InsetTabular::getLockingInset() const
501 return the_locking_inset ? the_locking_inset->getLockingInset() :
502 const_cast<InsetTabular *>(this);
506 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
510 if (the_locking_inset)
511 return the_locking_inset->getFirstLockingInsetOfType(c);
516 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
518 if (the_locking_inset)
519 return the_locking_inset->insertInset(bv, inset);
524 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
526 if (hasSelection() && cmd.button() == mouse_button::button3)
529 if (hasSelection()) {
531 updateLocal(cmd.view());
534 int const ocell = actcell;
535 BufferView * bv = cmd.view();
539 the_locking_inset = 0;
543 setPos(bv, cmd.x, cmd.y);
546 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
548 if ((ocell == actcell) && the_locking_inset && inset_hit) {
550 FuncRequest cmd1 = cmd;
553 the_locking_inset->localDispatch(cmd1);
557 if (the_locking_inset) {
558 the_locking_inset->insetUnlock(bv);
560 the_locking_inset = 0;
563 if (cmd.button() == mouse_button::button2) {
564 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
568 if (inset_hit && bv->theLockingInset()) {
569 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
570 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
573 FuncRequest cmd1 = cmd;
576 the_locking_inset->localDispatch(cmd1);
581 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
584 if (the_locking_inset) {
585 FuncRequest cmd1 = cmd;
588 ret = the_locking_inset->localDispatch(cmd1);
590 if (cmd.button() == mouse_button::button3 && !ret) {
591 InsetTabularMailer(*this).showDialog(cmd.view());
598 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
600 if (the_locking_inset) {
601 FuncRequest cmd1 = cmd;
604 the_locking_inset->localDispatch(cmd1);
608 BufferView * bv = cmd.view();
609 int const old_cell = actcell;
611 setPos(bv, cmd.x, cmd.y);
612 if (!hasSelection()) {
613 setSelection(actcell, actcell);
615 } else if (old_cell != actcell) {
616 setSelection(sel_cell_start, actcell);
622 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
624 // We need to save the value of the_locking_inset as the call to
625 // the_locking_inset->localDispatch might unlock it.
626 old_locking_inset = the_locking_inset;
627 RESULT result = UpdatableInset::localDispatch(cmd);
628 BufferView * bv = cmd.view();
630 if (cmd.action == LFUN_INSET_EDIT) {
632 if (!bv->lockInset(this)) {
633 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
639 the_locking_inset = 0;
643 if (cmd.argument.size()) {
644 if (cmd.argument == "left") {
645 if (isRightToLeft(bv))
646 actcell = tabular.getLastCellInRow(0);
650 if (isRightToLeft(bv))
651 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
653 actcell = tabular.getNumberOfCells() - 1;
661 setPos(bv, cmd.x, cmd.y);
664 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
665 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
671 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
676 if (cmd.action < 0 && cmd.argument.empty())
679 bool hs = hasSelection();
682 // this one have priority over the locked InsetText, if we're not already
683 // inside another tabular then that one get's priority!
684 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
685 switch (cmd.action) {
686 case LFUN_MOUSE_PRESS:
690 case LFUN_MOUSE_MOTION:
691 lfunMouseMotion(cmd);
694 case LFUN_MOUSE_RELEASE:
695 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
697 case LFUN_CELL_BACKWARD:
698 case LFUN_CELL_FORWARD:
699 unlockInsetInInset(bv, the_locking_inset);
700 if (cmd.action == LFUN_CELL_FORWARD)
701 moveNextCell(bv, old_locking_inset != 0);
703 movePrevCell(bv, old_locking_inset != 0);
707 if (!the_locking_inset)
708 return DISPATCHED_NOUPDATE;
710 // this to avoid compiler warnings.
716 kb_action action = cmd.action;
717 string arg = cmd.argument;
718 if (the_locking_inset) {
719 result = the_locking_inset->localDispatch(cmd);
720 if (result == DISPATCHED_NOUPDATE) {
723 if (sc != scroll()) { // inset has been scrolled
727 } else if (result == DISPATCHED) {
730 } else if (result == FINISHED_UP) {
732 // Make sure to reset status message after
733 // exiting, e.g. math inset
734 bv->owner()->clearMessage();
735 } else if (result == FINISHED_DOWN) {
737 bv->owner()->clearMessage();
738 } else if (result == FINISHED_RIGHT) {
740 bv->owner()->clearMessage();
741 } else if (result == FINISHED) {
742 bv->owner()->clearMessage();
748 // --- Cursor Movements ----------------------------------
749 case LFUN_RIGHTSEL: {
750 int const start = hasSelection() ? sel_cell_start : actcell;
751 if (tabular.isLastCellInRow(actcell)) {
752 setSelection(start, actcell);
757 // if we are starting a selection, only select
758 // the current cell at the beginning
759 if (hasSelection()) {
760 moveRight(bv, false);
763 setSelection(start, end);
768 result = moveRight(bv);
774 int const start = hasSelection() ? sel_cell_start : actcell;
775 if (tabular.isFirstCellInRow(actcell)) {
776 setSelection(start, actcell);
781 // if we are starting a selection, only select
782 // the current cell at the beginning
783 if (hasSelection()) {
787 setSelection(start, end);
792 result = moveLeft(bv);
798 int const start = hasSelection() ? sel_cell_start : actcell;
799 int const ocell = actcell;
800 // if we are starting a selection, only select
801 // the current cell at the beginning
802 if (hasSelection()) {
804 if (ocell == sel_cell_end ||
805 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
806 setSelection(start, tabular.getCellBelow(sel_cell_end));
808 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
810 setSelection(start, start);
816 result = moveDown(bv, old_locking_inset != 0);
822 int const start = hasSelection() ? sel_cell_start : actcell;
823 int const ocell = actcell;
824 // if we are starting a selection, only select
825 // the current cell at the beginning
826 if (hasSelection()) {
828 if ((ocell == sel_cell_end) ||
829 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
830 setSelection(start, tabular.getCellAbove(sel_cell_end));
832 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
834 setSelection(start, start);
840 result = moveUp(bv, old_locking_inset != 0);
849 unlockInsetInInset(bv, the_locking_inset);
850 if (bv->top_y() + bv->painter().paperHeight() <
851 top_baseline + tabular.getHeightOfTabular())
853 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
854 actcell = tabular.getCellBelow(first_visible_cell) + column;
856 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
866 unlockInsetInInset(bv, the_locking_inset);
867 if (top_baseline < 0) {
868 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
869 if (top_baseline > 0)
872 actcell = tabular.getCellBelow(first_visible_cell) + column;
880 // none of these make sense for insettabular,
881 // but we must catch them to prevent any
882 // selection from being confused
886 case LFUN_WORDLEFTSEL:
888 case LFUN_WORDRIGHTSEL:
890 case LFUN_DOWN_PARAGRAPH:
891 case LFUN_DOWN_PARAGRAPHSEL:
892 case LFUN_UP_PARAGRAPH:
893 case LFUN_UP_PARAGRAPHSEL:
899 case LFUN_BEGINNINGBUF:
900 case LFUN_BEGINNINGBUFSEL:
904 case LFUN_LAYOUT_TABULAR: {
905 InsetTabularMailer(*this).showDialog(bv);
908 case LFUN_INSET_DIALOG_UPDATE: {
909 InsetTabularMailer(*this).updateDialog(bv);
912 case LFUN_TABULAR_FEATURE:
913 if (!tabularFeatures(bv, arg))
914 result = UNDISPATCHED;
916 // insert file functions
917 case LFUN_FILE_INSERT_ASCII_PARA:
918 case LFUN_FILE_INSERT_ASCII:
920 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
923 if (insertAsciiString(bv, tmpstr, false))
926 result = UNDISPATCHED;
929 // cut and paste functions
931 if (!copySelection(bv))
935 recordUndo(bv, Undo::DELETE);
936 cutSelection(bv->buffer()->params);
945 case LFUN_PASTESELECTION:
947 string const clip(bv->getClipboard());
951 if (clip.find('\t') != string::npos) {
955 string::size_type len = clip.length();
956 string::size_type p = 0;
959 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
967 maxCols = max(cols, maxCols);
973 maxCols = max(cols, maxCols);
974 delete paste_tabular;
975 paste_tabular = new LyXTabular(bv->buffer()->params,
976 this, rows, maxCols);
977 string::size_type op = 0;
979 int cells = paste_tabular->getNumberOfCells();
981 while ((cell < cells) && (p < len) &&
982 (p = clip.find_first_of("\t\n", p)) != string::npos) {
987 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
992 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
993 while (cols++ < maxCols)
1001 // check for the last cell if there is no trailing '\n'
1002 if ((cell < cells) && (op < len))
1003 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1006 if (!insertAsciiString(bv, clip, true))
1009 // so that the clipboard is used and it goes on
1011 // and executes LFUN_PASTESELECTION in insettext!
1012 delete paste_tabular;
1017 if (hasPasteBuffer()) {
1018 recordUndo(bv, Undo::INSERT);
1023 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1025 // handle font changing stuff on selection before we lock the inset
1026 // in the default part!
1027 result = UNDISPATCHED;
1038 case LFUN_UNDERLINE:
1039 case LFUN_FONT_SIZE:
1040 if (bv->dispatch(FuncRequest(bv, action, arg)))
1041 result = DISPATCHED;
1047 // we try to activate the actual inset and put this event down to
1048 // the insets dispatch function.
1049 if (result == DISPATCHED || the_locking_inset)
1051 if (activateCellInset(bv)) {
1052 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1053 if (result == UNDISPATCHED || result >= FINISHED) {
1054 unlockInsetInInset(bv, the_locking_inset);
1055 // we need to update if this was requested before
1057 return UNDISPATCHED;
1066 if (result < FINISHED) {
1067 if (!the_locking_inset && bv->fitCursor())
1070 bv->unlockInset(this);
1075 int InsetTabular::latex(Buffer const & buf, ostream & os,
1076 LatexRunParams const & runparams) const
1078 return tabular.latex(buf, os, runparams);
1082 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1085 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1087 return tabular.ascii(buf, os, 0, false, 0);
1091 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1093 return tabular.linuxdoc(buf,os);
1097 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1102 // if the table is inside a float it doesn't need the informaltable
1103 // wrapper. Search for it.
1104 for (master = owner();
1105 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1106 master = master->owner());
1109 os << "<informaltable>";
1114 ret += tabular.docbook(buf, os, mixcont);
1116 os << "</informaltable>";
1125 void InsetTabular::validate(LaTeXFeatures & features) const
1127 tabular.validate(features);
1131 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1134 // if we have a locking_inset we should have to check only this cell for
1135 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1136 // This is _really_ important (André)
1137 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1140 for (int j = 0; j < tabular.columns(); ++j) {
1144 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1145 tabular.getCellInset(actrow, j).metrics(m, dim);
1146 maxAsc = max(dim.asc, maxAsc);
1147 maxDesc = max(dim.des, maxDesc);
1149 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1150 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1151 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1157 for (int i = 0; i < tabular.rows(); ++i) {
1160 for (int j = 0; j < tabular.columns(); ++j) {
1161 if (tabular.isPartOfMultiColumn(i, j))
1167 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1168 tabular.getCellInset(cell).metrics(m, dim);
1169 maxAsc = max(maxAsc, dim.asc);
1170 maxDesc = max(maxDesc, dim.des);
1171 tabular.setWidthOfCell(cell, dim.wid);
1173 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1174 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1179 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1181 if (the_locking_inset) {
1182 the_locking_inset->getCursor(bv, x, y);
1187 y = cursory_ + InsetTabular::y();
1190 int desc = tabular.getDescentOfRow(actrow);
1192 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1194 y += ADD_TO_HEIGHT * 2;
1195 y += TEXT_TO_INSET_OFFSET;
1199 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1201 if (the_locking_inset) {
1202 the_locking_inset->getCursorPos(bv, x, y);
1205 x = cursorx_ - top_x;
1210 void InsetTabular::fitInsetCursor(BufferView * bv) const
1212 if (the_locking_inset) {
1213 the_locking_inset->fitInsetCursor(bv);
1218 int const asc = font_metrics::maxAscent(font);
1219 int const desc = font_metrics::maxDescent(font);
1222 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1226 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1229 actcell = actrow = actcol = 0;
1230 int ly = tabular.getDescentOfRow(actrow);
1232 // first search the right row
1233 while (ly < y && actrow + 1 < tabular.rows()) {
1234 cursory_ += tabular.getDescentOfRow(actrow) +
1235 tabular.getAscentOfRow(actrow + 1) +
1236 tabular.getAdditionalHeight(actrow + 1);
1238 ly = cursory_ + tabular.getDescentOfRow(actrow);
1240 actcell = tabular.getCellNumber(actrow, actcol);
1242 // now search the right column
1243 int lx = tabular.getWidthOfColumn(actcell) -
1244 tabular.getAdditionalWidth(actcell);
1246 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1247 lx += tabular.getWidthOfColumn(actcell + 1)
1248 + tabular.getAdditionalWidth(actcell);
1250 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1255 int InsetTabular::getCellXPos(int cell) const
1259 for (; !tabular.isFirstCellInRow(c); --c)
1261 int lx = tabular.getWidthOfColumn(cell);
1262 for (; c < cell; ++c)
1263 lx += tabular.getWidthOfColumn(c);
1265 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1269 void InsetTabular::resetPos(BufferView * bv) const
1271 #ifdef WITH_WARNINGS
1272 #warning This should be fixed in the right manner (20011128 Jug)
1274 // fast hack to fix infinite repaintings!
1275 if (in_reset_pos > 0)
1279 actcol = tabular.column_of_cell(actcell);
1282 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1283 if (tabular.isLastCellInRow(cell)) {
1284 cursory_ += tabular.getDescentOfRow(actrow) +
1285 tabular.getAscentOfRow(actrow + 1) +
1286 tabular.getAdditionalHeight(actrow + 1);
1291 if (the_locking_inset)
1295 // we need this only from here on!!!
1297 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1298 int new_x = getCellXPos(actcell);
1299 int old_x = cursorx_;
1302 // cursor.x(getCellXPos(actcell) + offset);
1303 if (actcol < tabular.columns() - 1 && scroll(false) &&
1304 tabular.getWidthOfTabular() < bv->workWidth()-20)
1308 } else if (the_locking_inset &&
1309 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1311 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1312 if (xx > bv->workWidth()-20) {
1313 scroll(bv, -(xx - bv->workWidth() + 60));
1315 } else if (xx < 20) {
1323 } else if (cursorx_ - offset > 20 &&
1324 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1325 > bv->workWidth() - 20) {
1326 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1328 } else if (cursorx_ - offset < 20) {
1329 scroll(bv, 20 - cursorx_ + offset);
1331 } else if (scroll() && top_x > 20 &&
1332 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1333 scroll(bv, old_x - cursorx_);
1336 if (the_locking_inset) {
1337 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1340 if ((!the_locking_inset ||
1341 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1342 actcell != oldcell) {
1343 InsetTabularMailer(*this).updateDialog(bv);
1350 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1352 if (lock && !old_locking_inset) {
1353 if (activateCellInset(bv))
1356 bool moved = isRightToLeft(bv)
1357 ? movePrevCell(bv) : moveNextCell(bv);
1359 return FINISHED_RIGHT;
1360 if (lock && activateCellInset(bv))
1364 return DISPATCHED_NOUPDATE;
1368 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1370 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1373 if (lock) { // behind the inset
1374 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1378 return DISPATCHED_NOUPDATE;
1382 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1384 int const ocell = actcell;
1385 actcell = tabular.getCellAbove(actcell);
1386 if (actcell == ocell) // we moved out of the inset
1392 if (old_locking_inset) {
1393 old_locking_inset->getCursorPos(bv, x, y);
1394 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1396 if (activateCellInset(bv, x, 0))
1399 return DISPATCHED_NOUPDATE;
1403 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1405 int const ocell = actcell;
1406 actcell = tabular.getCellBelow(actcell);
1407 if (actcell == ocell) // we moved out of the inset
1408 return FINISHED_DOWN;
1413 if (old_locking_inset) {
1414 old_locking_inset->getCursorPos(bv, x, y);
1415 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1417 if (activateCellInset(bv, x, 0))
1420 return DISPATCHED_NOUPDATE;
1424 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1426 if (isRightToLeft(bv)) {
1427 if (tabular.isFirstCellInRow(actcell)) {
1428 int row = tabular.row_of_cell(actcell);
1429 if (row == tabular.rows() - 1)
1431 actcell = tabular.getLastCellInRow(row);
1432 actcell = tabular.getCellBelow(actcell);
1439 if (tabular.isLastCell(actcell))
1444 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1445 isRightToLeftPar(bv->buffer()->params);
1446 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1453 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1455 if (isRightToLeft(bv)) {
1456 if (tabular.isLastCellInRow(actcell)) {
1457 int row = tabular.row_of_cell(actcell);
1460 actcell = tabular.getFirstCellInRow(row);
1461 actcell = tabular.getCellAbove(actcell);
1463 if (tabular.isLastCell(actcell))
1468 if (!actcell) // first cell
1473 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1474 isRightToLeftPar(bv->buffer()->params);
1475 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1482 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1486 setSelection(0, tabular.getNumberOfCells() - 1);
1488 if (hasSelection()) {
1489 recordUndo(bv, Undo::ATOMIC);
1490 bool const frozen = undo_frozen;
1493 // apply the fontchange on the whole selection
1498 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1499 for(int i = sel_row_start; i <= sel_row_end; ++i)
1500 for(int j = sel_col_start; j <= sel_col_end; ++j)
1501 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1509 if (the_locking_inset)
1510 the_locking_inset->setFont(bv, font, tall);
1514 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1516 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1519 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1520 string const tmp = tabularFeature[i].feature;
1522 if (tmp == what.substr(0, tmp.length())) {
1523 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1524 //tabularFeatures[i].feature.length())) {
1525 action = tabularFeature[i].action;
1529 if (action == LyXTabular::LAST_ACTION)
1533 ltrim(what.substr(tabularFeature[i].feature.length()));
1534 tabularFeatures(bv, action, val);
1540 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1541 string const & special, bool & flag)
1543 if (special == "dl_above") {
1546 } else if (special == "dl_below") {
1547 ltt.bottomDL = flag;
1549 } else if (special == "empty") {
1561 void InsetTabular::tabularFeatures(BufferView * bv,
1562 LyXTabular::Feature feature,
1563 string const & value)
1569 bool setLines = false;
1570 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1571 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1575 case LyXTabular::M_ALIGN_LEFT:
1576 case LyXTabular::ALIGN_LEFT:
1577 setAlign = LYX_ALIGN_LEFT;
1580 case LyXTabular::M_ALIGN_RIGHT:
1581 case LyXTabular::ALIGN_RIGHT:
1582 setAlign = LYX_ALIGN_RIGHT;
1585 case LyXTabular::M_ALIGN_CENTER:
1586 case LyXTabular::ALIGN_CENTER:
1587 setAlign = LYX_ALIGN_CENTER;
1590 case LyXTabular::ALIGN_BLOCK:
1591 setAlign = LYX_ALIGN_BLOCK;
1594 case LyXTabular::M_VALIGN_TOP:
1595 case LyXTabular::VALIGN_TOP:
1596 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1599 case LyXTabular::M_VALIGN_BOTTOM:
1600 case LyXTabular::VALIGN_BOTTOM:
1601 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1604 case LyXTabular::M_VALIGN_MIDDLE:
1605 case LyXTabular::VALIGN_MIDDLE:
1606 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1613 if (hasSelection()) {
1614 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1616 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1617 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1619 recordUndo(bv, Undo::ATOMIC);
1621 int row = tabular.row_of_cell(actcell);
1622 int column = tabular.column_of_cell(actcell);
1624 LyXTabular::ltType ltt;
1628 case LyXTabular::SET_PWIDTH:
1630 LyXLength const vallen(value);
1631 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1633 bool const update = (tmplen != vallen);
1634 tabular.setColumnPWidth(actcell, vallen);
1636 // We need this otherwise we won't resize
1637 // the insettext of the active cell (if any)
1638 // until later (see InsetText::do_resize)
1639 unlockInsetInInset(bv, the_locking_inset);
1644 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1645 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1646 else if (!vallen.zero()
1647 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1648 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1652 case LyXTabular::SET_MPWIDTH:
1654 LyXLength const vallen(value);
1655 LyXLength const & tmplen = tabular.getPWidth(actcell);
1657 bool const update = (tmplen != vallen);
1658 tabular.setMColumnPWidth(actcell, vallen);
1660 // We need this otherwise we won't resize
1661 // the insettext of the active cell (if any)
1662 // until later (see InsetText::do_resize)
1663 unlockInsetInInset(bv, the_locking_inset);
1668 case LyXTabular::SET_SPECIAL_COLUMN:
1669 case LyXTabular::SET_SPECIAL_MULTI:
1670 tabular.setAlignSpecial(actcell,value,feature);
1673 case LyXTabular::APPEND_ROW:
1674 // append the row into the tabular
1675 unlockInsetInInset(bv, the_locking_inset);
1676 tabular.appendRow(bv->buffer()->params, actcell);
1679 case LyXTabular::APPEND_COLUMN:
1680 // append the column into the tabular
1681 unlockInsetInInset(bv, the_locking_inset);
1682 tabular.appendColumn(bv->buffer()->params, actcell);
1683 actcell = tabular.getCellNumber(row, column);
1686 case LyXTabular::DELETE_ROW:
1687 unlockInsetInInset(bv, the_locking_inset);
1688 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1689 tabular.deleteRow(sel_row_start);
1691 if (sel_row_start >= tabular.rows())
1693 actcell = tabular.getCellNumber(sel_row_start, column);
1697 case LyXTabular::DELETE_COLUMN:
1698 unlockInsetInInset(bv, the_locking_inset);
1699 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1700 tabular.deleteColumn(sel_col_start);
1702 if (sel_col_start >= tabular.columns())
1704 actcell = tabular.getCellNumber(row, sel_col_start);
1708 case LyXTabular::M_TOGGLE_LINE_TOP:
1710 case LyXTabular::TOGGLE_LINE_TOP:
1712 bool lineSet = !tabular.topLine(actcell, flag);
1713 for (int i = sel_row_start; i <= sel_row_end; ++i)
1714 for (int j = sel_col_start; j <= sel_col_end; ++j)
1716 tabular.getCellNumber(i, j),
1722 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1724 case LyXTabular::TOGGLE_LINE_BOTTOM:
1726 bool lineSet = !tabular.bottomLine(actcell, flag);
1727 for (int i = sel_row_start; i <= sel_row_end; ++i)
1728 for (int j = sel_col_start; j <= sel_col_end; ++j)
1729 tabular.setBottomLine(
1730 tabular.getCellNumber(i, j),
1737 case LyXTabular::M_TOGGLE_LINE_LEFT:
1739 case LyXTabular::TOGGLE_LINE_LEFT:
1741 bool lineSet = !tabular.leftLine(actcell, flag);
1742 for (int i = sel_row_start; i <= sel_row_end; ++i)
1743 for (int j = sel_col_start; j <= sel_col_end; ++j)
1744 tabular.setLeftLine(
1745 tabular.getCellNumber(i,j),
1752 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1754 case LyXTabular::TOGGLE_LINE_RIGHT:
1756 bool lineSet = !tabular.rightLine(actcell, flag);
1757 for (int i = sel_row_start; i <= sel_row_end; ++i)
1758 for (int j = sel_col_start; j <= sel_col_end; ++j)
1759 tabular.setRightLine(
1760 tabular.getCellNumber(i,j),
1767 case LyXTabular::M_ALIGN_LEFT:
1768 case LyXTabular::M_ALIGN_RIGHT:
1769 case LyXTabular::M_ALIGN_CENTER:
1771 case LyXTabular::ALIGN_LEFT:
1772 case LyXTabular::ALIGN_RIGHT:
1773 case LyXTabular::ALIGN_CENTER:
1774 case LyXTabular::ALIGN_BLOCK:
1775 for (int i = sel_row_start; i <= sel_row_end; ++i)
1776 for (int j = sel_col_start; j <= sel_col_end; ++j)
1777 tabular.setAlignment(
1778 tabular.getCellNumber(i, j),
1784 case LyXTabular::M_VALIGN_TOP:
1785 case LyXTabular::M_VALIGN_BOTTOM:
1786 case LyXTabular::M_VALIGN_MIDDLE:
1788 case LyXTabular::VALIGN_TOP:
1789 case LyXTabular::VALIGN_BOTTOM:
1790 case LyXTabular::VALIGN_MIDDLE:
1791 for (int i = sel_row_start; i <= sel_row_end; ++i)
1792 for (int j = sel_col_start; j <= sel_col_end; ++j)
1793 tabular.setVAlignment(
1794 tabular.getCellNumber(i, j),
1799 case LyXTabular::MULTICOLUMN: {
1800 if (sel_row_start != sel_row_end) {
1801 #ifdef WITH_WARNINGS
1802 #warning Need I say it ? This is horrible.
1804 Alert::error(_("Error setting multicolumn"),
1805 _("You cannot set multicolumn vertically."));
1808 // just multicol for one Single Cell
1809 if (!hasSelection()) {
1810 // check wether we are completly in a multicol
1811 if (tabular.isMultiColumn(actcell))
1812 tabular.unsetMultiColumn(actcell);
1814 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1818 // we have a selection so this means we just add all this
1819 // cells to form a multicolumn cell
1823 if (sel_cell_start > sel_cell_end) {
1824 s_start = sel_cell_end;
1825 s_end = sel_cell_start;
1827 s_start = sel_cell_start;
1828 s_end = sel_cell_end;
1830 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1837 case LyXTabular::SET_ALL_LINES:
1839 case LyXTabular::UNSET_ALL_LINES:
1840 for (int i = sel_row_start; i <= sel_row_end; ++i)
1841 for (int j = sel_col_start; j <= sel_col_end; ++j)
1842 tabular.setAllLines(
1843 tabular.getCellNumber(i,j), setLines);
1847 case LyXTabular::SET_LONGTABULAR:
1848 tabular.setLongTabular(true);
1849 updateLocal(bv); // because this toggles displayed
1852 case LyXTabular::UNSET_LONGTABULAR:
1853 tabular.setLongTabular(false);
1854 updateLocal(bv); // because this toggles displayed
1857 case LyXTabular::SET_ROTATE_TABULAR:
1858 tabular.setRotateTabular(true);
1861 case LyXTabular::UNSET_ROTATE_TABULAR:
1862 tabular.setRotateTabular(false);
1865 case LyXTabular::SET_ROTATE_CELL:
1866 for (int i = sel_row_start; i <= sel_row_end; ++i)
1867 for (int j = sel_col_start; j<=sel_col_end; ++j)
1868 tabular.setRotateCell(
1869 tabular.getCellNumber(i, j), true);
1872 case LyXTabular::UNSET_ROTATE_CELL:
1873 for (int i = sel_row_start; i <= sel_row_end; ++i)
1874 for (int j = sel_col_start; j <= sel_col_end; ++j)
1875 tabular.setRotateCell(
1876 tabular.getCellNumber(i, j), false);
1879 case LyXTabular::SET_USEBOX: {
1880 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1881 if (val == tabular.getUsebox(actcell))
1882 val = LyXTabular::BOX_NONE;
1883 for (int i = sel_row_start; i <= sel_row_end; ++i)
1884 for (int j = sel_col_start; j <= sel_col_end; ++j)
1885 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1889 case LyXTabular::UNSET_LTFIRSTHEAD:
1891 case LyXTabular::SET_LTFIRSTHEAD:
1892 tabular.getRowOfLTFirstHead(row, ltt);
1893 checkLongtableSpecial(ltt, value, flag);
1894 tabular.setLTHead(row, flag, ltt, true);
1897 case LyXTabular::UNSET_LTHEAD:
1899 case LyXTabular::SET_LTHEAD:
1900 tabular.getRowOfLTHead(row, ltt);
1901 checkLongtableSpecial(ltt, value, flag);
1902 tabular.setLTHead(row, flag, ltt, false);
1905 case LyXTabular::UNSET_LTFOOT:
1907 case LyXTabular::SET_LTFOOT:
1908 tabular.getRowOfLTFoot(row, ltt);
1909 checkLongtableSpecial(ltt, value, flag);
1910 tabular.setLTFoot(row, flag, ltt, false);
1913 case LyXTabular::UNSET_LTLASTFOOT:
1915 case LyXTabular::SET_LTLASTFOOT:
1916 tabular.getRowOfLTLastFoot(row, ltt);
1917 checkLongtableSpecial(ltt, value, flag);
1918 tabular.setLTFoot(row, flag, ltt, true);
1921 case LyXTabular::SET_LTNEWPAGE: {
1922 bool what = !tabular.getLTNewPage(row);
1923 tabular.setLTNewPage(row, what);
1927 // dummy stuff just to avoid warnings
1928 case LyXTabular::LAST_ACTION:
1932 InsetTabularMailer(*this).updateDialog(bv);
1936 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1937 mouse_button::state button, bool behind)
1939 UpdatableInset & inset = tabular.getCellInset(actcell);
1942 x = inset.x() + inset.width();
1943 y = inset.descent();
1945 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1946 //inset_y = cursory_;
1947 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1948 if (!the_locking_inset)
1951 return the_locking_inset;
1955 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1956 mouse_button::state button)
1958 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1960 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1964 bool InsetTabular::insetHit(BufferView *, int x, int) const
1966 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1970 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1972 //resizeLyXText(bv, recursive);
1976 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1977 bool const recursive) const
1979 if (the_locking_inset)
1980 return the_locking_inset->getLyXText(bv, recursive);
1981 return InsetOld::getLyXText(bv, recursive);
1985 bool InsetTabular::showInsetDialog(BufferView * bv) const
1987 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1988 InsetTabularMailer(*this).showDialog(bv);
1993 void InsetTabular::openLayoutDialog(BufferView * bv) const
1995 if (the_locking_inset) {
1996 InsetTabular * inset = static_cast<InsetTabular *>
1997 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
1999 inset->openLayoutDialog(bv);
2003 InsetTabularMailer(*this).showDialog(bv);
2008 // function returns an object as defined in func_status.h:
2009 // states OK, Unknown, Disabled, On, Off.
2011 FuncStatus InsetTabular::getStatus(string const & what) const
2013 int action = LyXTabular::LAST_ACTION;
2017 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2018 string const tmp = tabularFeature[i].feature;
2019 if (tmp == what.substr(0, tmp.length())) {
2020 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2021 // tabularFeatures[i].feature.length())) {
2022 action = tabularFeature[i].action;
2026 if (action == LyXTabular::LAST_ACTION) {
2028 return status.unknown(true);
2031 string const argument
2032 = ltrim(what.substr(tabularFeature[i].feature.length()));
2037 LyXTabular::ltType dummyltt;
2041 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2043 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2046 case LyXTabular::SET_PWIDTH:
2047 case LyXTabular::SET_MPWIDTH:
2048 case LyXTabular::SET_SPECIAL_COLUMN:
2049 case LyXTabular::SET_SPECIAL_MULTI:
2050 case LyXTabular::APPEND_ROW:
2051 case LyXTabular::APPEND_COLUMN:
2052 case LyXTabular::DELETE_ROW:
2053 case LyXTabular::DELETE_COLUMN:
2054 case LyXTabular::SET_ALL_LINES:
2055 case LyXTabular::UNSET_ALL_LINES:
2056 return status.clear();
2058 case LyXTabular::MULTICOLUMN:
2059 status.setOnOff(tabular.isMultiColumn(actcell));
2062 case LyXTabular::M_TOGGLE_LINE_TOP:
2064 case LyXTabular::TOGGLE_LINE_TOP:
2065 status.setOnOff(tabular.topLine(actcell, flag));
2068 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2070 case LyXTabular::TOGGLE_LINE_BOTTOM:
2071 status.setOnOff(tabular.bottomLine(actcell, flag));
2074 case LyXTabular::M_TOGGLE_LINE_LEFT:
2076 case LyXTabular::TOGGLE_LINE_LEFT:
2077 status.setOnOff(tabular.leftLine(actcell, flag));
2080 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2082 case LyXTabular::TOGGLE_LINE_RIGHT:
2083 status.setOnOff(tabular.rightLine(actcell, flag));
2086 case LyXTabular::M_ALIGN_LEFT:
2088 case LyXTabular::ALIGN_LEFT:
2089 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2092 case LyXTabular::M_ALIGN_RIGHT:
2094 case LyXTabular::ALIGN_RIGHT:
2095 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2098 case LyXTabular::M_ALIGN_CENTER:
2100 case LyXTabular::ALIGN_CENTER:
2101 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2104 case LyXTabular::ALIGN_BLOCK:
2105 status.disabled(tabular.getPWidth(actcell).zero());
2106 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2109 case LyXTabular::M_VALIGN_TOP:
2111 case LyXTabular::VALIGN_TOP:
2112 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2115 case LyXTabular::M_VALIGN_BOTTOM:
2117 case LyXTabular::VALIGN_BOTTOM:
2118 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2121 case LyXTabular::M_VALIGN_MIDDLE:
2123 case LyXTabular::VALIGN_MIDDLE:
2124 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2127 case LyXTabular::SET_LONGTABULAR:
2128 status.setOnOff(tabular.isLongTabular());
2131 case LyXTabular::UNSET_LONGTABULAR:
2132 status.setOnOff(!tabular.isLongTabular());
2135 case LyXTabular::SET_ROTATE_TABULAR:
2136 status.setOnOff(tabular.getRotateTabular());
2139 case LyXTabular::UNSET_ROTATE_TABULAR:
2140 status.setOnOff(!tabular.getRotateTabular());
2143 case LyXTabular::SET_ROTATE_CELL:
2144 status.setOnOff(tabular.getRotateCell(actcell));
2147 case LyXTabular::UNSET_ROTATE_CELL:
2148 status.setOnOff(!tabular.getRotateCell(actcell));
2151 case LyXTabular::SET_USEBOX:
2152 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2155 case LyXTabular::SET_LTFIRSTHEAD:
2156 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2159 case LyXTabular::SET_LTHEAD:
2160 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2163 case LyXTabular::SET_LTFOOT:
2164 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2167 case LyXTabular::SET_LTLASTFOOT:
2168 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2171 case LyXTabular::SET_LTNEWPAGE:
2172 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2177 status.disabled(true);
2184 void InsetTabular::getLabelList(std::vector<string> & list) const
2186 tabular.getLabelList(list);
2190 bool InsetTabular::copySelection(BufferView * bv)
2192 if (!hasSelection())
2195 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2196 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2197 if (sel_col_start > sel_col_end) {
2198 sel_col_start = sel_col_end;
2199 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2201 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2204 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2205 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2206 if (sel_row_start > sel_row_end)
2207 swap(sel_row_start, sel_row_end);
2209 delete paste_tabular;
2210 paste_tabular = new LyXTabular(bv->buffer()->params, this, tabular);
2212 for (int i = 0; i < sel_row_start; ++i)
2213 paste_tabular->deleteRow(0);
2215 int const rows = sel_row_end - sel_row_start + 1;
2216 while (paste_tabular->rows() > rows)
2217 paste_tabular->deleteRow(rows);
2219 paste_tabular->setTopLine(0, true, true);
2220 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2223 for (int i = 0; i < sel_col_start; ++i)
2224 paste_tabular->deleteColumn(0);
2226 int const columns = sel_col_end - sel_col_start + 1;
2227 while (paste_tabular->columns() > columns)
2228 paste_tabular->deleteColumn(columns);
2230 paste_tabular->setLeftLine(0, true, true);
2231 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2235 paste_tabular->ascii(*bv->buffer(), os,
2236 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2237 bv->stuffClipboard(STRCONV(os.str()));
2242 bool InsetTabular::pasteSelection(BufferView * bv)
2247 for (int r1 = 0, r2 = actrow;
2248 r1 < paste_tabular->rows() && r2 < tabular.rows();
2250 for (int c1 = 0, c2 = actcol;
2251 c1 < paste_tabular->columns() && c2 < tabular.columns();
2253 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2254 tabular.isPartOfMultiColumn(r2, c2))
2256 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2260 if (tabular.isPartOfMultiColumn(r2, c2)) {
2264 InsetText & inset = tabular.getCellInset(r2, c2);
2265 inset = paste_tabular->getCellInset(r1, c1);
2266 inset.setOwner(this);
2267 inset.deleteLyXText(bv);
2275 bool InsetTabular::cutSelection(BufferParams const & bp)
2277 if (!hasSelection())
2280 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2281 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2282 if (sel_col_start > sel_col_end) {
2283 sel_col_start = sel_col_end;
2284 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2286 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2289 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2290 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2292 if (sel_row_start > sel_row_end)
2293 swap(sel_row_start, sel_row_end);
2295 if (sel_cell_start > sel_cell_end)
2296 swap(sel_cell_start, sel_cell_end);
2298 for (int i = sel_row_start; i <= sel_row_end; ++i)
2299 for (int j = sel_col_start; j <= sel_col_end; ++j)
2300 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2305 bool InsetTabular::isRightToLeft(BufferView * bv)
2307 return bv->getParentLanguage(this)->RightToLeft();
2311 int InsetTabular::scroll(bool recursive) const
2313 int sx = UpdatableInset::scroll(false);
2315 if (recursive && the_locking_inset)
2316 sx += the_locking_inset->scroll(recursive);
2322 void InsetTabular::getSelection(int & srow, int & erow,
2323 int & scol, int & ecol) const
2325 int const start = hasSelection() ? sel_cell_start : actcell;
2326 int const end = hasSelection() ? sel_cell_end : actcell;
2328 srow = tabular.row_of_cell(start);
2329 erow = tabular.row_of_cell(end);
2333 scol = tabular.column_of_cell(start);
2334 ecol = tabular.column_of_cell(end);
2338 ecol = tabular.right_column_of_cell(end);
2342 ParagraphList * InsetTabular::getParagraphs(int i) const
2344 return (i < tabular.getNumberOfCells())
2345 ? tabular.getCellInset(i).getParagraphs(0)
2350 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2352 if (the_locking_inset)
2353 return the_locking_inset->cursor(bv);
2354 return InsetOld::cursor(bv);
2358 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2361 return const_cast<InsetTabular *>(this);
2363 for (int i = 0; i < tabular.rows(); ++i) {
2364 for (int j = 0; j < tabular.columns(); ++j) {
2365 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2375 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2377 if (the_locking_inset) {
2378 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2379 if (!word.word().empty())
2381 if (tabular.isLastCell(actcell)) {
2382 bv->unlockInset(const_cast<InsetTabular *>(this));
2383 return WordLangTuple();
2387 // otherwise we have to lock the next inset and ask for it's selecttion
2388 tabular.getCellInset(actcell)
2389 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2390 WordLangTuple word(selectNextWordInt(bv, value));
2391 if (!word.word().empty())
2397 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2399 // when entering this function the inset should be ALWAYS locked!
2400 Assert(the_locking_inset);
2402 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2403 if (!word.word().empty())
2406 if (tabular.isLastCell(actcell)) {
2407 bv->unlockInset(const_cast<InsetTabular *>(this));
2408 return WordLangTuple();
2411 // otherwise we have to lock the next inset and ask for it's selecttion
2413 tabular.getCellInset(actcell)
2414 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2415 return selectNextWordInt(bv, value);
2419 void InsetTabular::selectSelectedWord(BufferView * bv)
2421 if (the_locking_inset)
2422 the_locking_inset->selectSelectedWord(bv);
2426 void InsetTabular::markErased()
2428 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2429 tabular.getCellInset(cell).markErased();
2433 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2435 if (the_locking_inset) {
2436 if (the_locking_inset->nextChange(bv, length)) {
2440 if (tabular.isLastCell(actcell))
2444 InsetText & inset = tabular.getCellInset(actcell);
2445 if (inset.nextChange(bv, length)) {
2449 while (!tabular.isLastCell(actcell)) {
2451 InsetText & inset = tabular.getCellInset(actcell);
2452 if (inset.nextChange(bv, length)) {
2461 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2465 if (the_locking_inset) {
2466 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2470 if (tabular.isLastCell(actcell))
2474 InsetText & inset = tabular.getCellInset(cell);
2475 if (inset.searchForward(bv, str, cs, mw)) {
2479 while (!tabular.isLastCell(cell)) {
2481 InsetText & inset = tabular.getCellInset(cell);
2482 if (inset.searchForward(bv, str, cs, mw)) {
2491 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2494 int cell = tabular.getNumberOfCells();
2495 if (the_locking_inset) {
2496 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2505 InsetText & inset = tabular.getCellInset(cell);
2506 if (inset.searchBackward(bv, str, cs, mw)) {
2515 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2517 if (the_locking_inset)
2518 return the_locking_inset->insetAllowed(code);
2519 // we return true here because if the inset is not locked someone
2520 // wants to insert something in one of our insettexts and we generally
2526 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2528 const int cell = tabular.getCellFromInset(in, actcell);
2531 return tabular.getPWidth(cell).zero();
2533 // well we didn't obviously find it so maybe our owner knows more
2535 return owner()->forceDefaultParagraphs(in);
2537 lyxerr << "If we're here there is really something strange going on!"
2543 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2546 if (buf.length() <= 0)
2552 string::size_type len = buf.length();
2553 string::size_type p = 0;
2555 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2563 maxCols = max(cols, maxCols);
2569 maxCols = max(cols, maxCols);
2570 LyXTabular * loctab;
2575 delete paste_tabular;
2576 paste_tabular = new LyXTabular(bv->buffer()->params,
2577 this, rows, maxCols);
2578 loctab = paste_tabular;
2587 string::size_type op = 0;
2588 int cells = loctab->getNumberOfCells();
2591 rows = loctab->rows();
2592 int const columns = loctab->columns();
2594 while (cell < cells && p < len && row < rows &&
2595 (p = buf.find_first_of("\t\n", p)) != string::npos)
2601 // we can only set this if we are not too far right
2602 if (cols < columns) {
2603 InsetText & inset = loctab->getCellInset(cell);
2604 LyXFont const font = inset.getLyXText(bv)->
2605 getFont(inset.paragraphs.begin(), 0);
2606 inset.setText(buf.substr(op, p - op), font);
2612 // we can only set this if we are not too far right
2613 if (cols < columns) {
2614 InsetText & inset = tabular.getCellInset(cell);
2615 LyXFont const font = inset.getLyXText(bv)->
2616 getFont(inset.paragraphs.begin(), 0);
2617 inset.setText(buf.substr(op, p - op), font);
2622 cell = loctab->getCellNumber(row, cols);
2628 // check for the last cell if there is no trailing '\n'
2629 if (cell < cells && op < len) {
2630 InsetText & inset = loctab->getCellInset(cell);
2631 LyXFont const font = inset.getLyXText(bv)->
2632 getFont(inset.paragraphs.begin(), 0);
2633 inset.setText(buf.substr(op, len - op), font);
2640 void InsetTabular::addPreview(PreviewLoader & loader) const
2642 int const rows = tabular.rows();
2643 int const columns = tabular.columns();
2644 for (int i = 0; i < rows; ++i)
2645 for (int j = 0; j < columns; ++j)
2646 tabular.getCellInset(i, j).addPreview(loader);
2650 string const InsetTabularMailer::name_("tabular");
2652 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2653 : inset_(const_cast<InsetTabular &>(inset))
2657 string const InsetTabularMailer::inset2string(Buffer const &) const
2659 return params2string(inset_);
2663 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2665 istringstream data(STRCONV(in));
2667 lex.setStream(data);
2669 #warning CHECK verify that this is a sane value to return.
2675 string const token = lex.getString();
2683 string const token = lex.getString();
2684 if (token != "\\active_cell")
2687 cell = lex.getInteger();
2690 // This is part of the inset proper that is usually swallowed
2691 // by Buffer::readInset
2694 string const token = lex.getString();
2695 if (token != "Tabular")
2702 Buffer const & buffer = inset.buffer();
2703 inset.read(buffer, lex);
2705 // We can't set the active cell, but we can tell the frontend
2711 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2713 Buffer const & buffer = inset.buffer();
2716 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2717 inset.write(buffer, data);
2718 data << "\\end_inset\n";
2719 return STRCONV(data.str());