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->dispatch(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->dispatch(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 dispatch(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->dispatch(cmd1);
598 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
601 if (the_locking_inset) {
602 FuncRequest cmd1 = cmd;
605 ret = the_locking_inset->dispatch(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->dispatch(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 void InsetTabular::edit(BufferView * bv, int index)
641 lyxerr << "InsetTabular::edit" << endl;
642 if (!bv->lockInset(this)) {
643 lyxerr << "InsetTabular::Cannot lock inset (2)" << endl;
648 the_locking_inset = 0;
656 UpdatableInset & inset = tabular.getCellInset(actcell);
657 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
658 if (the_locking_inset)
664 InsetTabular::priv_dispatch(FuncRequest const & cmd,
665 idx_type & idx, pos_type & pos)
667 // We need to save the value of the_locking_inset as the call to
668 // the_locking_inset->localDispatch might unlock it.
669 old_locking_inset = the_locking_inset;
670 dispatch_result result = UpdatableInset::priv_dispatch(cmd, idx, pos);
671 BufferView * bv = cmd.view();
673 if (cmd.action == LFUN_INSET_EDIT) {
675 lyxerr << "InsetTabular::edit: " << this << " args: '"
676 << cmd.argument << "' first cell: "
677 << &tabular.cell_info[0][0].inset << endl;
679 if (!bv->lockInset(this)) {
680 lyxerr << "InsetTabular::Cannot lock inset" << endl;
686 the_locking_inset = 0;
690 if (cmd.argument.size()) {
691 if (cmd.argument == "left") {
692 if (isRightToLeft(bv))
693 actcell = tabular.getLastCellInRow(0);
697 if (isRightToLeft(bv))
698 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
700 actcell = tabular.getNumberOfCells() - 1;
708 setPos(bv, cmd.x, cmd.y);
711 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
712 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
714 activateCellInset(bv, cmd.x - inset_x, cmd.y - inset_y, cmd.button());
720 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
725 if (cmd.action < 0 && cmd.argument.empty())
728 bool hs = hasSelection();
731 // this one have priority over the locked InsetText, if we're not already
732 // inside another tabular then that one get's priority!
733 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
734 switch (cmd.action) {
735 case LFUN_MOUSE_PRESS:
739 case LFUN_MOUSE_MOTION:
740 lfunMouseMotion(cmd);
743 case LFUN_MOUSE_RELEASE:
744 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
746 case LFUN_CELL_BACKWARD:
747 case LFUN_CELL_FORWARD:
748 unlockInsetInInset(bv, the_locking_inset);
749 if (cmd.action == LFUN_CELL_FORWARD)
750 moveNextCell(bv, old_locking_inset != 0);
752 movePrevCell(bv, old_locking_inset != 0);
756 if (!the_locking_inset)
757 return DISPATCHED_NOUPDATE;
759 // this to avoid compiler warnings.
765 kb_action action = cmd.action;
766 string arg = cmd.argument;
767 if (the_locking_inset) {
768 result = the_locking_inset->dispatch(cmd);
769 if (result == DISPATCHED_NOUPDATE) {
772 if (sc != scroll()) { // inset has been scrolled
776 } else if (result == DISPATCHED) {
779 } else if (result == FINISHED_UP) {
781 // Make sure to reset status message after
782 // exiting, e.g. math inset
783 bv->owner()->clearMessage();
784 } else if (result == FINISHED_DOWN) {
786 bv->owner()->clearMessage();
787 } else if (result == FINISHED_RIGHT) {
789 bv->owner()->clearMessage();
790 } else if (result == FINISHED) {
791 bv->owner()->clearMessage();
797 // --- Cursor Movements ----------------------------------
798 case LFUN_RIGHTSEL: {
799 int const start = hasSelection() ? sel_cell_start : actcell;
800 if (tabular.isLastCellInRow(actcell)) {
801 setSelection(start, actcell);
806 // if we are starting a selection, only select
807 // the current cell at the beginning
808 if (hasSelection()) {
809 moveRight(bv, false);
812 setSelection(start, end);
817 result = moveRight(bv);
823 int const start = hasSelection() ? sel_cell_start : actcell;
824 if (tabular.isFirstCellInRow(actcell)) {
825 setSelection(start, actcell);
830 // if we are starting a selection, only select
831 // the current cell at the beginning
832 if (hasSelection()) {
836 setSelection(start, end);
841 result = moveLeft(bv);
847 int const start = hasSelection() ? sel_cell_start : actcell;
848 int const ocell = actcell;
849 // if we are starting a selection, only select
850 // the current cell at the beginning
851 if (hasSelection()) {
853 if (ocell == sel_cell_end ||
854 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
855 setSelection(start, tabular.getCellBelow(sel_cell_end));
857 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
859 setSelection(start, start);
865 result = moveDown(bv, old_locking_inset != 0);
871 int const start = hasSelection() ? sel_cell_start : actcell;
872 int const ocell = actcell;
873 // if we are starting a selection, only select
874 // the current cell at the beginning
875 if (hasSelection()) {
877 if ((ocell == sel_cell_end) ||
878 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
879 setSelection(start, tabular.getCellAbove(sel_cell_end));
881 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
883 setSelection(start, start);
889 result = moveUp(bv, old_locking_inset != 0);
898 unlockInsetInInset(bv, the_locking_inset);
899 if (bv->top_y() + bv->painter().paperHeight() <
900 top_baseline + tabular.getHeightOfTabular())
902 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
903 actcell = tabular.getCellBelow(first_visible_cell) + column;
905 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
915 unlockInsetInInset(bv, the_locking_inset);
916 if (top_baseline < 0) {
917 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
918 if (top_baseline > 0)
921 actcell = tabular.getCellBelow(first_visible_cell) + column;
929 // none of these make sense for insettabular,
930 // but we must catch them to prevent any
931 // selection from being confused
935 case LFUN_WORDLEFTSEL:
937 case LFUN_WORDRIGHTSEL:
939 case LFUN_DOWN_PARAGRAPH:
940 case LFUN_DOWN_PARAGRAPHSEL:
941 case LFUN_UP_PARAGRAPH:
942 case LFUN_UP_PARAGRAPHSEL:
948 case LFUN_BEGINNINGBUF:
949 case LFUN_BEGINNINGBUFSEL:
953 case LFUN_LAYOUT_TABULAR:
954 InsetTabularMailer(*this).showDialog(bv);
956 case LFUN_INSET_DIALOG_UPDATE:
957 InsetTabularMailer(*this).updateDialog(bv);
959 case LFUN_TABULAR_FEATURE:
960 if (!tabularFeatures(bv, arg))
961 result = UNDISPATCHED;
963 // insert file functions
964 case LFUN_FILE_INSERT_ASCII_PARA:
965 case LFUN_FILE_INSERT_ASCII:
967 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
970 if (insertAsciiString(bv, tmpstr, false))
973 result = UNDISPATCHED;
976 // cut and paste functions
978 if (!copySelection(bv))
982 recordUndo(bv, Undo::DELETE);
983 cutSelection(bv->buffer()->params());
992 case LFUN_PASTESELECTION:
994 string const clip = bv->getClipboard();
998 if (clip.find('\t') != string::npos) {
1002 string::size_type len = clip.length();
1003 string::size_type p = 0;
1006 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1014 maxCols = max(cols, maxCols);
1020 maxCols = max(cols, maxCols);
1021 delete paste_tabular;
1022 paste_tabular = new LyXTabular(bv->buffer()->params(),
1023 this, rows, maxCols);
1024 string::size_type op = 0;
1026 int cells = paste_tabular->getNumberOfCells();
1028 while ((cell < cells) && (p < len) &&
1029 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1034 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1039 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1040 while (cols++ < maxCols)
1048 // check for the last cell if there is no trailing '\n'
1049 if ((cell < cells) && (op < len))
1050 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1053 if (!insertAsciiString(bv, clip, true))
1056 // so that the clipboard is used and it goes on
1058 // and executes LFUN_PASTESELECTION in insettext!
1059 delete paste_tabular;
1064 if (hasPasteBuffer()) {
1065 recordUndo(bv, Undo::INSERT);
1070 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1072 // handle font changing stuff on selection before we lock the inset
1073 // in the default part!
1074 result = UNDISPATCHED;
1085 case LFUN_UNDERLINE:
1086 case LFUN_FONT_SIZE:
1087 if (bv->dispatch(FuncRequest(bv, action, arg)))
1088 result = DISPATCHED;
1094 // we try to activate the actual inset and put this event down to
1095 // the insets dispatch function.
1096 if (result == DISPATCHED || the_locking_inset)
1098 if (activateCellInset(bv)) {
1099 result = the_locking_inset->dispatch(FuncRequest(bv, action, arg));
1100 if (result == UNDISPATCHED || result >= FINISHED) {
1101 unlockInsetInInset(bv, the_locking_inset);
1102 // we need to update if this was requested before
1104 return UNDISPATCHED;
1113 if (result < FINISHED) {
1114 if (!the_locking_inset && bv->fitCursor())
1117 bv->unlockInset(this);
1122 int InsetTabular::latex(Buffer const & buf, ostream & os,
1123 LatexRunParams const & runparams) const
1125 return tabular.latex(buf, os, runparams);
1129 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1132 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1134 return tabular.ascii(buf, os, 0, false, 0);
1138 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1140 return tabular.linuxdoc(buf,os);
1144 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1149 // if the table is inside a float it doesn't need the informaltable
1150 // wrapper. Search for it.
1151 for (master = owner();
1152 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1153 master = master->owner());
1156 os << "<informaltable>";
1161 ret += tabular.docbook(buf, os, mixcont);
1163 os << "</informaltable>";
1172 void InsetTabular::validate(LaTeXFeatures & features) const
1174 tabular.validate(features);
1178 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1181 // if we have a locking_inset we should have to check only this cell for
1182 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1183 // This is _really_ important (André)
1184 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1187 for (int j = 0; j < tabular.columns(); ++j) {
1191 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1192 tabular.getCellInset(actrow, j).metrics(m, dim);
1193 maxAsc = max(dim.asc, maxAsc);
1194 maxDesc = max(dim.des, maxDesc);
1196 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1197 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1198 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1204 for (int i = 0; i < tabular.rows(); ++i) {
1207 for (int j = 0; j < tabular.columns(); ++j) {
1208 if (tabular.isPartOfMultiColumn(i, j))
1214 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1215 tabular.getCellInset(cell).metrics(m, dim);
1216 maxAsc = max(maxAsc, dim.asc);
1217 maxDesc = max(maxDesc, dim.des);
1218 tabular.setWidthOfCell(cell, dim.wid);
1220 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1221 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1226 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1228 if (the_locking_inset) {
1229 the_locking_inset->getCursor(bv, x, y);
1234 y = cursory_ + InsetTabular::y();
1237 int desc = tabular.getDescentOfRow(actrow);
1239 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1241 y += ADD_TO_HEIGHT * 2;
1242 y += TEXT_TO_INSET_OFFSET;
1246 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1248 if (the_locking_inset) {
1249 the_locking_inset->getCursorPos(bv, x, y);
1252 x = cursorx_ - top_x;
1257 void InsetTabular::fitInsetCursor(BufferView * bv) const
1259 if (the_locking_inset) {
1260 the_locking_inset->fitInsetCursor(bv);
1265 int const asc = font_metrics::maxAscent(font);
1266 int const desc = font_metrics::maxDescent(font);
1269 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1273 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1276 actcell = actrow = actcol = 0;
1277 int ly = tabular.getDescentOfRow(actrow);
1279 // first search the right row
1280 while (ly < y && actrow + 1 < tabular.rows()) {
1281 cursory_ += tabular.getDescentOfRow(actrow) +
1282 tabular.getAscentOfRow(actrow + 1) +
1283 tabular.getAdditionalHeight(actrow + 1);
1285 ly = cursory_ + tabular.getDescentOfRow(actrow);
1287 actcell = tabular.getCellNumber(actrow, actcol);
1289 // now search the right column
1290 int lx = tabular.getWidthOfColumn(actcell) -
1291 tabular.getAdditionalWidth(actcell);
1293 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1294 lx += tabular.getWidthOfColumn(actcell + 1)
1295 + tabular.getAdditionalWidth(actcell);
1297 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1302 int InsetTabular::getCellXPos(int cell) const
1306 for (; !tabular.isFirstCellInRow(c); --c)
1308 int lx = tabular.getWidthOfColumn(cell);
1309 for (; c < cell; ++c)
1310 lx += tabular.getWidthOfColumn(c);
1312 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1316 void InsetTabular::resetPos(BufferView * bv) const
1318 #ifdef WITH_WARNINGS
1319 #warning This should be fixed in the right manner (20011128 Jug)
1321 // fast hack to fix infinite repaintings!
1322 if (in_reset_pos > 0)
1326 actcol = tabular.column_of_cell(actcell);
1329 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1330 if (tabular.isLastCellInRow(cell)) {
1331 cursory_ += tabular.getDescentOfRow(actrow) +
1332 tabular.getAscentOfRow(actrow + 1) +
1333 tabular.getAdditionalHeight(actrow + 1);
1338 if (the_locking_inset)
1342 // we need this only from here on!!!
1344 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1345 int new_x = getCellXPos(actcell);
1346 int old_x = cursorx_;
1349 // cursor.x(getCellXPos(actcell) + offset);
1350 if (actcol < tabular.columns() - 1 && scroll(false) &&
1351 tabular.getWidthOfTabular() < bv->workWidth()-20)
1355 } else if (the_locking_inset &&
1356 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1358 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1359 if (xx > bv->workWidth()-20) {
1360 scroll(bv, -(xx - bv->workWidth() + 60));
1362 } else if (xx < 20) {
1370 } else if (cursorx_ - offset > 20 &&
1371 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1372 > bv->workWidth() - 20) {
1373 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1375 } else if (cursorx_ - offset < 20) {
1376 scroll(bv, 20 - cursorx_ + offset);
1378 } else if (scroll() && top_x > 20 &&
1379 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1380 scroll(bv, old_x - cursorx_);
1383 if (the_locking_inset) {
1384 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1387 if ((!the_locking_inset ||
1388 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1389 actcell != oldcell) {
1390 InsetTabularMailer(*this).updateDialog(bv);
1397 dispatch_result InsetTabular::moveRight(BufferView * bv, bool lock)
1399 if (lock && !old_locking_inset) {
1400 if (activateCellInset(bv))
1403 bool moved = isRightToLeft(bv)
1404 ? movePrevCell(bv) : moveNextCell(bv);
1406 return FINISHED_RIGHT;
1407 if (lock && activateCellInset(bv))
1411 return DISPATCHED_NOUPDATE;
1415 dispatch_result InsetTabular::moveLeft(BufferView * bv, bool lock)
1417 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1421 if (lock && activateCellInset(bv, 0, 0, mouse_button::none, true))
1424 return DISPATCHED_NOUPDATE;
1428 dispatch_result InsetTabular::moveUp(BufferView * bv, bool lock)
1430 int const ocell = actcell;
1431 actcell = tabular.getCellAbove(actcell);
1432 if (actcell == ocell) // we moved out of the inset
1438 if (old_locking_inset) {
1439 old_locking_inset->getCursorPos(bv, x, y);
1440 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1442 if (activateCellInset(bv, x, 0))
1445 return DISPATCHED_NOUPDATE;
1449 dispatch_result InsetTabular::moveDown(BufferView * bv, bool lock)
1451 int const ocell = actcell;
1452 actcell = tabular.getCellBelow(actcell);
1453 if (actcell == ocell) // we moved out of the inset
1454 return FINISHED_DOWN;
1459 if (old_locking_inset) {
1460 old_locking_inset->getCursorPos(bv, x, y);
1461 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1463 if (activateCellInset(bv, x, 0))
1466 return DISPATCHED_NOUPDATE;
1470 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1472 if (isRightToLeft(bv)) {
1473 if (tabular.isFirstCellInRow(actcell)) {
1474 int row = tabular.row_of_cell(actcell);
1475 if (row == tabular.rows() - 1)
1477 actcell = tabular.getLastCellInRow(row);
1478 actcell = tabular.getCellBelow(actcell);
1485 if (tabular.isLastCell(actcell))
1490 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1491 isRightToLeftPar(bv->buffer()->params());
1492 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1499 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1501 if (isRightToLeft(bv)) {
1502 if (tabular.isLastCellInRow(actcell)) {
1503 int row = tabular.row_of_cell(actcell);
1506 actcell = tabular.getFirstCellInRow(row);
1507 actcell = tabular.getCellAbove(actcell);
1509 if (tabular.isLastCell(actcell))
1514 if (!actcell) // first cell
1519 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1520 isRightToLeftPar(bv->buffer()->params());
1521 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1528 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1532 setSelection(0, tabular.getNumberOfCells() - 1);
1534 if (hasSelection()) {
1535 recordUndo(bv, Undo::ATOMIC);
1536 bool const frozen = undo_frozen;
1539 // apply the fontchange on the whole selection
1544 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1545 for (int i = sel_row_start; i <= sel_row_end; ++i)
1546 for (int j = sel_col_start; j <= sel_col_end; ++j)
1547 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1555 if (the_locking_inset)
1556 the_locking_inset->setFont(bv, font, tall);
1560 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1562 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1565 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1566 string const tmp = tabularFeature[i].feature;
1568 if (tmp == what.substr(0, tmp.length())) {
1569 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1570 //tabularFeatures[i].feature.length())) {
1571 action = tabularFeature[i].action;
1575 if (action == LyXTabular::LAST_ACTION)
1579 ltrim(what.substr(tabularFeature[i].feature.length()));
1580 tabularFeatures(bv, action, val);
1586 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1587 string const & special, bool & flag)
1589 if (special == "dl_above") {
1592 } else if (special == "dl_below") {
1593 ltt.bottomDL = flag;
1595 } else if (special == "empty") {
1607 void InsetTabular::tabularFeatures(BufferView * bv,
1608 LyXTabular::Feature feature, string const & value)
1614 bool setLines = false;
1615 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1616 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1620 case LyXTabular::M_ALIGN_LEFT:
1621 case LyXTabular::ALIGN_LEFT:
1622 setAlign = LYX_ALIGN_LEFT;
1625 case LyXTabular::M_ALIGN_RIGHT:
1626 case LyXTabular::ALIGN_RIGHT:
1627 setAlign = LYX_ALIGN_RIGHT;
1630 case LyXTabular::M_ALIGN_CENTER:
1631 case LyXTabular::ALIGN_CENTER:
1632 setAlign = LYX_ALIGN_CENTER;
1635 case LyXTabular::ALIGN_BLOCK:
1636 setAlign = LYX_ALIGN_BLOCK;
1639 case LyXTabular::M_VALIGN_TOP:
1640 case LyXTabular::VALIGN_TOP:
1641 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1644 case LyXTabular::M_VALIGN_BOTTOM:
1645 case LyXTabular::VALIGN_BOTTOM:
1646 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1649 case LyXTabular::M_VALIGN_MIDDLE:
1650 case LyXTabular::VALIGN_MIDDLE:
1651 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1658 if (hasSelection()) {
1659 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1661 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1662 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1664 recordUndo(bv, Undo::ATOMIC);
1666 int row = tabular.row_of_cell(actcell);
1667 int column = tabular.column_of_cell(actcell);
1669 LyXTabular::ltType ltt;
1673 case LyXTabular::SET_PWIDTH: {
1674 LyXLength const len(value);
1675 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1677 tabular.setColumnPWidth(actcell, len);
1678 if (oldlen != len) {
1679 // We need this otherwise we won't resize
1680 // the insettext of the active cell (if any)
1681 // until later (see InsetText::do_resize)
1682 unlockInsetInInset(bv, the_locking_inset);
1687 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1688 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1689 else if (!len.zero()
1690 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1691 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1695 case LyXTabular::SET_MPWIDTH:
1697 LyXLength const len(value);
1698 LyXLength const & oldlen = tabular.getPWidth(actcell);
1699 tabular.setMColumnPWidth(actcell, len);
1700 if (oldlen != len) {
1701 // We need this otherwise we won't resize
1702 // the insettext of the active cell (if any)
1703 // until later (see InsetText::do_resize)
1704 unlockInsetInInset(bv, the_locking_inset);
1710 case LyXTabular::SET_SPECIAL_COLUMN:
1711 case LyXTabular::SET_SPECIAL_MULTI:
1712 tabular.setAlignSpecial(actcell,value,feature);
1716 case LyXTabular::APPEND_ROW:
1717 // append the row into the tabular
1718 unlockInsetInInset(bv, the_locking_inset);
1719 tabular.appendRow(bv->buffer()->params(), actcell);
1720 tabular.setOwner(this);
1724 case LyXTabular::APPEND_COLUMN:
1725 // append the column into the tabular
1726 unlockInsetInInset(bv, the_locking_inset);
1727 tabular.appendColumn(bv->buffer()->params(), actcell);
1728 tabular.setOwner(this);
1729 actcell = tabular.getCellNumber(row, column);
1733 case LyXTabular::DELETE_ROW:
1734 unlockInsetInInset(bv, the_locking_inset);
1735 for (int i = sel_row_start; i <= sel_row_end; ++i)
1736 tabular.deleteRow(sel_row_start);
1737 if (sel_row_start >= tabular.rows())
1739 actcell = tabular.getCellNumber(sel_row_start, column);
1744 case LyXTabular::DELETE_COLUMN:
1745 unlockInsetInInset(bv, the_locking_inset);
1746 for (int i = sel_col_start; i <= sel_col_end; ++i)
1747 tabular.deleteColumn(sel_col_start);
1748 if (sel_col_start >= tabular.columns())
1750 actcell = tabular.getCellNumber(row, sel_col_start);
1755 case LyXTabular::M_TOGGLE_LINE_TOP:
1757 case LyXTabular::TOGGLE_LINE_TOP: {
1758 bool lineSet = !tabular.topLine(actcell, flag);
1759 for (int i = sel_row_start; i <= sel_row_end; ++i)
1760 for (int j = sel_col_start; j <= sel_col_end; ++j)
1762 tabular.getCellNumber(i, j),
1768 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1770 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1771 bool lineSet = !tabular.bottomLine(actcell, flag);
1772 for (int i = sel_row_start; i <= sel_row_end; ++i)
1773 for (int j = sel_col_start; j <= sel_col_end; ++j)
1774 tabular.setBottomLine(
1775 tabular.getCellNumber(i, j),
1782 case LyXTabular::M_TOGGLE_LINE_LEFT:
1784 case LyXTabular::TOGGLE_LINE_LEFT: {
1785 bool lineSet = !tabular.leftLine(actcell, flag);
1786 for (int i = sel_row_start; i <= sel_row_end; ++i)
1787 for (int j = sel_col_start; j <= sel_col_end; ++j)
1788 tabular.setLeftLine(
1789 tabular.getCellNumber(i,j),
1796 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1798 case LyXTabular::TOGGLE_LINE_RIGHT: {
1799 bool lineSet = !tabular.rightLine(actcell, flag);
1800 for (int i = sel_row_start; i <= sel_row_end; ++i)
1801 for (int j = sel_col_start; j <= sel_col_end; ++j)
1802 tabular.setRightLine(
1803 tabular.getCellNumber(i,j),
1810 case LyXTabular::M_ALIGN_LEFT:
1811 case LyXTabular::M_ALIGN_RIGHT:
1812 case LyXTabular::M_ALIGN_CENTER:
1814 case LyXTabular::ALIGN_LEFT:
1815 case LyXTabular::ALIGN_RIGHT:
1816 case LyXTabular::ALIGN_CENTER:
1817 case LyXTabular::ALIGN_BLOCK:
1818 for (int i = sel_row_start; i <= sel_row_end; ++i)
1819 for (int j = sel_col_start; j <= sel_col_end; ++j)
1820 tabular.setAlignment(
1821 tabular.getCellNumber(i, j),
1827 case LyXTabular::M_VALIGN_TOP:
1828 case LyXTabular::M_VALIGN_BOTTOM:
1829 case LyXTabular::M_VALIGN_MIDDLE:
1831 case LyXTabular::VALIGN_TOP:
1832 case LyXTabular::VALIGN_BOTTOM:
1833 case LyXTabular::VALIGN_MIDDLE:
1834 for (int i = sel_row_start; i <= sel_row_end; ++i)
1835 for (int j = sel_col_start; j <= sel_col_end; ++j)
1836 tabular.setVAlignment(
1837 tabular.getCellNumber(i, j),
1842 case LyXTabular::MULTICOLUMN: {
1843 if (sel_row_start != sel_row_end) {
1844 #ifdef WITH_WARNINGS
1845 #warning Need I say it ? This is horrible.
1847 Alert::error(_("Error setting multicolumn"),
1848 _("You cannot set multicolumn vertically."));
1851 // just multicol for one Single Cell
1852 if (!hasSelection()) {
1853 // check wether we are completly in a multicol
1854 if (tabular.isMultiColumn(actcell))
1855 tabular.unsetMultiColumn(actcell);
1857 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1861 // we have a selection so this means we just add all this
1862 // cells to form a multicolumn cell
1866 if (sel_cell_start > sel_cell_end) {
1867 s_start = sel_cell_end;
1868 s_end = sel_cell_start;
1870 s_start = sel_cell_start;
1871 s_end = sel_cell_end;
1873 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1880 case LyXTabular::SET_ALL_LINES:
1882 case LyXTabular::UNSET_ALL_LINES:
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.setAllLines(
1886 tabular.getCellNumber(i,j), setLines);
1890 case LyXTabular::SET_LONGTABULAR:
1891 tabular.setLongTabular(true);
1892 updateLocal(bv); // because this toggles displayed
1895 case LyXTabular::UNSET_LONGTABULAR:
1896 tabular.setLongTabular(false);
1897 updateLocal(bv); // because this toggles displayed
1900 case LyXTabular::SET_ROTATE_TABULAR:
1901 tabular.setRotateTabular(true);
1904 case LyXTabular::UNSET_ROTATE_TABULAR:
1905 tabular.setRotateTabular(false);
1908 case LyXTabular::SET_ROTATE_CELL:
1909 for (int i = sel_row_start; i <= sel_row_end; ++i)
1910 for (int j = sel_col_start; j <= sel_col_end; ++j)
1911 tabular.setRotateCell(
1912 tabular.getCellNumber(i, j), true);
1915 case LyXTabular::UNSET_ROTATE_CELL:
1916 for (int i = sel_row_start; i <= sel_row_end; ++i)
1917 for (int j = sel_col_start; j <= sel_col_end; ++j)
1918 tabular.setRotateCell(
1919 tabular.getCellNumber(i, j), false);
1922 case LyXTabular::SET_USEBOX: {
1923 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1924 if (val == tabular.getUsebox(actcell))
1925 val = LyXTabular::BOX_NONE;
1926 for (int i = sel_row_start; i <= sel_row_end; ++i)
1927 for (int j = sel_col_start; j <= sel_col_end; ++j)
1928 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1932 case LyXTabular::UNSET_LTFIRSTHEAD:
1934 case LyXTabular::SET_LTFIRSTHEAD:
1935 tabular.getRowOfLTFirstHead(row, ltt);
1936 checkLongtableSpecial(ltt, value, flag);
1937 tabular.setLTHead(row, flag, ltt, true);
1940 case LyXTabular::UNSET_LTHEAD:
1942 case LyXTabular::SET_LTHEAD:
1943 tabular.getRowOfLTHead(row, ltt);
1944 checkLongtableSpecial(ltt, value, flag);
1945 tabular.setLTHead(row, flag, ltt, false);
1948 case LyXTabular::UNSET_LTFOOT:
1950 case LyXTabular::SET_LTFOOT:
1951 tabular.getRowOfLTFoot(row, ltt);
1952 checkLongtableSpecial(ltt, value, flag);
1953 tabular.setLTFoot(row, flag, ltt, false);
1956 case LyXTabular::UNSET_LTLASTFOOT:
1958 case LyXTabular::SET_LTLASTFOOT:
1959 tabular.getRowOfLTLastFoot(row, ltt);
1960 checkLongtableSpecial(ltt, value, flag);
1961 tabular.setLTFoot(row, flag, ltt, true);
1964 case LyXTabular::SET_LTNEWPAGE:
1965 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1968 // dummy stuff just to avoid warnings
1969 case LyXTabular::LAST_ACTION:
1973 InsetTabularMailer(*this).updateDialog(bv);
1977 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1978 mouse_button::state button, bool behind)
1980 UpdatableInset & inset = tabular.getCellInset(actcell);
1983 x = inset.x() + inset.width();
1984 y = inset.descent();
1986 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1987 //inset_y = cursory_;
1988 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1989 if (!the_locking_inset)
1992 return the_locking_inset;
1996 bool InsetTabular::insetHit(BufferView *, int x, int) const
1998 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
2002 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2004 //resizeLyXText(bv, recursive);
2008 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2009 bool const recursive) const
2011 if (the_locking_inset)
2012 return the_locking_inset->getLyXText(bv, recursive);
2013 return InsetOld::getLyXText(bv, recursive);
2017 bool InsetTabular::showInsetDialog(BufferView * bv) const
2019 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2020 InsetTabularMailer(*this).showDialog(bv);
2025 void InsetTabular::openLayoutDialog(BufferView * bv) const
2027 if (the_locking_inset) {
2028 InsetTabular * inset = static_cast<InsetTabular *>
2029 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2031 inset->openLayoutDialog(bv);
2035 InsetTabularMailer(*this).showDialog(bv);
2040 // function returns an object as defined in func_status.h:
2041 // states OK, Unknown, Disabled, On, Off.
2043 FuncStatus InsetTabular::getStatus(string const & what) const
2045 int action = LyXTabular::LAST_ACTION;
2049 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2050 string const tmp = tabularFeature[i].feature;
2051 if (tmp == what.substr(0, tmp.length())) {
2052 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2053 // tabularFeatures[i].feature.length())) {
2054 action = tabularFeature[i].action;
2058 if (action == LyXTabular::LAST_ACTION) {
2060 return status.unknown(true);
2063 string const argument
2064 = ltrim(what.substr(tabularFeature[i].feature.length()));
2069 LyXTabular::ltType dummyltt;
2073 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2075 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2078 case LyXTabular::SET_PWIDTH:
2079 case LyXTabular::SET_MPWIDTH:
2080 case LyXTabular::SET_SPECIAL_COLUMN:
2081 case LyXTabular::SET_SPECIAL_MULTI:
2082 case LyXTabular::APPEND_ROW:
2083 case LyXTabular::APPEND_COLUMN:
2084 case LyXTabular::DELETE_ROW:
2085 case LyXTabular::DELETE_COLUMN:
2086 case LyXTabular::SET_ALL_LINES:
2087 case LyXTabular::UNSET_ALL_LINES:
2088 return status.clear();
2090 case LyXTabular::MULTICOLUMN:
2091 status.setOnOff(tabular.isMultiColumn(actcell));
2094 case LyXTabular::M_TOGGLE_LINE_TOP:
2096 case LyXTabular::TOGGLE_LINE_TOP:
2097 status.setOnOff(tabular.topLine(actcell, flag));
2100 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2102 case LyXTabular::TOGGLE_LINE_BOTTOM:
2103 status.setOnOff(tabular.bottomLine(actcell, flag));
2106 case LyXTabular::M_TOGGLE_LINE_LEFT:
2108 case LyXTabular::TOGGLE_LINE_LEFT:
2109 status.setOnOff(tabular.leftLine(actcell, flag));
2112 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2114 case LyXTabular::TOGGLE_LINE_RIGHT:
2115 status.setOnOff(tabular.rightLine(actcell, flag));
2118 case LyXTabular::M_ALIGN_LEFT:
2120 case LyXTabular::ALIGN_LEFT:
2121 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2124 case LyXTabular::M_ALIGN_RIGHT:
2126 case LyXTabular::ALIGN_RIGHT:
2127 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2130 case LyXTabular::M_ALIGN_CENTER:
2132 case LyXTabular::ALIGN_CENTER:
2133 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2136 case LyXTabular::ALIGN_BLOCK:
2137 status.disabled(tabular.getPWidth(actcell).zero());
2138 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2141 case LyXTabular::M_VALIGN_TOP:
2143 case LyXTabular::VALIGN_TOP:
2144 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2147 case LyXTabular::M_VALIGN_BOTTOM:
2149 case LyXTabular::VALIGN_BOTTOM:
2150 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2153 case LyXTabular::M_VALIGN_MIDDLE:
2155 case LyXTabular::VALIGN_MIDDLE:
2156 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2159 case LyXTabular::SET_LONGTABULAR:
2160 status.setOnOff(tabular.isLongTabular());
2163 case LyXTabular::UNSET_LONGTABULAR:
2164 status.setOnOff(!tabular.isLongTabular());
2167 case LyXTabular::SET_ROTATE_TABULAR:
2168 status.setOnOff(tabular.getRotateTabular());
2171 case LyXTabular::UNSET_ROTATE_TABULAR:
2172 status.setOnOff(!tabular.getRotateTabular());
2175 case LyXTabular::SET_ROTATE_CELL:
2176 status.setOnOff(tabular.getRotateCell(actcell));
2179 case LyXTabular::UNSET_ROTATE_CELL:
2180 status.setOnOff(!tabular.getRotateCell(actcell));
2183 case LyXTabular::SET_USEBOX:
2184 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2187 case LyXTabular::SET_LTFIRSTHEAD:
2188 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2191 case LyXTabular::SET_LTHEAD:
2192 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2195 case LyXTabular::SET_LTFOOT:
2196 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2199 case LyXTabular::SET_LTLASTFOOT:
2200 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2203 case LyXTabular::SET_LTNEWPAGE:
2204 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2209 status.disabled(true);
2216 void InsetTabular::getLabelList(Buffer const & buffer,
2217 std::vector<string> & list) const
2219 tabular.getLabelList(buffer, list);
2223 bool InsetTabular::copySelection(BufferView * bv)
2225 if (!hasSelection())
2228 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2229 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2230 if (sel_col_start > sel_col_end) {
2231 sel_col_start = sel_col_end;
2232 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2234 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2237 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2238 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2239 if (sel_row_start > sel_row_end)
2240 swap(sel_row_start, sel_row_end);
2242 delete paste_tabular;
2243 paste_tabular = new LyXTabular(tabular);
2244 paste_tabular->setOwner(this);
2246 for (int i = 0; i < sel_row_start; ++i)
2247 paste_tabular->deleteRow(0);
2249 int const rows = sel_row_end - sel_row_start + 1;
2250 while (paste_tabular->rows() > rows)
2251 paste_tabular->deleteRow(rows);
2253 paste_tabular->setTopLine(0, true, true);
2254 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2257 for (int i = 0; i < sel_col_start; ++i)
2258 paste_tabular->deleteColumn(0);
2260 int const columns = sel_col_end - sel_col_start + 1;
2261 while (paste_tabular->columns() > columns)
2262 paste_tabular->deleteColumn(columns);
2264 paste_tabular->setLeftLine(0, true, true);
2265 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2269 paste_tabular->ascii(*bv->buffer(), os,
2270 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2271 bv->stuffClipboard(os.str());
2276 bool InsetTabular::pasteSelection(BufferView * bv)
2281 for (int r1 = 0, r2 = actrow;
2282 r1 < paste_tabular->rows() && r2 < tabular.rows();
2284 for (int c1 = 0, c2 = actcol;
2285 c1 < paste_tabular->columns() && c2 < tabular.columns();
2287 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2288 tabular.isPartOfMultiColumn(r2, c2))
2290 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2294 if (tabular.isPartOfMultiColumn(r2, c2)) {
2298 InsetText & inset = tabular.getCellInset(r2, c2);
2299 inset = paste_tabular->getCellInset(r1, c1);
2300 inset.setOwner(this);
2301 inset.deleteLyXText(bv);
2309 bool InsetTabular::cutSelection(BufferParams const & bp)
2311 if (!hasSelection())
2314 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2315 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2316 if (sel_col_start > sel_col_end) {
2317 sel_col_start = sel_col_end;
2318 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2320 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2323 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2324 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2326 if (sel_row_start > sel_row_end)
2327 swap(sel_row_start, sel_row_end);
2329 if (sel_cell_start > sel_cell_end)
2330 swap(sel_cell_start, sel_cell_end);
2332 for (int i = sel_row_start; i <= sel_row_end; ++i)
2333 for (int j = sel_col_start; j <= sel_col_end; ++j)
2334 tabular.getCellInset(tabular.getCellNumber(i, j))
2335 .clear(bp.tracking_changes);
2340 bool InsetTabular::isRightToLeft(BufferView * bv)
2342 return bv->getParentLanguage(this)->RightToLeft();
2346 int InsetTabular::scroll(bool recursive) const
2348 int sx = UpdatableInset::scroll(false);
2350 if (recursive && the_locking_inset)
2351 sx += the_locking_inset->scroll(recursive);
2357 void InsetTabular::getSelection(int & srow, int & erow,
2358 int & scol, int & ecol) const
2360 int const start = hasSelection() ? sel_cell_start : actcell;
2361 int const end = hasSelection() ? sel_cell_end : actcell;
2363 srow = tabular.row_of_cell(start);
2364 erow = tabular.row_of_cell(end);
2368 scol = tabular.column_of_cell(start);
2369 ecol = tabular.column_of_cell(end);
2373 ecol = tabular.right_column_of_cell(end);
2377 ParagraphList * InsetTabular::getParagraphs(int i) const
2379 return i < tabular.getNumberOfCells()
2380 ? tabular.getCellInset(i).getParagraphs(0)
2385 LyXText * InsetTabular::getText(int i) const
2387 return i < tabular.getNumberOfCells()
2388 ? tabular.getCellInset(i).getText(0)
2393 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2395 if (the_locking_inset)
2396 return the_locking_inset->cursor(bv);
2397 return InsetOld::cursor(bv);
2401 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2404 return const_cast<InsetTabular *>(this);
2406 for (int i = 0; i < tabular.rows(); ++i) {
2407 for (int j = 0; j < tabular.columns(); ++j) {
2408 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2418 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2420 if (the_locking_inset) {
2421 WordLangTuple word =
2422 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2423 if (!word.word().empty())
2425 if (tabular.isLastCell(actcell)) {
2426 bv->unlockInset(const_cast<InsetTabular *>(this));
2427 return WordLangTuple();
2431 // otherwise we have to lock the next inset and ask for it's selecttion
2432 tabular.getCellInset(actcell)
2433 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2434 WordLangTuple word = selectNextWordInt(bv, value);
2435 if (!word.word().empty())
2441 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2443 // when entering this function the inset should be ALWAYS locked!
2444 BOOST_ASSERT(the_locking_inset);
2446 WordLangTuple word =
2447 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2448 if (!word.word().empty())
2451 if (tabular.isLastCell(actcell)) {
2452 bv->unlockInset(const_cast<InsetTabular *>(this));
2453 return WordLangTuple();
2456 // otherwise we have to lock the next inset and ask for it's selecttion
2458 tabular.getCellInset(actcell)
2459 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2460 return selectNextWordInt(bv, value);
2464 void InsetTabular::selectSelectedWord(BufferView * bv)
2466 if (the_locking_inset)
2467 the_locking_inset->selectSelectedWord(bv);
2471 void InsetTabular::markErased()
2473 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2474 tabular.getCellInset(cell).markErased();
2478 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2480 if (the_locking_inset) {
2481 if (the_locking_inset->nextChange(bv, length)) {
2485 if (tabular.isLastCell(actcell))
2489 InsetText & inset = tabular.getCellInset(actcell);
2490 if (inset.nextChange(bv, length)) {
2494 while (!tabular.isLastCell(actcell)) {
2496 InsetText & inset = tabular.getCellInset(actcell);
2497 if (inset.nextChange(bv, length)) {
2506 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2510 if (the_locking_inset) {
2511 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2515 if (tabular.isLastCell(actcell))
2519 InsetText & inset = tabular.getCellInset(cell);
2520 if (inset.searchForward(bv, str, cs, mw)) {
2524 while (!tabular.isLastCell(cell)) {
2526 InsetText & inset = tabular.getCellInset(cell);
2527 if (inset.searchForward(bv, str, cs, mw)) {
2536 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2539 int cell = tabular.getNumberOfCells();
2540 if (the_locking_inset) {
2541 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2550 InsetText & inset = tabular.getCellInset(cell);
2551 if (inset.searchBackward(bv, str, cs, mw)) {
2560 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2562 if (the_locking_inset)
2563 return the_locking_inset->insetAllowed(code);
2564 // we return true here because if the inset is not locked someone
2565 // wants to insert something in one of our insettexts and we generally
2571 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2573 const int cell = tabular.getCellFromInset(in);
2576 return tabular.getPWidth(cell).zero();
2578 // this is a workaround for a crash (New, Insert->Tabular,
2579 // Insert->FootNote)
2583 // well we didn't obviously find it so maybe our owner knows more
2584 BOOST_ASSERT(owner());
2585 return owner()->forceDefaultParagraphs(in);
2589 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2592 if (buf.length() <= 0)
2598 string::size_type len = buf.length();
2599 string::size_type p = 0;
2601 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2609 maxCols = max(cols, maxCols);
2615 maxCols = max(cols, maxCols);
2616 LyXTabular * loctab;
2621 delete paste_tabular;
2622 paste_tabular = new LyXTabular(bv->buffer()->params(),
2624 paste_tabular->setOwner(this);
2625 loctab = paste_tabular;
2634 string::size_type op = 0;
2635 int cells = loctab->getNumberOfCells();
2638 rows = loctab->rows();
2639 int const columns = loctab->columns();
2641 while (cell < cells && p < len && row < rows &&
2642 (p = buf.find_first_of("\t\n", p)) != string::npos)
2648 // we can only set this if we are not too far right
2649 if (cols < columns) {
2650 InsetText & inset = loctab->getCellInset(cell);
2651 LyXFont const font = inset.getLyXText(bv)->
2652 getFont(inset.paragraphs.begin(), 0);
2653 inset.setText(buf.substr(op, p - op), font);
2659 // we can only set this if we are not too far right
2660 if (cols < columns) {
2661 InsetText & inset = tabular.getCellInset(cell);
2662 LyXFont const font = inset.getLyXText(bv)->
2663 getFont(inset.paragraphs.begin(), 0);
2664 inset.setText(buf.substr(op, p - op), font);
2669 cell = loctab->getCellNumber(row, cols);
2675 // check for the last cell if there is no trailing '\n'
2676 if (cell < cells && op < len) {
2677 InsetText & inset = loctab->getCellInset(cell);
2678 LyXFont const font = inset.getLyXText(bv)->
2679 getFont(inset.paragraphs.begin(), 0);
2680 inset.setText(buf.substr(op, len - op), font);
2687 void InsetTabular::addPreview(PreviewLoader & loader) const
2689 int const rows = tabular.rows();
2690 int const columns = tabular.columns();
2691 for (int i = 0; i < rows; ++i)
2692 for (int j = 0; j < columns; ++j)
2693 tabular.getCellInset(i, j).addPreview(loader);
2697 string const InsetTabularMailer::name_("tabular");
2699 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2700 : inset_(const_cast<InsetTabular &>(inset))
2704 string const InsetTabularMailer::inset2string(Buffer const &) const
2706 return params2string(inset_);
2710 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2712 istringstream data(in);
2714 lex.setStream(data);
2716 #warning CHECK verify that this is a sane value to return.
2722 string const token = lex.getString();
2730 string const token = lex.getString();
2731 if (token != "\\active_cell")
2734 cell = lex.getInteger();
2737 // This is part of the inset proper that is usually swallowed
2738 // by Buffer::readInset
2741 string const token = lex.getString();
2742 if (token != "Tabular")
2749 Buffer const & buffer = inset.buffer();
2750 inset.read(buffer, lex);
2752 // We can't set the active cell, but we can tell the frontend
2758 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2760 Buffer const & buffer = inset.buffer();
2763 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2764 inset.write(buffer, data);
2765 data << "\\end_inset\n";