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 "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
29 #include "paragraph_funcs.h"
30 #include "ParagraphParameters.h"
32 #include "WordLangTuple.h"
34 #include "frontends/Alert.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/Painter.h"
39 #include "support/std_sstream.h"
42 using lyx::graphics::PreviewLoader;
44 using lyx::support::ltrim;
45 using lyx::support::strToInt;
52 using std::istringstream;
54 using std::ostringstream;
59 int const ADD_TO_HEIGHT = 2;
60 int const ADD_TO_TABULAR_WIDTH = 2;
63 LyXTabular * paste_tabular = 0;
66 struct TabularFeature {
67 LyXTabular::Feature action;
72 TabularFeature tabularFeature[] =
74 { LyXTabular::APPEND_ROW, "append-row" },
75 { LyXTabular::APPEND_COLUMN, "append-column" },
76 { LyXTabular::DELETE_ROW, "delete-row" },
77 { LyXTabular::DELETE_COLUMN, "delete-column" },
78 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
79 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
80 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
81 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
82 { LyXTabular::ALIGN_LEFT, "align-left" },
83 { LyXTabular::ALIGN_RIGHT, "align-right" },
84 { LyXTabular::ALIGN_CENTER, "align-center" },
85 { LyXTabular::ALIGN_BLOCK, "align-block" },
86 { LyXTabular::VALIGN_TOP, "valign-top" },
87 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
88 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
89 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
90 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
91 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
92 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
93 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
94 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
95 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
96 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
97 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
98 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
99 { LyXTabular::MULTICOLUMN, "multicolumn" },
100 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
101 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
102 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
103 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
104 { LyXTabular::SET_PWIDTH, "set-pwidth" },
105 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
106 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
107 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
108 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
109 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
110 { LyXTabular::SET_USEBOX, "set-usebox" },
111 { LyXTabular::SET_LTHEAD, "set-lthead" },
112 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
113 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
114 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
115 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
116 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
117 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
118 { LyXTabular::LAST_ACTION, "" }
122 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
123 bool operator()(TabularFeature & tf)
125 return tf.action == feature_;
128 LyXTabular::Feature feature_;
134 string const featureAsString(LyXTabular::Feature feature)
136 TabularFeature * it = tabularFeature;
137 TabularFeature * end = it +
138 sizeof(tabularFeature) / sizeof(TabularFeature);
139 it = std::find_if(it, end, FindFeature(feature));
140 return (it == end) ? string() : it->feature;
144 bool InsetTabular::hasPasteBuffer() const
146 return (paste_tabular != 0);
150 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
151 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
152 buffer_(&buf), cursorx_(0), cursory_(0)
154 tabular.setOwner(this);
155 // for now make it always display as display() inset
157 the_locking_inset = 0;
158 old_locking_inset = 0;
161 actrow = actcell = 0;
169 InsetTabular::InsetTabular(InsetTabular const & tab)
170 : UpdatableInset(tab), tabular(tab.tabular),
171 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
173 tabular.setOwner(this);
174 the_locking_inset = 0;
175 old_locking_inset = 0;
178 actrow = actcell = 0;
186 InsetTabular::~InsetTabular()
188 InsetTabularMailer(*this).hideDialog();
192 auto_ptr<InsetBase> InsetTabular::clone() const
194 return auto_ptr<InsetBase>(new InsetTabular(*this));
198 Buffer const & InsetTabular::buffer() const
204 BufferView * InsetTabular::view() const
211 void InsetTabular::buffer(Buffer * b)
217 void InsetTabular::write(Buffer const & buf, ostream & os) const
219 os << "Tabular" << endl;
220 tabular.write(buf, os);
224 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
226 bool const old_format = (lex.getString() == "\\LyXTable");
228 tabular.read(buf, lex);
234 string token = lex.getString();
235 while (lex.isOK() && (token != "\\end_inset")) {
237 token = lex.getString();
239 if (token != "\\end_inset") {
240 lex.printError("Missing \\end_inset at this point. "
246 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
248 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
249 // mi.base.textwidth << "\n";
251 lyxerr << "InsetTabular::metrics: need bv" << endl;
255 calculate_dimensions_of_cells(mi);
257 dim.asc = tabular.getAscentOfRow(0);
258 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
259 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
264 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
266 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
268 BufferView * bv = pi.base.bv;
271 UpdatableInset::draw(pi, x, y);
279 x += ADD_TO_TABULAR_WIDTH;
282 first_visible_cell = -1;
283 for (int i = 0; i < tabular.rows(); ++i) {
285 cell = tabular.getCellNumber(i, 0);
286 if (y + tabular.getDescentOfRow(i) <= 0 &&
287 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
289 y += tabular.getDescentOfRow(i) +
290 tabular.getAscentOfRow(i + 1) +
291 tabular.getAdditionalHeight(i + 1);
294 for (int j = 0; j < tabular.columns(); ++j) {
295 if (nx > bv->workWidth())
297 if (tabular.isPartOfMultiColumn(i, j))
299 int cx = nx + tabular.getBeginningOfTextInCell(cell);
300 if (first_visible_cell < 0)
301 first_visible_cell = cell;
302 if (hasSelection()) {
303 drawCellSelection(pi.pain, nx, y, i, j, cell);
306 tabular.getCellInset(cell).draw(pi, cx, y);
307 drawCellLines(pi.pain, nx, y, i, cell);
308 nx += tabular.getWidthOfColumn(cell);
312 // Would be nice, but for some completely unfathomable reason,
313 // on a col resize to a new fixed width, even though the insettexts
314 // are resized, the cell isn't, but drawing all cells in a tall table
315 // has the desired effect somehow. Complete dark magic.
317 // avoiding drawing the rest of a long table is
318 // a pretty big speedup
319 if (y > bv->workHeight())
323 y += tabular.getDescentOfRow(i) +
324 tabular.getAscentOfRow(i + 1) +
325 tabular.getAdditionalHeight(i + 1);
330 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
331 int row, int cell) const
333 int x2 = x + tabular.getWidthOfColumn(cell);
336 if (!tabular.topAlreadyDrawn(cell)) {
337 on_off = !tabular.topLine(cell);
338 pain.line(x, y - tabular.getAscentOfRow(row),
339 x2, y - tabular.getAscentOfRow(row),
340 on_off ? LColor::tabularonoffline : LColor::tabularline,
341 on_off ? Painter::line_onoffdash : Painter::line_solid);
343 on_off = !tabular.bottomLine(cell);
344 pain.line(x, y + tabular.getDescentOfRow(row),
345 x2, y + tabular.getDescentOfRow(row),
346 on_off ? LColor::tabularonoffline : LColor::tabularline,
347 on_off ? Painter::line_onoffdash : Painter::line_solid);
348 if (!tabular.leftAlreadyDrawn(cell)) {
349 on_off = !tabular.leftLine(cell);
350 pain.line(x, y - tabular.getAscentOfRow(row),
351 x, y + tabular.getDescentOfRow(row),
352 on_off ? LColor::tabularonoffline : LColor::tabularline,
353 on_off ? Painter::line_onoffdash : Painter::line_solid);
355 on_off = !tabular.rightLine(cell);
356 pain.line(x2 - tabular.getAdditionalWidth(cell),
357 y - tabular.getAscentOfRow(row),
358 x2 - tabular.getAdditionalWidth(cell),
359 y + tabular.getDescentOfRow(row),
360 on_off ? LColor::tabularonoffline : LColor::tabularline,
361 on_off ? Painter::line_onoffdash : Painter::line_solid);
365 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
366 int row, int column, int cell) const
368 BOOST_ASSERT(hasSelection());
369 int cs = tabular.column_of_cell(sel_cell_start);
370 int ce = tabular.column_of_cell(sel_cell_end);
373 cs = tabular.column_of_cell(sel_cell_end);
375 ce = tabular.right_column_of_cell(sel_cell_end);
378 int rs = tabular.row_of_cell(sel_cell_start);
379 int re = tabular.row_of_cell(sel_cell_end);
383 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
384 int w = tabular.getWidthOfColumn(cell);
385 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
386 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
387 w, h, LColor::selection);
392 string const InsetTabular::editMessage() const
394 return _("Opened table");
398 void InsetTabular::insetUnlock(BufferView * bv)
400 if (the_locking_inset) {
401 the_locking_inset->insetUnlock(bv);
403 the_locking_inset = 0;
408 if (scroll(false) || hasSelection()) {
417 void InsetTabular::updateLocal(BufferView * bv) const
419 bv->updateInset(this);
425 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
427 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
434 if (inset == &tabular.getCellInset(actcell)) {
435 lyxerr[Debug::INSETTEXT] << "OK" << endl;
436 the_locking_inset = &tabular.getCellInset(actcell);
441 if (!the_locking_inset) {
442 int const n = tabular.getNumberOfCells();
443 int const id = inset->id();
444 for (int i = 0; i < n; ++i) {
445 InsetText * in = &tabular.getCellInset(i);
448 the_locking_inset = in;
453 if (in->getInsetFromID(id)) {
455 in->dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
456 return the_locking_inset->lockInsetInInset(bv, inset);
462 if (the_locking_inset && (the_locking_inset == inset)) {
463 lyxerr[Debug::INSETTEXT] << "OK" << endl;
468 if (the_locking_inset) {
469 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
470 return the_locking_inset->lockInsetInInset(bv, inset);
473 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
478 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
481 if (!the_locking_inset)
483 if (the_locking_inset == inset) {
484 the_locking_inset->insetUnlock(bv);
486 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
493 // this has to be here otherwise we don't redraw the cell!
494 the_locking_inset = 0;
497 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
498 if (inset->lyxCode() == TABULAR_CODE &&
499 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
500 InsetTabularMailer(*this).updateDialog(bv);
509 int InsetTabular::insetInInsetY() const
511 if (!the_locking_inset)
513 return inset_y + the_locking_inset->insetInInsetY();
517 UpdatableInset * InsetTabular::getLockingInset() const
519 return the_locking_inset ? the_locking_inset->getLockingInset() :
520 const_cast<InsetTabular *>(this);
524 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
528 if (the_locking_inset)
529 return the_locking_inset->getFirstLockingInsetOfType(c);
534 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
536 if (the_locking_inset)
537 return the_locking_inset->insertInset(bv, inset);
542 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
544 if (hasSelection() && cmd.button() == mouse_button::button3)
547 if (hasSelection()) {
549 updateLocal(cmd.view());
552 int const ocell = actcell;
553 BufferView * bv = cmd.view();
557 the_locking_inset = 0;
561 setPos(bv, cmd.x, cmd.y);
564 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
566 if (ocell == actcell && the_locking_inset && inset_hit) {
568 FuncRequest cmd1 = cmd;
571 the_locking_inset->dispatch(cmd1);
575 if (the_locking_inset) {
576 the_locking_inset->insetUnlock(bv);
578 the_locking_inset = 0;
581 if (cmd.button() == mouse_button::button2) {
582 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
586 if (inset_hit && bv->theLockingInset()) {
587 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
588 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
591 FuncRequest cmd1 = cmd;
594 the_locking_inset->dispatch(cmd1);
599 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
602 if (the_locking_inset) {
603 FuncRequest cmd1 = cmd;
606 ret = the_locking_inset->dispatch(cmd1);
608 if (cmd.button() == mouse_button::button3 && !ret) {
609 InsetTabularMailer(*this).showDialog(cmd.view());
616 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
618 if (the_locking_inset) {
619 FuncRequest cmd1 = cmd;
622 the_locking_inset->dispatch(cmd1);
626 BufferView * bv = cmd.view();
627 int const old_cell = actcell;
629 setPos(bv, cmd.x, cmd.y);
630 if (!hasSelection()) {
631 setSelection(actcell, actcell);
633 } else if (old_cell != actcell) {
634 setSelection(sel_cell_start, actcell);
640 void InsetTabular::edit(BufferView * bv, int index)
642 lyxerr << "InsetTabular::edit" << endl;
643 if (!bv->lockInset(this)) {
644 lyxerr << "InsetTabular::Cannot lock inset (2)" << endl;
649 the_locking_inset = 0;
657 UpdatableInset & inset = tabular.getCellInset(actcell);
658 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
659 if (the_locking_inset)
665 InsetTabular::priv_dispatch(FuncRequest const & cmd,
666 idx_type & idx, pos_type & pos)
668 // We need to save the value of the_locking_inset as the call to
669 // the_locking_inset->localDispatch might unlock it.
670 old_locking_inset = the_locking_inset;
671 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
672 BufferView * bv = cmd.view();
674 if (cmd.action == LFUN_INSET_EDIT) {
676 lyxerr << "InsetTabular::edit: " << this << " args: '"
677 << cmd.argument << "' first cell: "
678 << &tabular.cell_info[0][0].inset << endl;
680 if (!bv->lockInset(this)) {
681 lyxerr << "InsetTabular::Cannot lock inset" << endl;
687 the_locking_inset = 0;
691 if (cmd.argument.size()) {
692 if (cmd.argument == "left") {
693 if (isRightToLeft(bv))
694 actcell = tabular.getLastCellInRow(0);
698 if (isRightToLeft(bv))
699 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
701 actcell = tabular.getNumberOfCells() - 1;
709 setPos(bv, cmd.x, cmd.y);
712 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
713 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
715 activateCellInset(bv, cmd.x - inset_x, cmd.y - inset_y, cmd.button());
721 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
726 if (cmd.action < 0 && cmd.argument.empty())
729 bool hs = hasSelection();
732 // this one have priority over the locked InsetText, if we're not already
733 // inside another tabular then that one get's priority!
734 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
735 switch (cmd.action) {
736 case LFUN_MOUSE_PRESS:
740 case LFUN_MOUSE_MOTION:
741 lfunMouseMotion(cmd);
744 case LFUN_MOUSE_RELEASE:
745 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
747 case LFUN_CELL_BACKWARD:
748 case LFUN_CELL_FORWARD:
749 unlockInsetInInset(bv, the_locking_inset);
750 if (cmd.action == LFUN_CELL_FORWARD)
751 moveNextCell(bv, old_locking_inset != 0);
753 movePrevCell(bv, old_locking_inset != 0);
757 if (!the_locking_inset)
758 return DISPATCHED_NOUPDATE;
760 // this to avoid compiler warnings.
766 kb_action action = cmd.action;
767 string arg = cmd.argument;
768 if (the_locking_inset) {
769 result = the_locking_inset->dispatch(cmd);
770 if (result == DISPATCHED_NOUPDATE) {
773 if (sc != scroll()) { // inset has been scrolled
777 } else if (result == DISPATCHED) {
780 } else if (result == FINISHED_UP) {
782 // Make sure to reset status message after
783 // exiting, e.g. math inset
784 bv->owner()->clearMessage();
785 } else if (result == FINISHED_DOWN) {
787 bv->owner()->clearMessage();
788 } else if (result == FINISHED_RIGHT) {
790 bv->owner()->clearMessage();
791 } else if (result == FINISHED) {
792 bv->owner()->clearMessage();
798 // --- Cursor Movements ----------------------------------
799 case LFUN_RIGHTSEL: {
800 int const start = hasSelection() ? sel_cell_start : actcell;
801 if (tabular.isLastCellInRow(actcell)) {
802 setSelection(start, actcell);
807 // if we are starting a selection, only select
808 // the current cell at the beginning
809 if (hasSelection()) {
810 moveRight(bv, false);
813 setSelection(start, end);
818 result = moveRight(bv);
824 int const start = hasSelection() ? sel_cell_start : actcell;
825 if (tabular.isFirstCellInRow(actcell)) {
826 setSelection(start, actcell);
831 // if we are starting a selection, only select
832 // the current cell at the beginning
833 if (hasSelection()) {
837 setSelection(start, end);
842 result = moveLeft(bv);
848 int const start = hasSelection() ? sel_cell_start : actcell;
849 int const ocell = actcell;
850 // if we are starting a selection, only select
851 // the current cell at the beginning
852 if (hasSelection()) {
854 if (ocell == sel_cell_end ||
855 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
856 setSelection(start, tabular.getCellBelow(sel_cell_end));
858 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
860 setSelection(start, start);
866 result = moveDown(bv, old_locking_inset != 0);
872 int const start = hasSelection() ? sel_cell_start : actcell;
873 int const ocell = actcell;
874 // if we are starting a selection, only select
875 // the current cell at the beginning
876 if (hasSelection()) {
878 if ((ocell == sel_cell_end) ||
879 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
880 setSelection(start, tabular.getCellAbove(sel_cell_end));
882 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
884 setSelection(start, start);
890 result = moveUp(bv, old_locking_inset != 0);
899 unlockInsetInInset(bv, the_locking_inset);
900 if (bv->top_y() + bv->painter().paperHeight() <
901 top_baseline + tabular.getHeightOfTabular())
903 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
904 actcell = tabular.getCellBelow(first_visible_cell) + column;
906 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
916 unlockInsetInInset(bv, the_locking_inset);
917 if (top_baseline < 0) {
918 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
919 if (top_baseline > 0)
922 actcell = tabular.getCellBelow(first_visible_cell) + column;
930 // none of these make sense for insettabular,
931 // but we must catch them to prevent any
932 // selection from being confused
936 case LFUN_WORDLEFTSEL:
938 case LFUN_WORDRIGHTSEL:
940 case LFUN_DOWN_PARAGRAPH:
941 case LFUN_DOWN_PARAGRAPHSEL:
942 case LFUN_UP_PARAGRAPH:
943 case LFUN_UP_PARAGRAPHSEL:
949 case LFUN_BEGINNINGBUF:
950 case LFUN_BEGINNINGBUFSEL:
954 case LFUN_LAYOUT_TABULAR:
955 InsetTabularMailer(*this).showDialog(bv);
957 case LFUN_INSET_DIALOG_UPDATE:
958 InsetTabularMailer(*this).updateDialog(bv);
960 case LFUN_TABULAR_FEATURE:
961 if (!tabularFeatures(bv, arg))
962 result = UNDISPATCHED;
964 // insert file functions
965 case LFUN_FILE_INSERT_ASCII_PARA:
966 case LFUN_FILE_INSERT_ASCII:
968 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
971 if (insertAsciiString(bv, tmpstr, false))
974 result = UNDISPATCHED;
977 // cut and paste functions
979 if (!copySelection(bv))
983 recordUndo(bv, Undo::DELETE);
984 cutSelection(bv->buffer()->params());
993 case LFUN_PASTESELECTION:
995 string const clip = bv->getClipboard();
999 if (clip.find('\t') != string::npos) {
1003 string::size_type len = clip.length();
1004 string::size_type p = 0;
1007 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1015 maxCols = max(cols, maxCols);
1021 maxCols = max(cols, maxCols);
1022 delete paste_tabular;
1023 paste_tabular = new LyXTabular(bv->buffer()->params(),
1024 this, rows, maxCols);
1025 string::size_type op = 0;
1027 int cells = paste_tabular->getNumberOfCells();
1029 while ((cell < cells) && (p < len) &&
1030 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1035 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1040 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1041 while (cols++ < maxCols)
1049 // check for the last cell if there is no trailing '\n'
1050 if ((cell < cells) && (op < len))
1051 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1054 if (!insertAsciiString(bv, clip, true))
1057 // so that the clipboard is used and it goes on
1059 // and executes LFUN_PASTESELECTION in insettext!
1060 delete paste_tabular;
1065 if (hasPasteBuffer()) {
1066 recordUndo(bv, Undo::INSERT);
1071 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1073 // handle font changing stuff on selection before we lock the inset
1074 // in the default part!
1075 result = UNDISPATCHED;
1086 case LFUN_UNDERLINE:
1087 case LFUN_FONT_SIZE:
1088 if (bv->dispatch(FuncRequest(bv, action, arg)))
1089 result = DISPATCHED;
1095 // we try to activate the actual inset and put this event down to
1096 // the insets dispatch function.
1097 if (result == DISPATCHED || the_locking_inset)
1099 if (activateCellInset(bv)) {
1100 result = the_locking_inset->dispatch(FuncRequest(bv, action, arg));
1101 if (result == UNDISPATCHED || result >= FINISHED) {
1102 unlockInsetInInset(bv, the_locking_inset);
1103 // we need to update if this was requested before
1105 return UNDISPATCHED;
1114 if (result < FINISHED) {
1115 if (!the_locking_inset && bv->fitCursor())
1118 bv->unlockInset(this);
1123 int InsetTabular::latex(Buffer const & buf, ostream & os,
1124 LatexRunParams const & runparams) const
1126 return tabular.latex(buf, os, runparams);
1130 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1133 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1135 return tabular.ascii(buf, os, 0, false, 0);
1139 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1141 return tabular.linuxdoc(buf,os);
1145 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1150 // if the table is inside a float it doesn't need the informaltable
1151 // wrapper. Search for it.
1152 for (master = owner();
1153 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1154 master = master->owner());
1157 os << "<informaltable>";
1162 ret += tabular.docbook(buf, os, mixcont);
1164 os << "</informaltable>";
1173 void InsetTabular::validate(LaTeXFeatures & features) const
1175 tabular.validate(features);
1179 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1182 // if we have a locking_inset we should have to check only this cell for
1183 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1184 // This is _really_ important (André)
1185 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1188 for (int j = 0; j < tabular.columns(); ++j) {
1192 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1193 tabular.getCellInset(actrow, j).metrics(m, dim);
1194 maxAsc = max(dim.asc, maxAsc);
1195 maxDesc = max(dim.des, maxDesc);
1197 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1198 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1199 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1205 for (int i = 0; i < tabular.rows(); ++i) {
1208 for (int j = 0; j < tabular.columns(); ++j) {
1209 if (tabular.isPartOfMultiColumn(i, j))
1215 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1216 tabular.getCellInset(cell).metrics(m, dim);
1217 maxAsc = max(maxAsc, dim.asc);
1218 maxDesc = max(maxDesc, dim.des);
1219 tabular.setWidthOfCell(cell, dim.wid);
1221 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1222 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1227 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1229 if (the_locking_inset) {
1230 the_locking_inset->getCursor(bv, x, y);
1235 y = cursory_ + InsetTabular::y();
1238 int desc = tabular.getDescentOfRow(actrow);
1240 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1242 y += ADD_TO_HEIGHT * 2;
1243 y += TEXT_TO_INSET_OFFSET;
1247 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1249 if (the_locking_inset) {
1250 the_locking_inset->getCursorPos(bv, x, y);
1253 x = cursorx_ - top_x;
1258 void InsetTabular::fitInsetCursor(BufferView * bv) const
1260 if (the_locking_inset) {
1261 the_locking_inset->fitInsetCursor(bv);
1266 int const asc = font_metrics::maxAscent(font);
1267 int const desc = font_metrics::maxDescent(font);
1270 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1274 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1277 actcell = actrow = actcol = 0;
1278 int ly = tabular.getDescentOfRow(actrow);
1280 // first search the right row
1281 while (ly < y && actrow + 1 < tabular.rows()) {
1282 cursory_ += tabular.getDescentOfRow(actrow) +
1283 tabular.getAscentOfRow(actrow + 1) +
1284 tabular.getAdditionalHeight(actrow + 1);
1286 ly = cursory_ + tabular.getDescentOfRow(actrow);
1288 actcell = tabular.getCellNumber(actrow, actcol);
1290 // now search the right column
1291 int lx = tabular.getWidthOfColumn(actcell) -
1292 tabular.getAdditionalWidth(actcell);
1294 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1295 lx += tabular.getWidthOfColumn(actcell + 1)
1296 + tabular.getAdditionalWidth(actcell);
1298 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1303 int InsetTabular::getCellXPos(int cell) const
1307 for (; !tabular.isFirstCellInRow(c); --c)
1309 int lx = tabular.getWidthOfColumn(cell);
1310 for (; c < cell; ++c)
1311 lx += tabular.getWidthOfColumn(c);
1313 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1317 void InsetTabular::resetPos(BufferView * bv) const
1319 #ifdef WITH_WARNINGS
1320 #warning This should be fixed in the right manner (20011128 Jug)
1322 // fast hack to fix infinite repaintings!
1323 if (in_reset_pos > 0)
1327 actcol = tabular.column_of_cell(actcell);
1330 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1331 if (tabular.isLastCellInRow(cell)) {
1332 cursory_ += tabular.getDescentOfRow(actrow) +
1333 tabular.getAscentOfRow(actrow + 1) +
1334 tabular.getAdditionalHeight(actrow + 1);
1339 if (the_locking_inset)
1343 // we need this only from here on!!!
1345 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1346 int new_x = getCellXPos(actcell);
1347 int old_x = cursorx_;
1350 // cursor.x(getCellXPos(actcell) + offset);
1351 if (actcol < tabular.columns() - 1 && scroll(false) &&
1352 tabular.getWidthOfTabular() < bv->workWidth()-20)
1356 } else if (the_locking_inset &&
1357 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1359 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1360 if (xx > bv->workWidth()-20) {
1361 scroll(bv, -(xx - bv->workWidth() + 60));
1363 } else if (xx < 20) {
1371 } else if (cursorx_ - offset > 20 &&
1372 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1373 > bv->workWidth() - 20) {
1374 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1376 } else if (cursorx_ - offset < 20) {
1377 scroll(bv, 20 - cursorx_ + offset);
1379 } else if (scroll() && top_x > 20 &&
1380 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1381 scroll(bv, old_x - cursorx_);
1384 if (the_locking_inset) {
1385 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1388 if ((!the_locking_inset ||
1389 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1390 actcell != oldcell) {
1391 InsetTabularMailer(*this).updateDialog(bv);
1398 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1400 if (lock && !old_locking_inset) {
1401 if (activateCellInset(bv))
1404 bool moved = isRightToLeft(bv)
1405 ? movePrevCell(bv) : moveNextCell(bv);
1407 return FINISHED_RIGHT;
1408 if (lock && activateCellInset(bv))
1412 return DISPATCHED_NOUPDATE;
1416 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1418 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1422 if (lock && activateCellInset(bv, 0, 0, mouse_button::none, true))
1425 return DISPATCHED_NOUPDATE;
1429 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1431 int const ocell = actcell;
1432 actcell = tabular.getCellAbove(actcell);
1433 if (actcell == ocell) // we moved out of the inset
1439 if (old_locking_inset) {
1440 old_locking_inset->getCursorPos(bv, x, y);
1441 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1443 if (activateCellInset(bv, x, 0))
1446 return DISPATCHED_NOUPDATE;
1450 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1452 int const ocell = actcell;
1453 actcell = tabular.getCellBelow(actcell);
1454 if (actcell == ocell) // we moved out of the inset
1455 return FINISHED_DOWN;
1460 if (old_locking_inset) {
1461 old_locking_inset->getCursorPos(bv, x, y);
1462 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1464 if (activateCellInset(bv, x, 0))
1467 return DISPATCHED_NOUPDATE;
1471 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1473 if (isRightToLeft(bv)) {
1474 if (tabular.isFirstCellInRow(actcell)) {
1475 int row = tabular.row_of_cell(actcell);
1476 if (row == tabular.rows() - 1)
1478 actcell = tabular.getLastCellInRow(row);
1479 actcell = tabular.getCellBelow(actcell);
1486 if (tabular.isLastCell(actcell))
1491 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1492 isRightToLeftPar(bv->buffer()->params());
1493 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1500 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1502 if (isRightToLeft(bv)) {
1503 if (tabular.isLastCellInRow(actcell)) {
1504 int row = tabular.row_of_cell(actcell);
1507 actcell = tabular.getFirstCellInRow(row);
1508 actcell = tabular.getCellAbove(actcell);
1510 if (tabular.isLastCell(actcell))
1515 if (!actcell) // first cell
1520 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1521 isRightToLeftPar(bv->buffer()->params());
1522 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1529 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1533 setSelection(0, tabular.getNumberOfCells() - 1);
1535 if (hasSelection()) {
1536 recordUndo(bv, Undo::ATOMIC);
1537 bool const frozen = undo_frozen;
1540 // apply the fontchange on the whole selection
1545 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1546 for (int i = sel_row_start; i <= sel_row_end; ++i)
1547 for (int j = sel_col_start; j <= sel_col_end; ++j)
1548 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1556 if (the_locking_inset)
1557 the_locking_inset->setFont(bv, font, tall);
1561 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1563 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1566 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1567 string const tmp = tabularFeature[i].feature;
1569 if (tmp == what.substr(0, tmp.length())) {
1570 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1571 //tabularFeatures[i].feature.length())) {
1572 action = tabularFeature[i].action;
1576 if (action == LyXTabular::LAST_ACTION)
1580 ltrim(what.substr(tabularFeature[i].feature.length()));
1581 tabularFeatures(bv, action, val);
1587 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1588 string const & special, bool & flag)
1590 if (special == "dl_above") {
1593 } else if (special == "dl_below") {
1594 ltt.bottomDL = flag;
1596 } else if (special == "empty") {
1608 void InsetTabular::tabularFeatures(BufferView * bv,
1609 LyXTabular::Feature feature, string const & value)
1615 bool setLines = false;
1616 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1617 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1621 case LyXTabular::M_ALIGN_LEFT:
1622 case LyXTabular::ALIGN_LEFT:
1623 setAlign = LYX_ALIGN_LEFT;
1626 case LyXTabular::M_ALIGN_RIGHT:
1627 case LyXTabular::ALIGN_RIGHT:
1628 setAlign = LYX_ALIGN_RIGHT;
1631 case LyXTabular::M_ALIGN_CENTER:
1632 case LyXTabular::ALIGN_CENTER:
1633 setAlign = LYX_ALIGN_CENTER;
1636 case LyXTabular::ALIGN_BLOCK:
1637 setAlign = LYX_ALIGN_BLOCK;
1640 case LyXTabular::M_VALIGN_TOP:
1641 case LyXTabular::VALIGN_TOP:
1642 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1645 case LyXTabular::M_VALIGN_BOTTOM:
1646 case LyXTabular::VALIGN_BOTTOM:
1647 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1650 case LyXTabular::M_VALIGN_MIDDLE:
1651 case LyXTabular::VALIGN_MIDDLE:
1652 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1659 if (hasSelection()) {
1660 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1662 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1663 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1665 recordUndo(bv, Undo::ATOMIC);
1667 int row = tabular.row_of_cell(actcell);
1668 int column = tabular.column_of_cell(actcell);
1670 LyXTabular::ltType ltt;
1674 case LyXTabular::SET_PWIDTH: {
1675 LyXLength const len(value);
1676 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1678 tabular.setColumnPWidth(actcell, len);
1679 if (oldlen != len) {
1680 // We need this otherwise we won't resize
1681 // the insettext of the active cell (if any)
1682 // until later (see InsetText::do_resize)
1683 unlockInsetInInset(bv, the_locking_inset);
1688 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1689 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1690 else if (!len.zero()
1691 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1692 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1696 case LyXTabular::SET_MPWIDTH:
1698 LyXLength const len(value);
1699 LyXLength const & oldlen = tabular.getPWidth(actcell);
1700 tabular.setMColumnPWidth(actcell, len);
1701 if (oldlen != len) {
1702 // We need this otherwise we won't resize
1703 // the insettext of the active cell (if any)
1704 // until later (see InsetText::do_resize)
1705 unlockInsetInInset(bv, the_locking_inset);
1711 case LyXTabular::SET_SPECIAL_COLUMN:
1712 case LyXTabular::SET_SPECIAL_MULTI:
1713 tabular.setAlignSpecial(actcell,value,feature);
1717 case LyXTabular::APPEND_ROW:
1718 // append the row into the tabular
1719 unlockInsetInInset(bv, the_locking_inset);
1720 tabular.appendRow(bv->buffer()->params(), actcell);
1721 tabular.setOwner(this);
1725 case LyXTabular::APPEND_COLUMN:
1726 // append the column into the tabular
1727 unlockInsetInInset(bv, the_locking_inset);
1728 tabular.appendColumn(bv->buffer()->params(), actcell);
1729 tabular.setOwner(this);
1730 actcell = tabular.getCellNumber(row, column);
1734 case LyXTabular::DELETE_ROW:
1735 unlockInsetInInset(bv, the_locking_inset);
1736 for (int i = sel_row_start; i <= sel_row_end; ++i)
1737 tabular.deleteRow(sel_row_start);
1738 if (sel_row_start >= tabular.rows())
1740 actcell = tabular.getCellNumber(sel_row_start, column);
1745 case LyXTabular::DELETE_COLUMN:
1746 unlockInsetInInset(bv, the_locking_inset);
1747 for (int i = sel_col_start; i <= sel_col_end; ++i)
1748 tabular.deleteColumn(sel_col_start);
1749 if (sel_col_start >= tabular.columns())
1751 actcell = tabular.getCellNumber(row, sel_col_start);
1756 case LyXTabular::M_TOGGLE_LINE_TOP:
1758 case LyXTabular::TOGGLE_LINE_TOP: {
1759 bool lineSet = !tabular.topLine(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)
1763 tabular.getCellNumber(i, j),
1769 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1771 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1772 bool lineSet = !tabular.bottomLine(actcell, flag);
1773 for (int i = sel_row_start; i <= sel_row_end; ++i)
1774 for (int j = sel_col_start; j <= sel_col_end; ++j)
1775 tabular.setBottomLine(
1776 tabular.getCellNumber(i, j),
1783 case LyXTabular::M_TOGGLE_LINE_LEFT:
1785 case LyXTabular::TOGGLE_LINE_LEFT: {
1786 bool lineSet = !tabular.leftLine(actcell, flag);
1787 for (int i = sel_row_start; i <= sel_row_end; ++i)
1788 for (int j = sel_col_start; j <= sel_col_end; ++j)
1789 tabular.setLeftLine(
1790 tabular.getCellNumber(i,j),
1797 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1799 case LyXTabular::TOGGLE_LINE_RIGHT: {
1800 bool lineSet = !tabular.rightLine(actcell, flag);
1801 for (int i = sel_row_start; i <= sel_row_end; ++i)
1802 for (int j = sel_col_start; j <= sel_col_end; ++j)
1803 tabular.setRightLine(
1804 tabular.getCellNumber(i,j),
1811 case LyXTabular::M_ALIGN_LEFT:
1812 case LyXTabular::M_ALIGN_RIGHT:
1813 case LyXTabular::M_ALIGN_CENTER:
1815 case LyXTabular::ALIGN_LEFT:
1816 case LyXTabular::ALIGN_RIGHT:
1817 case LyXTabular::ALIGN_CENTER:
1818 case LyXTabular::ALIGN_BLOCK:
1819 for (int i = sel_row_start; i <= sel_row_end; ++i)
1820 for (int j = sel_col_start; j <= sel_col_end; ++j)
1821 tabular.setAlignment(
1822 tabular.getCellNumber(i, j),
1828 case LyXTabular::M_VALIGN_TOP:
1829 case LyXTabular::M_VALIGN_BOTTOM:
1830 case LyXTabular::M_VALIGN_MIDDLE:
1832 case LyXTabular::VALIGN_TOP:
1833 case LyXTabular::VALIGN_BOTTOM:
1834 case LyXTabular::VALIGN_MIDDLE:
1835 for (int i = sel_row_start; i <= sel_row_end; ++i)
1836 for (int j = sel_col_start; j <= sel_col_end; ++j)
1837 tabular.setVAlignment(
1838 tabular.getCellNumber(i, j),
1843 case LyXTabular::MULTICOLUMN: {
1844 if (sel_row_start != sel_row_end) {
1845 #ifdef WITH_WARNINGS
1846 #warning Need I say it ? This is horrible.
1848 Alert::error(_("Error setting multicolumn"),
1849 _("You cannot set multicolumn vertically."));
1852 // just multicol for one Single Cell
1853 if (!hasSelection()) {
1854 // check wether we are completly in a multicol
1855 if (tabular.isMultiColumn(actcell))
1856 tabular.unsetMultiColumn(actcell);
1858 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1862 // we have a selection so this means we just add all this
1863 // cells to form a multicolumn cell
1867 if (sel_cell_start > sel_cell_end) {
1868 s_start = sel_cell_end;
1869 s_end = sel_cell_start;
1871 s_start = sel_cell_start;
1872 s_end = sel_cell_end;
1874 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1881 case LyXTabular::SET_ALL_LINES:
1883 case LyXTabular::UNSET_ALL_LINES:
1884 for (int i = sel_row_start; i <= sel_row_end; ++i)
1885 for (int j = sel_col_start; j <= sel_col_end; ++j)
1886 tabular.setAllLines(
1887 tabular.getCellNumber(i,j), setLines);
1891 case LyXTabular::SET_LONGTABULAR:
1892 tabular.setLongTabular(true);
1893 updateLocal(bv); // because this toggles displayed
1896 case LyXTabular::UNSET_LONGTABULAR:
1897 tabular.setLongTabular(false);
1898 updateLocal(bv); // because this toggles displayed
1901 case LyXTabular::SET_ROTATE_TABULAR:
1902 tabular.setRotateTabular(true);
1905 case LyXTabular::UNSET_ROTATE_TABULAR:
1906 tabular.setRotateTabular(false);
1909 case LyXTabular::SET_ROTATE_CELL:
1910 for (int i = sel_row_start; i <= sel_row_end; ++i)
1911 for (int j = sel_col_start; j <= sel_col_end; ++j)
1912 tabular.setRotateCell(
1913 tabular.getCellNumber(i, j), true);
1916 case LyXTabular::UNSET_ROTATE_CELL:
1917 for (int i = sel_row_start; i <= sel_row_end; ++i)
1918 for (int j = sel_col_start; j <= sel_col_end; ++j)
1919 tabular.setRotateCell(
1920 tabular.getCellNumber(i, j), false);
1923 case LyXTabular::SET_USEBOX: {
1924 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1925 if (val == tabular.getUsebox(actcell))
1926 val = LyXTabular::BOX_NONE;
1927 for (int i = sel_row_start; i <= sel_row_end; ++i)
1928 for (int j = sel_col_start; j <= sel_col_end; ++j)
1929 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1933 case LyXTabular::UNSET_LTFIRSTHEAD:
1935 case LyXTabular::SET_LTFIRSTHEAD:
1936 tabular.getRowOfLTFirstHead(row, ltt);
1937 checkLongtableSpecial(ltt, value, flag);
1938 tabular.setLTHead(row, flag, ltt, true);
1941 case LyXTabular::UNSET_LTHEAD:
1943 case LyXTabular::SET_LTHEAD:
1944 tabular.getRowOfLTHead(row, ltt);
1945 checkLongtableSpecial(ltt, value, flag);
1946 tabular.setLTHead(row, flag, ltt, false);
1949 case LyXTabular::UNSET_LTFOOT:
1951 case LyXTabular::SET_LTFOOT:
1952 tabular.getRowOfLTFoot(row, ltt);
1953 checkLongtableSpecial(ltt, value, flag);
1954 tabular.setLTFoot(row, flag, ltt, false);
1957 case LyXTabular::UNSET_LTLASTFOOT:
1959 case LyXTabular::SET_LTLASTFOOT:
1960 tabular.getRowOfLTLastFoot(row, ltt);
1961 checkLongtableSpecial(ltt, value, flag);
1962 tabular.setLTFoot(row, flag, ltt, true);
1965 case LyXTabular::SET_LTNEWPAGE:
1966 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1969 // dummy stuff just to avoid warnings
1970 case LyXTabular::LAST_ACTION:
1974 InsetTabularMailer(*this).updateDialog(bv);
1978 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1979 mouse_button::state button, bool behind)
1981 UpdatableInset & inset = tabular.getCellInset(actcell);
1984 x = inset.x() + inset.width();
1985 y = inset.descent();
1987 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1988 //inset_y = cursory_;
1989 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1990 if (!the_locking_inset)
1993 return the_locking_inset;
1997 bool InsetTabular::insetHit(BufferView *, int x, int) const
1999 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
2003 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2005 //resizeLyXText(bv, recursive);
2009 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2010 bool const recursive) const
2012 if (the_locking_inset)
2013 return the_locking_inset->getLyXText(bv, recursive);
2014 return InsetOld::getLyXText(bv, recursive);
2018 bool InsetTabular::showInsetDialog(BufferView * bv) const
2020 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2021 InsetTabularMailer(*this).showDialog(bv);
2026 void InsetTabular::openLayoutDialog(BufferView * bv) const
2028 if (the_locking_inset) {
2029 InsetTabular * inset = static_cast<InsetTabular *>
2030 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2032 inset->openLayoutDialog(bv);
2036 InsetTabularMailer(*this).showDialog(bv);
2041 // function returns an object as defined in func_status.h:
2042 // states OK, Unknown, Disabled, On, Off.
2044 FuncStatus InsetTabular::getStatus(string const & what) const
2046 int action = LyXTabular::LAST_ACTION;
2050 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2051 string const tmp = tabularFeature[i].feature;
2052 if (tmp == what.substr(0, tmp.length())) {
2053 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2054 // tabularFeatures[i].feature.length())) {
2055 action = tabularFeature[i].action;
2059 if (action == LyXTabular::LAST_ACTION) {
2061 status.unknown(true);
2065 string const argument
2066 = ltrim(what.substr(tabularFeature[i].feature.length()));
2071 LyXTabular::ltType dummyltt;
2075 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2077 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2080 case LyXTabular::SET_PWIDTH:
2081 case LyXTabular::SET_MPWIDTH:
2082 case LyXTabular::SET_SPECIAL_COLUMN:
2083 case LyXTabular::SET_SPECIAL_MULTI:
2084 case LyXTabular::APPEND_ROW:
2085 case LyXTabular::APPEND_COLUMN:
2086 case LyXTabular::DELETE_ROW:
2087 case LyXTabular::DELETE_COLUMN:
2088 case LyXTabular::SET_ALL_LINES:
2089 case LyXTabular::UNSET_ALL_LINES:
2093 case LyXTabular::MULTICOLUMN:
2094 status.setOnOff(tabular.isMultiColumn(actcell));
2097 case LyXTabular::M_TOGGLE_LINE_TOP:
2099 case LyXTabular::TOGGLE_LINE_TOP:
2100 status.setOnOff(tabular.topLine(actcell, flag));
2103 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2105 case LyXTabular::TOGGLE_LINE_BOTTOM:
2106 status.setOnOff(tabular.bottomLine(actcell, flag));
2109 case LyXTabular::M_TOGGLE_LINE_LEFT:
2111 case LyXTabular::TOGGLE_LINE_LEFT:
2112 status.setOnOff(tabular.leftLine(actcell, flag));
2115 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2117 case LyXTabular::TOGGLE_LINE_RIGHT:
2118 status.setOnOff(tabular.rightLine(actcell, flag));
2121 case LyXTabular::M_ALIGN_LEFT:
2123 case LyXTabular::ALIGN_LEFT:
2124 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2127 case LyXTabular::M_ALIGN_RIGHT:
2129 case LyXTabular::ALIGN_RIGHT:
2130 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2133 case LyXTabular::M_ALIGN_CENTER:
2135 case LyXTabular::ALIGN_CENTER:
2136 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2139 case LyXTabular::ALIGN_BLOCK:
2140 status.disabled(tabular.getPWidth(actcell).zero());
2141 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2144 case LyXTabular::M_VALIGN_TOP:
2146 case LyXTabular::VALIGN_TOP:
2147 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2150 case LyXTabular::M_VALIGN_BOTTOM:
2152 case LyXTabular::VALIGN_BOTTOM:
2153 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2156 case LyXTabular::M_VALIGN_MIDDLE:
2158 case LyXTabular::VALIGN_MIDDLE:
2159 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2162 case LyXTabular::SET_LONGTABULAR:
2163 status.setOnOff(tabular.isLongTabular());
2166 case LyXTabular::UNSET_LONGTABULAR:
2167 status.setOnOff(!tabular.isLongTabular());
2170 case LyXTabular::SET_ROTATE_TABULAR:
2171 status.setOnOff(tabular.getRotateTabular());
2174 case LyXTabular::UNSET_ROTATE_TABULAR:
2175 status.setOnOff(!tabular.getRotateTabular());
2178 case LyXTabular::SET_ROTATE_CELL:
2179 status.setOnOff(tabular.getRotateCell(actcell));
2182 case LyXTabular::UNSET_ROTATE_CELL:
2183 status.setOnOff(!tabular.getRotateCell(actcell));
2186 case LyXTabular::SET_USEBOX:
2187 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2190 case LyXTabular::SET_LTFIRSTHEAD:
2191 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2194 case LyXTabular::SET_LTHEAD:
2195 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2198 case LyXTabular::SET_LTFOOT:
2199 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2202 case LyXTabular::SET_LTLASTFOOT:
2203 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2206 case LyXTabular::SET_LTNEWPAGE:
2207 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2212 status.disabled(true);
2219 void InsetTabular::getLabelList(Buffer const & buffer,
2220 std::vector<string> & list) const
2222 tabular.getLabelList(buffer, list);
2226 bool InsetTabular::copySelection(BufferView * bv)
2228 if (!hasSelection())
2231 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2232 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2233 if (sel_col_start > sel_col_end) {
2234 sel_col_start = sel_col_end;
2235 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2237 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2240 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2241 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2242 if (sel_row_start > sel_row_end)
2243 swap(sel_row_start, sel_row_end);
2245 delete paste_tabular;
2246 paste_tabular = new LyXTabular(tabular);
2247 paste_tabular->setOwner(this);
2249 for (int i = 0; i < sel_row_start; ++i)
2250 paste_tabular->deleteRow(0);
2252 int const rows = sel_row_end - sel_row_start + 1;
2253 while (paste_tabular->rows() > rows)
2254 paste_tabular->deleteRow(rows);
2256 paste_tabular->setTopLine(0, true, true);
2257 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2260 for (int i = 0; i < sel_col_start; ++i)
2261 paste_tabular->deleteColumn(0);
2263 int const columns = sel_col_end - sel_col_start + 1;
2264 while (paste_tabular->columns() > columns)
2265 paste_tabular->deleteColumn(columns);
2267 paste_tabular->setLeftLine(0, true, true);
2268 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2272 paste_tabular->ascii(*bv->buffer(), os,
2273 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2274 bv->stuffClipboard(os.str());
2279 bool InsetTabular::pasteSelection(BufferView * bv)
2284 for (int r1 = 0, r2 = actrow;
2285 r1 < paste_tabular->rows() && r2 < tabular.rows();
2287 for (int c1 = 0, c2 = actcol;
2288 c1 < paste_tabular->columns() && c2 < tabular.columns();
2290 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2291 tabular.isPartOfMultiColumn(r2, c2))
2293 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2297 if (tabular.isPartOfMultiColumn(r2, c2)) {
2301 InsetText & inset = tabular.getCellInset(r2, c2);
2302 inset = paste_tabular->getCellInset(r1, c1);
2303 inset.setOwner(this);
2304 inset.deleteLyXText(bv);
2312 bool InsetTabular::cutSelection(BufferParams const & bp)
2314 if (!hasSelection())
2317 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2318 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2319 if (sel_col_start > sel_col_end) {
2320 sel_col_start = sel_col_end;
2321 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2323 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2326 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2327 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2329 if (sel_row_start > sel_row_end)
2330 swap(sel_row_start, sel_row_end);
2332 if (sel_cell_start > sel_cell_end)
2333 swap(sel_cell_start, sel_cell_end);
2335 for (int i = sel_row_start; i <= sel_row_end; ++i)
2336 for (int j = sel_col_start; j <= sel_col_end; ++j)
2337 tabular.getCellInset(tabular.getCellNumber(i, j))
2338 .clear(bp.tracking_changes);
2343 bool InsetTabular::isRightToLeft(BufferView * bv)
2345 return bv->getParentLanguage(this)->RightToLeft();
2349 int InsetTabular::scroll(bool recursive) const
2351 int sx = UpdatableInset::scroll(false);
2353 if (recursive && the_locking_inset)
2354 sx += the_locking_inset->scroll(recursive);
2360 void InsetTabular::getSelection(int & srow, int & erow,
2361 int & scol, int & ecol) const
2363 int const start = hasSelection() ? sel_cell_start : actcell;
2364 int const end = hasSelection() ? sel_cell_end : actcell;
2366 srow = tabular.row_of_cell(start);
2367 erow = tabular.row_of_cell(end);
2371 scol = tabular.column_of_cell(start);
2372 ecol = tabular.column_of_cell(end);
2376 ecol = tabular.right_column_of_cell(end);
2380 ParagraphList * InsetTabular::getParagraphs(int i) const
2382 return i < tabular.getNumberOfCells()
2383 ? tabular.getCellInset(i).getParagraphs(0)
2388 LyXText * InsetTabular::getText(int i) const
2390 return i < tabular.getNumberOfCells()
2391 ? tabular.getCellInset(i).getText(0)
2396 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2398 if (the_locking_inset)
2399 return the_locking_inset->cursor(bv);
2400 return InsetOld::cursor(bv);
2404 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2407 return const_cast<InsetTabular *>(this);
2409 for (int i = 0; i < tabular.rows(); ++i) {
2410 for (int j = 0; j < tabular.columns(); ++j) {
2411 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2421 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2423 if (the_locking_inset) {
2424 WordLangTuple word =
2425 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2426 if (!word.word().empty())
2428 if (tabular.isLastCell(actcell)) {
2429 bv->unlockInset(const_cast<InsetTabular *>(this));
2430 return WordLangTuple();
2434 // otherwise we have to lock the next inset and ask for it's selecttion
2435 tabular.getCellInset(actcell)
2436 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2437 WordLangTuple word = selectNextWordInt(bv, value);
2438 if (!word.word().empty())
2444 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2446 // when entering this function the inset should be ALWAYS locked!
2447 BOOST_ASSERT(the_locking_inset);
2449 WordLangTuple word =
2450 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2451 if (!word.word().empty())
2454 if (tabular.isLastCell(actcell)) {
2455 bv->unlockInset(const_cast<InsetTabular *>(this));
2456 return WordLangTuple();
2459 // otherwise we have to lock the next inset and ask for it's selecttion
2461 tabular.getCellInset(actcell)
2462 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2463 return selectNextWordInt(bv, value);
2467 void InsetTabular::selectSelectedWord(BufferView * bv)
2469 if (the_locking_inset)
2470 the_locking_inset->selectSelectedWord(bv);
2474 void InsetTabular::markErased()
2476 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2477 tabular.getCellInset(cell).markErased();
2481 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2483 if (the_locking_inset) {
2484 if (the_locking_inset->nextChange(bv, length)) {
2488 if (tabular.isLastCell(actcell))
2492 InsetText & inset = tabular.getCellInset(actcell);
2493 if (inset.nextChange(bv, length)) {
2497 while (!tabular.isLastCell(actcell)) {
2499 InsetText & inset = tabular.getCellInset(actcell);
2500 if (inset.nextChange(bv, length)) {
2509 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2513 if (the_locking_inset) {
2514 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2518 if (tabular.isLastCell(actcell))
2522 InsetText & inset = tabular.getCellInset(cell);
2523 if (inset.searchForward(bv, str, cs, mw)) {
2527 while (!tabular.isLastCell(cell)) {
2529 InsetText & inset = tabular.getCellInset(cell);
2530 if (inset.searchForward(bv, str, cs, mw)) {
2539 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2542 int cell = tabular.getNumberOfCells();
2543 if (the_locking_inset) {
2544 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2553 InsetText & inset = tabular.getCellInset(cell);
2554 if (inset.searchBackward(bv, str, cs, mw)) {
2563 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2565 if (the_locking_inset)
2566 return the_locking_inset->insetAllowed(code);
2567 // we return true here because if the inset is not locked someone
2568 // wants to insert something in one of our insettexts and we generally
2574 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2576 const int cell = tabular.getCellFromInset(in);
2579 return tabular.getPWidth(cell).zero();
2581 // this is a workaround for a crash (New, Insert->Tabular,
2582 // Insert->FootNote)
2586 // well we didn't obviously find it so maybe our owner knows more
2587 BOOST_ASSERT(owner());
2588 return owner()->forceDefaultParagraphs(in);
2592 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2595 if (buf.length() <= 0)
2601 string::size_type len = buf.length();
2602 string::size_type p = 0;
2604 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2612 maxCols = max(cols, maxCols);
2618 maxCols = max(cols, maxCols);
2619 LyXTabular * loctab;
2624 delete paste_tabular;
2625 paste_tabular = new LyXTabular(bv->buffer()->params(),
2627 paste_tabular->setOwner(this);
2628 loctab = paste_tabular;
2637 string::size_type op = 0;
2638 int cells = loctab->getNumberOfCells();
2641 rows = loctab->rows();
2642 int const columns = loctab->columns();
2644 while (cell < cells && p < len && row < rows &&
2645 (p = buf.find_first_of("\t\n", p)) != string::npos)
2651 // we can only set this if we are not too far right
2652 if (cols < columns) {
2653 InsetText & inset = loctab->getCellInset(cell);
2654 LyXFont const font = inset.getLyXText(bv)->
2655 getFont(inset.paragraphs.begin(), 0);
2656 inset.setText(buf.substr(op, p - op), font);
2662 // we can only set this if we are not too far right
2663 if (cols < columns) {
2664 InsetText & inset = tabular.getCellInset(cell);
2665 LyXFont const font = inset.getLyXText(bv)->
2666 getFont(inset.paragraphs.begin(), 0);
2667 inset.setText(buf.substr(op, p - op), font);
2672 cell = loctab->getCellNumber(row, cols);
2678 // check for the last cell if there is no trailing '\n'
2679 if (cell < cells && op < len) {
2680 InsetText & inset = loctab->getCellInset(cell);
2681 LyXFont const font = inset.getLyXText(bv)->
2682 getFont(inset.paragraphs.begin(), 0);
2683 inset.setText(buf.substr(op, len - op), font);
2690 void InsetTabular::addPreview(PreviewLoader & loader) const
2692 int const rows = tabular.rows();
2693 int const columns = tabular.columns();
2694 for (int i = 0; i < rows; ++i)
2695 for (int j = 0; j < columns; ++j)
2696 tabular.getCellInset(i, j).addPreview(loader);
2700 string const InsetTabularMailer::name_("tabular");
2702 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2703 : inset_(const_cast<InsetTabular &>(inset))
2707 string const InsetTabularMailer::inset2string(Buffer const &) const
2709 return params2string(inset_);
2713 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2715 istringstream data(in);
2717 lex.setStream(data);
2719 #warning CHECK verify that this is a sane value to return.
2725 string const token = lex.getString();
2733 string const token = lex.getString();
2734 if (token != "\\active_cell")
2737 cell = lex.getInteger();
2740 // This is part of the inset proper that is usually swallowed
2741 // by Buffer::readInset
2744 string const token = lex.getString();
2745 if (token != "Tabular")
2752 Buffer const & buffer = inset.buffer();
2753 inset.read(buffer, lex);
2755 // We can't set the active cell, but we can tell the frontend
2761 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2763 Buffer const & buffer = inset.buffer();
2766 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2767 inset.write(buffer, data);
2768 data << "\\end_inset\n";