3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
31 #include "WordLangTuple.h"
33 #include "frontends/Alert.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/LyXView.h"
36 #include "frontends/Painter.h"
38 #include "support/std_sstream.h"
41 using lyx::graphics::PreviewLoader;
43 using lyx::support::ltrim;
44 using lyx::support::strToInt;
51 using std::istringstream;
53 using std::ostringstream;
58 int const ADD_TO_HEIGHT = 2;
59 int const ADD_TO_TABULAR_WIDTH = 2;
62 LyXTabular * paste_tabular = 0;
65 struct TabularFeature {
66 LyXTabular::Feature action;
71 TabularFeature tabularFeature[] =
73 { LyXTabular::APPEND_ROW, "append-row" },
74 { LyXTabular::APPEND_COLUMN, "append-column" },
75 { LyXTabular::DELETE_ROW, "delete-row" },
76 { LyXTabular::DELETE_COLUMN, "delete-column" },
77 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
78 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
79 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
80 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
81 { LyXTabular::ALIGN_LEFT, "align-left" },
82 { LyXTabular::ALIGN_RIGHT, "align-right" },
83 { LyXTabular::ALIGN_CENTER, "align-center" },
84 { LyXTabular::ALIGN_BLOCK, "align-block" },
85 { LyXTabular::VALIGN_TOP, "valign-top" },
86 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
87 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
88 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
89 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
90 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
91 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
92 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
93 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
94 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
95 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
96 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
97 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
98 { LyXTabular::MULTICOLUMN, "multicolumn" },
99 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
100 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
101 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
102 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
103 { LyXTabular::SET_PWIDTH, "set-pwidth" },
104 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
105 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
106 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
107 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
108 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
109 { LyXTabular::SET_USEBOX, "set-usebox" },
110 { LyXTabular::SET_LTHEAD, "set-lthead" },
111 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
112 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
113 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
114 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
115 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
116 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
117 { LyXTabular::LAST_ACTION, "" }
121 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
122 bool operator()(TabularFeature & tf)
124 return tf.action == feature_;
127 LyXTabular::Feature feature_;
133 string const featureAsString(LyXTabular::Feature feature)
135 TabularFeature * it = tabularFeature;
136 TabularFeature * end = it +
137 sizeof(tabularFeature) / sizeof(TabularFeature);
138 it = std::find_if(it, end, FindFeature(feature));
139 return (it == end) ? string() : it->feature;
143 bool InsetTabular::hasPasteBuffer() const
145 return (paste_tabular != 0);
149 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
150 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
151 buffer_(&buf), cursorx_(0), cursory_(0)
153 tabular.setOwner(this);
154 // for now make it always display as display() inset
156 the_locking_inset = 0;
157 old_locking_inset = 0;
160 actrow = actcell = 0;
168 InsetTabular::InsetTabular(InsetTabular const & tab)
169 : UpdatableInset(tab), tabular(tab.tabular),
170 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
172 tabular.setOwner(this);
173 the_locking_inset = 0;
174 old_locking_inset = 0;
177 actrow = actcell = 0;
185 InsetTabular::~InsetTabular()
187 InsetTabularMailer(*this).hideDialog();
191 auto_ptr<InsetBase> InsetTabular::clone() const
193 return auto_ptr<InsetBase>(new InsetTabular(*this));
197 Buffer const & InsetTabular::buffer() const
203 BufferView * InsetTabular::view() const
210 void InsetTabular::buffer(Buffer * b)
216 void InsetTabular::write(Buffer const & buf, ostream & os) const
218 os << "Tabular" << endl;
219 tabular.write(buf, os);
223 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
225 bool const old_format = (lex.getString() == "\\LyXTable");
227 tabular.read(buf, lex);
233 string token = lex.getString();
234 while (lex.isOK() && (token != "\\end_inset")) {
236 token = lex.getString();
238 if (token != "\\end_inset") {
239 lex.printError("Missing \\end_inset at this point. "
245 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
247 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
248 // mi.base.textwidth << "\n";
250 lyxerr << "InsetTabular::metrics: need bv" << endl;
254 calculate_dimensions_of_cells(mi);
256 dim.asc = tabular.getAscentOfRow(0);
257 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
258 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
263 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
265 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
267 BufferView * bv = pi.base.bv;
270 UpdatableInset::draw(pi, x, y);
278 x += ADD_TO_TABULAR_WIDTH;
281 first_visible_cell = -1;
282 for (int i = 0; i < tabular.rows(); ++i) {
284 cell = tabular.getCellNumber(i, 0);
285 if (y + tabular.getDescentOfRow(i) <= 0 &&
286 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
288 y += tabular.getDescentOfRow(i) +
289 tabular.getAscentOfRow(i + 1) +
290 tabular.getAdditionalHeight(i + 1);
293 for (int j = 0; j < tabular.columns(); ++j) {
294 if (nx > bv->workWidth())
296 if (tabular.isPartOfMultiColumn(i, j))
298 int cx = nx + tabular.getBeginningOfTextInCell(cell);
299 if (first_visible_cell < 0)
300 first_visible_cell = cell;
301 if (hasSelection()) {
302 drawCellSelection(pi.pain, nx, y, i, j, cell);
305 tabular.getCellInset(cell).draw(pi, cx, y);
306 drawCellLines(pi.pain, nx, y, i, cell);
307 nx += tabular.getWidthOfColumn(cell);
311 // Would be nice, but for some completely unfathomable reason,
312 // on a col resize to a new fixed width, even though the insettexts
313 // are resized, the cell isn't, but drawing all cells in a tall table
314 // has the desired effect somehow. Complete dark magic.
316 // avoiding drawing the rest of a long table is
317 // a pretty big speedup
318 if (y > bv->workHeight())
322 y += tabular.getDescentOfRow(i) +
323 tabular.getAscentOfRow(i + 1) +
324 tabular.getAdditionalHeight(i + 1);
329 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
330 int row, int cell) const
332 int x2 = x + tabular.getWidthOfColumn(cell);
335 if (!tabular.topAlreadyDrawn(cell)) {
336 on_off = !tabular.topLine(cell);
337 pain.line(x, y - tabular.getAscentOfRow(row),
338 x2, y - tabular.getAscentOfRow(row),
339 on_off ? LColor::tabularonoffline : LColor::tabularline,
340 on_off ? Painter::line_onoffdash : Painter::line_solid);
342 on_off = !tabular.bottomLine(cell);
343 pain.line(x, y + tabular.getDescentOfRow(row),
344 x2, y + tabular.getDescentOfRow(row),
345 on_off ? LColor::tabularonoffline : LColor::tabularline,
346 on_off ? Painter::line_onoffdash : Painter::line_solid);
347 if (!tabular.leftAlreadyDrawn(cell)) {
348 on_off = !tabular.leftLine(cell);
349 pain.line(x, y - tabular.getAscentOfRow(row),
350 x, y + tabular.getDescentOfRow(row),
351 on_off ? LColor::tabularonoffline : LColor::tabularline,
352 on_off ? Painter::line_onoffdash : Painter::line_solid);
354 on_off = !tabular.rightLine(cell);
355 pain.line(x2 - tabular.getAdditionalWidth(cell),
356 y - tabular.getAscentOfRow(row),
357 x2 - tabular.getAdditionalWidth(cell),
358 y + tabular.getDescentOfRow(row),
359 on_off ? LColor::tabularonoffline : LColor::tabularline,
360 on_off ? Painter::line_onoffdash : Painter::line_solid);
364 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
365 int row, int column, int cell) const
367 BOOST_ASSERT(hasSelection());
368 int cs = tabular.column_of_cell(sel_cell_start);
369 int ce = tabular.column_of_cell(sel_cell_end);
372 cs = tabular.column_of_cell(sel_cell_end);
374 ce = tabular.right_column_of_cell(sel_cell_end);
377 int rs = tabular.row_of_cell(sel_cell_start);
378 int re = tabular.row_of_cell(sel_cell_end);
382 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
383 int w = tabular.getWidthOfColumn(cell);
384 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
385 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
386 w, h, LColor::selection);
391 string const InsetTabular::editMessage() const
393 return _("Opened table");
397 void InsetTabular::insetUnlock(BufferView * bv)
399 if (the_locking_inset) {
400 the_locking_inset->insetUnlock(bv);
402 the_locking_inset = 0;
407 if (scroll(false) || hasSelection()) {
416 void InsetTabular::updateLocal(BufferView * bv) const
418 bv->updateInset(this);
424 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
426 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
433 if (inset == &tabular.getCellInset(actcell)) {
434 lyxerr[Debug::INSETTEXT] << "OK" << endl;
435 the_locking_inset = &tabular.getCellInset(actcell);
440 if (!the_locking_inset) {
441 int const n = tabular.getNumberOfCells();
442 int const id = inset->id();
443 for (int i = 0; i < n; ++i) {
444 InsetText * in = &tabular.getCellInset(i);
447 the_locking_inset = in;
452 if (in->getInsetFromID(id)) {
454 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
455 return the_locking_inset->lockInsetInInset(bv, inset);
461 if (the_locking_inset && (the_locking_inset == inset)) {
462 lyxerr[Debug::INSETTEXT] << "OK" << endl;
467 if (the_locking_inset) {
468 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
469 return the_locking_inset->lockInsetInInset(bv, inset);
472 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
477 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
480 if (!the_locking_inset)
482 if (the_locking_inset == inset) {
483 the_locking_inset->insetUnlock(bv);
485 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
492 // this has to be here otherwise we don't redraw the cell!
493 the_locking_inset = 0;
496 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
497 if (inset->lyxCode() == TABULAR_CODE &&
498 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
499 InsetTabularMailer(*this).updateDialog(bv);
508 int InsetTabular::insetInInsetY() const
510 if (!the_locking_inset)
512 return inset_y + the_locking_inset->insetInInsetY();
516 UpdatableInset * InsetTabular::getLockingInset() const
518 return the_locking_inset ? the_locking_inset->getLockingInset() :
519 const_cast<InsetTabular *>(this);
523 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
527 if (the_locking_inset)
528 return the_locking_inset->getFirstLockingInsetOfType(c);
533 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
535 if (the_locking_inset)
536 return the_locking_inset->insertInset(bv, inset);
541 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
543 if (hasSelection() && cmd.button() == mouse_button::button3)
546 if (hasSelection()) {
548 updateLocal(cmd.view());
551 int const ocell = actcell;
552 BufferView * bv = cmd.view();
556 the_locking_inset = 0;
560 setPos(bv, cmd.x, cmd.y);
563 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
565 if (ocell == actcell && the_locking_inset && inset_hit) {
567 FuncRequest cmd1 = cmd;
570 the_locking_inset->localDispatch(cmd1);
574 if (the_locking_inset) {
575 the_locking_inset->insetUnlock(bv);
577 the_locking_inset = 0;
580 if (cmd.button() == mouse_button::button2) {
581 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
585 if (inset_hit && bv->theLockingInset()) {
586 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
587 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
590 FuncRequest cmd1 = cmd;
593 the_locking_inset->localDispatch(cmd1);
598 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
601 if (the_locking_inset) {
602 FuncRequest cmd1 = cmd;
605 ret = the_locking_inset->localDispatch(cmd1);
607 if (cmd.button() == mouse_button::button3 && !ret) {
608 InsetTabularMailer(*this).showDialog(cmd.view());
615 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
617 if (the_locking_inset) {
618 FuncRequest cmd1 = cmd;
621 the_locking_inset->localDispatch(cmd1);
625 BufferView * bv = cmd.view();
626 int const old_cell = actcell;
628 setPos(bv, cmd.x, cmd.y);
629 if (!hasSelection()) {
630 setSelection(actcell, actcell);
632 } else if (old_cell != actcell) {
633 setSelection(sel_cell_start, actcell);
639 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.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
658 if (the_locking_inset)
663 dispatch_result InsetTabular::localDispatch(FuncRequest const & cmd)
665 // We need to save the value of the_locking_inset as the call to
666 // the_locking_inset->localDispatch might unlock it.
667 old_locking_inset = the_locking_inset;
668 dispatch_result result = UpdatableInset::localDispatch(cmd);
669 BufferView * bv = cmd.view();
671 if (cmd.action == LFUN_INSET_EDIT) {
673 lyxerr << "InsetTabular::edit: " << this << " args: '"
674 << cmd.argument << "' first cell: "
675 << &tabular.cell_info[0][0].inset << endl;
677 if (!bv->lockInset(this)) {
678 lyxerr << "InsetTabular::Cannot lock inset" << endl;
684 the_locking_inset = 0;
688 if (cmd.argument.size()) {
689 if (cmd.argument == "left") {
690 if (isRightToLeft(bv))
691 actcell = tabular.getLastCellInRow(0);
695 if (isRightToLeft(bv))
696 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
698 actcell = tabular.getNumberOfCells() - 1;
706 setPos(bv, cmd.x, cmd.y);
709 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
710 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
712 activateCellInset(bv, cmd.x - inset_x, cmd.y - inset_y, cmd.button());
718 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
723 if (cmd.action < 0 && cmd.argument.empty())
726 bool hs = hasSelection();
729 // this one have priority over the locked InsetText, if we're not already
730 // inside another tabular then that one get's priority!
731 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
732 switch (cmd.action) {
733 case LFUN_MOUSE_PRESS:
737 case LFUN_MOUSE_MOTION:
738 lfunMouseMotion(cmd);
741 case LFUN_MOUSE_RELEASE:
742 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
744 case LFUN_CELL_BACKWARD:
745 case LFUN_CELL_FORWARD:
746 unlockInsetInInset(bv, the_locking_inset);
747 if (cmd.action == LFUN_CELL_FORWARD)
748 moveNextCell(bv, old_locking_inset != 0);
750 movePrevCell(bv, old_locking_inset != 0);
754 if (!the_locking_inset)
755 return DISPATCHED_NOUPDATE;
757 // this to avoid compiler warnings.
763 kb_action action = cmd.action;
764 string arg = cmd.argument;
765 if (the_locking_inset) {
766 result = the_locking_inset->localDispatch(cmd);
767 if (result == DISPATCHED_NOUPDATE) {
770 if (sc != scroll()) { // inset has been scrolled
774 } else if (result == DISPATCHED) {
777 } else if (result == FINISHED_UP) {
779 // Make sure to reset status message after
780 // exiting, e.g. math inset
781 bv->owner()->clearMessage();
782 } else if (result == FINISHED_DOWN) {
784 bv->owner()->clearMessage();
785 } else if (result == FINISHED_RIGHT) {
787 bv->owner()->clearMessage();
788 } else if (result == FINISHED) {
789 bv->owner()->clearMessage();
795 // --- Cursor Movements ----------------------------------
796 case LFUN_RIGHTSEL: {
797 int const start = hasSelection() ? sel_cell_start : actcell;
798 if (tabular.isLastCellInRow(actcell)) {
799 setSelection(start, actcell);
804 // if we are starting a selection, only select
805 // the current cell at the beginning
806 if (hasSelection()) {
807 moveRight(bv, false);
810 setSelection(start, end);
815 result = moveRight(bv);
821 int const start = hasSelection() ? sel_cell_start : actcell;
822 if (tabular.isFirstCellInRow(actcell)) {
823 setSelection(start, actcell);
828 // if we are starting a selection, only select
829 // the current cell at the beginning
830 if (hasSelection()) {
834 setSelection(start, end);
839 result = moveLeft(bv);
845 int const start = hasSelection() ? sel_cell_start : actcell;
846 int const ocell = actcell;
847 // if we are starting a selection, only select
848 // the current cell at the beginning
849 if (hasSelection()) {
851 if (ocell == sel_cell_end ||
852 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
853 setSelection(start, tabular.getCellBelow(sel_cell_end));
855 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
857 setSelection(start, start);
863 result = moveDown(bv, old_locking_inset != 0);
869 int const start = hasSelection() ? sel_cell_start : actcell;
870 int const ocell = actcell;
871 // if we are starting a selection, only select
872 // the current cell at the beginning
873 if (hasSelection()) {
875 if ((ocell == sel_cell_end) ||
876 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
877 setSelection(start, tabular.getCellAbove(sel_cell_end));
879 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
881 setSelection(start, start);
887 result = moveUp(bv, old_locking_inset != 0);
896 unlockInsetInInset(bv, the_locking_inset);
897 if (bv->top_y() + bv->painter().paperHeight() <
898 top_baseline + tabular.getHeightOfTabular())
900 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
901 actcell = tabular.getCellBelow(first_visible_cell) + column;
903 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
913 unlockInsetInInset(bv, the_locking_inset);
914 if (top_baseline < 0) {
915 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
916 if (top_baseline > 0)
919 actcell = tabular.getCellBelow(first_visible_cell) + column;
927 // none of these make sense for insettabular,
928 // but we must catch them to prevent any
929 // selection from being confused
933 case LFUN_WORDLEFTSEL:
935 case LFUN_WORDRIGHTSEL:
937 case LFUN_DOWN_PARAGRAPH:
938 case LFUN_DOWN_PARAGRAPHSEL:
939 case LFUN_UP_PARAGRAPH:
940 case LFUN_UP_PARAGRAPHSEL:
946 case LFUN_BEGINNINGBUF:
947 case LFUN_BEGINNINGBUFSEL:
951 case LFUN_LAYOUT_TABULAR:
952 InsetTabularMailer(*this).showDialog(bv);
954 case LFUN_INSET_DIALOG_UPDATE:
955 InsetTabularMailer(*this).updateDialog(bv);
957 case LFUN_TABULAR_FEATURE:
958 if (!tabularFeatures(bv, arg))
959 result = UNDISPATCHED;
961 // insert file functions
962 case LFUN_FILE_INSERT_ASCII_PARA:
963 case LFUN_FILE_INSERT_ASCII:
965 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
968 if (insertAsciiString(bv, tmpstr, false))
971 result = UNDISPATCHED;
974 // cut and paste functions
976 if (!copySelection(bv))
980 recordUndo(bv, Undo::DELETE);
981 cutSelection(bv->buffer()->params());
990 case LFUN_PASTESELECTION:
992 string const clip = bv->getClipboard();
996 if (clip.find('\t') != string::npos) {
1000 string::size_type len = clip.length();
1001 string::size_type p = 0;
1004 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1012 maxCols = max(cols, maxCols);
1018 maxCols = max(cols, maxCols);
1019 delete paste_tabular;
1020 paste_tabular = new LyXTabular(bv->buffer()->params(),
1021 this, rows, maxCols);
1022 string::size_type op = 0;
1024 int cells = paste_tabular->getNumberOfCells();
1026 while ((cell < cells) && (p < len) &&
1027 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1032 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1037 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1038 while (cols++ < maxCols)
1046 // check for the last cell if there is no trailing '\n'
1047 if ((cell < cells) && (op < len))
1048 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1051 if (!insertAsciiString(bv, clip, true))
1054 // so that the clipboard is used and it goes on
1056 // and executes LFUN_PASTESELECTION in insettext!
1057 delete paste_tabular;
1062 if (hasPasteBuffer()) {
1063 recordUndo(bv, Undo::INSERT);
1068 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1070 // handle font changing stuff on selection before we lock the inset
1071 // in the default part!
1072 result = UNDISPATCHED;
1083 case LFUN_UNDERLINE:
1084 case LFUN_FONT_SIZE:
1085 if (bv->dispatch(FuncRequest(bv, action, arg)))
1086 result = DISPATCHED;
1092 // we try to activate the actual inset and put this event down to
1093 // the insets dispatch function.
1094 if (result == DISPATCHED || the_locking_inset)
1096 if (activateCellInset(bv)) {
1097 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1098 if (result == UNDISPATCHED || result >= FINISHED) {
1099 unlockInsetInInset(bv, the_locking_inset);
1100 // we need to update if this was requested before
1102 return UNDISPATCHED;
1111 if (result < FINISHED) {
1112 if (!the_locking_inset && bv->fitCursor())
1115 bv->unlockInset(this);
1120 int InsetTabular::latex(Buffer const & buf, ostream & os,
1121 LatexRunParams const & runparams) const
1123 return tabular.latex(buf, os, runparams);
1127 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1130 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1132 return tabular.ascii(buf, os, 0, false, 0);
1136 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1138 return tabular.linuxdoc(buf,os);
1142 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1147 // if the table is inside a float it doesn't need the informaltable
1148 // wrapper. Search for it.
1149 for (master = owner();
1150 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1151 master = master->owner());
1154 os << "<informaltable>";
1159 ret += tabular.docbook(buf, os, mixcont);
1161 os << "</informaltable>";
1170 void InsetTabular::validate(LaTeXFeatures & features) const
1172 tabular.validate(features);
1176 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1179 // if we have a locking_inset we should have to check only this cell for
1180 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1181 // This is _really_ important (André)
1182 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1185 for (int j = 0; j < tabular.columns(); ++j) {
1189 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1190 tabular.getCellInset(actrow, j).metrics(m, dim);
1191 maxAsc = max(dim.asc, maxAsc);
1192 maxDesc = max(dim.des, maxDesc);
1194 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1195 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1196 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1202 for (int i = 0; i < tabular.rows(); ++i) {
1205 for (int j = 0; j < tabular.columns(); ++j) {
1206 if (tabular.isPartOfMultiColumn(i, j))
1212 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1213 tabular.getCellInset(cell).metrics(m, dim);
1214 maxAsc = max(maxAsc, dim.asc);
1215 maxDesc = max(maxDesc, dim.des);
1216 tabular.setWidthOfCell(cell, dim.wid);
1218 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1219 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1224 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1226 if (the_locking_inset) {
1227 the_locking_inset->getCursor(bv, x, y);
1232 y = cursory_ + InsetTabular::y();
1235 int desc = tabular.getDescentOfRow(actrow);
1237 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1239 y += ADD_TO_HEIGHT * 2;
1240 y += TEXT_TO_INSET_OFFSET;
1244 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1246 if (the_locking_inset) {
1247 the_locking_inset->getCursorPos(bv, x, y);
1250 x = cursorx_ - top_x;
1255 void InsetTabular::fitInsetCursor(BufferView * bv) const
1257 if (the_locking_inset) {
1258 the_locking_inset->fitInsetCursor(bv);
1263 int const asc = font_metrics::maxAscent(font);
1264 int const desc = font_metrics::maxDescent(font);
1267 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1271 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1274 actcell = actrow = actcol = 0;
1275 int ly = tabular.getDescentOfRow(actrow);
1277 // first search the right row
1278 while (ly < y && actrow + 1 < tabular.rows()) {
1279 cursory_ += tabular.getDescentOfRow(actrow) +
1280 tabular.getAscentOfRow(actrow + 1) +
1281 tabular.getAdditionalHeight(actrow + 1);
1283 ly = cursory_ + tabular.getDescentOfRow(actrow);
1285 actcell = tabular.getCellNumber(actrow, actcol);
1287 // now search the right column
1288 int lx = tabular.getWidthOfColumn(actcell) -
1289 tabular.getAdditionalWidth(actcell);
1291 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1292 lx += tabular.getWidthOfColumn(actcell + 1)
1293 + tabular.getAdditionalWidth(actcell);
1295 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1300 int InsetTabular::getCellXPos(int cell) const
1304 for (; !tabular.isFirstCellInRow(c); --c)
1306 int lx = tabular.getWidthOfColumn(cell);
1307 for (; c < cell; ++c)
1308 lx += tabular.getWidthOfColumn(c);
1310 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1314 void InsetTabular::resetPos(BufferView * bv) const
1316 #ifdef WITH_WARNINGS
1317 #warning This should be fixed in the right manner (20011128 Jug)
1319 // fast hack to fix infinite repaintings!
1320 if (in_reset_pos > 0)
1324 actcol = tabular.column_of_cell(actcell);
1327 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1328 if (tabular.isLastCellInRow(cell)) {
1329 cursory_ += tabular.getDescentOfRow(actrow) +
1330 tabular.getAscentOfRow(actrow + 1) +
1331 tabular.getAdditionalHeight(actrow + 1);
1336 if (the_locking_inset)
1340 // we need this only from here on!!!
1342 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1343 int new_x = getCellXPos(actcell);
1344 int old_x = cursorx_;
1347 // cursor.x(getCellXPos(actcell) + offset);
1348 if (actcol < tabular.columns() - 1 && scroll(false) &&
1349 tabular.getWidthOfTabular() < bv->workWidth()-20)
1353 } else if (the_locking_inset &&
1354 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1356 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1357 if (xx > bv->workWidth()-20) {
1358 scroll(bv, -(xx - bv->workWidth() + 60));
1360 } else if (xx < 20) {
1368 } else if (cursorx_ - offset > 20 &&
1369 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1370 > bv->workWidth() - 20) {
1371 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1373 } else if (cursorx_ - offset < 20) {
1374 scroll(bv, 20 - cursorx_ + offset);
1376 } else if (scroll() && top_x > 20 &&
1377 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1378 scroll(bv, old_x - cursorx_);
1381 if (the_locking_inset) {
1382 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1385 if ((!the_locking_inset ||
1386 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1387 actcell != oldcell) {
1388 InsetTabularMailer(*this).updateDialog(bv);
1395 dispatch_result InsetTabular::moveRight(BufferView * bv, bool lock)
1397 if (lock && !old_locking_inset) {
1398 if (activateCellInset(bv))
1401 bool moved = isRightToLeft(bv)
1402 ? movePrevCell(bv) : moveNextCell(bv);
1404 return FINISHED_RIGHT;
1405 if (lock && activateCellInset(bv))
1409 return DISPATCHED_NOUPDATE;
1413 dispatch_result InsetTabular::moveLeft(BufferView * bv, bool lock)
1415 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1419 if (lock && activateCellInset(bv, 0, 0, mouse_button::none, true))
1422 return DISPATCHED_NOUPDATE;
1426 dispatch_result InsetTabular::moveUp(BufferView * bv, bool lock)
1428 int const ocell = actcell;
1429 actcell = tabular.getCellAbove(actcell);
1430 if (actcell == ocell) // we moved out of the inset
1436 if (old_locking_inset) {
1437 old_locking_inset->getCursorPos(bv, x, y);
1438 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1440 if (activateCellInset(bv, x, 0))
1443 return DISPATCHED_NOUPDATE;
1447 dispatch_result InsetTabular::moveDown(BufferView * bv, bool lock)
1449 int const ocell = actcell;
1450 actcell = tabular.getCellBelow(actcell);
1451 if (actcell == ocell) // we moved out of the inset
1452 return FINISHED_DOWN;
1457 if (old_locking_inset) {
1458 old_locking_inset->getCursorPos(bv, x, y);
1459 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1461 if (activateCellInset(bv, x, 0))
1464 return DISPATCHED_NOUPDATE;
1468 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1470 if (isRightToLeft(bv)) {
1471 if (tabular.isFirstCellInRow(actcell)) {
1472 int row = tabular.row_of_cell(actcell);
1473 if (row == tabular.rows() - 1)
1475 actcell = tabular.getLastCellInRow(row);
1476 actcell = tabular.getCellBelow(actcell);
1483 if (tabular.isLastCell(actcell))
1488 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1489 isRightToLeftPar(bv->buffer()->params());
1490 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1497 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1499 if (isRightToLeft(bv)) {
1500 if (tabular.isLastCellInRow(actcell)) {
1501 int row = tabular.row_of_cell(actcell);
1504 actcell = tabular.getFirstCellInRow(row);
1505 actcell = tabular.getCellAbove(actcell);
1507 if (tabular.isLastCell(actcell))
1512 if (!actcell) // first cell
1517 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1518 isRightToLeftPar(bv->buffer()->params());
1519 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1526 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1530 setSelection(0, tabular.getNumberOfCells() - 1);
1532 if (hasSelection()) {
1533 recordUndo(bv, Undo::ATOMIC);
1534 bool const frozen = undo_frozen;
1537 // apply the fontchange on the whole selection
1542 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1543 for (int i = sel_row_start; i <= sel_row_end; ++i)
1544 for (int j = sel_col_start; j <= sel_col_end; ++j)
1545 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1553 if (the_locking_inset)
1554 the_locking_inset->setFont(bv, font, tall);
1558 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1560 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1563 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1564 string const tmp = tabularFeature[i].feature;
1566 if (tmp == what.substr(0, tmp.length())) {
1567 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1568 //tabularFeatures[i].feature.length())) {
1569 action = tabularFeature[i].action;
1573 if (action == LyXTabular::LAST_ACTION)
1577 ltrim(what.substr(tabularFeature[i].feature.length()));
1578 tabularFeatures(bv, action, val);
1584 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1585 string const & special, bool & flag)
1587 if (special == "dl_above") {
1590 } else if (special == "dl_below") {
1591 ltt.bottomDL = flag;
1593 } else if (special == "empty") {
1605 void InsetTabular::tabularFeatures(BufferView * bv,
1606 LyXTabular::Feature feature, string const & value)
1612 bool setLines = false;
1613 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1614 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1618 case LyXTabular::M_ALIGN_LEFT:
1619 case LyXTabular::ALIGN_LEFT:
1620 setAlign = LYX_ALIGN_LEFT;
1623 case LyXTabular::M_ALIGN_RIGHT:
1624 case LyXTabular::ALIGN_RIGHT:
1625 setAlign = LYX_ALIGN_RIGHT;
1628 case LyXTabular::M_ALIGN_CENTER:
1629 case LyXTabular::ALIGN_CENTER:
1630 setAlign = LYX_ALIGN_CENTER;
1633 case LyXTabular::ALIGN_BLOCK:
1634 setAlign = LYX_ALIGN_BLOCK;
1637 case LyXTabular::M_VALIGN_TOP:
1638 case LyXTabular::VALIGN_TOP:
1639 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1642 case LyXTabular::M_VALIGN_BOTTOM:
1643 case LyXTabular::VALIGN_BOTTOM:
1644 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1647 case LyXTabular::M_VALIGN_MIDDLE:
1648 case LyXTabular::VALIGN_MIDDLE:
1649 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1656 if (hasSelection()) {
1657 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1659 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1660 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1662 recordUndo(bv, Undo::ATOMIC);
1664 int row = tabular.row_of_cell(actcell);
1665 int column = tabular.column_of_cell(actcell);
1667 LyXTabular::ltType ltt;
1671 case LyXTabular::SET_PWIDTH: {
1672 LyXLength const len(value);
1673 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1675 tabular.setColumnPWidth(actcell, len);
1676 if (oldlen != len) {
1677 // We need this otherwise we won't resize
1678 // the insettext of the active cell (if any)
1679 // until later (see InsetText::do_resize)
1680 unlockInsetInInset(bv, the_locking_inset);
1685 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1686 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1687 else if (!len.zero()
1688 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1689 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1693 case LyXTabular::SET_MPWIDTH:
1695 LyXLength const len(value);
1696 LyXLength const & oldlen = tabular.getPWidth(actcell);
1697 tabular.setMColumnPWidth(actcell, len);
1698 if (oldlen != len) {
1699 // We need this otherwise we won't resize
1700 // the insettext of the active cell (if any)
1701 // until later (see InsetText::do_resize)
1702 unlockInsetInInset(bv, the_locking_inset);
1708 case LyXTabular::SET_SPECIAL_COLUMN:
1709 case LyXTabular::SET_SPECIAL_MULTI:
1710 tabular.setAlignSpecial(actcell,value,feature);
1714 case LyXTabular::APPEND_ROW:
1715 // append the row into the tabular
1716 unlockInsetInInset(bv, the_locking_inset);
1717 tabular.appendRow(bv->buffer()->params(), actcell);
1718 tabular.setOwner(this);
1722 case LyXTabular::APPEND_COLUMN:
1723 // append the column into the tabular
1724 unlockInsetInInset(bv, the_locking_inset);
1725 tabular.appendColumn(bv->buffer()->params(), actcell);
1726 tabular.setOwner(this);
1727 actcell = tabular.getCellNumber(row, column);
1731 case LyXTabular::DELETE_ROW:
1732 unlockInsetInInset(bv, the_locking_inset);
1733 for (int i = sel_row_start; i <= sel_row_end; ++i)
1734 tabular.deleteRow(sel_row_start);
1735 if (sel_row_start >= tabular.rows())
1737 actcell = tabular.getCellNumber(sel_row_start, column);
1742 case LyXTabular::DELETE_COLUMN:
1743 unlockInsetInInset(bv, the_locking_inset);
1744 for (int i = sel_col_start; i <= sel_col_end; ++i)
1745 tabular.deleteColumn(sel_col_start);
1746 if (sel_col_start >= tabular.columns())
1748 actcell = tabular.getCellNumber(row, sel_col_start);
1753 case LyXTabular::M_TOGGLE_LINE_TOP:
1755 case LyXTabular::TOGGLE_LINE_TOP: {
1756 bool lineSet = !tabular.topLine(actcell, flag);
1757 for (int i = sel_row_start; i <= sel_row_end; ++i)
1758 for (int j = sel_col_start; j <= sel_col_end; ++j)
1760 tabular.getCellNumber(i, j),
1766 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1768 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1769 bool lineSet = !tabular.bottomLine(actcell, flag);
1770 for (int i = sel_row_start; i <= sel_row_end; ++i)
1771 for (int j = sel_col_start; j <= sel_col_end; ++j)
1772 tabular.setBottomLine(
1773 tabular.getCellNumber(i, j),
1780 case LyXTabular::M_TOGGLE_LINE_LEFT:
1782 case LyXTabular::TOGGLE_LINE_LEFT: {
1783 bool lineSet = !tabular.leftLine(actcell, flag);
1784 for (int i = sel_row_start; i <= sel_row_end; ++i)
1785 for (int j = sel_col_start; j <= sel_col_end; ++j)
1786 tabular.setLeftLine(
1787 tabular.getCellNumber(i,j),
1794 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1796 case LyXTabular::TOGGLE_LINE_RIGHT: {
1797 bool lineSet = !tabular.rightLine(actcell, flag);
1798 for (int i = sel_row_start; i <= sel_row_end; ++i)
1799 for (int j = sel_col_start; j <= sel_col_end; ++j)
1800 tabular.setRightLine(
1801 tabular.getCellNumber(i,j),
1808 case LyXTabular::M_ALIGN_LEFT:
1809 case LyXTabular::M_ALIGN_RIGHT:
1810 case LyXTabular::M_ALIGN_CENTER:
1812 case LyXTabular::ALIGN_LEFT:
1813 case LyXTabular::ALIGN_RIGHT:
1814 case LyXTabular::ALIGN_CENTER:
1815 case LyXTabular::ALIGN_BLOCK:
1816 for (int i = sel_row_start; i <= sel_row_end; ++i)
1817 for (int j = sel_col_start; j <= sel_col_end; ++j)
1818 tabular.setAlignment(
1819 tabular.getCellNumber(i, j),
1825 case LyXTabular::M_VALIGN_TOP:
1826 case LyXTabular::M_VALIGN_BOTTOM:
1827 case LyXTabular::M_VALIGN_MIDDLE:
1829 case LyXTabular::VALIGN_TOP:
1830 case LyXTabular::VALIGN_BOTTOM:
1831 case LyXTabular::VALIGN_MIDDLE:
1832 for (int i = sel_row_start; i <= sel_row_end; ++i)
1833 for (int j = sel_col_start; j <= sel_col_end; ++j)
1834 tabular.setVAlignment(
1835 tabular.getCellNumber(i, j),
1840 case LyXTabular::MULTICOLUMN: {
1841 if (sel_row_start != sel_row_end) {
1842 #ifdef WITH_WARNINGS
1843 #warning Need I say it ? This is horrible.
1845 Alert::error(_("Error setting multicolumn"),
1846 _("You cannot set multicolumn vertically."));
1849 // just multicol for one Single Cell
1850 if (!hasSelection()) {
1851 // check wether we are completly in a multicol
1852 if (tabular.isMultiColumn(actcell))
1853 tabular.unsetMultiColumn(actcell);
1855 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1859 // we have a selection so this means we just add all this
1860 // cells to form a multicolumn cell
1864 if (sel_cell_start > sel_cell_end) {
1865 s_start = sel_cell_end;
1866 s_end = sel_cell_start;
1868 s_start = sel_cell_start;
1869 s_end = sel_cell_end;
1871 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1878 case LyXTabular::SET_ALL_LINES:
1880 case LyXTabular::UNSET_ALL_LINES:
1881 for (int i = sel_row_start; i <= sel_row_end; ++i)
1882 for (int j = sel_col_start; j <= sel_col_end; ++j)
1883 tabular.setAllLines(
1884 tabular.getCellNumber(i,j), setLines);
1888 case LyXTabular::SET_LONGTABULAR:
1889 tabular.setLongTabular(true);
1890 updateLocal(bv); // because this toggles displayed
1893 case LyXTabular::UNSET_LONGTABULAR:
1894 tabular.setLongTabular(false);
1895 updateLocal(bv); // because this toggles displayed
1898 case LyXTabular::SET_ROTATE_TABULAR:
1899 tabular.setRotateTabular(true);
1902 case LyXTabular::UNSET_ROTATE_TABULAR:
1903 tabular.setRotateTabular(false);
1906 case LyXTabular::SET_ROTATE_CELL:
1907 for (int i = sel_row_start; i <= sel_row_end; ++i)
1908 for (int j = sel_col_start; j <= sel_col_end; ++j)
1909 tabular.setRotateCell(
1910 tabular.getCellNumber(i, j), true);
1913 case LyXTabular::UNSET_ROTATE_CELL:
1914 for (int i = sel_row_start; i <= sel_row_end; ++i)
1915 for (int j = sel_col_start; j <= sel_col_end; ++j)
1916 tabular.setRotateCell(
1917 tabular.getCellNumber(i, j), false);
1920 case LyXTabular::SET_USEBOX: {
1921 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1922 if (val == tabular.getUsebox(actcell))
1923 val = LyXTabular::BOX_NONE;
1924 for (int i = sel_row_start; i <= sel_row_end; ++i)
1925 for (int j = sel_col_start; j <= sel_col_end; ++j)
1926 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1930 case LyXTabular::UNSET_LTFIRSTHEAD:
1932 case LyXTabular::SET_LTFIRSTHEAD:
1933 tabular.getRowOfLTFirstHead(row, ltt);
1934 checkLongtableSpecial(ltt, value, flag);
1935 tabular.setLTHead(row, flag, ltt, true);
1938 case LyXTabular::UNSET_LTHEAD:
1940 case LyXTabular::SET_LTHEAD:
1941 tabular.getRowOfLTHead(row, ltt);
1942 checkLongtableSpecial(ltt, value, flag);
1943 tabular.setLTHead(row, flag, ltt, false);
1946 case LyXTabular::UNSET_LTFOOT:
1948 case LyXTabular::SET_LTFOOT:
1949 tabular.getRowOfLTFoot(row, ltt);
1950 checkLongtableSpecial(ltt, value, flag);
1951 tabular.setLTFoot(row, flag, ltt, false);
1954 case LyXTabular::UNSET_LTLASTFOOT:
1956 case LyXTabular::SET_LTLASTFOOT:
1957 tabular.getRowOfLTLastFoot(row, ltt);
1958 checkLongtableSpecial(ltt, value, flag);
1959 tabular.setLTFoot(row, flag, ltt, true);
1962 case LyXTabular::SET_LTNEWPAGE:
1963 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1966 // dummy stuff just to avoid warnings
1967 case LyXTabular::LAST_ACTION:
1971 InsetTabularMailer(*this).updateDialog(bv);
1975 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1976 mouse_button::state button, bool behind)
1978 UpdatableInset & inset = tabular.getCellInset(actcell);
1981 x = inset.x() + inset.width();
1982 y = inset.descent();
1984 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1985 //inset_y = cursory_;
1986 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1987 if (!the_locking_inset)
1990 return the_locking_inset;
1994 bool InsetTabular::insetHit(BufferView *, int x, int) const
1996 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
2000 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2002 //resizeLyXText(bv, recursive);
2006 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2007 bool const recursive) const
2009 if (the_locking_inset)
2010 return the_locking_inset->getLyXText(bv, recursive);
2011 return InsetOld::getLyXText(bv, recursive);
2015 bool InsetTabular::showInsetDialog(BufferView * bv) const
2017 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2018 InsetTabularMailer(*this).showDialog(bv);
2023 void InsetTabular::openLayoutDialog(BufferView * bv) const
2025 if (the_locking_inset) {
2026 InsetTabular * inset = static_cast<InsetTabular *>
2027 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2029 inset->openLayoutDialog(bv);
2033 InsetTabularMailer(*this).showDialog(bv);
2038 // function returns an object as defined in func_status.h:
2039 // states OK, Unknown, Disabled, On, Off.
2041 FuncStatus InsetTabular::getStatus(string const & what) const
2043 int action = LyXTabular::LAST_ACTION;
2047 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2048 string const tmp = tabularFeature[i].feature;
2049 if (tmp == what.substr(0, tmp.length())) {
2050 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2051 // tabularFeatures[i].feature.length())) {
2052 action = tabularFeature[i].action;
2056 if (action == LyXTabular::LAST_ACTION) {
2058 return status.unknown(true);
2061 string const argument
2062 = ltrim(what.substr(tabularFeature[i].feature.length()));
2067 LyXTabular::ltType dummyltt;
2071 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2073 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2076 case LyXTabular::SET_PWIDTH:
2077 case LyXTabular::SET_MPWIDTH:
2078 case LyXTabular::SET_SPECIAL_COLUMN:
2079 case LyXTabular::SET_SPECIAL_MULTI:
2080 case LyXTabular::APPEND_ROW:
2081 case LyXTabular::APPEND_COLUMN:
2082 case LyXTabular::DELETE_ROW:
2083 case LyXTabular::DELETE_COLUMN:
2084 case LyXTabular::SET_ALL_LINES:
2085 case LyXTabular::UNSET_ALL_LINES:
2086 return status.clear();
2088 case LyXTabular::MULTICOLUMN:
2089 status.setOnOff(tabular.isMultiColumn(actcell));
2092 case LyXTabular::M_TOGGLE_LINE_TOP:
2094 case LyXTabular::TOGGLE_LINE_TOP:
2095 status.setOnOff(tabular.topLine(actcell, flag));
2098 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2100 case LyXTabular::TOGGLE_LINE_BOTTOM:
2101 status.setOnOff(tabular.bottomLine(actcell, flag));
2104 case LyXTabular::M_TOGGLE_LINE_LEFT:
2106 case LyXTabular::TOGGLE_LINE_LEFT:
2107 status.setOnOff(tabular.leftLine(actcell, flag));
2110 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2112 case LyXTabular::TOGGLE_LINE_RIGHT:
2113 status.setOnOff(tabular.rightLine(actcell, flag));
2116 case LyXTabular::M_ALIGN_LEFT:
2118 case LyXTabular::ALIGN_LEFT:
2119 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2122 case LyXTabular::M_ALIGN_RIGHT:
2124 case LyXTabular::ALIGN_RIGHT:
2125 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2128 case LyXTabular::M_ALIGN_CENTER:
2130 case LyXTabular::ALIGN_CENTER:
2131 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2134 case LyXTabular::ALIGN_BLOCK:
2135 status.disabled(tabular.getPWidth(actcell).zero());
2136 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2139 case LyXTabular::M_VALIGN_TOP:
2141 case LyXTabular::VALIGN_TOP:
2142 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2145 case LyXTabular::M_VALIGN_BOTTOM:
2147 case LyXTabular::VALIGN_BOTTOM:
2148 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2151 case LyXTabular::M_VALIGN_MIDDLE:
2153 case LyXTabular::VALIGN_MIDDLE:
2154 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2157 case LyXTabular::SET_LONGTABULAR:
2158 status.setOnOff(tabular.isLongTabular());
2161 case LyXTabular::UNSET_LONGTABULAR:
2162 status.setOnOff(!tabular.isLongTabular());
2165 case LyXTabular::SET_ROTATE_TABULAR:
2166 status.setOnOff(tabular.getRotateTabular());
2169 case LyXTabular::UNSET_ROTATE_TABULAR:
2170 status.setOnOff(!tabular.getRotateTabular());
2173 case LyXTabular::SET_ROTATE_CELL:
2174 status.setOnOff(tabular.getRotateCell(actcell));
2177 case LyXTabular::UNSET_ROTATE_CELL:
2178 status.setOnOff(!tabular.getRotateCell(actcell));
2181 case LyXTabular::SET_USEBOX:
2182 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2185 case LyXTabular::SET_LTFIRSTHEAD:
2186 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2189 case LyXTabular::SET_LTHEAD:
2190 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2193 case LyXTabular::SET_LTFOOT:
2194 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2197 case LyXTabular::SET_LTLASTFOOT:
2198 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2201 case LyXTabular::SET_LTNEWPAGE:
2202 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2207 status.disabled(true);
2214 void InsetTabular::getLabelList(Buffer const & buffer,
2215 std::vector<string> & list) const
2217 tabular.getLabelList(buffer, list);
2221 bool InsetTabular::copySelection(BufferView * bv)
2223 if (!hasSelection())
2226 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2227 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2228 if (sel_col_start > sel_col_end) {
2229 sel_col_start = sel_col_end;
2230 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2232 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2235 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2236 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2237 if (sel_row_start > sel_row_end)
2238 swap(sel_row_start, sel_row_end);
2240 delete paste_tabular;
2241 paste_tabular = new LyXTabular(tabular);
2242 paste_tabular->setOwner(this);
2244 for (int i = 0; i < sel_row_start; ++i)
2245 paste_tabular->deleteRow(0);
2247 int const rows = sel_row_end - sel_row_start + 1;
2248 while (paste_tabular->rows() > rows)
2249 paste_tabular->deleteRow(rows);
2251 paste_tabular->setTopLine(0, true, true);
2252 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2255 for (int i = 0; i < sel_col_start; ++i)
2256 paste_tabular->deleteColumn(0);
2258 int const columns = sel_col_end - sel_col_start + 1;
2259 while (paste_tabular->columns() > columns)
2260 paste_tabular->deleteColumn(columns);
2262 paste_tabular->setLeftLine(0, true, true);
2263 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2267 paste_tabular->ascii(*bv->buffer(), os,
2268 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2269 bv->stuffClipboard(os.str());
2274 bool InsetTabular::pasteSelection(BufferView * bv)
2279 for (int r1 = 0, r2 = actrow;
2280 r1 < paste_tabular->rows() && r2 < tabular.rows();
2282 for (int c1 = 0, c2 = actcol;
2283 c1 < paste_tabular->columns() && c2 < tabular.columns();
2285 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2286 tabular.isPartOfMultiColumn(r2, c2))
2288 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2292 if (tabular.isPartOfMultiColumn(r2, c2)) {
2296 InsetText & inset = tabular.getCellInset(r2, c2);
2297 inset = paste_tabular->getCellInset(r1, c1);
2298 inset.setOwner(this);
2299 inset.deleteLyXText(bv);
2307 bool InsetTabular::cutSelection(BufferParams const & bp)
2309 if (!hasSelection())
2312 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2313 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2314 if (sel_col_start > sel_col_end) {
2315 sel_col_start = sel_col_end;
2316 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2318 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2321 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2322 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2324 if (sel_row_start > sel_row_end)
2325 swap(sel_row_start, sel_row_end);
2327 if (sel_cell_start > sel_cell_end)
2328 swap(sel_cell_start, sel_cell_end);
2330 for (int i = sel_row_start; i <= sel_row_end; ++i)
2331 for (int j = sel_col_start; j <= sel_col_end; ++j)
2332 tabular.getCellInset(tabular.getCellNumber(i, j))
2333 .clear(bp.tracking_changes);
2338 bool InsetTabular::isRightToLeft(BufferView * bv)
2340 return bv->getParentLanguage(this)->RightToLeft();
2344 int InsetTabular::scroll(bool recursive) const
2346 int sx = UpdatableInset::scroll(false);
2348 if (recursive && the_locking_inset)
2349 sx += the_locking_inset->scroll(recursive);
2355 void InsetTabular::getSelection(int & srow, int & erow,
2356 int & scol, int & ecol) const
2358 int const start = hasSelection() ? sel_cell_start : actcell;
2359 int const end = hasSelection() ? sel_cell_end : actcell;
2361 srow = tabular.row_of_cell(start);
2362 erow = tabular.row_of_cell(end);
2366 scol = tabular.column_of_cell(start);
2367 ecol = tabular.column_of_cell(end);
2371 ecol = tabular.right_column_of_cell(end);
2375 ParagraphList * InsetTabular::getParagraphs(int i) const
2377 return i < tabular.getNumberOfCells()
2378 ? tabular.getCellInset(i).getParagraphs(0)
2383 LyXText * InsetTabular::getText(int i) const
2385 return i < tabular.getNumberOfCells()
2386 ? tabular.getCellInset(i).getText(0)
2391 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2393 if (the_locking_inset)
2394 return the_locking_inset->cursor(bv);
2395 return InsetOld::cursor(bv);
2399 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2402 return const_cast<InsetTabular *>(this);
2404 for (int i = 0; i < tabular.rows(); ++i) {
2405 for (int j = 0; j < tabular.columns(); ++j) {
2406 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2416 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2418 if (the_locking_inset) {
2419 WordLangTuple word =
2420 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2421 if (!word.word().empty())
2423 if (tabular.isLastCell(actcell)) {
2424 bv->unlockInset(const_cast<InsetTabular *>(this));
2425 return WordLangTuple();
2429 // otherwise we have to lock the next inset and ask for it's selecttion
2430 tabular.getCellInset(actcell)
2431 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2432 WordLangTuple word = selectNextWordInt(bv, value);
2433 if (!word.word().empty())
2439 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2441 // when entering this function the inset should be ALWAYS locked!
2442 BOOST_ASSERT(the_locking_inset);
2444 WordLangTuple word =
2445 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2446 if (!word.word().empty())
2449 if (tabular.isLastCell(actcell)) {
2450 bv->unlockInset(const_cast<InsetTabular *>(this));
2451 return WordLangTuple();
2454 // otherwise we have to lock the next inset and ask for it's selecttion
2456 tabular.getCellInset(actcell)
2457 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2458 return selectNextWordInt(bv, value);
2462 void InsetTabular::selectSelectedWord(BufferView * bv)
2464 if (the_locking_inset)
2465 the_locking_inset->selectSelectedWord(bv);
2469 void InsetTabular::markErased()
2471 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2472 tabular.getCellInset(cell).markErased();
2476 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2478 if (the_locking_inset) {
2479 if (the_locking_inset->nextChange(bv, length)) {
2483 if (tabular.isLastCell(actcell))
2487 InsetText & inset = tabular.getCellInset(actcell);
2488 if (inset.nextChange(bv, length)) {
2492 while (!tabular.isLastCell(actcell)) {
2494 InsetText & inset = tabular.getCellInset(actcell);
2495 if (inset.nextChange(bv, length)) {
2504 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2508 if (the_locking_inset) {
2509 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2513 if (tabular.isLastCell(actcell))
2517 InsetText & inset = tabular.getCellInset(cell);
2518 if (inset.searchForward(bv, str, cs, mw)) {
2522 while (!tabular.isLastCell(cell)) {
2524 InsetText & inset = tabular.getCellInset(cell);
2525 if (inset.searchForward(bv, str, cs, mw)) {
2534 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2537 int cell = tabular.getNumberOfCells();
2538 if (the_locking_inset) {
2539 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2548 InsetText & inset = tabular.getCellInset(cell);
2549 if (inset.searchBackward(bv, str, cs, mw)) {
2558 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2560 if (the_locking_inset)
2561 return the_locking_inset->insetAllowed(code);
2562 // we return true here because if the inset is not locked someone
2563 // wants to insert something in one of our insettexts and we generally
2569 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2571 const int cell = tabular.getCellFromInset(in);
2574 return tabular.getPWidth(cell).zero();
2576 // this is a workaround for a crash (New, Insert->Tabular,
2577 // Insert->FootNote)
2581 // well we didn't obviously find it so maybe our owner knows more
2582 BOOST_ASSERT(owner());
2583 return owner()->forceDefaultParagraphs(in);
2587 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2590 if (buf.length() <= 0)
2596 string::size_type len = buf.length();
2597 string::size_type p = 0;
2599 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2607 maxCols = max(cols, maxCols);
2613 maxCols = max(cols, maxCols);
2614 LyXTabular * loctab;
2619 delete paste_tabular;
2620 paste_tabular = new LyXTabular(bv->buffer()->params(),
2622 paste_tabular->setOwner(this);
2623 loctab = paste_tabular;
2632 string::size_type op = 0;
2633 int cells = loctab->getNumberOfCells();
2636 rows = loctab->rows();
2637 int const columns = loctab->columns();
2639 while (cell < cells && p < len && row < rows &&
2640 (p = buf.find_first_of("\t\n", p)) != string::npos)
2646 // we can only set this if we are not too far right
2647 if (cols < columns) {
2648 InsetText & inset = loctab->getCellInset(cell);
2649 LyXFont const font = inset.getLyXText(bv)->
2650 getFont(inset.paragraphs.begin(), 0);
2651 inset.setText(buf.substr(op, p - op), font);
2657 // we can only set this if we are not too far right
2658 if (cols < columns) {
2659 InsetText & inset = tabular.getCellInset(cell);
2660 LyXFont const font = inset.getLyXText(bv)->
2661 getFont(inset.paragraphs.begin(), 0);
2662 inset.setText(buf.substr(op, p - op), font);
2667 cell = loctab->getCellNumber(row, cols);
2673 // check for the last cell if there is no trailing '\n'
2674 if (cell < cells && op < len) {
2675 InsetText & inset = loctab->getCellInset(cell);
2676 LyXFont const font = inset.getLyXText(bv)->
2677 getFont(inset.paragraphs.begin(), 0);
2678 inset.setText(buf.substr(op, len - op), font);
2685 void InsetTabular::addPreview(PreviewLoader & loader) const
2687 int const rows = tabular.rows();
2688 int const columns = tabular.columns();
2689 for (int i = 0; i < rows; ++i)
2690 for (int j = 0; j < columns; ++j)
2691 tabular.getCellInset(i, j).addPreview(loader);
2695 string const InsetTabularMailer::name_("tabular");
2697 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2698 : inset_(const_cast<InsetTabular &>(inset))
2702 string const InsetTabularMailer::inset2string(Buffer const &) const
2704 return params2string(inset_);
2708 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2710 istringstream data(in);
2712 lex.setStream(data);
2714 #warning CHECK verify that this is a sane value to return.
2720 string const token = lex.getString();
2728 string const token = lex.getString();
2729 if (token != "\\active_cell")
2732 cell = lex.getInteger();
2735 // This is part of the inset proper that is usually swallowed
2736 // by Buffer::readInset
2739 string const token = lex.getString();
2740 if (token != "Tabular")
2747 Buffer const & buffer = inset.buffer();
2748 inset.read(buffer, lex);
2750 // We can't set the active cell, but we can tell the frontend
2756 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2758 Buffer const & buffer = inset.buffer();
2761 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2762 inset.write(buffer, data);
2763 data << "\\end_inset\n";