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"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27 #include "paragraph_funcs.h"
28 #include "ParagraphParameters.h"
29 #include "undo_funcs.h"
30 #include "WordLangTuple.h"
32 #include "frontends/Alert.h"
33 #include "frontends/font_metrics.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/Painter.h"
37 #include "support/std_sstream.h"
39 using lyx::graphics::PreviewLoader;
41 using lyx::support::ltrim;
42 using lyx::support::strToInt;
49 using std::istringstream;
51 using std::ostringstream;
56 int const ADD_TO_HEIGHT = 2;
57 int const ADD_TO_TABULAR_WIDTH = 2;
60 LyXTabular * paste_tabular = 0;
63 struct TabularFeature {
64 LyXTabular::Feature action;
69 TabularFeature tabularFeature[] =
71 { LyXTabular::APPEND_ROW, "append-row" },
72 { LyXTabular::APPEND_COLUMN, "append-column" },
73 { LyXTabular::DELETE_ROW, "delete-row" },
74 { LyXTabular::DELETE_COLUMN, "delete-column" },
75 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
76 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
77 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
78 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
79 { LyXTabular::ALIGN_LEFT, "align-left" },
80 { LyXTabular::ALIGN_RIGHT, "align-right" },
81 { LyXTabular::ALIGN_CENTER, "align-center" },
82 { LyXTabular::ALIGN_BLOCK, "align-block" },
83 { LyXTabular::VALIGN_TOP, "valign-top" },
84 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
85 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
86 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
87 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
88 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
89 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
90 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
91 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
92 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
93 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
94 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
95 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
96 { LyXTabular::MULTICOLUMN, "multicolumn" },
97 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
98 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
99 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
100 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
101 { LyXTabular::SET_PWIDTH, "set-pwidth" },
102 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
103 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
104 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
105 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
106 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
107 { LyXTabular::SET_USEBOX, "set-usebox" },
108 { LyXTabular::SET_LTHEAD, "set-lthead" },
109 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
110 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
111 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
112 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
113 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
114 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
115 { LyXTabular::LAST_ACTION, "" }
119 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
120 bool operator()(TabularFeature & tf)
122 return tf.action == feature_;
125 LyXTabular::Feature feature_;
131 string const featureAsString(LyXTabular::Feature feature)
133 TabularFeature * it = tabularFeature;
134 TabularFeature * end = it +
135 sizeof(tabularFeature) / sizeof(TabularFeature);
136 it = std::find_if(it, end, FindFeature(feature));
137 return (it == end) ? string() : it->feature;
141 bool InsetTabular::hasPasteBuffer() const
143 return (paste_tabular != 0);
147 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
148 : tabular(buf.params(), this, max(rows, 1), max(columns, 1)),
149 buffer_(&buf), cursorx_(0), cursory_(0)
151 // for now make it always display as display() inset
153 the_locking_inset = 0;
154 old_locking_inset = 0;
157 actrow = actcell = 0;
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166 : UpdatableInset(tab),
167 tabular(tab.buffer_->params(), this, tab.tabular),
168 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
170 the_locking_inset = 0;
171 old_locking_inset = 0;
174 actrow = actcell = 0;
182 InsetTabular::~InsetTabular()
184 InsetTabularMailer(*this).hideDialog();
188 auto_ptr<InsetBase> InsetTabular::clone() const
190 return auto_ptr<InsetBase>(new InsetTabular(*this));
194 Buffer const & InsetTabular::buffer() const
200 BufferView * InsetTabular::view() const
207 void InsetTabular::buffer(Buffer * b)
213 void InsetTabular::write(Buffer const & buf, ostream & os) const
215 os << "Tabular" << endl;
216 tabular.write(buf, os);
220 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
222 bool const old_format = (lex.getString() == "\\LyXTable");
224 tabular.read(buf, lex);
230 string token = lex.getString();
231 while (lex.isOK() && (token != "\\end_inset")) {
233 token = lex.getString();
235 if (token != "\\end_inset") {
236 lex.printError("Missing \\end_inset at this point. "
242 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
244 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
245 // mi.base.textwidth << "\n";
247 lyxerr << "InsetTabular::metrics: need bv" << endl;
251 calculate_dimensions_of_cells(mi);
253 dim.asc = tabular.getAscentOfRow(0);
254 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
255 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
260 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
262 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
264 BufferView * bv = pi.base.bv;
267 UpdatableInset::draw(pi, x, y);
275 x += ADD_TO_TABULAR_WIDTH;
278 first_visible_cell = -1;
279 for (int i = 0; i < tabular.rows(); ++i) {
281 cell = tabular.getCellNumber(i, 0);
282 if (y + tabular.getDescentOfRow(i) <= 0 &&
283 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
285 y += tabular.getDescentOfRow(i) +
286 tabular.getAscentOfRow(i + 1) +
287 tabular.getAdditionalHeight(i + 1);
290 for (int j = 0; j < tabular.columns(); ++j) {
291 if (nx > bv->workWidth())
293 if (tabular.isPartOfMultiColumn(i, j))
295 int cx = nx + tabular.getBeginningOfTextInCell(cell);
296 if (first_visible_cell < 0)
297 first_visible_cell = cell;
298 if (hasSelection()) {
299 drawCellSelection(pi.pain, nx, y, i, j, cell);
302 tabular.getCellInset(cell).draw(pi, cx, y);
303 drawCellLines(pi.pain, nx, y, i, cell);
304 nx += tabular.getWidthOfColumn(cell);
308 // Would be nice, but for some completely unfathomable reason,
309 // on a col resize to a new fixed width, even though the insettexts
310 // are resized, the cell isn't, but drawing all cells in a tall table
311 // has the desired effect somehow. Complete dark magic.
313 // avoiding drawing the rest of a long table is
314 // a pretty big speedup
315 if (y > bv->workHeight())
319 y += tabular.getDescentOfRow(i) +
320 tabular.getAscentOfRow(i + 1) +
321 tabular.getAdditionalHeight(i + 1);
326 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
327 int row, int cell) const
329 int x2 = x + tabular.getWidthOfColumn(cell);
332 if (!tabular.topAlreadyDrawn(cell)) {
333 on_off = !tabular.topLine(cell);
334 pain.line(x, y - tabular.getAscentOfRow(row),
335 x2, y - tabular.getAscentOfRow(row),
336 on_off ? LColor::tabularonoffline : LColor::tabularline,
337 on_off ? Painter::line_onoffdash : Painter::line_solid);
339 on_off = !tabular.bottomLine(cell);
340 pain.line(x, y + tabular.getDescentOfRow(row),
341 x2, y + tabular.getDescentOfRow(row),
342 on_off ? LColor::tabularonoffline : LColor::tabularline,
343 on_off ? Painter::line_onoffdash : Painter::line_solid);
344 if (!tabular.leftAlreadyDrawn(cell)) {
345 on_off = !tabular.leftLine(cell);
346 pain.line(x, y - tabular.getAscentOfRow(row),
347 x, y + tabular.getDescentOfRow(row),
348 on_off ? LColor::tabularonoffline : LColor::tabularline,
349 on_off ? Painter::line_onoffdash : Painter::line_solid);
351 on_off = !tabular.rightLine(cell);
352 pain.line(x2 - tabular.getAdditionalWidth(cell),
353 y - tabular.getAscentOfRow(row),
354 x2 - tabular.getAdditionalWidth(cell),
355 y + tabular.getDescentOfRow(row),
356 on_off ? LColor::tabularonoffline : LColor::tabularline,
357 on_off ? Painter::line_onoffdash : Painter::line_solid);
361 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
362 int row, int column, int cell) const
364 BOOST_ASSERT(hasSelection());
365 int cs = tabular.column_of_cell(sel_cell_start);
366 int ce = tabular.column_of_cell(sel_cell_end);
369 cs = tabular.column_of_cell(sel_cell_end);
371 ce = tabular.right_column_of_cell(sel_cell_end);
374 int rs = tabular.row_of_cell(sel_cell_start);
375 int re = tabular.row_of_cell(sel_cell_end);
379 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
380 int w = tabular.getWidthOfColumn(cell);
381 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
382 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
383 w, h, LColor::selection);
388 string const InsetTabular::editMessage() const
390 return _("Opened table");
394 void InsetTabular::insetUnlock(BufferView * bv)
396 if (the_locking_inset) {
397 the_locking_inset->insetUnlock(bv);
399 the_locking_inset = 0;
404 if (scroll(false) || hasSelection()) {
413 void InsetTabular::updateLocal(BufferView * bv) const
415 bv->updateInset(this);
421 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
423 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
428 if (inset == &tabular.getCellInset(actcell)) {
429 lyxerr[Debug::INSETTEXT] << "OK" << endl;
430 the_locking_inset = &tabular.getCellInset(actcell);
433 } else if (!the_locking_inset) {
434 int const n = tabular.getNumberOfCells();
435 int const id = inset->id();
436 for (int i = 0; i < n; ++i) {
437 InsetText * in = &tabular.getCellInset(i);
440 the_locking_inset = in;
445 if (in->getInsetFromID(id)) {
447 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
448 return the_locking_inset->lockInsetInInset(bv, inset);
451 } else if (the_locking_inset && (the_locking_inset == inset)) {
452 lyxerr[Debug::INSETTEXT] << "OK" << endl;
454 } else if (the_locking_inset) {
455 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
456 return the_locking_inset->lockInsetInInset(bv, inset);
458 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
463 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
466 if (!the_locking_inset)
468 if (the_locking_inset == inset) {
469 the_locking_inset->insetUnlock(bv);
471 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
478 // this has to be here otherwise we don't redraw the cell!
479 the_locking_inset = 0;
482 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
483 if (inset->lyxCode() == TABULAR_CODE &&
484 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
485 InsetTabularMailer(*this).updateDialog(bv);
494 int InsetTabular::insetInInsetY() const
496 if (!the_locking_inset)
498 return inset_y + the_locking_inset->insetInInsetY();
502 UpdatableInset * InsetTabular::getLockingInset() const
504 return the_locking_inset ? the_locking_inset->getLockingInset() :
505 const_cast<InsetTabular *>(this);
509 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
513 if (the_locking_inset)
514 return the_locking_inset->getFirstLockingInsetOfType(c);
519 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
521 if (the_locking_inset)
522 return the_locking_inset->insertInset(bv, inset);
527 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
529 if (hasSelection() && cmd.button() == mouse_button::button3)
532 if (hasSelection()) {
534 updateLocal(cmd.view());
537 int const ocell = actcell;
538 BufferView * bv = cmd.view();
542 the_locking_inset = 0;
546 setPos(bv, cmd.x, cmd.y);
549 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
551 if ((ocell == actcell) && the_locking_inset && inset_hit) {
553 FuncRequest cmd1 = cmd;
556 the_locking_inset->localDispatch(cmd1);
560 if (the_locking_inset) {
561 the_locking_inset->insetUnlock(bv);
563 the_locking_inset = 0;
566 if (cmd.button() == mouse_button::button2) {
567 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
571 if (inset_hit && bv->theLockingInset()) {
572 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
573 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
576 FuncRequest cmd1 = cmd;
579 the_locking_inset->localDispatch(cmd1);
584 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
587 if (the_locking_inset) {
588 FuncRequest cmd1 = cmd;
591 ret = the_locking_inset->localDispatch(cmd1);
593 if (cmd.button() == mouse_button::button3 && !ret) {
594 InsetTabularMailer(*this).showDialog(cmd.view());
601 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
603 if (the_locking_inset) {
604 FuncRequest cmd1 = cmd;
607 the_locking_inset->localDispatch(cmd1);
611 BufferView * bv = cmd.view();
612 int const old_cell = actcell;
614 setPos(bv, cmd.x, cmd.y);
615 if (!hasSelection()) {
616 setSelection(actcell, actcell);
618 } else if (old_cell != actcell) {
619 setSelection(sel_cell_start, actcell);
625 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
627 // We need to save the value of the_locking_inset as the call to
628 // the_locking_inset->localDispatch might unlock it.
629 old_locking_inset = the_locking_inset;
630 RESULT result = UpdatableInset::localDispatch(cmd);
631 BufferView * bv = cmd.view();
633 if (cmd.action == LFUN_INSET_EDIT) {
635 if (!bv->lockInset(this)) {
636 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
642 the_locking_inset = 0;
646 if (cmd.argument.size()) {
647 if (cmd.argument == "left") {
648 if (isRightToLeft(bv))
649 actcell = tabular.getLastCellInRow(0);
653 if (isRightToLeft(bv))
654 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
656 actcell = tabular.getNumberOfCells() - 1;
664 setPos(bv, cmd.x, cmd.y);
667 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
668 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
674 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
679 if (cmd.action < 0 && cmd.argument.empty())
682 bool hs = hasSelection();
685 // this one have priority over the locked InsetText, if we're not already
686 // inside another tabular then that one get's priority!
687 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
688 switch (cmd.action) {
689 case LFUN_MOUSE_PRESS:
693 case LFUN_MOUSE_MOTION:
694 lfunMouseMotion(cmd);
697 case LFUN_MOUSE_RELEASE:
698 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
700 case LFUN_CELL_BACKWARD:
701 case LFUN_CELL_FORWARD:
702 unlockInsetInInset(bv, the_locking_inset);
703 if (cmd.action == LFUN_CELL_FORWARD)
704 moveNextCell(bv, old_locking_inset != 0);
706 movePrevCell(bv, old_locking_inset != 0);
710 if (!the_locking_inset)
711 return DISPATCHED_NOUPDATE;
713 // this to avoid compiler warnings.
719 kb_action action = cmd.action;
720 string arg = cmd.argument;
721 if (the_locking_inset) {
722 result = the_locking_inset->localDispatch(cmd);
723 if (result == DISPATCHED_NOUPDATE) {
726 if (sc != scroll()) { // inset has been scrolled
730 } else if (result == DISPATCHED) {
733 } else if (result == FINISHED_UP) {
735 // Make sure to reset status message after
736 // exiting, e.g. math inset
737 bv->owner()->clearMessage();
738 } else if (result == FINISHED_DOWN) {
740 bv->owner()->clearMessage();
741 } else if (result == FINISHED_RIGHT) {
743 bv->owner()->clearMessage();
744 } else if (result == FINISHED) {
745 bv->owner()->clearMessage();
751 // --- Cursor Movements ----------------------------------
752 case LFUN_RIGHTSEL: {
753 int const start = hasSelection() ? sel_cell_start : actcell;
754 if (tabular.isLastCellInRow(actcell)) {
755 setSelection(start, actcell);
760 // if we are starting a selection, only select
761 // the current cell at the beginning
762 if (hasSelection()) {
763 moveRight(bv, false);
766 setSelection(start, end);
771 result = moveRight(bv);
777 int const start = hasSelection() ? sel_cell_start : actcell;
778 if (tabular.isFirstCellInRow(actcell)) {
779 setSelection(start, actcell);
784 // if we are starting a selection, only select
785 // the current cell at the beginning
786 if (hasSelection()) {
790 setSelection(start, end);
795 result = moveLeft(bv);
801 int const start = hasSelection() ? sel_cell_start : actcell;
802 int const ocell = actcell;
803 // if we are starting a selection, only select
804 // the current cell at the beginning
805 if (hasSelection()) {
807 if (ocell == sel_cell_end ||
808 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
809 setSelection(start, tabular.getCellBelow(sel_cell_end));
811 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
813 setSelection(start, start);
819 result = moveDown(bv, old_locking_inset != 0);
825 int const start = hasSelection() ? sel_cell_start : actcell;
826 int const ocell = actcell;
827 // if we are starting a selection, only select
828 // the current cell at the beginning
829 if (hasSelection()) {
831 if ((ocell == sel_cell_end) ||
832 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
833 setSelection(start, tabular.getCellAbove(sel_cell_end));
835 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
837 setSelection(start, start);
843 result = moveUp(bv, old_locking_inset != 0);
852 unlockInsetInInset(bv, the_locking_inset);
853 if (bv->top_y() + bv->painter().paperHeight() <
854 top_baseline + tabular.getHeightOfTabular())
856 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
857 actcell = tabular.getCellBelow(first_visible_cell) + column;
859 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
869 unlockInsetInInset(bv, the_locking_inset);
870 if (top_baseline < 0) {
871 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
872 if (top_baseline > 0)
875 actcell = tabular.getCellBelow(first_visible_cell) + column;
883 // none of these make sense for insettabular,
884 // but we must catch them to prevent any
885 // selection from being confused
889 case LFUN_WORDLEFTSEL:
891 case LFUN_WORDRIGHTSEL:
893 case LFUN_DOWN_PARAGRAPH:
894 case LFUN_DOWN_PARAGRAPHSEL:
895 case LFUN_UP_PARAGRAPH:
896 case LFUN_UP_PARAGRAPHSEL:
902 case LFUN_BEGINNINGBUF:
903 case LFUN_BEGINNINGBUFSEL:
907 case LFUN_LAYOUT_TABULAR: {
908 InsetTabularMailer(*this).showDialog(bv);
911 case LFUN_INSET_DIALOG_UPDATE: {
912 InsetTabularMailer(*this).updateDialog(bv);
915 case LFUN_TABULAR_FEATURE:
916 if (!tabularFeatures(bv, arg))
917 result = UNDISPATCHED;
919 // insert file functions
920 case LFUN_FILE_INSERT_ASCII_PARA:
921 case LFUN_FILE_INSERT_ASCII:
923 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
926 if (insertAsciiString(bv, tmpstr, false))
929 result = UNDISPATCHED;
932 // cut and paste functions
934 if (!copySelection(bv))
938 recordUndo(bv, Undo::DELETE);
939 cutSelection(bv->buffer()->params());
948 case LFUN_PASTESELECTION:
950 string const clip(bv->getClipboard());
954 if (clip.find('\t') != string::npos) {
958 string::size_type len = clip.length();
959 string::size_type p = 0;
962 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
970 maxCols = max(cols, maxCols);
976 maxCols = max(cols, maxCols);
977 delete paste_tabular;
978 paste_tabular = new LyXTabular(bv->buffer()->params(),
979 this, rows, maxCols);
980 string::size_type op = 0;
982 int cells = paste_tabular->getNumberOfCells();
984 while ((cell < cells) && (p < len) &&
985 (p = clip.find_first_of("\t\n", p)) != string::npos) {
990 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
995 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
996 while (cols++ < maxCols)
1004 // check for the last cell if there is no trailing '\n'
1005 if ((cell < cells) && (op < len))
1006 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1009 if (!insertAsciiString(bv, clip, true))
1012 // so that the clipboard is used and it goes on
1014 // and executes LFUN_PASTESELECTION in insettext!
1015 delete paste_tabular;
1020 if (hasPasteBuffer()) {
1021 recordUndo(bv, Undo::INSERT);
1026 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1028 // handle font changing stuff on selection before we lock the inset
1029 // in the default part!
1030 result = UNDISPATCHED;
1041 case LFUN_UNDERLINE:
1042 case LFUN_FONT_SIZE:
1043 if (bv->dispatch(FuncRequest(bv, action, arg)))
1044 result = DISPATCHED;
1050 // we try to activate the actual inset and put this event down to
1051 // the insets dispatch function.
1052 if (result == DISPATCHED || the_locking_inset)
1054 if (activateCellInset(bv)) {
1055 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1056 if (result == UNDISPATCHED || result >= FINISHED) {
1057 unlockInsetInInset(bv, the_locking_inset);
1058 // we need to update if this was requested before
1060 return UNDISPATCHED;
1069 if (result < FINISHED) {
1070 if (!the_locking_inset && bv->fitCursor())
1073 bv->unlockInset(this);
1078 int InsetTabular::latex(Buffer const & buf, ostream & os,
1079 LatexRunParams const & runparams) const
1081 return tabular.latex(buf, os, runparams);
1085 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1088 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1090 return tabular.ascii(buf, os, 0, false, 0);
1094 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1096 return tabular.linuxdoc(buf,os);
1100 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1105 // if the table is inside a float it doesn't need the informaltable
1106 // wrapper. Search for it.
1107 for (master = owner();
1108 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1109 master = master->owner());
1112 os << "<informaltable>";
1117 ret += tabular.docbook(buf, os, mixcont);
1119 os << "</informaltable>";
1128 void InsetTabular::validate(LaTeXFeatures & features) const
1130 tabular.validate(features);
1134 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1137 // if we have a locking_inset we should have to check only this cell for
1138 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1139 // This is _really_ important (André)
1140 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1143 for (int j = 0; j < tabular.columns(); ++j) {
1147 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1148 tabular.getCellInset(actrow, j).metrics(m, dim);
1149 maxAsc = max(dim.asc, maxAsc);
1150 maxDesc = max(dim.des, maxDesc);
1152 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1153 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1154 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1160 for (int i = 0; i < tabular.rows(); ++i) {
1163 for (int j = 0; j < tabular.columns(); ++j) {
1164 if (tabular.isPartOfMultiColumn(i, j))
1170 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1171 tabular.getCellInset(cell).metrics(m, dim);
1172 maxAsc = max(maxAsc, dim.asc);
1173 maxDesc = max(maxDesc, dim.des);
1174 tabular.setWidthOfCell(cell, dim.wid);
1176 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1177 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1182 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1184 if (the_locking_inset) {
1185 the_locking_inset->getCursor(bv, x, y);
1190 y = cursory_ + InsetTabular::y();
1193 int desc = tabular.getDescentOfRow(actrow);
1195 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1197 y += ADD_TO_HEIGHT * 2;
1198 y += TEXT_TO_INSET_OFFSET;
1202 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1204 if (the_locking_inset) {
1205 the_locking_inset->getCursorPos(bv, x, y);
1208 x = cursorx_ - top_x;
1213 void InsetTabular::fitInsetCursor(BufferView * bv) const
1215 if (the_locking_inset) {
1216 the_locking_inset->fitInsetCursor(bv);
1221 int const asc = font_metrics::maxAscent(font);
1222 int const desc = font_metrics::maxDescent(font);
1225 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1229 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1232 actcell = actrow = actcol = 0;
1233 int ly = tabular.getDescentOfRow(actrow);
1235 // first search the right row
1236 while (ly < y && actrow + 1 < tabular.rows()) {
1237 cursory_ += tabular.getDescentOfRow(actrow) +
1238 tabular.getAscentOfRow(actrow + 1) +
1239 tabular.getAdditionalHeight(actrow + 1);
1241 ly = cursory_ + tabular.getDescentOfRow(actrow);
1243 actcell = tabular.getCellNumber(actrow, actcol);
1245 // now search the right column
1246 int lx = tabular.getWidthOfColumn(actcell) -
1247 tabular.getAdditionalWidth(actcell);
1249 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1250 lx += tabular.getWidthOfColumn(actcell + 1)
1251 + tabular.getAdditionalWidth(actcell);
1253 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1258 int InsetTabular::getCellXPos(int cell) const
1262 for (; !tabular.isFirstCellInRow(c); --c)
1264 int lx = tabular.getWidthOfColumn(cell);
1265 for (; c < cell; ++c)
1266 lx += tabular.getWidthOfColumn(c);
1268 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1272 void InsetTabular::resetPos(BufferView * bv) const
1274 #ifdef WITH_WARNINGS
1275 #warning This should be fixed in the right manner (20011128 Jug)
1277 // fast hack to fix infinite repaintings!
1278 if (in_reset_pos > 0)
1282 actcol = tabular.column_of_cell(actcell);
1285 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1286 if (tabular.isLastCellInRow(cell)) {
1287 cursory_ += tabular.getDescentOfRow(actrow) +
1288 tabular.getAscentOfRow(actrow + 1) +
1289 tabular.getAdditionalHeight(actrow + 1);
1294 if (the_locking_inset)
1298 // we need this only from here on!!!
1300 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1301 int new_x = getCellXPos(actcell);
1302 int old_x = cursorx_;
1305 // cursor.x(getCellXPos(actcell) + offset);
1306 if (actcol < tabular.columns() - 1 && scroll(false) &&
1307 tabular.getWidthOfTabular() < bv->workWidth()-20)
1311 } else if (the_locking_inset &&
1312 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1314 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1315 if (xx > bv->workWidth()-20) {
1316 scroll(bv, -(xx - bv->workWidth() + 60));
1318 } else if (xx < 20) {
1326 } else if (cursorx_ - offset > 20 &&
1327 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1328 > bv->workWidth() - 20) {
1329 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1331 } else if (cursorx_ - offset < 20) {
1332 scroll(bv, 20 - cursorx_ + offset);
1334 } else if (scroll() && top_x > 20 &&
1335 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1336 scroll(bv, old_x - cursorx_);
1339 if (the_locking_inset) {
1340 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1343 if ((!the_locking_inset ||
1344 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1345 actcell != oldcell) {
1346 InsetTabularMailer(*this).updateDialog(bv);
1353 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1355 if (lock && !old_locking_inset) {
1356 if (activateCellInset(bv))
1359 bool moved = isRightToLeft(bv)
1360 ? movePrevCell(bv) : moveNextCell(bv);
1362 return FINISHED_RIGHT;
1363 if (lock && activateCellInset(bv))
1367 return DISPATCHED_NOUPDATE;
1371 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1373 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1376 if (lock) { // behind the inset
1377 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1381 return DISPATCHED_NOUPDATE;
1385 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1387 int const ocell = actcell;
1388 actcell = tabular.getCellAbove(actcell);
1389 if (actcell == ocell) // we moved out of the inset
1395 if (old_locking_inset) {
1396 old_locking_inset->getCursorPos(bv, x, y);
1397 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1399 if (activateCellInset(bv, x, 0))
1402 return DISPATCHED_NOUPDATE;
1406 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1408 int const ocell = actcell;
1409 actcell = tabular.getCellBelow(actcell);
1410 if (actcell == ocell) // we moved out of the inset
1411 return FINISHED_DOWN;
1416 if (old_locking_inset) {
1417 old_locking_inset->getCursorPos(bv, x, y);
1418 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1420 if (activateCellInset(bv, x, 0))
1423 return DISPATCHED_NOUPDATE;
1427 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1429 if (isRightToLeft(bv)) {
1430 if (tabular.isFirstCellInRow(actcell)) {
1431 int row = tabular.row_of_cell(actcell);
1432 if (row == tabular.rows() - 1)
1434 actcell = tabular.getLastCellInRow(row);
1435 actcell = tabular.getCellBelow(actcell);
1442 if (tabular.isLastCell(actcell))
1447 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1448 isRightToLeftPar(bv->buffer()->params());
1449 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1456 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1458 if (isRightToLeft(bv)) {
1459 if (tabular.isLastCellInRow(actcell)) {
1460 int row = tabular.row_of_cell(actcell);
1463 actcell = tabular.getFirstCellInRow(row);
1464 actcell = tabular.getCellAbove(actcell);
1466 if (tabular.isLastCell(actcell))
1471 if (!actcell) // first cell
1476 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1477 isRightToLeftPar(bv->buffer()->params());
1478 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1485 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1489 setSelection(0, tabular.getNumberOfCells() - 1);
1491 if (hasSelection()) {
1492 recordUndo(bv, Undo::ATOMIC);
1493 bool const frozen = undo_frozen;
1496 // apply the fontchange on the whole selection
1501 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1502 for(int i = sel_row_start; i <= sel_row_end; ++i)
1503 for(int j = sel_col_start; j <= sel_col_end; ++j)
1504 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1512 if (the_locking_inset)
1513 the_locking_inset->setFont(bv, font, tall);
1517 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1519 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1522 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1523 string const tmp = tabularFeature[i].feature;
1525 if (tmp == what.substr(0, tmp.length())) {
1526 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1527 //tabularFeatures[i].feature.length())) {
1528 action = tabularFeature[i].action;
1532 if (action == LyXTabular::LAST_ACTION)
1536 ltrim(what.substr(tabularFeature[i].feature.length()));
1537 tabularFeatures(bv, action, val);
1543 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1544 string const & special, bool & flag)
1546 if (special == "dl_above") {
1549 } else if (special == "dl_below") {
1550 ltt.bottomDL = flag;
1552 } else if (special == "empty") {
1564 void InsetTabular::tabularFeatures(BufferView * bv,
1565 LyXTabular::Feature feature,
1566 string const & value)
1572 bool setLines = false;
1573 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1574 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1578 case LyXTabular::M_ALIGN_LEFT:
1579 case LyXTabular::ALIGN_LEFT:
1580 setAlign = LYX_ALIGN_LEFT;
1583 case LyXTabular::M_ALIGN_RIGHT:
1584 case LyXTabular::ALIGN_RIGHT:
1585 setAlign = LYX_ALIGN_RIGHT;
1588 case LyXTabular::M_ALIGN_CENTER:
1589 case LyXTabular::ALIGN_CENTER:
1590 setAlign = LYX_ALIGN_CENTER;
1593 case LyXTabular::ALIGN_BLOCK:
1594 setAlign = LYX_ALIGN_BLOCK;
1597 case LyXTabular::M_VALIGN_TOP:
1598 case LyXTabular::VALIGN_TOP:
1599 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1602 case LyXTabular::M_VALIGN_BOTTOM:
1603 case LyXTabular::VALIGN_BOTTOM:
1604 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1607 case LyXTabular::M_VALIGN_MIDDLE:
1608 case LyXTabular::VALIGN_MIDDLE:
1609 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1616 if (hasSelection()) {
1617 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1619 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1620 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1622 recordUndo(bv, Undo::ATOMIC);
1624 int row = tabular.row_of_cell(actcell);
1625 int column = tabular.column_of_cell(actcell);
1627 LyXTabular::ltType ltt;
1631 case LyXTabular::SET_PWIDTH:
1633 LyXLength const vallen(value);
1634 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1636 bool const update = (tmplen != vallen);
1637 tabular.setColumnPWidth(actcell, vallen);
1639 // We need this otherwise we won't resize
1640 // the insettext of the active cell (if any)
1641 // until later (see InsetText::do_resize)
1642 unlockInsetInInset(bv, the_locking_inset);
1647 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1648 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1649 else if (!vallen.zero()
1650 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1651 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1655 case LyXTabular::SET_MPWIDTH:
1657 LyXLength const vallen(value);
1658 LyXLength const & tmplen = tabular.getPWidth(actcell);
1660 bool const update = (tmplen != vallen);
1661 tabular.setMColumnPWidth(actcell, vallen);
1663 // We need this otherwise we won't resize
1664 // the insettext of the active cell (if any)
1665 // until later (see InsetText::do_resize)
1666 unlockInsetInInset(bv, the_locking_inset);
1671 case LyXTabular::SET_SPECIAL_COLUMN:
1672 case LyXTabular::SET_SPECIAL_MULTI:
1673 tabular.setAlignSpecial(actcell,value,feature);
1676 case LyXTabular::APPEND_ROW:
1677 // append the row into the tabular
1678 unlockInsetInInset(bv, the_locking_inset);
1679 tabular.appendRow(bv->buffer()->params(), actcell);
1682 case LyXTabular::APPEND_COLUMN:
1683 // append the column into the tabular
1684 unlockInsetInInset(bv, the_locking_inset);
1685 tabular.appendColumn(bv->buffer()->params(), actcell);
1686 actcell = tabular.getCellNumber(row, column);
1689 case LyXTabular::DELETE_ROW:
1690 unlockInsetInInset(bv, the_locking_inset);
1691 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1692 tabular.deleteRow(sel_row_start);
1694 if (sel_row_start >= tabular.rows())
1696 actcell = tabular.getCellNumber(sel_row_start, column);
1700 case LyXTabular::DELETE_COLUMN:
1701 unlockInsetInInset(bv, the_locking_inset);
1702 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1703 tabular.deleteColumn(sel_col_start);
1705 if (sel_col_start >= tabular.columns())
1707 actcell = tabular.getCellNumber(row, sel_col_start);
1711 case LyXTabular::M_TOGGLE_LINE_TOP:
1713 case LyXTabular::TOGGLE_LINE_TOP:
1715 bool lineSet = !tabular.topLine(actcell, flag);
1716 for (int i = sel_row_start; i <= sel_row_end; ++i)
1717 for (int j = sel_col_start; j <= sel_col_end; ++j)
1719 tabular.getCellNumber(i, j),
1725 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1727 case LyXTabular::TOGGLE_LINE_BOTTOM:
1729 bool lineSet = !tabular.bottomLine(actcell, flag);
1730 for (int i = sel_row_start; i <= sel_row_end; ++i)
1731 for (int j = sel_col_start; j <= sel_col_end; ++j)
1732 tabular.setBottomLine(
1733 tabular.getCellNumber(i, j),
1740 case LyXTabular::M_TOGGLE_LINE_LEFT:
1742 case LyXTabular::TOGGLE_LINE_LEFT:
1744 bool lineSet = !tabular.leftLine(actcell, flag);
1745 for (int i = sel_row_start; i <= sel_row_end; ++i)
1746 for (int j = sel_col_start; j <= sel_col_end; ++j)
1747 tabular.setLeftLine(
1748 tabular.getCellNumber(i,j),
1755 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1757 case LyXTabular::TOGGLE_LINE_RIGHT:
1759 bool lineSet = !tabular.rightLine(actcell, flag);
1760 for (int i = sel_row_start; i <= sel_row_end; ++i)
1761 for (int j = sel_col_start; j <= sel_col_end; ++j)
1762 tabular.setRightLine(
1763 tabular.getCellNumber(i,j),
1770 case LyXTabular::M_ALIGN_LEFT:
1771 case LyXTabular::M_ALIGN_RIGHT:
1772 case LyXTabular::M_ALIGN_CENTER:
1774 case LyXTabular::ALIGN_LEFT:
1775 case LyXTabular::ALIGN_RIGHT:
1776 case LyXTabular::ALIGN_CENTER:
1777 case LyXTabular::ALIGN_BLOCK:
1778 for (int i = sel_row_start; i <= sel_row_end; ++i)
1779 for (int j = sel_col_start; j <= sel_col_end; ++j)
1780 tabular.setAlignment(
1781 tabular.getCellNumber(i, j),
1787 case LyXTabular::M_VALIGN_TOP:
1788 case LyXTabular::M_VALIGN_BOTTOM:
1789 case LyXTabular::M_VALIGN_MIDDLE:
1791 case LyXTabular::VALIGN_TOP:
1792 case LyXTabular::VALIGN_BOTTOM:
1793 case LyXTabular::VALIGN_MIDDLE:
1794 for (int i = sel_row_start; i <= sel_row_end; ++i)
1795 for (int j = sel_col_start; j <= sel_col_end; ++j)
1796 tabular.setVAlignment(
1797 tabular.getCellNumber(i, j),
1802 case LyXTabular::MULTICOLUMN: {
1803 if (sel_row_start != sel_row_end) {
1804 #ifdef WITH_WARNINGS
1805 #warning Need I say it ? This is horrible.
1807 Alert::error(_("Error setting multicolumn"),
1808 _("You cannot set multicolumn vertically."));
1811 // just multicol for one Single Cell
1812 if (!hasSelection()) {
1813 // check wether we are completly in a multicol
1814 if (tabular.isMultiColumn(actcell))
1815 tabular.unsetMultiColumn(actcell);
1817 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1821 // we have a selection so this means we just add all this
1822 // cells to form a multicolumn cell
1826 if (sel_cell_start > sel_cell_end) {
1827 s_start = sel_cell_end;
1828 s_end = sel_cell_start;
1830 s_start = sel_cell_start;
1831 s_end = sel_cell_end;
1833 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1840 case LyXTabular::SET_ALL_LINES:
1842 case LyXTabular::UNSET_ALL_LINES:
1843 for (int i = sel_row_start; i <= sel_row_end; ++i)
1844 for (int j = sel_col_start; j <= sel_col_end; ++j)
1845 tabular.setAllLines(
1846 tabular.getCellNumber(i,j), setLines);
1850 case LyXTabular::SET_LONGTABULAR:
1851 tabular.setLongTabular(true);
1852 updateLocal(bv); // because this toggles displayed
1855 case LyXTabular::UNSET_LONGTABULAR:
1856 tabular.setLongTabular(false);
1857 updateLocal(bv); // because this toggles displayed
1860 case LyXTabular::SET_ROTATE_TABULAR:
1861 tabular.setRotateTabular(true);
1864 case LyXTabular::UNSET_ROTATE_TABULAR:
1865 tabular.setRotateTabular(false);
1868 case LyXTabular::SET_ROTATE_CELL:
1869 for (int i = sel_row_start; i <= sel_row_end; ++i)
1870 for (int j = sel_col_start; j<=sel_col_end; ++j)
1871 tabular.setRotateCell(
1872 tabular.getCellNumber(i, j), true);
1875 case LyXTabular::UNSET_ROTATE_CELL:
1876 for (int i = sel_row_start; i <= sel_row_end; ++i)
1877 for (int j = sel_col_start; j <= sel_col_end; ++j)
1878 tabular.setRotateCell(
1879 tabular.getCellNumber(i, j), false);
1882 case LyXTabular::SET_USEBOX: {
1883 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1884 if (val == tabular.getUsebox(actcell))
1885 val = LyXTabular::BOX_NONE;
1886 for (int i = sel_row_start; i <= sel_row_end; ++i)
1887 for (int j = sel_col_start; j <= sel_col_end; ++j)
1888 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1892 case LyXTabular::UNSET_LTFIRSTHEAD:
1894 case LyXTabular::SET_LTFIRSTHEAD:
1895 tabular.getRowOfLTFirstHead(row, ltt);
1896 checkLongtableSpecial(ltt, value, flag);
1897 tabular.setLTHead(row, flag, ltt, true);
1900 case LyXTabular::UNSET_LTHEAD:
1902 case LyXTabular::SET_LTHEAD:
1903 tabular.getRowOfLTHead(row, ltt);
1904 checkLongtableSpecial(ltt, value, flag);
1905 tabular.setLTHead(row, flag, ltt, false);
1908 case LyXTabular::UNSET_LTFOOT:
1910 case LyXTabular::SET_LTFOOT:
1911 tabular.getRowOfLTFoot(row, ltt);
1912 checkLongtableSpecial(ltt, value, flag);
1913 tabular.setLTFoot(row, flag, ltt, false);
1916 case LyXTabular::UNSET_LTLASTFOOT:
1918 case LyXTabular::SET_LTLASTFOOT:
1919 tabular.getRowOfLTLastFoot(row, ltt);
1920 checkLongtableSpecial(ltt, value, flag);
1921 tabular.setLTFoot(row, flag, ltt, true);
1924 case LyXTabular::SET_LTNEWPAGE: {
1925 bool what = !tabular.getLTNewPage(row);
1926 tabular.setLTNewPage(row, what);
1930 // dummy stuff just to avoid warnings
1931 case LyXTabular::LAST_ACTION:
1935 InsetTabularMailer(*this).updateDialog(bv);
1939 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1940 mouse_button::state button, bool behind)
1942 UpdatableInset & inset = tabular.getCellInset(actcell);
1945 x = inset.x() + inset.width();
1946 y = inset.descent();
1948 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1949 //inset_y = cursory_;
1950 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1951 if (!the_locking_inset)
1954 return the_locking_inset;
1958 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1959 mouse_button::state button)
1961 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1963 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1967 bool InsetTabular::insetHit(BufferView *, int x, int) const
1969 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1973 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1975 //resizeLyXText(bv, recursive);
1979 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1980 bool const recursive) const
1982 if (the_locking_inset)
1983 return the_locking_inset->getLyXText(bv, recursive);
1984 return InsetOld::getLyXText(bv, recursive);
1988 bool InsetTabular::showInsetDialog(BufferView * bv) const
1990 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1991 InsetTabularMailer(*this).showDialog(bv);
1996 void InsetTabular::openLayoutDialog(BufferView * bv) const
1998 if (the_locking_inset) {
1999 InsetTabular * inset = static_cast<InsetTabular *>
2000 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2002 inset->openLayoutDialog(bv);
2006 InsetTabularMailer(*this).showDialog(bv);
2011 // function returns an object as defined in func_status.h:
2012 // states OK, Unknown, Disabled, On, Off.
2014 FuncStatus InsetTabular::getStatus(string const & what) const
2016 int action = LyXTabular::LAST_ACTION;
2020 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2021 string const tmp = tabularFeature[i].feature;
2022 if (tmp == what.substr(0, tmp.length())) {
2023 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2024 // tabularFeatures[i].feature.length())) {
2025 action = tabularFeature[i].action;
2029 if (action == LyXTabular::LAST_ACTION) {
2031 return status.unknown(true);
2034 string const argument
2035 = ltrim(what.substr(tabularFeature[i].feature.length()));
2040 LyXTabular::ltType dummyltt;
2044 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2046 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2049 case LyXTabular::SET_PWIDTH:
2050 case LyXTabular::SET_MPWIDTH:
2051 case LyXTabular::SET_SPECIAL_COLUMN:
2052 case LyXTabular::SET_SPECIAL_MULTI:
2053 case LyXTabular::APPEND_ROW:
2054 case LyXTabular::APPEND_COLUMN:
2055 case LyXTabular::DELETE_ROW:
2056 case LyXTabular::DELETE_COLUMN:
2057 case LyXTabular::SET_ALL_LINES:
2058 case LyXTabular::UNSET_ALL_LINES:
2059 return status.clear();
2061 case LyXTabular::MULTICOLUMN:
2062 status.setOnOff(tabular.isMultiColumn(actcell));
2065 case LyXTabular::M_TOGGLE_LINE_TOP:
2067 case LyXTabular::TOGGLE_LINE_TOP:
2068 status.setOnOff(tabular.topLine(actcell, flag));
2071 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2073 case LyXTabular::TOGGLE_LINE_BOTTOM:
2074 status.setOnOff(tabular.bottomLine(actcell, flag));
2077 case LyXTabular::M_TOGGLE_LINE_LEFT:
2079 case LyXTabular::TOGGLE_LINE_LEFT:
2080 status.setOnOff(tabular.leftLine(actcell, flag));
2083 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2085 case LyXTabular::TOGGLE_LINE_RIGHT:
2086 status.setOnOff(tabular.rightLine(actcell, flag));
2089 case LyXTabular::M_ALIGN_LEFT:
2091 case LyXTabular::ALIGN_LEFT:
2092 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2095 case LyXTabular::M_ALIGN_RIGHT:
2097 case LyXTabular::ALIGN_RIGHT:
2098 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2101 case LyXTabular::M_ALIGN_CENTER:
2103 case LyXTabular::ALIGN_CENTER:
2104 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2107 case LyXTabular::ALIGN_BLOCK:
2108 status.disabled(tabular.getPWidth(actcell).zero());
2109 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2112 case LyXTabular::M_VALIGN_TOP:
2114 case LyXTabular::VALIGN_TOP:
2115 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2118 case LyXTabular::M_VALIGN_BOTTOM:
2120 case LyXTabular::VALIGN_BOTTOM:
2121 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2124 case LyXTabular::M_VALIGN_MIDDLE:
2126 case LyXTabular::VALIGN_MIDDLE:
2127 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2130 case LyXTabular::SET_LONGTABULAR:
2131 status.setOnOff(tabular.isLongTabular());
2134 case LyXTabular::UNSET_LONGTABULAR:
2135 status.setOnOff(!tabular.isLongTabular());
2138 case LyXTabular::SET_ROTATE_TABULAR:
2139 status.setOnOff(tabular.getRotateTabular());
2142 case LyXTabular::UNSET_ROTATE_TABULAR:
2143 status.setOnOff(!tabular.getRotateTabular());
2146 case LyXTabular::SET_ROTATE_CELL:
2147 status.setOnOff(tabular.getRotateCell(actcell));
2150 case LyXTabular::UNSET_ROTATE_CELL:
2151 status.setOnOff(!tabular.getRotateCell(actcell));
2154 case LyXTabular::SET_USEBOX:
2155 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2158 case LyXTabular::SET_LTFIRSTHEAD:
2159 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2162 case LyXTabular::SET_LTHEAD:
2163 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2166 case LyXTabular::SET_LTFOOT:
2167 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2170 case LyXTabular::SET_LTLASTFOOT:
2171 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2174 case LyXTabular::SET_LTNEWPAGE:
2175 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2180 status.disabled(true);
2187 void InsetTabular::getLabelList(std::vector<string> & list) const
2189 tabular.getLabelList(list);
2193 bool InsetTabular::copySelection(BufferView * bv)
2195 if (!hasSelection())
2198 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2199 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2200 if (sel_col_start > sel_col_end) {
2201 sel_col_start = sel_col_end;
2202 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2204 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2207 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2208 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2209 if (sel_row_start > sel_row_end)
2210 swap(sel_row_start, sel_row_end);
2212 delete paste_tabular;
2213 paste_tabular = new LyXTabular(bv->buffer()->params(), this, tabular);
2215 for (int i = 0; i < sel_row_start; ++i)
2216 paste_tabular->deleteRow(0);
2218 int const rows = sel_row_end - sel_row_start + 1;
2219 while (paste_tabular->rows() > rows)
2220 paste_tabular->deleteRow(rows);
2222 paste_tabular->setTopLine(0, true, true);
2223 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2226 for (int i = 0; i < sel_col_start; ++i)
2227 paste_tabular->deleteColumn(0);
2229 int const columns = sel_col_end - sel_col_start + 1;
2230 while (paste_tabular->columns() > columns)
2231 paste_tabular->deleteColumn(columns);
2233 paste_tabular->setLeftLine(0, true, true);
2234 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2238 paste_tabular->ascii(*bv->buffer(), os,
2239 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2240 bv->stuffClipboard(STRCONV(os.str()));
2245 bool InsetTabular::pasteSelection(BufferView * bv)
2250 for (int r1 = 0, r2 = actrow;
2251 r1 < paste_tabular->rows() && r2 < tabular.rows();
2253 for (int c1 = 0, c2 = actcol;
2254 c1 < paste_tabular->columns() && c2 < tabular.columns();
2256 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2257 tabular.isPartOfMultiColumn(r2, c2))
2259 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2263 if (tabular.isPartOfMultiColumn(r2, c2)) {
2267 InsetText & inset = tabular.getCellInset(r2, c2);
2268 inset = paste_tabular->getCellInset(r1, c1);
2269 inset.setOwner(this);
2270 inset.deleteLyXText(bv);
2278 bool InsetTabular::cutSelection(BufferParams const & bp)
2280 if (!hasSelection())
2283 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2284 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2285 if (sel_col_start > sel_col_end) {
2286 sel_col_start = sel_col_end;
2287 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2289 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2292 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2293 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2295 if (sel_row_start > sel_row_end)
2296 swap(sel_row_start, sel_row_end);
2298 if (sel_cell_start > sel_cell_end)
2299 swap(sel_cell_start, sel_cell_end);
2301 for (int i = sel_row_start; i <= sel_row_end; ++i)
2302 for (int j = sel_col_start; j <= sel_col_end; ++j)
2303 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2308 bool InsetTabular::isRightToLeft(BufferView * bv)
2310 return bv->getParentLanguage(this)->RightToLeft();
2314 int InsetTabular::scroll(bool recursive) const
2316 int sx = UpdatableInset::scroll(false);
2318 if (recursive && the_locking_inset)
2319 sx += the_locking_inset->scroll(recursive);
2325 void InsetTabular::getSelection(int & srow, int & erow,
2326 int & scol, int & ecol) const
2328 int const start = hasSelection() ? sel_cell_start : actcell;
2329 int const end = hasSelection() ? sel_cell_end : actcell;
2331 srow = tabular.row_of_cell(start);
2332 erow = tabular.row_of_cell(end);
2336 scol = tabular.column_of_cell(start);
2337 ecol = tabular.column_of_cell(end);
2341 ecol = tabular.right_column_of_cell(end);
2345 ParagraphList * InsetTabular::getParagraphs(int i) const
2347 return (i < tabular.getNumberOfCells())
2348 ? tabular.getCellInset(i).getParagraphs(0)
2353 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2355 if (the_locking_inset)
2356 return the_locking_inset->cursor(bv);
2357 return InsetOld::cursor(bv);
2361 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2364 return const_cast<InsetTabular *>(this);
2366 for (int i = 0; i < tabular.rows(); ++i) {
2367 for (int j = 0; j < tabular.columns(); ++j) {
2368 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2378 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2380 if (the_locking_inset) {
2381 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2382 if (!word.word().empty())
2384 if (tabular.isLastCell(actcell)) {
2385 bv->unlockInset(const_cast<InsetTabular *>(this));
2386 return WordLangTuple();
2390 // otherwise we have to lock the next inset and ask for it's selecttion
2391 tabular.getCellInset(actcell)
2392 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2393 WordLangTuple word(selectNextWordInt(bv, value));
2394 if (!word.word().empty())
2400 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2402 // when entering this function the inset should be ALWAYS locked!
2403 BOOST_ASSERT(the_locking_inset);
2405 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2406 if (!word.word().empty())
2409 if (tabular.isLastCell(actcell)) {
2410 bv->unlockInset(const_cast<InsetTabular *>(this));
2411 return WordLangTuple();
2414 // otherwise we have to lock the next inset and ask for it's selecttion
2416 tabular.getCellInset(actcell)
2417 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2418 return selectNextWordInt(bv, value);
2422 void InsetTabular::selectSelectedWord(BufferView * bv)
2424 if (the_locking_inset)
2425 the_locking_inset->selectSelectedWord(bv);
2429 void InsetTabular::markErased()
2431 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2432 tabular.getCellInset(cell).markErased();
2436 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2438 if (the_locking_inset) {
2439 if (the_locking_inset->nextChange(bv, length)) {
2443 if (tabular.isLastCell(actcell))
2447 InsetText & inset = tabular.getCellInset(actcell);
2448 if (inset.nextChange(bv, length)) {
2452 while (!tabular.isLastCell(actcell)) {
2454 InsetText & inset = tabular.getCellInset(actcell);
2455 if (inset.nextChange(bv, length)) {
2464 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2468 if (the_locking_inset) {
2469 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2473 if (tabular.isLastCell(actcell))
2477 InsetText & inset = tabular.getCellInset(cell);
2478 if (inset.searchForward(bv, str, cs, mw)) {
2482 while (!tabular.isLastCell(cell)) {
2484 InsetText & inset = tabular.getCellInset(cell);
2485 if (inset.searchForward(bv, str, cs, mw)) {
2494 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2497 int cell = tabular.getNumberOfCells();
2498 if (the_locking_inset) {
2499 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2508 InsetText & inset = tabular.getCellInset(cell);
2509 if (inset.searchBackward(bv, str, cs, mw)) {
2518 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2520 if (the_locking_inset)
2521 return the_locking_inset->insetAllowed(code);
2522 // we return true here because if the inset is not locked someone
2523 // wants to insert something in one of our insettexts and we generally
2529 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2531 const int cell = tabular.getCellFromInset(in, actcell);
2534 return tabular.getPWidth(cell).zero();
2536 // well we didn't obviously find it so maybe our owner knows more
2538 return owner()->forceDefaultParagraphs(in);
2540 lyxerr << "If we're here there is really something strange going on!"
2546 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2549 if (buf.length() <= 0)
2555 string::size_type len = buf.length();
2556 string::size_type p = 0;
2558 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2566 maxCols = max(cols, maxCols);
2572 maxCols = max(cols, maxCols);
2573 LyXTabular * loctab;
2578 delete paste_tabular;
2579 paste_tabular = new LyXTabular(bv->buffer()->params(),
2580 this, rows, maxCols);
2581 loctab = paste_tabular;
2590 string::size_type op = 0;
2591 int cells = loctab->getNumberOfCells();
2594 rows = loctab->rows();
2595 int const columns = loctab->columns();
2597 while (cell < cells && p < len && row < rows &&
2598 (p = buf.find_first_of("\t\n", p)) != string::npos)
2604 // we can only set this if we are not too far right
2605 if (cols < columns) {
2606 InsetText & inset = loctab->getCellInset(cell);
2607 LyXFont const font = inset.getLyXText(bv)->
2608 getFont(inset.paragraphs.begin(), 0);
2609 inset.setText(buf.substr(op, p - op), font);
2615 // we can only set this if we are not too far right
2616 if (cols < columns) {
2617 InsetText & inset = tabular.getCellInset(cell);
2618 LyXFont const font = inset.getLyXText(bv)->
2619 getFont(inset.paragraphs.begin(), 0);
2620 inset.setText(buf.substr(op, p - op), font);
2625 cell = loctab->getCellNumber(row, cols);
2631 // check for the last cell if there is no trailing '\n'
2632 if (cell < cells && op < len) {
2633 InsetText & inset = loctab->getCellInset(cell);
2634 LyXFont const font = inset.getLyXText(bv)->
2635 getFont(inset.paragraphs.begin(), 0);
2636 inset.setText(buf.substr(op, len - op), font);
2643 void InsetTabular::addPreview(PreviewLoader & loader) const
2645 int const rows = tabular.rows();
2646 int const columns = tabular.columns();
2647 for (int i = 0; i < rows; ++i)
2648 for (int j = 0; j < columns; ++j)
2649 tabular.getCellInset(i, j).addPreview(loader);
2653 string const InsetTabularMailer::name_("tabular");
2655 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2656 : inset_(const_cast<InsetTabular &>(inset))
2660 string const InsetTabularMailer::inset2string(Buffer const &) const
2662 return params2string(inset_);
2666 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2668 istringstream data(STRCONV(in));
2670 lex.setStream(data);
2672 #warning CHECK verify that this is a sane value to return.
2678 string const token = lex.getString();
2686 string const token = lex.getString();
2687 if (token != "\\active_cell")
2690 cell = lex.getInteger();
2693 // This is part of the inset proper that is usually swallowed
2694 // by Buffer::readInset
2697 string const token = lex.getString();
2698 if (token != "Tabular")
2705 Buffer const & buffer = inset.buffer();
2706 inset.read(buffer, lex);
2708 // We can't set the active cell, but we can tell the frontend
2714 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2716 Buffer const & buffer = inset.buffer();
2719 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2720 inset.write(buffer, data);
2721 data << "\\end_inset\n";
2722 return STRCONV(data.str());