3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
31 #include "WordLangTuple.h"
33 #include "frontends/Alert.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/Painter.h"
38 #include "support/std_sstream.h"
41 using lyx::graphics::PreviewLoader;
43 using lyx::support::ltrim;
44 using lyx::support::strToInt;
51 using std::istringstream;
53 using std::ostringstream;
58 int const ADD_TO_HEIGHT = 2;
59 int const ADD_TO_TABULAR_WIDTH = 2;
62 LyXTabular * paste_tabular = 0;
65 struct TabularFeature {
66 LyXTabular::Feature action;
71 TabularFeature tabularFeature[] =
73 { LyXTabular::APPEND_ROW, "append-row" },
74 { LyXTabular::APPEND_COLUMN, "append-column" },
75 { LyXTabular::DELETE_ROW, "delete-row" },
76 { LyXTabular::DELETE_COLUMN, "delete-column" },
77 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
78 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
79 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
80 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
81 { LyXTabular::ALIGN_LEFT, "align-left" },
82 { LyXTabular::ALIGN_RIGHT, "align-right" },
83 { LyXTabular::ALIGN_CENTER, "align-center" },
84 { LyXTabular::ALIGN_BLOCK, "align-block" },
85 { LyXTabular::VALIGN_TOP, "valign-top" },
86 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
87 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
88 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
89 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
90 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
91 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
92 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
93 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
94 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
95 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
96 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
97 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
98 { LyXTabular::MULTICOLUMN, "multicolumn" },
99 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
100 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
101 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
102 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
103 { LyXTabular::SET_PWIDTH, "set-pwidth" },
104 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
105 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
106 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
107 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
108 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
109 { LyXTabular::SET_USEBOX, "set-usebox" },
110 { LyXTabular::SET_LTHEAD, "set-lthead" },
111 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
112 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
113 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
114 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
115 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
116 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
117 { LyXTabular::LAST_ACTION, "" }
121 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
122 bool operator()(TabularFeature & tf)
124 return tf.action == feature_;
127 LyXTabular::Feature feature_;
133 string const featureAsString(LyXTabular::Feature feature)
135 TabularFeature * it = tabularFeature;
136 TabularFeature * end = it +
137 sizeof(tabularFeature) / sizeof(TabularFeature);
138 it = std::find_if(it, end, FindFeature(feature));
139 return (it == end) ? string() : it->feature;
143 bool InsetTabular::hasPasteBuffer() const
145 return (paste_tabular != 0);
149 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
150 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
151 buffer_(&buf), cursorx_(0), cursory_(0)
153 tabular.setOwner(this);
154 // for now make it always display as display() inset
156 the_locking_inset = 0;
157 old_locking_inset = 0;
160 actrow = actcell = 0;
168 InsetTabular::InsetTabular(InsetTabular const & tab)
169 : UpdatableInset(tab), tabular(tab.tabular),
170 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
172 tabular.setOwner(this);
173 the_locking_inset = 0;
174 old_locking_inset = 0;
177 actrow = actcell = 0;
185 InsetTabular::~InsetTabular()
187 InsetTabularMailer(*this).hideDialog();
191 auto_ptr<InsetBase> InsetTabular::clone() const
193 return auto_ptr<InsetBase>(new InsetTabular(*this));
197 Buffer const & InsetTabular::buffer() const
203 BufferView * InsetTabular::view() const
210 void InsetTabular::buffer(Buffer * b)
216 void InsetTabular::write(Buffer const & buf, ostream & os) const
218 os << "Tabular" << endl;
219 tabular.write(buf, os);
223 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
225 bool const old_format = (lex.getString() == "\\LyXTable");
227 tabular.read(buf, lex);
233 string token = lex.getString();
234 while (lex.isOK() && (token != "\\end_inset")) {
236 token = lex.getString();
238 if (token != "\\end_inset") {
239 lex.printError("Missing \\end_inset at this point. "
245 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
247 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
248 // mi.base.textwidth << "\n";
250 lyxerr << "InsetTabular::metrics: need bv" << endl;
254 calculate_dimensions_of_cells(mi);
256 dim.asc = tabular.getAscentOfRow(0);
257 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
258 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
263 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
265 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
267 BufferView * bv = pi.base.bv;
270 UpdatableInset::draw(pi, x, y);
278 x += ADD_TO_TABULAR_WIDTH;
281 first_visible_cell = -1;
282 for (int i = 0; i < tabular.rows(); ++i) {
284 cell = tabular.getCellNumber(i, 0);
285 if (y + tabular.getDescentOfRow(i) <= 0 &&
286 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
288 y += tabular.getDescentOfRow(i) +
289 tabular.getAscentOfRow(i + 1) +
290 tabular.getAdditionalHeight(i + 1);
293 for (int j = 0; j < tabular.columns(); ++j) {
294 if (nx > bv->workWidth())
296 if (tabular.isPartOfMultiColumn(i, j))
298 int cx = nx + tabular.getBeginningOfTextInCell(cell);
299 if (first_visible_cell < 0)
300 first_visible_cell = cell;
301 if (hasSelection()) {
302 drawCellSelection(pi.pain, nx, y, i, j, cell);
305 tabular.getCellInset(cell).draw(pi, cx, y);
306 drawCellLines(pi.pain, nx, y, i, cell);
307 nx += tabular.getWidthOfColumn(cell);
311 // Would be nice, but for some completely unfathomable reason,
312 // on a col resize to a new fixed width, even though the insettexts
313 // are resized, the cell isn't, but drawing all cells in a tall table
314 // has the desired effect somehow. Complete dark magic.
316 // avoiding drawing the rest of a long table is
317 // a pretty big speedup
318 if (y > bv->workHeight())
322 y += tabular.getDescentOfRow(i) +
323 tabular.getAscentOfRow(i + 1) +
324 tabular.getAdditionalHeight(i + 1);
329 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
330 int row, int cell) const
332 int x2 = x + tabular.getWidthOfColumn(cell);
335 if (!tabular.topAlreadyDrawn(cell)) {
336 on_off = !tabular.topLine(cell);
337 pain.line(x, y - tabular.getAscentOfRow(row),
338 x2, y - tabular.getAscentOfRow(row),
339 on_off ? LColor::tabularonoffline : LColor::tabularline,
340 on_off ? Painter::line_onoffdash : Painter::line_solid);
342 on_off = !tabular.bottomLine(cell);
343 pain.line(x, y + tabular.getDescentOfRow(row),
344 x2, y + tabular.getDescentOfRow(row),
345 on_off ? LColor::tabularonoffline : LColor::tabularline,
346 on_off ? Painter::line_onoffdash : Painter::line_solid);
347 if (!tabular.leftAlreadyDrawn(cell)) {
348 on_off = !tabular.leftLine(cell);
349 pain.line(x, y - tabular.getAscentOfRow(row),
350 x, y + tabular.getDescentOfRow(row),
351 on_off ? LColor::tabularonoffline : LColor::tabularline,
352 on_off ? Painter::line_onoffdash : Painter::line_solid);
354 on_off = !tabular.rightLine(cell);
355 pain.line(x2 - tabular.getAdditionalWidth(cell),
356 y - tabular.getAscentOfRow(row),
357 x2 - tabular.getAdditionalWidth(cell),
358 y + tabular.getDescentOfRow(row),
359 on_off ? LColor::tabularonoffline : LColor::tabularline,
360 on_off ? Painter::line_onoffdash : Painter::line_solid);
364 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
365 int row, int column, int cell) const
367 BOOST_ASSERT(hasSelection());
368 int cs = tabular.column_of_cell(sel_cell_start);
369 int ce = tabular.column_of_cell(sel_cell_end);
372 cs = tabular.column_of_cell(sel_cell_end);
374 ce = tabular.right_column_of_cell(sel_cell_end);
377 int rs = tabular.row_of_cell(sel_cell_start);
378 int re = tabular.row_of_cell(sel_cell_end);
382 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
383 int w = tabular.getWidthOfColumn(cell);
384 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
385 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
386 w, h, LColor::selection);
391 string const InsetTabular::editMessage() const
393 return _("Opened table");
397 void InsetTabular::insetUnlock(BufferView * bv)
399 if (the_locking_inset) {
400 the_locking_inset->insetUnlock(bv);
402 the_locking_inset = 0;
407 if (scroll(false) || hasSelection()) {
416 void InsetTabular::updateLocal(BufferView * bv) const
418 bv->updateInset(this);
424 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
426 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
433 if (inset == &tabular.getCellInset(actcell)) {
434 lyxerr[Debug::INSETTEXT] << "OK" << endl;
435 the_locking_inset = &tabular.getCellInset(actcell);
440 if (!the_locking_inset) {
441 int const n = tabular.getNumberOfCells();
442 int const id = inset->id();
443 for (int i = 0; i < n; ++i) {
444 InsetText * in = &tabular.getCellInset(i);
447 the_locking_inset = in;
452 if (in->getInsetFromID(id)) {
454 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
455 return the_locking_inset->lockInsetInInset(bv, inset);
461 if (the_locking_inset && (the_locking_inset == inset)) {
462 lyxerr[Debug::INSETTEXT] << "OK" << endl;
467 if (the_locking_inset) {
468 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
469 return the_locking_inset->lockInsetInInset(bv, inset);
472 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
477 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
480 if (!the_locking_inset)
482 if (the_locking_inset == inset) {
483 the_locking_inset->insetUnlock(bv);
485 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
492 // this has to be here otherwise we don't redraw the cell!
493 the_locking_inset = 0;
496 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
497 if (inset->lyxCode() == TABULAR_CODE &&
498 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
499 InsetTabularMailer(*this).updateDialog(bv);
508 int InsetTabular::insetInInsetY() const
510 if (!the_locking_inset)
512 return inset_y + the_locking_inset->insetInInsetY();
516 UpdatableInset * InsetTabular::getLockingInset() const
518 return the_locking_inset ? the_locking_inset->getLockingInset() :
519 const_cast<InsetTabular *>(this);
523 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
527 if (the_locking_inset)
528 return the_locking_inset->getFirstLockingInsetOfType(c);
533 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
535 if (the_locking_inset)
536 return the_locking_inset->insertInset(bv, inset);
541 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
543 if (hasSelection() && cmd.button() == mouse_button::button3)
546 if (hasSelection()) {
548 updateLocal(cmd.view());
551 int const ocell = actcell;
552 BufferView * bv = cmd.view();
556 the_locking_inset = 0;
560 setPos(bv, cmd.x, cmd.y);
563 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
565 if (ocell == actcell && the_locking_inset && inset_hit) {
567 FuncRequest cmd1 = cmd;
570 the_locking_inset->localDispatch(cmd1);
574 if (the_locking_inset) {
575 the_locking_inset->insetUnlock(bv);
577 the_locking_inset = 0;
580 if (cmd.button() == mouse_button::button2) {
581 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
585 if (inset_hit && bv->theLockingInset()) {
586 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
587 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
590 FuncRequest cmd1 = cmd;
593 the_locking_inset->localDispatch(cmd1);
598 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
601 if (the_locking_inset) {
602 FuncRequest cmd1 = cmd;
605 ret = the_locking_inset->localDispatch(cmd1);
607 if (cmd.button() == mouse_button::button3 && !ret) {
608 InsetTabularMailer(*this).showDialog(cmd.view());
615 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
617 if (the_locking_inset) {
618 FuncRequest cmd1 = cmd;
621 the_locking_inset->localDispatch(cmd1);
625 BufferView * bv = cmd.view();
626 int const old_cell = actcell;
628 setPos(bv, cmd.x, cmd.y);
629 if (!hasSelection()) {
630 setSelection(actcell, actcell);
632 } else if (old_cell != actcell) {
633 setSelection(sel_cell_start, actcell);
639 dispatch_result InsetTabular::localDispatch(FuncRequest const & cmd)
641 // We need to save the value of the_locking_inset as the call to
642 // the_locking_inset->localDispatch might unlock it.
643 old_locking_inset = the_locking_inset;
644 dispatch_result result = UpdatableInset::localDispatch(cmd);
645 BufferView * bv = cmd.view();
647 if (cmd.action == LFUN_INSET_EDIT) {
649 lyxerr << "InsetTabular::edit: " << this << " args: '"
650 << cmd.argument << "' first cell: "
651 << &tabular.cell_info[0][0].inset << endl;
653 if (!bv->lockInset(this)) {
654 lyxerr << "InsetTabular::Cannot lock inset" << endl;
660 the_locking_inset = 0;
664 if (cmd.argument.size()) {
665 if (cmd.argument == "left") {
666 if (isRightToLeft(bv))
667 actcell = tabular.getLastCellInRow(0);
671 if (isRightToLeft(bv))
672 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
674 actcell = tabular.getNumberOfCells() - 1;
682 setPos(bv, cmd.x, cmd.y);
685 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
686 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
688 activateCellInset(bv, cmd.x - inset_x, cmd.y - inset_y, cmd.button());
694 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
699 if (cmd.action < 0 && cmd.argument.empty())
702 bool hs = hasSelection();
705 // this one have priority over the locked InsetText, if we're not already
706 // inside another tabular then that one get's priority!
707 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
708 switch (cmd.action) {
709 case LFUN_MOUSE_PRESS:
713 case LFUN_MOUSE_MOTION:
714 lfunMouseMotion(cmd);
717 case LFUN_MOUSE_RELEASE:
718 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
720 case LFUN_CELL_BACKWARD:
721 case LFUN_CELL_FORWARD:
722 unlockInsetInInset(bv, the_locking_inset);
723 if (cmd.action == LFUN_CELL_FORWARD)
724 moveNextCell(bv, old_locking_inset != 0);
726 movePrevCell(bv, old_locking_inset != 0);
730 if (!the_locking_inset)
731 return DISPATCHED_NOUPDATE;
733 // this to avoid compiler warnings.
739 kb_action action = cmd.action;
740 string arg = cmd.argument;
741 if (the_locking_inset) {
742 result = the_locking_inset->localDispatch(cmd);
743 if (result == DISPATCHED_NOUPDATE) {
746 if (sc != scroll()) { // inset has been scrolled
750 } else if (result == DISPATCHED) {
753 } else if (result == FINISHED_UP) {
755 // Make sure to reset status message after
756 // exiting, e.g. math inset
757 bv->owner()->clearMessage();
758 } else if (result == FINISHED_DOWN) {
760 bv->owner()->clearMessage();
761 } else if (result == FINISHED_RIGHT) {
763 bv->owner()->clearMessage();
764 } else if (result == FINISHED) {
765 bv->owner()->clearMessage();
771 // --- Cursor Movements ----------------------------------
772 case LFUN_RIGHTSEL: {
773 int const start = hasSelection() ? sel_cell_start : actcell;
774 if (tabular.isLastCellInRow(actcell)) {
775 setSelection(start, actcell);
780 // if we are starting a selection, only select
781 // the current cell at the beginning
782 if (hasSelection()) {
783 moveRight(bv, false);
786 setSelection(start, end);
791 result = moveRight(bv);
797 int const start = hasSelection() ? sel_cell_start : actcell;
798 if (tabular.isFirstCellInRow(actcell)) {
799 setSelection(start, actcell);
804 // if we are starting a selection, only select
805 // the current cell at the beginning
806 if (hasSelection()) {
810 setSelection(start, end);
815 result = moveLeft(bv);
821 int const start = hasSelection() ? sel_cell_start : actcell;
822 int const ocell = actcell;
823 // if we are starting a selection, only select
824 // the current cell at the beginning
825 if (hasSelection()) {
827 if (ocell == sel_cell_end ||
828 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
829 setSelection(start, tabular.getCellBelow(sel_cell_end));
831 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
833 setSelection(start, start);
839 result = moveDown(bv, old_locking_inset != 0);
845 int const start = hasSelection() ? sel_cell_start : actcell;
846 int const ocell = actcell;
847 // if we are starting a selection, only select
848 // the current cell at the beginning
849 if (hasSelection()) {
851 if ((ocell == sel_cell_end) ||
852 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
853 setSelection(start, tabular.getCellAbove(sel_cell_end));
855 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
857 setSelection(start, start);
863 result = moveUp(bv, old_locking_inset != 0);
872 unlockInsetInInset(bv, the_locking_inset);
873 if (bv->top_y() + bv->painter().paperHeight() <
874 top_baseline + tabular.getHeightOfTabular())
876 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
877 actcell = tabular.getCellBelow(first_visible_cell) + column;
879 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
889 unlockInsetInInset(bv, the_locking_inset);
890 if (top_baseline < 0) {
891 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
892 if (top_baseline > 0)
895 actcell = tabular.getCellBelow(first_visible_cell) + column;
903 // none of these make sense for insettabular,
904 // but we must catch them to prevent any
905 // selection from being confused
909 case LFUN_WORDLEFTSEL:
911 case LFUN_WORDRIGHTSEL:
913 case LFUN_DOWN_PARAGRAPH:
914 case LFUN_DOWN_PARAGRAPHSEL:
915 case LFUN_UP_PARAGRAPH:
916 case LFUN_UP_PARAGRAPHSEL:
922 case LFUN_BEGINNINGBUF:
923 case LFUN_BEGINNINGBUFSEL:
927 case LFUN_LAYOUT_TABULAR:
928 InsetTabularMailer(*this).showDialog(bv);
930 case LFUN_INSET_DIALOG_UPDATE:
931 InsetTabularMailer(*this).updateDialog(bv);
933 case LFUN_TABULAR_FEATURE:
934 if (!tabularFeatures(bv, arg))
935 result = UNDISPATCHED;
937 // insert file functions
938 case LFUN_FILE_INSERT_ASCII_PARA:
939 case LFUN_FILE_INSERT_ASCII:
941 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
944 if (insertAsciiString(bv, tmpstr, false))
947 result = UNDISPATCHED;
950 // cut and paste functions
952 if (!copySelection(bv))
956 recordUndo(bv, Undo::DELETE);
957 cutSelection(bv->buffer()->params());
966 case LFUN_PASTESELECTION:
968 string const clip = bv->getClipboard();
972 if (clip.find('\t') != string::npos) {
976 string::size_type len = clip.length();
977 string::size_type p = 0;
980 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
988 maxCols = max(cols, maxCols);
994 maxCols = max(cols, maxCols);
995 delete paste_tabular;
996 paste_tabular = new LyXTabular(bv->buffer()->params(),
997 this, rows, maxCols);
998 string::size_type op = 0;
1000 int cells = paste_tabular->getNumberOfCells();
1002 while ((cell < cells) && (p < len) &&
1003 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1008 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1013 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1014 while (cols++ < maxCols)
1022 // check for the last cell if there is no trailing '\n'
1023 if ((cell < cells) && (op < len))
1024 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1027 if (!insertAsciiString(bv, clip, true))
1030 // so that the clipboard is used and it goes on
1032 // and executes LFUN_PASTESELECTION in insettext!
1033 delete paste_tabular;
1038 if (hasPasteBuffer()) {
1039 recordUndo(bv, Undo::INSERT);
1044 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1046 // handle font changing stuff on selection before we lock the inset
1047 // in the default part!
1048 result = UNDISPATCHED;
1059 case LFUN_UNDERLINE:
1060 case LFUN_FONT_SIZE:
1061 if (bv->dispatch(FuncRequest(bv, action, arg)))
1062 result = DISPATCHED;
1068 // we try to activate the actual inset and put this event down to
1069 // the insets dispatch function.
1070 if (result == DISPATCHED || the_locking_inset)
1072 if (activateCellInset(bv)) {
1073 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1074 if (result == UNDISPATCHED || result >= FINISHED) {
1075 unlockInsetInInset(bv, the_locking_inset);
1076 // we need to update if this was requested before
1078 return UNDISPATCHED;
1087 if (result < FINISHED) {
1088 if (!the_locking_inset && bv->fitCursor())
1091 bv->unlockInset(this);
1096 int InsetTabular::latex(Buffer const & buf, ostream & os,
1097 LatexRunParams const & runparams) const
1099 return tabular.latex(buf, os, runparams);
1103 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1106 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1108 return tabular.ascii(buf, os, 0, false, 0);
1112 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1114 return tabular.linuxdoc(buf,os);
1118 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1123 // if the table is inside a float it doesn't need the informaltable
1124 // wrapper. Search for it.
1125 for (master = owner();
1126 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1127 master = master->owner());
1130 os << "<informaltable>";
1135 ret += tabular.docbook(buf, os, mixcont);
1137 os << "</informaltable>";
1146 void InsetTabular::validate(LaTeXFeatures & features) const
1148 tabular.validate(features);
1152 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1155 // if we have a locking_inset we should have to check only this cell for
1156 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1157 // This is _really_ important (André)
1158 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1161 for (int j = 0; j < tabular.columns(); ++j) {
1165 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1166 tabular.getCellInset(actrow, j).metrics(m, dim);
1167 maxAsc = max(dim.asc, maxAsc);
1168 maxDesc = max(dim.des, maxDesc);
1170 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1171 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1172 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1178 for (int i = 0; i < tabular.rows(); ++i) {
1181 for (int j = 0; j < tabular.columns(); ++j) {
1182 if (tabular.isPartOfMultiColumn(i, j))
1188 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1189 tabular.getCellInset(cell).metrics(m, dim);
1190 maxAsc = max(maxAsc, dim.asc);
1191 maxDesc = max(maxDesc, dim.des);
1192 tabular.setWidthOfCell(cell, dim.wid);
1194 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1195 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1200 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1202 if (the_locking_inset) {
1203 the_locking_inset->getCursor(bv, x, y);
1208 y = cursory_ + InsetTabular::y();
1211 int desc = tabular.getDescentOfRow(actrow);
1213 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1215 y += ADD_TO_HEIGHT * 2;
1216 y += TEXT_TO_INSET_OFFSET;
1220 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1222 if (the_locking_inset) {
1223 the_locking_inset->getCursorPos(bv, x, y);
1226 x = cursorx_ - top_x;
1231 void InsetTabular::fitInsetCursor(BufferView * bv) const
1233 if (the_locking_inset) {
1234 the_locking_inset->fitInsetCursor(bv);
1239 int const asc = font_metrics::maxAscent(font);
1240 int const desc = font_metrics::maxDescent(font);
1243 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1247 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1250 actcell = actrow = actcol = 0;
1251 int ly = tabular.getDescentOfRow(actrow);
1253 // first search the right row
1254 while (ly < y && actrow + 1 < tabular.rows()) {
1255 cursory_ += tabular.getDescentOfRow(actrow) +
1256 tabular.getAscentOfRow(actrow + 1) +
1257 tabular.getAdditionalHeight(actrow + 1);
1259 ly = cursory_ + tabular.getDescentOfRow(actrow);
1261 actcell = tabular.getCellNumber(actrow, actcol);
1263 // now search the right column
1264 int lx = tabular.getWidthOfColumn(actcell) -
1265 tabular.getAdditionalWidth(actcell);
1267 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1268 lx += tabular.getWidthOfColumn(actcell + 1)
1269 + tabular.getAdditionalWidth(actcell);
1271 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1276 int InsetTabular::getCellXPos(int cell) const
1280 for (; !tabular.isFirstCellInRow(c); --c)
1282 int lx = tabular.getWidthOfColumn(cell);
1283 for (; c < cell; ++c)
1284 lx += tabular.getWidthOfColumn(c);
1286 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1290 void InsetTabular::resetPos(BufferView * bv) const
1292 #ifdef WITH_WARNINGS
1293 #warning This should be fixed in the right manner (20011128 Jug)
1295 // fast hack to fix infinite repaintings!
1296 if (in_reset_pos > 0)
1300 actcol = tabular.column_of_cell(actcell);
1303 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1304 if (tabular.isLastCellInRow(cell)) {
1305 cursory_ += tabular.getDescentOfRow(actrow) +
1306 tabular.getAscentOfRow(actrow + 1) +
1307 tabular.getAdditionalHeight(actrow + 1);
1312 if (the_locking_inset)
1316 // we need this only from here on!!!
1318 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1319 int new_x = getCellXPos(actcell);
1320 int old_x = cursorx_;
1323 // cursor.x(getCellXPos(actcell) + offset);
1324 if (actcol < tabular.columns() - 1 && scroll(false) &&
1325 tabular.getWidthOfTabular() < bv->workWidth()-20)
1329 } else if (the_locking_inset &&
1330 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1332 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1333 if (xx > bv->workWidth()-20) {
1334 scroll(bv, -(xx - bv->workWidth() + 60));
1336 } else if (xx < 20) {
1344 } else if (cursorx_ - offset > 20 &&
1345 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1346 > bv->workWidth() - 20) {
1347 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1349 } else if (cursorx_ - offset < 20) {
1350 scroll(bv, 20 - cursorx_ + offset);
1352 } else if (scroll() && top_x > 20 &&
1353 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1354 scroll(bv, old_x - cursorx_);
1357 if (the_locking_inset) {
1358 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1361 if ((!the_locking_inset ||
1362 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1363 actcell != oldcell) {
1364 InsetTabularMailer(*this).updateDialog(bv);
1371 dispatch_result InsetTabular::moveRight(BufferView * bv, bool lock)
1373 if (lock && !old_locking_inset) {
1374 if (activateCellInset(bv))
1377 bool moved = isRightToLeft(bv)
1378 ? movePrevCell(bv) : moveNextCell(bv);
1380 return FINISHED_RIGHT;
1381 if (lock && activateCellInset(bv))
1385 return DISPATCHED_NOUPDATE;
1389 dispatch_result InsetTabular::moveLeft(BufferView * bv, bool lock)
1391 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1395 if (lock && activateCellInset(bv, 0, 0, mouse_button::none, true))
1398 return DISPATCHED_NOUPDATE;
1402 dispatch_result InsetTabular::moveUp(BufferView * bv, bool lock)
1404 int const ocell = actcell;
1405 actcell = tabular.getCellAbove(actcell);
1406 if (actcell == ocell) // we moved out of the inset
1412 if (old_locking_inset) {
1413 old_locking_inset->getCursorPos(bv, x, y);
1414 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1416 if (activateCellInset(bv, x, 0))
1419 return DISPATCHED_NOUPDATE;
1423 dispatch_result InsetTabular::moveDown(BufferView * bv, bool lock)
1425 int const ocell = actcell;
1426 actcell = tabular.getCellBelow(actcell);
1427 if (actcell == ocell) // we moved out of the inset
1428 return FINISHED_DOWN;
1433 if (old_locking_inset) {
1434 old_locking_inset->getCursorPos(bv, x, y);
1435 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1437 if (activateCellInset(bv, x, 0))
1440 return DISPATCHED_NOUPDATE;
1444 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1446 if (isRightToLeft(bv)) {
1447 if (tabular.isFirstCellInRow(actcell)) {
1448 int row = tabular.row_of_cell(actcell);
1449 if (row == tabular.rows() - 1)
1451 actcell = tabular.getLastCellInRow(row);
1452 actcell = tabular.getCellBelow(actcell);
1459 if (tabular.isLastCell(actcell))
1464 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1465 isRightToLeftPar(bv->buffer()->params());
1466 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1473 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1475 if (isRightToLeft(bv)) {
1476 if (tabular.isLastCellInRow(actcell)) {
1477 int row = tabular.row_of_cell(actcell);
1480 actcell = tabular.getFirstCellInRow(row);
1481 actcell = tabular.getCellAbove(actcell);
1483 if (tabular.isLastCell(actcell))
1488 if (!actcell) // first cell
1493 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1494 isRightToLeftPar(bv->buffer()->params());
1495 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1502 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1506 setSelection(0, tabular.getNumberOfCells() - 1);
1508 if (hasSelection()) {
1509 recordUndo(bv, Undo::ATOMIC);
1510 bool const frozen = undo_frozen;
1513 // apply the fontchange on the whole selection
1518 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1519 for (int i = sel_row_start; i <= sel_row_end; ++i)
1520 for (int j = sel_col_start; j <= sel_col_end; ++j)
1521 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1529 if (the_locking_inset)
1530 the_locking_inset->setFont(bv, font, tall);
1534 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1536 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1539 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1540 string const tmp = tabularFeature[i].feature;
1542 if (tmp == what.substr(0, tmp.length())) {
1543 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1544 //tabularFeatures[i].feature.length())) {
1545 action = tabularFeature[i].action;
1549 if (action == LyXTabular::LAST_ACTION)
1553 ltrim(what.substr(tabularFeature[i].feature.length()));
1554 tabularFeatures(bv, action, val);
1560 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1561 string const & special, bool & flag)
1563 if (special == "dl_above") {
1566 } else if (special == "dl_below") {
1567 ltt.bottomDL = flag;
1569 } else if (special == "empty") {
1581 void InsetTabular::tabularFeatures(BufferView * bv,
1582 LyXTabular::Feature feature, string const & value)
1588 bool setLines = false;
1589 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1590 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1594 case LyXTabular::M_ALIGN_LEFT:
1595 case LyXTabular::ALIGN_LEFT:
1596 setAlign = LYX_ALIGN_LEFT;
1599 case LyXTabular::M_ALIGN_RIGHT:
1600 case LyXTabular::ALIGN_RIGHT:
1601 setAlign = LYX_ALIGN_RIGHT;
1604 case LyXTabular::M_ALIGN_CENTER:
1605 case LyXTabular::ALIGN_CENTER:
1606 setAlign = LYX_ALIGN_CENTER;
1609 case LyXTabular::ALIGN_BLOCK:
1610 setAlign = LYX_ALIGN_BLOCK;
1613 case LyXTabular::M_VALIGN_TOP:
1614 case LyXTabular::VALIGN_TOP:
1615 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1618 case LyXTabular::M_VALIGN_BOTTOM:
1619 case LyXTabular::VALIGN_BOTTOM:
1620 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1623 case LyXTabular::M_VALIGN_MIDDLE:
1624 case LyXTabular::VALIGN_MIDDLE:
1625 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1632 if (hasSelection()) {
1633 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1635 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1636 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1638 recordUndo(bv, Undo::ATOMIC);
1640 int row = tabular.row_of_cell(actcell);
1641 int column = tabular.column_of_cell(actcell);
1643 LyXTabular::ltType ltt;
1647 case LyXTabular::SET_PWIDTH: {
1648 LyXLength const len(value);
1649 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1651 tabular.setColumnPWidth(actcell, len);
1652 if (oldlen != len) {
1653 // We need this otherwise we won't resize
1654 // the insettext of the active cell (if any)
1655 // until later (see InsetText::do_resize)
1656 unlockInsetInInset(bv, the_locking_inset);
1661 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1662 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1663 else if (!len.zero()
1664 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1665 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1669 case LyXTabular::SET_MPWIDTH:
1671 LyXLength const len(value);
1672 LyXLength const & oldlen = tabular.getPWidth(actcell);
1673 tabular.setMColumnPWidth(actcell, len);
1674 if (oldlen != len) {
1675 // We need this otherwise we won't resize
1676 // the insettext of the active cell (if any)
1677 // until later (see InsetText::do_resize)
1678 unlockInsetInInset(bv, the_locking_inset);
1684 case LyXTabular::SET_SPECIAL_COLUMN:
1685 case LyXTabular::SET_SPECIAL_MULTI:
1686 tabular.setAlignSpecial(actcell,value,feature);
1690 case LyXTabular::APPEND_ROW:
1691 // append the row into the tabular
1692 unlockInsetInInset(bv, the_locking_inset);
1693 tabular.appendRow(bv->buffer()->params(), actcell);
1694 tabular.setOwner(this);
1698 case LyXTabular::APPEND_COLUMN:
1699 // append the column into the tabular
1700 unlockInsetInInset(bv, the_locking_inset);
1701 tabular.appendColumn(bv->buffer()->params(), actcell);
1702 tabular.setOwner(this);
1703 actcell = tabular.getCellNumber(row, column);
1707 case LyXTabular::DELETE_ROW:
1708 unlockInsetInInset(bv, the_locking_inset);
1709 for (int i = sel_row_start; i <= sel_row_end; ++i)
1710 tabular.deleteRow(sel_row_start);
1711 if (sel_row_start >= tabular.rows())
1713 actcell = tabular.getCellNumber(sel_row_start, column);
1718 case LyXTabular::DELETE_COLUMN:
1719 unlockInsetInInset(bv, the_locking_inset);
1720 for (int i = sel_col_start; i <= sel_col_end; ++i)
1721 tabular.deleteColumn(sel_col_start);
1722 if (sel_col_start >= tabular.columns())
1724 actcell = tabular.getCellNumber(row, sel_col_start);
1729 case LyXTabular::M_TOGGLE_LINE_TOP:
1731 case LyXTabular::TOGGLE_LINE_TOP: {
1732 bool lineSet = !tabular.topLine(actcell, flag);
1733 for (int i = sel_row_start; i <= sel_row_end; ++i)
1734 for (int j = sel_col_start; j <= sel_col_end; ++j)
1736 tabular.getCellNumber(i, j),
1742 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1744 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1745 bool lineSet = !tabular.bottomLine(actcell, flag);
1746 for (int i = sel_row_start; i <= sel_row_end; ++i)
1747 for (int j = sel_col_start; j <= sel_col_end; ++j)
1748 tabular.setBottomLine(
1749 tabular.getCellNumber(i, j),
1756 case LyXTabular::M_TOGGLE_LINE_LEFT:
1758 case LyXTabular::TOGGLE_LINE_LEFT: {
1759 bool lineSet = !tabular.leftLine(actcell, flag);
1760 for (int i = sel_row_start; i <= sel_row_end; ++i)
1761 for (int j = sel_col_start; j <= sel_col_end; ++j)
1762 tabular.setLeftLine(
1763 tabular.getCellNumber(i,j),
1770 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1772 case LyXTabular::TOGGLE_LINE_RIGHT: {
1773 bool lineSet = !tabular.rightLine(actcell, flag);
1774 for (int i = sel_row_start; i <= sel_row_end; ++i)
1775 for (int j = sel_col_start; j <= sel_col_end; ++j)
1776 tabular.setRightLine(
1777 tabular.getCellNumber(i,j),
1784 case LyXTabular::M_ALIGN_LEFT:
1785 case LyXTabular::M_ALIGN_RIGHT:
1786 case LyXTabular::M_ALIGN_CENTER:
1788 case LyXTabular::ALIGN_LEFT:
1789 case LyXTabular::ALIGN_RIGHT:
1790 case LyXTabular::ALIGN_CENTER:
1791 case LyXTabular::ALIGN_BLOCK:
1792 for (int i = sel_row_start; i <= sel_row_end; ++i)
1793 for (int j = sel_col_start; j <= sel_col_end; ++j)
1794 tabular.setAlignment(
1795 tabular.getCellNumber(i, j),
1801 case LyXTabular::M_VALIGN_TOP:
1802 case LyXTabular::M_VALIGN_BOTTOM:
1803 case LyXTabular::M_VALIGN_MIDDLE:
1805 case LyXTabular::VALIGN_TOP:
1806 case LyXTabular::VALIGN_BOTTOM:
1807 case LyXTabular::VALIGN_MIDDLE:
1808 for (int i = sel_row_start; i <= sel_row_end; ++i)
1809 for (int j = sel_col_start; j <= sel_col_end; ++j)
1810 tabular.setVAlignment(
1811 tabular.getCellNumber(i, j),
1816 case LyXTabular::MULTICOLUMN: {
1817 if (sel_row_start != sel_row_end) {
1818 #ifdef WITH_WARNINGS
1819 #warning Need I say it ? This is horrible.
1821 Alert::error(_("Error setting multicolumn"),
1822 _("You cannot set multicolumn vertically."));
1825 // just multicol for one Single Cell
1826 if (!hasSelection()) {
1827 // check wether we are completly in a multicol
1828 if (tabular.isMultiColumn(actcell))
1829 tabular.unsetMultiColumn(actcell);
1831 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1835 // we have a selection so this means we just add all this
1836 // cells to form a multicolumn cell
1840 if (sel_cell_start > sel_cell_end) {
1841 s_start = sel_cell_end;
1842 s_end = sel_cell_start;
1844 s_start = sel_cell_start;
1845 s_end = sel_cell_end;
1847 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1854 case LyXTabular::SET_ALL_LINES:
1856 case LyXTabular::UNSET_ALL_LINES:
1857 for (int i = sel_row_start; i <= sel_row_end; ++i)
1858 for (int j = sel_col_start; j <= sel_col_end; ++j)
1859 tabular.setAllLines(
1860 tabular.getCellNumber(i,j), setLines);
1864 case LyXTabular::SET_LONGTABULAR:
1865 tabular.setLongTabular(true);
1866 updateLocal(bv); // because this toggles displayed
1869 case LyXTabular::UNSET_LONGTABULAR:
1870 tabular.setLongTabular(false);
1871 updateLocal(bv); // because this toggles displayed
1874 case LyXTabular::SET_ROTATE_TABULAR:
1875 tabular.setRotateTabular(true);
1878 case LyXTabular::UNSET_ROTATE_TABULAR:
1879 tabular.setRotateTabular(false);
1882 case LyXTabular::SET_ROTATE_CELL:
1883 for (int i = sel_row_start; i <= sel_row_end; ++i)
1884 for (int j = sel_col_start; j <= sel_col_end; ++j)
1885 tabular.setRotateCell(
1886 tabular.getCellNumber(i, j), true);
1889 case LyXTabular::UNSET_ROTATE_CELL:
1890 for (int i = sel_row_start; i <= sel_row_end; ++i)
1891 for (int j = sel_col_start; j <= sel_col_end; ++j)
1892 tabular.setRotateCell(
1893 tabular.getCellNumber(i, j), false);
1896 case LyXTabular::SET_USEBOX: {
1897 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1898 if (val == tabular.getUsebox(actcell))
1899 val = LyXTabular::BOX_NONE;
1900 for (int i = sel_row_start; i <= sel_row_end; ++i)
1901 for (int j = sel_col_start; j <= sel_col_end; ++j)
1902 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1906 case LyXTabular::UNSET_LTFIRSTHEAD:
1908 case LyXTabular::SET_LTFIRSTHEAD:
1909 tabular.getRowOfLTFirstHead(row, ltt);
1910 checkLongtableSpecial(ltt, value, flag);
1911 tabular.setLTHead(row, flag, ltt, true);
1914 case LyXTabular::UNSET_LTHEAD:
1916 case LyXTabular::SET_LTHEAD:
1917 tabular.getRowOfLTHead(row, ltt);
1918 checkLongtableSpecial(ltt, value, flag);
1919 tabular.setLTHead(row, flag, ltt, false);
1922 case LyXTabular::UNSET_LTFOOT:
1924 case LyXTabular::SET_LTFOOT:
1925 tabular.getRowOfLTFoot(row, ltt);
1926 checkLongtableSpecial(ltt, value, flag);
1927 tabular.setLTFoot(row, flag, ltt, false);
1930 case LyXTabular::UNSET_LTLASTFOOT:
1932 case LyXTabular::SET_LTLASTFOOT:
1933 tabular.getRowOfLTLastFoot(row, ltt);
1934 checkLongtableSpecial(ltt, value, flag);
1935 tabular.setLTFoot(row, flag, ltt, true);
1938 case LyXTabular::SET_LTNEWPAGE:
1939 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1942 // dummy stuff just to avoid warnings
1943 case LyXTabular::LAST_ACTION:
1947 InsetTabularMailer(*this).updateDialog(bv);
1951 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1952 mouse_button::state button, bool behind)
1954 UpdatableInset & inset = tabular.getCellInset(actcell);
1957 x = inset.x() + inset.width();
1958 y = inset.descent();
1960 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1961 //inset_y = cursory_;
1962 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1963 if (!the_locking_inset)
1966 return the_locking_inset;
1970 bool InsetTabular::insetHit(BufferView *, int x, int) const
1972 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1976 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1978 //resizeLyXText(bv, recursive);
1982 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1983 bool const recursive) const
1985 if (the_locking_inset)
1986 return the_locking_inset->getLyXText(bv, recursive);
1987 return InsetOld::getLyXText(bv, recursive);
1991 bool InsetTabular::showInsetDialog(BufferView * bv) const
1993 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1994 InsetTabularMailer(*this).showDialog(bv);
1999 void InsetTabular::openLayoutDialog(BufferView * bv) const
2001 if (the_locking_inset) {
2002 InsetTabular * inset = static_cast<InsetTabular *>
2003 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2005 inset->openLayoutDialog(bv);
2009 InsetTabularMailer(*this).showDialog(bv);
2014 // function returns an object as defined in func_status.h:
2015 // states OK, Unknown, Disabled, On, Off.
2017 FuncStatus InsetTabular::getStatus(string const & what) const
2019 int action = LyXTabular::LAST_ACTION;
2023 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2024 string const tmp = tabularFeature[i].feature;
2025 if (tmp == what.substr(0, tmp.length())) {
2026 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2027 // tabularFeatures[i].feature.length())) {
2028 action = tabularFeature[i].action;
2032 if (action == LyXTabular::LAST_ACTION) {
2034 return status.unknown(true);
2037 string const argument
2038 = ltrim(what.substr(tabularFeature[i].feature.length()));
2043 LyXTabular::ltType dummyltt;
2047 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2049 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2052 case LyXTabular::SET_PWIDTH:
2053 case LyXTabular::SET_MPWIDTH:
2054 case LyXTabular::SET_SPECIAL_COLUMN:
2055 case LyXTabular::SET_SPECIAL_MULTI:
2056 case LyXTabular::APPEND_ROW:
2057 case LyXTabular::APPEND_COLUMN:
2058 case LyXTabular::DELETE_ROW:
2059 case LyXTabular::DELETE_COLUMN:
2060 case LyXTabular::SET_ALL_LINES:
2061 case LyXTabular::UNSET_ALL_LINES:
2062 return status.clear();
2064 case LyXTabular::MULTICOLUMN:
2065 status.setOnOff(tabular.isMultiColumn(actcell));
2068 case LyXTabular::M_TOGGLE_LINE_TOP:
2070 case LyXTabular::TOGGLE_LINE_TOP:
2071 status.setOnOff(tabular.topLine(actcell, flag));
2074 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2076 case LyXTabular::TOGGLE_LINE_BOTTOM:
2077 status.setOnOff(tabular.bottomLine(actcell, flag));
2080 case LyXTabular::M_TOGGLE_LINE_LEFT:
2082 case LyXTabular::TOGGLE_LINE_LEFT:
2083 status.setOnOff(tabular.leftLine(actcell, flag));
2086 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2088 case LyXTabular::TOGGLE_LINE_RIGHT:
2089 status.setOnOff(tabular.rightLine(actcell, flag));
2092 case LyXTabular::M_ALIGN_LEFT:
2094 case LyXTabular::ALIGN_LEFT:
2095 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2098 case LyXTabular::M_ALIGN_RIGHT:
2100 case LyXTabular::ALIGN_RIGHT:
2101 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2104 case LyXTabular::M_ALIGN_CENTER:
2106 case LyXTabular::ALIGN_CENTER:
2107 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2110 case LyXTabular::ALIGN_BLOCK:
2111 status.disabled(tabular.getPWidth(actcell).zero());
2112 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2115 case LyXTabular::M_VALIGN_TOP:
2117 case LyXTabular::VALIGN_TOP:
2118 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2121 case LyXTabular::M_VALIGN_BOTTOM:
2123 case LyXTabular::VALIGN_BOTTOM:
2124 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2127 case LyXTabular::M_VALIGN_MIDDLE:
2129 case LyXTabular::VALIGN_MIDDLE:
2130 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2133 case LyXTabular::SET_LONGTABULAR:
2134 status.setOnOff(tabular.isLongTabular());
2137 case LyXTabular::UNSET_LONGTABULAR:
2138 status.setOnOff(!tabular.isLongTabular());
2141 case LyXTabular::SET_ROTATE_TABULAR:
2142 status.setOnOff(tabular.getRotateTabular());
2145 case LyXTabular::UNSET_ROTATE_TABULAR:
2146 status.setOnOff(!tabular.getRotateTabular());
2149 case LyXTabular::SET_ROTATE_CELL:
2150 status.setOnOff(tabular.getRotateCell(actcell));
2153 case LyXTabular::UNSET_ROTATE_CELL:
2154 status.setOnOff(!tabular.getRotateCell(actcell));
2157 case LyXTabular::SET_USEBOX:
2158 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2161 case LyXTabular::SET_LTFIRSTHEAD:
2162 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2165 case LyXTabular::SET_LTHEAD:
2166 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2169 case LyXTabular::SET_LTFOOT:
2170 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2173 case LyXTabular::SET_LTLASTFOOT:
2174 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2177 case LyXTabular::SET_LTNEWPAGE:
2178 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2183 status.disabled(true);
2190 void InsetTabular::getLabelList(Buffer const & buffer,
2191 std::vector<string> & list) const
2193 tabular.getLabelList(buffer, list);
2197 bool InsetTabular::copySelection(BufferView * bv)
2199 if (!hasSelection())
2202 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2203 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2204 if (sel_col_start > sel_col_end) {
2205 sel_col_start = sel_col_end;
2206 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2208 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2211 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2212 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2213 if (sel_row_start > sel_row_end)
2214 swap(sel_row_start, sel_row_end);
2216 delete paste_tabular;
2217 paste_tabular = new LyXTabular(tabular);
2218 paste_tabular->setOwner(this);
2220 for (int i = 0; i < sel_row_start; ++i)
2221 paste_tabular->deleteRow(0);
2223 int const rows = sel_row_end - sel_row_start + 1;
2224 while (paste_tabular->rows() > rows)
2225 paste_tabular->deleteRow(rows);
2227 paste_tabular->setTopLine(0, true, true);
2228 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2231 for (int i = 0; i < sel_col_start; ++i)
2232 paste_tabular->deleteColumn(0);
2234 int const columns = sel_col_end - sel_col_start + 1;
2235 while (paste_tabular->columns() > columns)
2236 paste_tabular->deleteColumn(columns);
2238 paste_tabular->setLeftLine(0, true, true);
2239 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2243 paste_tabular->ascii(*bv->buffer(), os,
2244 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2245 bv->stuffClipboard(os.str());
2250 bool InsetTabular::pasteSelection(BufferView * bv)
2255 for (int r1 = 0, r2 = actrow;
2256 r1 < paste_tabular->rows() && r2 < tabular.rows();
2258 for (int c1 = 0, c2 = actcol;
2259 c1 < paste_tabular->columns() && c2 < tabular.columns();
2261 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2262 tabular.isPartOfMultiColumn(r2, c2))
2264 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2268 if (tabular.isPartOfMultiColumn(r2, c2)) {
2272 InsetText & inset = tabular.getCellInset(r2, c2);
2273 inset = paste_tabular->getCellInset(r1, c1);
2274 inset.setOwner(this);
2275 inset.deleteLyXText(bv);
2283 bool InsetTabular::cutSelection(BufferParams const & bp)
2285 if (!hasSelection())
2288 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2289 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2290 if (sel_col_start > sel_col_end) {
2291 sel_col_start = sel_col_end;
2292 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2294 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2297 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2298 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2300 if (sel_row_start > sel_row_end)
2301 swap(sel_row_start, sel_row_end);
2303 if (sel_cell_start > sel_cell_end)
2304 swap(sel_cell_start, sel_cell_end);
2306 for (int i = sel_row_start; i <= sel_row_end; ++i)
2307 for (int j = sel_col_start; j <= sel_col_end; ++j)
2308 tabular.getCellInset(tabular.getCellNumber(i, j))
2309 .clear(bp.tracking_changes);
2314 bool InsetTabular::isRightToLeft(BufferView * bv)
2316 return bv->getParentLanguage(this)->RightToLeft();
2320 int InsetTabular::scroll(bool recursive) const
2322 int sx = UpdatableInset::scroll(false);
2324 if (recursive && the_locking_inset)
2325 sx += the_locking_inset->scroll(recursive);
2331 void InsetTabular::getSelection(int & srow, int & erow,
2332 int & scol, int & ecol) const
2334 int const start = hasSelection() ? sel_cell_start : actcell;
2335 int const end = hasSelection() ? sel_cell_end : actcell;
2337 srow = tabular.row_of_cell(start);
2338 erow = tabular.row_of_cell(end);
2342 scol = tabular.column_of_cell(start);
2343 ecol = tabular.column_of_cell(end);
2347 ecol = tabular.right_column_of_cell(end);
2351 ParagraphList * InsetTabular::getParagraphs(int i) const
2353 return i < tabular.getNumberOfCells()
2354 ? tabular.getCellInset(i).getParagraphs(0)
2359 LyXText * InsetTabular::getText(int i) const
2361 return i < tabular.getNumberOfCells()
2362 ? tabular.getCellInset(i).getText(0)
2367 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2369 if (the_locking_inset)
2370 return the_locking_inset->cursor(bv);
2371 return InsetOld::cursor(bv);
2375 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2378 return const_cast<InsetTabular *>(this);
2380 for (int i = 0; i < tabular.rows(); ++i) {
2381 for (int j = 0; j < tabular.columns(); ++j) {
2382 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2392 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2394 if (the_locking_inset) {
2395 WordLangTuple word =
2396 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2397 if (!word.word().empty())
2399 if (tabular.isLastCell(actcell)) {
2400 bv->unlockInset(const_cast<InsetTabular *>(this));
2401 return WordLangTuple();
2405 // otherwise we have to lock the next inset and ask for it's selecttion
2406 tabular.getCellInset(actcell)
2407 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2408 WordLangTuple word = selectNextWordInt(bv, value);
2409 if (!word.word().empty())
2415 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2417 // when entering this function the inset should be ALWAYS locked!
2418 BOOST_ASSERT(the_locking_inset);
2420 WordLangTuple word =
2421 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2422 if (!word.word().empty())
2425 if (tabular.isLastCell(actcell)) {
2426 bv->unlockInset(const_cast<InsetTabular *>(this));
2427 return WordLangTuple();
2430 // otherwise we have to lock the next inset and ask for it's selecttion
2432 tabular.getCellInset(actcell)
2433 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2434 return selectNextWordInt(bv, value);
2438 void InsetTabular::selectSelectedWord(BufferView * bv)
2440 if (the_locking_inset)
2441 the_locking_inset->selectSelectedWord(bv);
2445 void InsetTabular::markErased()
2447 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2448 tabular.getCellInset(cell).markErased();
2452 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2454 if (the_locking_inset) {
2455 if (the_locking_inset->nextChange(bv, length)) {
2459 if (tabular.isLastCell(actcell))
2463 InsetText & inset = tabular.getCellInset(actcell);
2464 if (inset.nextChange(bv, length)) {
2468 while (!tabular.isLastCell(actcell)) {
2470 InsetText & inset = tabular.getCellInset(actcell);
2471 if (inset.nextChange(bv, length)) {
2480 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2484 if (the_locking_inset) {
2485 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2489 if (tabular.isLastCell(actcell))
2493 InsetText & inset = tabular.getCellInset(cell);
2494 if (inset.searchForward(bv, str, cs, mw)) {
2498 while (!tabular.isLastCell(cell)) {
2500 InsetText & inset = tabular.getCellInset(cell);
2501 if (inset.searchForward(bv, str, cs, mw)) {
2510 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2513 int cell = tabular.getNumberOfCells();
2514 if (the_locking_inset) {
2515 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2524 InsetText & inset = tabular.getCellInset(cell);
2525 if (inset.searchBackward(bv, str, cs, mw)) {
2534 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2536 if (the_locking_inset)
2537 return the_locking_inset->insetAllowed(code);
2538 // we return true here because if the inset is not locked someone
2539 // wants to insert something in one of our insettexts and we generally
2545 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2547 const int cell = tabular.getCellFromInset(in);
2550 return tabular.getPWidth(cell).zero();
2552 // this is a workaround for a crash (New, Insert->Tabular,
2553 // Insert->FootNote)
2557 // well we didn't obviously find it so maybe our owner knows more
2558 BOOST_ASSERT(owner());
2559 return owner()->forceDefaultParagraphs(in);
2563 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2566 if (buf.length() <= 0)
2572 string::size_type len = buf.length();
2573 string::size_type p = 0;
2575 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2583 maxCols = max(cols, maxCols);
2589 maxCols = max(cols, maxCols);
2590 LyXTabular * loctab;
2595 delete paste_tabular;
2596 paste_tabular = new LyXTabular(bv->buffer()->params(),
2598 paste_tabular->setOwner(this);
2599 loctab = paste_tabular;
2608 string::size_type op = 0;
2609 int cells = loctab->getNumberOfCells();
2612 rows = loctab->rows();
2613 int const columns = loctab->columns();
2615 while (cell < cells && p < len && row < rows &&
2616 (p = buf.find_first_of("\t\n", p)) != string::npos)
2622 // we can only set this if we are not too far right
2623 if (cols < columns) {
2624 InsetText & inset = loctab->getCellInset(cell);
2625 LyXFont const font = inset.getLyXText(bv)->
2626 getFont(inset.paragraphs.begin(), 0);
2627 inset.setText(buf.substr(op, p - op), font);
2633 // we can only set this if we are not too far right
2634 if (cols < columns) {
2635 InsetText & inset = tabular.getCellInset(cell);
2636 LyXFont const font = inset.getLyXText(bv)->
2637 getFont(inset.paragraphs.begin(), 0);
2638 inset.setText(buf.substr(op, p - op), font);
2643 cell = loctab->getCellNumber(row, cols);
2649 // check for the last cell if there is no trailing '\n'
2650 if (cell < cells && op < len) {
2651 InsetText & inset = loctab->getCellInset(cell);
2652 LyXFont const font = inset.getLyXText(bv)->
2653 getFont(inset.paragraphs.begin(), 0);
2654 inset.setText(buf.substr(op, len - op), font);
2661 void InsetTabular::addPreview(PreviewLoader & loader) const
2663 int const rows = tabular.rows();
2664 int const columns = tabular.columns();
2665 for (int i = 0; i < rows; ++i)
2666 for (int j = 0; j < columns; ++j)
2667 tabular.getCellInset(i, j).addPreview(loader);
2671 string const InsetTabularMailer::name_("tabular");
2673 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2674 : inset_(const_cast<InsetTabular &>(inset))
2678 string const InsetTabularMailer::inset2string(Buffer const &) const
2680 return params2string(inset_);
2684 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2686 istringstream data(in);
2688 lex.setStream(data);
2690 #warning CHECK verify that this is a sane value to return.
2696 string const token = lex.getString();
2704 string const token = lex.getString();
2705 if (token != "\\active_cell")
2708 cell = lex.getInteger();
2711 // This is part of the inset proper that is usually swallowed
2712 // by Buffer::readInset
2715 string const token = lex.getString();
2716 if (token != "Tabular")
2723 Buffer const & buffer = inset.buffer();
2724 inset.read(buffer, lex);
2726 // We can't set the active cell, but we can tell the frontend
2732 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2734 Buffer const & buffer = inset.buffer();
2737 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2738 inset.write(buffer, data);
2739 data << "\\end_inset\n";