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"
30 #include "undo_funcs.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("
431 if (inset == &tabular.getCellInset(actcell)) {
432 lyxerr[Debug::INSETTEXT] << "OK" << endl;
433 the_locking_inset = &tabular.getCellInset(actcell);
436 } else if (!the_locking_inset) {
437 int const n = tabular.getNumberOfCells();
438 int const id = inset->id();
439 for (int i = 0; i < n; ++i) {
440 InsetText * in = &tabular.getCellInset(i);
443 the_locking_inset = in;
448 if (in->getInsetFromID(id)) {
450 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
451 return the_locking_inset->lockInsetInInset(bv, inset);
454 } else if (the_locking_inset && (the_locking_inset == inset)) {
455 lyxerr[Debug::INSETTEXT] << "OK" << endl;
457 } else if (the_locking_inset) {
458 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
459 return the_locking_inset->lockInsetInInset(bv, inset);
461 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
466 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
469 if (!the_locking_inset)
471 if (the_locking_inset == inset) {
472 the_locking_inset->insetUnlock(bv);
474 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
481 // this has to be here otherwise we don't redraw the cell!
482 the_locking_inset = 0;
485 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
486 if (inset->lyxCode() == TABULAR_CODE &&
487 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
488 InsetTabularMailer(*this).updateDialog(bv);
497 int InsetTabular::insetInInsetY() const
499 if (!the_locking_inset)
501 return inset_y + the_locking_inset->insetInInsetY();
505 UpdatableInset * InsetTabular::getLockingInset() const
507 return the_locking_inset ? the_locking_inset->getLockingInset() :
508 const_cast<InsetTabular *>(this);
512 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
516 if (the_locking_inset)
517 return the_locking_inset->getFirstLockingInsetOfType(c);
522 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
524 if (the_locking_inset)
525 return the_locking_inset->insertInset(bv, inset);
530 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
532 if (hasSelection() && cmd.button() == mouse_button::button3)
535 if (hasSelection()) {
537 updateLocal(cmd.view());
540 int const ocell = actcell;
541 BufferView * bv = cmd.view();
545 the_locking_inset = 0;
549 setPos(bv, cmd.x, cmd.y);
552 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
554 if ((ocell == actcell) && the_locking_inset && inset_hit) {
556 FuncRequest cmd1 = cmd;
559 the_locking_inset->localDispatch(cmd1);
563 if (the_locking_inset) {
564 the_locking_inset->insetUnlock(bv);
566 the_locking_inset = 0;
569 if (cmd.button() == mouse_button::button2) {
570 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
574 if (inset_hit && bv->theLockingInset()) {
575 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
576 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
579 FuncRequest cmd1 = cmd;
582 the_locking_inset->localDispatch(cmd1);
587 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
590 if (the_locking_inset) {
591 FuncRequest cmd1 = cmd;
594 ret = the_locking_inset->localDispatch(cmd1);
596 if (cmd.button() == mouse_button::button3 && !ret) {
597 InsetTabularMailer(*this).showDialog(cmd.view());
604 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
606 if (the_locking_inset) {
607 FuncRequest cmd1 = cmd;
610 the_locking_inset->localDispatch(cmd1);
614 BufferView * bv = cmd.view();
615 int const old_cell = actcell;
617 setPos(bv, cmd.x, cmd.y);
618 if (!hasSelection()) {
619 setSelection(actcell, actcell);
621 } else if (old_cell != actcell) {
622 setSelection(sel_cell_start, actcell);
628 dispatch_result InsetTabular::localDispatch(FuncRequest const & cmd)
630 // We need to save the value of the_locking_inset as the call to
631 // the_locking_inset->localDispatch might unlock it.
632 old_locking_inset = the_locking_inset;
633 dispatch_result result = UpdatableInset::localDispatch(cmd);
634 BufferView * bv = cmd.view();
636 if (cmd.action == LFUN_INSET_EDIT) {
638 lyxerr << "InsetTabular::edit: " << this << " args: '"
639 << cmd.argument << "' first cell: "
640 << &tabular.cell_info[0][0].inset << endl;
642 if (!bv->lockInset(this)) {
643 lyxerr << "InsetTabular::Cannot lock inset" << endl;
649 the_locking_inset = 0;
653 if (cmd.argument.size()) {
654 if (cmd.argument == "left") {
655 if (isRightToLeft(bv))
656 actcell = tabular.getLastCellInRow(0);
660 if (isRightToLeft(bv))
661 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
663 actcell = tabular.getNumberOfCells() - 1;
671 setPos(bv, cmd.x, cmd.y);
674 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
675 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
681 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
686 if (cmd.action < 0 && cmd.argument.empty())
689 bool hs = hasSelection();
692 // this one have priority over the locked InsetText, if we're not already
693 // inside another tabular then that one get's priority!
694 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
695 switch (cmd.action) {
696 case LFUN_MOUSE_PRESS:
700 case LFUN_MOUSE_MOTION:
701 lfunMouseMotion(cmd);
704 case LFUN_MOUSE_RELEASE:
705 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
707 case LFUN_CELL_BACKWARD:
708 case LFUN_CELL_FORWARD:
709 unlockInsetInInset(bv, the_locking_inset);
710 if (cmd.action == LFUN_CELL_FORWARD)
711 moveNextCell(bv, old_locking_inset != 0);
713 movePrevCell(bv, old_locking_inset != 0);
717 if (!the_locking_inset)
718 return DISPATCHED_NOUPDATE;
720 // this to avoid compiler warnings.
726 kb_action action = cmd.action;
727 string arg = cmd.argument;
728 if (the_locking_inset) {
729 result = the_locking_inset->localDispatch(cmd);
730 if (result == DISPATCHED_NOUPDATE) {
733 if (sc != scroll()) { // inset has been scrolled
737 } else if (result == DISPATCHED) {
740 } else if (result == FINISHED_UP) {
742 // Make sure to reset status message after
743 // exiting, e.g. math inset
744 bv->owner()->clearMessage();
745 } else if (result == FINISHED_DOWN) {
747 bv->owner()->clearMessage();
748 } else if (result == FINISHED_RIGHT) {
750 bv->owner()->clearMessage();
751 } else if (result == FINISHED) {
752 bv->owner()->clearMessage();
758 // --- Cursor Movements ----------------------------------
759 case LFUN_RIGHTSEL: {
760 int const start = hasSelection() ? sel_cell_start : actcell;
761 if (tabular.isLastCellInRow(actcell)) {
762 setSelection(start, actcell);
767 // if we are starting a selection, only select
768 // the current cell at the beginning
769 if (hasSelection()) {
770 moveRight(bv, false);
773 setSelection(start, end);
778 result = moveRight(bv);
784 int const start = hasSelection() ? sel_cell_start : actcell;
785 if (tabular.isFirstCellInRow(actcell)) {
786 setSelection(start, actcell);
791 // if we are starting a selection, only select
792 // the current cell at the beginning
793 if (hasSelection()) {
797 setSelection(start, end);
802 result = moveLeft(bv);
808 int const start = hasSelection() ? sel_cell_start : actcell;
809 int const ocell = actcell;
810 // if we are starting a selection, only select
811 // the current cell at the beginning
812 if (hasSelection()) {
814 if (ocell == sel_cell_end ||
815 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
816 setSelection(start, tabular.getCellBelow(sel_cell_end));
818 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
820 setSelection(start, start);
826 result = moveDown(bv, old_locking_inset != 0);
832 int const start = hasSelection() ? sel_cell_start : actcell;
833 int const ocell = actcell;
834 // if we are starting a selection, only select
835 // the current cell at the beginning
836 if (hasSelection()) {
838 if ((ocell == sel_cell_end) ||
839 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
840 setSelection(start, tabular.getCellAbove(sel_cell_end));
842 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
844 setSelection(start, start);
850 result = moveUp(bv, old_locking_inset != 0);
859 unlockInsetInInset(bv, the_locking_inset);
860 if (bv->top_y() + bv->painter().paperHeight() <
861 top_baseline + tabular.getHeightOfTabular())
863 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
864 actcell = tabular.getCellBelow(first_visible_cell) + column;
866 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
876 unlockInsetInInset(bv, the_locking_inset);
877 if (top_baseline < 0) {
878 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
879 if (top_baseline > 0)
882 actcell = tabular.getCellBelow(first_visible_cell) + column;
890 // none of these make sense for insettabular,
891 // but we must catch them to prevent any
892 // selection from being confused
896 case LFUN_WORDLEFTSEL:
898 case LFUN_WORDRIGHTSEL:
900 case LFUN_DOWN_PARAGRAPH:
901 case LFUN_DOWN_PARAGRAPHSEL:
902 case LFUN_UP_PARAGRAPH:
903 case LFUN_UP_PARAGRAPHSEL:
909 case LFUN_BEGINNINGBUF:
910 case LFUN_BEGINNINGBUFSEL:
914 case LFUN_LAYOUT_TABULAR: {
915 InsetTabularMailer(*this).showDialog(bv);
918 case LFUN_INSET_DIALOG_UPDATE: {
919 InsetTabularMailer(*this).updateDialog(bv);
922 case LFUN_TABULAR_FEATURE:
923 if (!tabularFeatures(bv, arg))
924 result = UNDISPATCHED;
926 // insert file functions
927 case LFUN_FILE_INSERT_ASCII_PARA:
928 case LFUN_FILE_INSERT_ASCII:
930 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
933 if (insertAsciiString(bv, tmpstr, false))
936 result = UNDISPATCHED;
939 // cut and paste functions
941 if (!copySelection(bv))
945 recordUndo(bv, Undo::DELETE);
946 cutSelection(bv->buffer()->params());
955 case LFUN_PASTESELECTION:
957 string const clip(bv->getClipboard());
961 if (clip.find('\t') != string::npos) {
965 string::size_type len = clip.length();
966 string::size_type p = 0;
969 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
977 maxCols = max(cols, maxCols);
983 maxCols = max(cols, maxCols);
984 delete paste_tabular;
985 paste_tabular = new LyXTabular(bv->buffer()->params(),
986 this, rows, maxCols);
987 string::size_type op = 0;
989 int cells = paste_tabular->getNumberOfCells();
991 while ((cell < cells) && (p < len) &&
992 (p = clip.find_first_of("\t\n", p)) != string::npos) {
997 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1002 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1003 while (cols++ < maxCols)
1011 // check for the last cell if there is no trailing '\n'
1012 if ((cell < cells) && (op < len))
1013 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1016 if (!insertAsciiString(bv, clip, true))
1019 // so that the clipboard is used and it goes on
1021 // and executes LFUN_PASTESELECTION in insettext!
1022 delete paste_tabular;
1027 if (hasPasteBuffer()) {
1028 recordUndo(bv, Undo::INSERT);
1033 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1035 // handle font changing stuff on selection before we lock the inset
1036 // in the default part!
1037 result = UNDISPATCHED;
1048 case LFUN_UNDERLINE:
1049 case LFUN_FONT_SIZE:
1050 if (bv->dispatch(FuncRequest(bv, action, arg)))
1051 result = DISPATCHED;
1057 // we try to activate the actual inset and put this event down to
1058 // the insets dispatch function.
1059 if (result == DISPATCHED || the_locking_inset)
1061 if (activateCellInset(bv)) {
1062 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1063 if (result == UNDISPATCHED || result >= FINISHED) {
1064 unlockInsetInInset(bv, the_locking_inset);
1065 // we need to update if this was requested before
1067 return UNDISPATCHED;
1076 if (result < FINISHED) {
1077 if (!the_locking_inset && bv->fitCursor())
1080 bv->unlockInset(this);
1085 int InsetTabular::latex(Buffer const & buf, ostream & os,
1086 LatexRunParams const & runparams) const
1088 return tabular.latex(buf, os, runparams);
1092 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1095 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1097 return tabular.ascii(buf, os, 0, false, 0);
1101 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1103 return tabular.linuxdoc(buf,os);
1107 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1112 // if the table is inside a float it doesn't need the informaltable
1113 // wrapper. Search for it.
1114 for (master = owner();
1115 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1116 master = master->owner());
1119 os << "<informaltable>";
1124 ret += tabular.docbook(buf, os, mixcont);
1126 os << "</informaltable>";
1135 void InsetTabular::validate(LaTeXFeatures & features) const
1137 tabular.validate(features);
1141 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1144 // if we have a locking_inset we should have to check only this cell for
1145 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1146 // This is _really_ important (André)
1147 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1150 for (int j = 0; j < tabular.columns(); ++j) {
1154 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1155 tabular.getCellInset(actrow, j).metrics(m, dim);
1156 maxAsc = max(dim.asc, maxAsc);
1157 maxDesc = max(dim.des, maxDesc);
1159 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1160 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1161 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1167 for (int i = 0; i < tabular.rows(); ++i) {
1170 for (int j = 0; j < tabular.columns(); ++j) {
1171 if (tabular.isPartOfMultiColumn(i, j))
1177 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1178 tabular.getCellInset(cell).metrics(m, dim);
1179 maxAsc = max(maxAsc, dim.asc);
1180 maxDesc = max(maxDesc, dim.des);
1181 tabular.setWidthOfCell(cell, dim.wid);
1183 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1184 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1189 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1191 if (the_locking_inset) {
1192 the_locking_inset->getCursor(bv, x, y);
1197 y = cursory_ + InsetTabular::y();
1200 int desc = tabular.getDescentOfRow(actrow);
1202 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1204 y += ADD_TO_HEIGHT * 2;
1205 y += TEXT_TO_INSET_OFFSET;
1209 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1211 if (the_locking_inset) {
1212 the_locking_inset->getCursorPos(bv, x, y);
1215 x = cursorx_ - top_x;
1220 void InsetTabular::fitInsetCursor(BufferView * bv) const
1222 if (the_locking_inset) {
1223 the_locking_inset->fitInsetCursor(bv);
1228 int const asc = font_metrics::maxAscent(font);
1229 int const desc = font_metrics::maxDescent(font);
1232 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1236 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1239 actcell = actrow = actcol = 0;
1240 int ly = tabular.getDescentOfRow(actrow);
1242 // first search the right row
1243 while (ly < y && actrow + 1 < tabular.rows()) {
1244 cursory_ += tabular.getDescentOfRow(actrow) +
1245 tabular.getAscentOfRow(actrow + 1) +
1246 tabular.getAdditionalHeight(actrow + 1);
1248 ly = cursory_ + tabular.getDescentOfRow(actrow);
1250 actcell = tabular.getCellNumber(actrow, actcol);
1252 // now search the right column
1253 int lx = tabular.getWidthOfColumn(actcell) -
1254 tabular.getAdditionalWidth(actcell);
1256 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1257 lx += tabular.getWidthOfColumn(actcell + 1)
1258 + tabular.getAdditionalWidth(actcell);
1260 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1265 int InsetTabular::getCellXPos(int cell) const
1269 for (; !tabular.isFirstCellInRow(c); --c)
1271 int lx = tabular.getWidthOfColumn(cell);
1272 for (; c < cell; ++c)
1273 lx += tabular.getWidthOfColumn(c);
1275 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1279 void InsetTabular::resetPos(BufferView * bv) const
1281 #ifdef WITH_WARNINGS
1282 #warning This should be fixed in the right manner (20011128 Jug)
1284 // fast hack to fix infinite repaintings!
1285 if (in_reset_pos > 0)
1289 actcol = tabular.column_of_cell(actcell);
1292 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1293 if (tabular.isLastCellInRow(cell)) {
1294 cursory_ += tabular.getDescentOfRow(actrow) +
1295 tabular.getAscentOfRow(actrow + 1) +
1296 tabular.getAdditionalHeight(actrow + 1);
1301 if (the_locking_inset)
1305 // we need this only from here on!!!
1307 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1308 int new_x = getCellXPos(actcell);
1309 int old_x = cursorx_;
1312 // cursor.x(getCellXPos(actcell) + offset);
1313 if (actcol < tabular.columns() - 1 && scroll(false) &&
1314 tabular.getWidthOfTabular() < bv->workWidth()-20)
1318 } else if (the_locking_inset &&
1319 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1321 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1322 if (xx > bv->workWidth()-20) {
1323 scroll(bv, -(xx - bv->workWidth() + 60));
1325 } else if (xx < 20) {
1333 } else if (cursorx_ - offset > 20 &&
1334 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1335 > bv->workWidth() - 20) {
1336 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1338 } else if (cursorx_ - offset < 20) {
1339 scroll(bv, 20 - cursorx_ + offset);
1341 } else if (scroll() && top_x > 20 &&
1342 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1343 scroll(bv, old_x - cursorx_);
1346 if (the_locking_inset) {
1347 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1350 if ((!the_locking_inset ||
1351 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1352 actcell != oldcell) {
1353 InsetTabularMailer(*this).updateDialog(bv);
1360 dispatch_result InsetTabular::moveRight(BufferView * bv, bool lock)
1362 if (lock && !old_locking_inset) {
1363 if (activateCellInset(bv))
1366 bool moved = isRightToLeft(bv)
1367 ? movePrevCell(bv) : moveNextCell(bv);
1369 return FINISHED_RIGHT;
1370 if (lock && activateCellInset(bv))
1374 return DISPATCHED_NOUPDATE;
1378 dispatch_result InsetTabular::moveLeft(BufferView * bv, bool lock)
1380 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1383 if (lock) { // behind the inset
1384 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1388 return DISPATCHED_NOUPDATE;
1392 dispatch_result InsetTabular::moveUp(BufferView * bv, bool lock)
1394 int const ocell = actcell;
1395 actcell = tabular.getCellAbove(actcell);
1396 if (actcell == ocell) // we moved out of the inset
1402 if (old_locking_inset) {
1403 old_locking_inset->getCursorPos(bv, x, y);
1404 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1406 if (activateCellInset(bv, x, 0))
1409 return DISPATCHED_NOUPDATE;
1413 dispatch_result InsetTabular::moveDown(BufferView * bv, bool lock)
1415 int const ocell = actcell;
1416 actcell = tabular.getCellBelow(actcell);
1417 if (actcell == ocell) // we moved out of the inset
1418 return FINISHED_DOWN;
1423 if (old_locking_inset) {
1424 old_locking_inset->getCursorPos(bv, x, y);
1425 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1427 if (activateCellInset(bv, x, 0))
1430 return DISPATCHED_NOUPDATE;
1434 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1436 if (isRightToLeft(bv)) {
1437 if (tabular.isFirstCellInRow(actcell)) {
1438 int row = tabular.row_of_cell(actcell);
1439 if (row == tabular.rows() - 1)
1441 actcell = tabular.getLastCellInRow(row);
1442 actcell = tabular.getCellBelow(actcell);
1449 if (tabular.isLastCell(actcell))
1454 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1455 isRightToLeftPar(bv->buffer()->params());
1456 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1463 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1465 if (isRightToLeft(bv)) {
1466 if (tabular.isLastCellInRow(actcell)) {
1467 int row = tabular.row_of_cell(actcell);
1470 actcell = tabular.getFirstCellInRow(row);
1471 actcell = tabular.getCellAbove(actcell);
1473 if (tabular.isLastCell(actcell))
1478 if (!actcell) // first cell
1483 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1484 isRightToLeftPar(bv->buffer()->params());
1485 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1492 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1496 setSelection(0, tabular.getNumberOfCells() - 1);
1498 if (hasSelection()) {
1499 recordUndo(bv, Undo::ATOMIC);
1500 bool const frozen = undo_frozen;
1503 // apply the fontchange on the whole selection
1508 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1509 for(int i = sel_row_start; i <= sel_row_end; ++i)
1510 for(int j = sel_col_start; j <= sel_col_end; ++j)
1511 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1519 if (the_locking_inset)
1520 the_locking_inset->setFont(bv, font, tall);
1524 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1526 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1529 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1530 string const tmp = tabularFeature[i].feature;
1532 if (tmp == what.substr(0, tmp.length())) {
1533 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1534 //tabularFeatures[i].feature.length())) {
1535 action = tabularFeature[i].action;
1539 if (action == LyXTabular::LAST_ACTION)
1543 ltrim(what.substr(tabularFeature[i].feature.length()));
1544 tabularFeatures(bv, action, val);
1550 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1551 string const & special, bool & flag)
1553 if (special == "dl_above") {
1556 } else if (special == "dl_below") {
1557 ltt.bottomDL = flag;
1559 } else if (special == "empty") {
1571 void InsetTabular::tabularFeatures(BufferView * bv,
1572 LyXTabular::Feature feature,
1573 string const & value)
1579 bool setLines = false;
1580 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1581 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1585 case LyXTabular::M_ALIGN_LEFT:
1586 case LyXTabular::ALIGN_LEFT:
1587 setAlign = LYX_ALIGN_LEFT;
1590 case LyXTabular::M_ALIGN_RIGHT:
1591 case LyXTabular::ALIGN_RIGHT:
1592 setAlign = LYX_ALIGN_RIGHT;
1595 case LyXTabular::M_ALIGN_CENTER:
1596 case LyXTabular::ALIGN_CENTER:
1597 setAlign = LYX_ALIGN_CENTER;
1600 case LyXTabular::ALIGN_BLOCK:
1601 setAlign = LYX_ALIGN_BLOCK;
1604 case LyXTabular::M_VALIGN_TOP:
1605 case LyXTabular::VALIGN_TOP:
1606 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1609 case LyXTabular::M_VALIGN_BOTTOM:
1610 case LyXTabular::VALIGN_BOTTOM:
1611 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1614 case LyXTabular::M_VALIGN_MIDDLE:
1615 case LyXTabular::VALIGN_MIDDLE:
1616 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1623 if (hasSelection()) {
1624 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1626 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1627 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1629 recordUndo(bv, Undo::ATOMIC);
1631 int row = tabular.row_of_cell(actcell);
1632 int column = tabular.column_of_cell(actcell);
1634 LyXTabular::ltType ltt;
1638 case LyXTabular::SET_PWIDTH:
1640 LyXLength const len(value);
1641 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1643 tabular.setColumnPWidth(actcell, len);
1644 if (oldlen != len) {
1645 // We need this otherwise we won't resize
1646 // the insettext of the active cell (if any)
1647 // until later (see InsetText::do_resize)
1648 unlockInsetInInset(bv, the_locking_inset);
1653 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1654 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1655 else if (!len.zero()
1656 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1657 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1661 case LyXTabular::SET_MPWIDTH:
1663 LyXLength const len(value);
1664 LyXLength const & oldlen = tabular.getPWidth(actcell);
1665 tabular.setMColumnPWidth(actcell, len);
1666 if (oldlen != len) {
1667 // We need this otherwise we won't resize
1668 // the insettext of the active cell (if any)
1669 // until later (see InsetText::do_resize)
1670 unlockInsetInInset(bv, the_locking_inset);
1675 case LyXTabular::SET_SPECIAL_COLUMN:
1676 case LyXTabular::SET_SPECIAL_MULTI:
1677 tabular.setAlignSpecial(actcell,value,feature);
1680 case LyXTabular::APPEND_ROW:
1681 // append the row into the tabular
1682 unlockInsetInInset(bv, the_locking_inset);
1683 tabular.appendRow(bv->buffer()->params(), actcell);
1684 tabular.setOwner(this);
1687 case LyXTabular::APPEND_COLUMN:
1688 // append the column into the tabular
1689 unlockInsetInInset(bv, the_locking_inset);
1690 tabular.appendColumn(bv->buffer()->params(), actcell);
1691 tabular.setOwner(this);
1692 actcell = tabular.getCellNumber(row, column);
1695 case LyXTabular::DELETE_ROW:
1696 unlockInsetInInset(bv, the_locking_inset);
1697 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1698 tabular.deleteRow(sel_row_start);
1700 if (sel_row_start >= tabular.rows())
1702 actcell = tabular.getCellNumber(sel_row_start, column);
1706 case LyXTabular::DELETE_COLUMN:
1707 unlockInsetInInset(bv, the_locking_inset);
1708 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1709 tabular.deleteColumn(sel_col_start);
1711 if (sel_col_start >= tabular.columns())
1713 actcell = tabular.getCellNumber(row, sel_col_start);
1717 case LyXTabular::M_TOGGLE_LINE_TOP:
1719 case LyXTabular::TOGGLE_LINE_TOP:
1721 bool lineSet = !tabular.topLine(actcell, flag);
1722 for (int i = sel_row_start; i <= sel_row_end; ++i)
1723 for (int j = sel_col_start; j <= sel_col_end; ++j)
1725 tabular.getCellNumber(i, j),
1731 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1733 case LyXTabular::TOGGLE_LINE_BOTTOM:
1735 bool lineSet = !tabular.bottomLine(actcell, flag);
1736 for (int i = sel_row_start; i <= sel_row_end; ++i)
1737 for (int j = sel_col_start; j <= sel_col_end; ++j)
1738 tabular.setBottomLine(
1739 tabular.getCellNumber(i, j),
1746 case LyXTabular::M_TOGGLE_LINE_LEFT:
1748 case LyXTabular::TOGGLE_LINE_LEFT:
1750 bool lineSet = !tabular.leftLine(actcell, flag);
1751 for (int i = sel_row_start; i <= sel_row_end; ++i)
1752 for (int j = sel_col_start; j <= sel_col_end; ++j)
1753 tabular.setLeftLine(
1754 tabular.getCellNumber(i,j),
1761 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1763 case LyXTabular::TOGGLE_LINE_RIGHT:
1765 bool lineSet = !tabular.rightLine(actcell, flag);
1766 for (int i = sel_row_start; i <= sel_row_end; ++i)
1767 for (int j = sel_col_start; j <= sel_col_end; ++j)
1768 tabular.setRightLine(
1769 tabular.getCellNumber(i,j),
1776 case LyXTabular::M_ALIGN_LEFT:
1777 case LyXTabular::M_ALIGN_RIGHT:
1778 case LyXTabular::M_ALIGN_CENTER:
1780 case LyXTabular::ALIGN_LEFT:
1781 case LyXTabular::ALIGN_RIGHT:
1782 case LyXTabular::ALIGN_CENTER:
1783 case LyXTabular::ALIGN_BLOCK:
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.setAlignment(
1787 tabular.getCellNumber(i, j),
1793 case LyXTabular::M_VALIGN_TOP:
1794 case LyXTabular::M_VALIGN_BOTTOM:
1795 case LyXTabular::M_VALIGN_MIDDLE:
1797 case LyXTabular::VALIGN_TOP:
1798 case LyXTabular::VALIGN_BOTTOM:
1799 case LyXTabular::VALIGN_MIDDLE:
1800 for (int i = sel_row_start; i <= sel_row_end; ++i)
1801 for (int j = sel_col_start; j <= sel_col_end; ++j)
1802 tabular.setVAlignment(
1803 tabular.getCellNumber(i, j),
1808 case LyXTabular::MULTICOLUMN: {
1809 if (sel_row_start != sel_row_end) {
1810 #ifdef WITH_WARNINGS
1811 #warning Need I say it ? This is horrible.
1813 Alert::error(_("Error setting multicolumn"),
1814 _("You cannot set multicolumn vertically."));
1817 // just multicol for one Single Cell
1818 if (!hasSelection()) {
1819 // check wether we are completly in a multicol
1820 if (tabular.isMultiColumn(actcell))
1821 tabular.unsetMultiColumn(actcell);
1823 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1827 // we have a selection so this means we just add all this
1828 // cells to form a multicolumn cell
1832 if (sel_cell_start > sel_cell_end) {
1833 s_start = sel_cell_end;
1834 s_end = sel_cell_start;
1836 s_start = sel_cell_start;
1837 s_end = sel_cell_end;
1839 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1846 case LyXTabular::SET_ALL_LINES:
1848 case LyXTabular::UNSET_ALL_LINES:
1849 for (int i = sel_row_start; i <= sel_row_end; ++i)
1850 for (int j = sel_col_start; j <= sel_col_end; ++j)
1851 tabular.setAllLines(
1852 tabular.getCellNumber(i,j), setLines);
1856 case LyXTabular::SET_LONGTABULAR:
1857 tabular.setLongTabular(true);
1858 updateLocal(bv); // because this toggles displayed
1861 case LyXTabular::UNSET_LONGTABULAR:
1862 tabular.setLongTabular(false);
1863 updateLocal(bv); // because this toggles displayed
1866 case LyXTabular::SET_ROTATE_TABULAR:
1867 tabular.setRotateTabular(true);
1870 case LyXTabular::UNSET_ROTATE_TABULAR:
1871 tabular.setRotateTabular(false);
1874 case LyXTabular::SET_ROTATE_CELL:
1875 for (int i = sel_row_start; i <= sel_row_end; ++i)
1876 for (int j = sel_col_start; j<=sel_col_end; ++j)
1877 tabular.setRotateCell(
1878 tabular.getCellNumber(i, j), true);
1881 case LyXTabular::UNSET_ROTATE_CELL:
1882 for (int i = sel_row_start; i <= sel_row_end; ++i)
1883 for (int j = sel_col_start; j <= sel_col_end; ++j)
1884 tabular.setRotateCell(
1885 tabular.getCellNumber(i, j), false);
1888 case LyXTabular::SET_USEBOX: {
1889 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1890 if (val == tabular.getUsebox(actcell))
1891 val = LyXTabular::BOX_NONE;
1892 for (int i = sel_row_start; i <= sel_row_end; ++i)
1893 for (int j = sel_col_start; j <= sel_col_end; ++j)
1894 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1898 case LyXTabular::UNSET_LTFIRSTHEAD:
1900 case LyXTabular::SET_LTFIRSTHEAD:
1901 tabular.getRowOfLTFirstHead(row, ltt);
1902 checkLongtableSpecial(ltt, value, flag);
1903 tabular.setLTHead(row, flag, ltt, true);
1906 case LyXTabular::UNSET_LTHEAD:
1908 case LyXTabular::SET_LTHEAD:
1909 tabular.getRowOfLTHead(row, ltt);
1910 checkLongtableSpecial(ltt, value, flag);
1911 tabular.setLTHead(row, flag, ltt, false);
1914 case LyXTabular::UNSET_LTFOOT:
1916 case LyXTabular::SET_LTFOOT:
1917 tabular.getRowOfLTFoot(row, ltt);
1918 checkLongtableSpecial(ltt, value, flag);
1919 tabular.setLTFoot(row, flag, ltt, false);
1922 case LyXTabular::UNSET_LTLASTFOOT:
1924 case LyXTabular::SET_LTLASTFOOT:
1925 tabular.getRowOfLTLastFoot(row, ltt);
1926 checkLongtableSpecial(ltt, value, flag);
1927 tabular.setLTFoot(row, flag, ltt, true);
1930 case LyXTabular::SET_LTNEWPAGE: {
1931 bool what = !tabular.getLTNewPage(row);
1932 tabular.setLTNewPage(row, what);
1936 // dummy stuff just to avoid warnings
1937 case LyXTabular::LAST_ACTION:
1941 InsetTabularMailer(*this).updateDialog(bv);
1945 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1946 mouse_button::state button, bool behind)
1948 UpdatableInset & inset = tabular.getCellInset(actcell);
1951 x = inset.x() + inset.width();
1952 y = inset.descent();
1954 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1955 //inset_y = cursory_;
1956 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1957 if (!the_locking_inset)
1960 return the_locking_inset;
1964 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1965 mouse_button::state button)
1967 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1969 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1973 bool InsetTabular::insetHit(BufferView *, int x, int) const
1975 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1979 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1981 //resizeLyXText(bv, recursive);
1985 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1986 bool const recursive) const
1988 if (the_locking_inset)
1989 return the_locking_inset->getLyXText(bv, recursive);
1990 return InsetOld::getLyXText(bv, recursive);
1994 bool InsetTabular::showInsetDialog(BufferView * bv) const
1996 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
1997 InsetTabularMailer(*this).showDialog(bv);
2002 void InsetTabular::openLayoutDialog(BufferView * bv) const
2004 if (the_locking_inset) {
2005 InsetTabular * inset = static_cast<InsetTabular *>
2006 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2008 inset->openLayoutDialog(bv);
2012 InsetTabularMailer(*this).showDialog(bv);
2017 // function returns an object as defined in func_status.h:
2018 // states OK, Unknown, Disabled, On, Off.
2020 FuncStatus InsetTabular::getStatus(string const & what) const
2022 int action = LyXTabular::LAST_ACTION;
2026 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2027 string const tmp = tabularFeature[i].feature;
2028 if (tmp == what.substr(0, tmp.length())) {
2029 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2030 // tabularFeatures[i].feature.length())) {
2031 action = tabularFeature[i].action;
2035 if (action == LyXTabular::LAST_ACTION) {
2037 return status.unknown(true);
2040 string const argument
2041 = ltrim(what.substr(tabularFeature[i].feature.length()));
2046 LyXTabular::ltType dummyltt;
2050 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2052 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2055 case LyXTabular::SET_PWIDTH:
2056 case LyXTabular::SET_MPWIDTH:
2057 case LyXTabular::SET_SPECIAL_COLUMN:
2058 case LyXTabular::SET_SPECIAL_MULTI:
2059 case LyXTabular::APPEND_ROW:
2060 case LyXTabular::APPEND_COLUMN:
2061 case LyXTabular::DELETE_ROW:
2062 case LyXTabular::DELETE_COLUMN:
2063 case LyXTabular::SET_ALL_LINES:
2064 case LyXTabular::UNSET_ALL_LINES:
2065 return status.clear();
2067 case LyXTabular::MULTICOLUMN:
2068 status.setOnOff(tabular.isMultiColumn(actcell));
2071 case LyXTabular::M_TOGGLE_LINE_TOP:
2073 case LyXTabular::TOGGLE_LINE_TOP:
2074 status.setOnOff(tabular.topLine(actcell, flag));
2077 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2079 case LyXTabular::TOGGLE_LINE_BOTTOM:
2080 status.setOnOff(tabular.bottomLine(actcell, flag));
2083 case LyXTabular::M_TOGGLE_LINE_LEFT:
2085 case LyXTabular::TOGGLE_LINE_LEFT:
2086 status.setOnOff(tabular.leftLine(actcell, flag));
2089 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2091 case LyXTabular::TOGGLE_LINE_RIGHT:
2092 status.setOnOff(tabular.rightLine(actcell, flag));
2095 case LyXTabular::M_ALIGN_LEFT:
2097 case LyXTabular::ALIGN_LEFT:
2098 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2101 case LyXTabular::M_ALIGN_RIGHT:
2103 case LyXTabular::ALIGN_RIGHT:
2104 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2107 case LyXTabular::M_ALIGN_CENTER:
2109 case LyXTabular::ALIGN_CENTER:
2110 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2113 case LyXTabular::ALIGN_BLOCK:
2114 status.disabled(tabular.getPWidth(actcell).zero());
2115 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2118 case LyXTabular::M_VALIGN_TOP:
2120 case LyXTabular::VALIGN_TOP:
2121 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2124 case LyXTabular::M_VALIGN_BOTTOM:
2126 case LyXTabular::VALIGN_BOTTOM:
2127 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2130 case LyXTabular::M_VALIGN_MIDDLE:
2132 case LyXTabular::VALIGN_MIDDLE:
2133 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2136 case LyXTabular::SET_LONGTABULAR:
2137 status.setOnOff(tabular.isLongTabular());
2140 case LyXTabular::UNSET_LONGTABULAR:
2141 status.setOnOff(!tabular.isLongTabular());
2144 case LyXTabular::SET_ROTATE_TABULAR:
2145 status.setOnOff(tabular.getRotateTabular());
2148 case LyXTabular::UNSET_ROTATE_TABULAR:
2149 status.setOnOff(!tabular.getRotateTabular());
2152 case LyXTabular::SET_ROTATE_CELL:
2153 status.setOnOff(tabular.getRotateCell(actcell));
2156 case LyXTabular::UNSET_ROTATE_CELL:
2157 status.setOnOff(!tabular.getRotateCell(actcell));
2160 case LyXTabular::SET_USEBOX:
2161 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2164 case LyXTabular::SET_LTFIRSTHEAD:
2165 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2168 case LyXTabular::SET_LTHEAD:
2169 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2172 case LyXTabular::SET_LTFOOT:
2173 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2176 case LyXTabular::SET_LTLASTFOOT:
2177 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2180 case LyXTabular::SET_LTNEWPAGE:
2181 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2186 status.disabled(true);
2193 void InsetTabular::getLabelList(Buffer const & buffer,
2194 std::vector<string> & list) const
2196 tabular.getLabelList(buffer, list);
2200 bool InsetTabular::copySelection(BufferView * bv)
2202 if (!hasSelection())
2205 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2206 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2207 if (sel_col_start > sel_col_end) {
2208 sel_col_start = sel_col_end;
2209 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2211 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2214 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2215 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2216 if (sel_row_start > sel_row_end)
2217 swap(sel_row_start, sel_row_end);
2219 delete paste_tabular;
2220 paste_tabular = new LyXTabular(tabular);
2221 paste_tabular->setOwner(this);
2223 for (int i = 0; i < sel_row_start; ++i)
2224 paste_tabular->deleteRow(0);
2226 int const rows = sel_row_end - sel_row_start + 1;
2227 while (paste_tabular->rows() > rows)
2228 paste_tabular->deleteRow(rows);
2230 paste_tabular->setTopLine(0, true, true);
2231 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2234 for (int i = 0; i < sel_col_start; ++i)
2235 paste_tabular->deleteColumn(0);
2237 int const columns = sel_col_end - sel_col_start + 1;
2238 while (paste_tabular->columns() > columns)
2239 paste_tabular->deleteColumn(columns);
2241 paste_tabular->setLeftLine(0, true, true);
2242 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2246 paste_tabular->ascii(*bv->buffer(), os,
2247 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2248 bv->stuffClipboard(os.str());
2253 bool InsetTabular::pasteSelection(BufferView * bv)
2258 for (int r1 = 0, r2 = actrow;
2259 r1 < paste_tabular->rows() && r2 < tabular.rows();
2261 for (int c1 = 0, c2 = actcol;
2262 c1 < paste_tabular->columns() && c2 < tabular.columns();
2264 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2265 tabular.isPartOfMultiColumn(r2, c2))
2267 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2271 if (tabular.isPartOfMultiColumn(r2, c2)) {
2275 InsetText & inset = tabular.getCellInset(r2, c2);
2276 inset = paste_tabular->getCellInset(r1, c1);
2277 inset.setOwner(this);
2278 inset.deleteLyXText(bv);
2286 bool InsetTabular::cutSelection(BufferParams const & bp)
2288 if (!hasSelection())
2291 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2292 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2293 if (sel_col_start > sel_col_end) {
2294 sel_col_start = sel_col_end;
2295 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2297 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2300 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2301 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2303 if (sel_row_start > sel_row_end)
2304 swap(sel_row_start, sel_row_end);
2306 if (sel_cell_start > sel_cell_end)
2307 swap(sel_cell_start, sel_cell_end);
2309 for (int i = sel_row_start; i <= sel_row_end; ++i)
2310 for (int j = sel_col_start; j <= sel_col_end; ++j)
2311 tabular.getCellInset(tabular.getCellNumber(i, j))
2312 .clear(bp.tracking_changes);
2317 bool InsetTabular::isRightToLeft(BufferView * bv)
2319 return bv->getParentLanguage(this)->RightToLeft();
2323 int InsetTabular::scroll(bool recursive) const
2325 int sx = UpdatableInset::scroll(false);
2327 if (recursive && the_locking_inset)
2328 sx += the_locking_inset->scroll(recursive);
2334 void InsetTabular::getSelection(int & srow, int & erow,
2335 int & scol, int & ecol) const
2337 int const start = hasSelection() ? sel_cell_start : actcell;
2338 int const end = hasSelection() ? sel_cell_end : actcell;
2340 srow = tabular.row_of_cell(start);
2341 erow = tabular.row_of_cell(end);
2345 scol = tabular.column_of_cell(start);
2346 ecol = tabular.column_of_cell(end);
2350 ecol = tabular.right_column_of_cell(end);
2354 ParagraphList * InsetTabular::getParagraphs(int i) const
2356 return (i < tabular.getNumberOfCells())
2357 ? tabular.getCellInset(i).getParagraphs(0)
2362 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2364 if (the_locking_inset)
2365 return the_locking_inset->cursor(bv);
2366 return InsetOld::cursor(bv);
2370 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2373 return const_cast<InsetTabular *>(this);
2375 for (int i = 0; i < tabular.rows(); ++i) {
2376 for (int j = 0; j < tabular.columns(); ++j) {
2377 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2387 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2389 if (the_locking_inset) {
2390 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2391 if (!word.word().empty())
2393 if (tabular.isLastCell(actcell)) {
2394 bv->unlockInset(const_cast<InsetTabular *>(this));
2395 return WordLangTuple();
2399 // otherwise we have to lock the next inset and ask for it's selecttion
2400 tabular.getCellInset(actcell)
2401 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2402 WordLangTuple word(selectNextWordInt(bv, value));
2403 if (!word.word().empty())
2409 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2411 // when entering this function the inset should be ALWAYS locked!
2412 BOOST_ASSERT(the_locking_inset);
2414 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2415 if (!word.word().empty())
2418 if (tabular.isLastCell(actcell)) {
2419 bv->unlockInset(const_cast<InsetTabular *>(this));
2420 return WordLangTuple();
2423 // otherwise we have to lock the next inset and ask for it's selecttion
2425 tabular.getCellInset(actcell)
2426 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2427 return selectNextWordInt(bv, value);
2431 void InsetTabular::selectSelectedWord(BufferView * bv)
2433 if (the_locking_inset)
2434 the_locking_inset->selectSelectedWord(bv);
2438 void InsetTabular::markErased()
2440 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2441 tabular.getCellInset(cell).markErased();
2445 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2447 if (the_locking_inset) {
2448 if (the_locking_inset->nextChange(bv, length)) {
2452 if (tabular.isLastCell(actcell))
2456 InsetText & inset = tabular.getCellInset(actcell);
2457 if (inset.nextChange(bv, length)) {
2461 while (!tabular.isLastCell(actcell)) {
2463 InsetText & inset = tabular.getCellInset(actcell);
2464 if (inset.nextChange(bv, length)) {
2473 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2477 if (the_locking_inset) {
2478 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2482 if (tabular.isLastCell(actcell))
2486 InsetText & inset = tabular.getCellInset(cell);
2487 if (inset.searchForward(bv, str, cs, mw)) {
2491 while (!tabular.isLastCell(cell)) {
2493 InsetText & inset = tabular.getCellInset(cell);
2494 if (inset.searchForward(bv, str, cs, mw)) {
2503 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2506 int cell = tabular.getNumberOfCells();
2507 if (the_locking_inset) {
2508 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2517 InsetText & inset = tabular.getCellInset(cell);
2518 if (inset.searchBackward(bv, str, cs, mw)) {
2527 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2529 if (the_locking_inset)
2530 return the_locking_inset->insetAllowed(code);
2531 // we return true here because if the inset is not locked someone
2532 // wants to insert something in one of our insettexts and we generally
2538 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2540 const int cell = tabular.getCellFromInset(in);
2543 return tabular.getPWidth(cell).zero();
2545 // this is a workaround for a crash (New, Insert->Tabular,
2546 // Insert->FootNote)
2550 // well we didn't obviously find it so maybe our owner knows more
2551 BOOST_ASSERT(owner());
2552 return owner()->forceDefaultParagraphs(in);
2556 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2559 if (buf.length() <= 0)
2565 string::size_type len = buf.length();
2566 string::size_type p = 0;
2568 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2576 maxCols = max(cols, maxCols);
2582 maxCols = max(cols, maxCols);
2583 LyXTabular * loctab;
2588 delete paste_tabular;
2589 paste_tabular = new LyXTabular(bv->buffer()->params(),
2591 paste_tabular->setOwner(this);
2592 loctab = paste_tabular;
2601 string::size_type op = 0;
2602 int cells = loctab->getNumberOfCells();
2605 rows = loctab->rows();
2606 int const columns = loctab->columns();
2608 while (cell < cells && p < len && row < rows &&
2609 (p = buf.find_first_of("\t\n", p)) != string::npos)
2615 // we can only set this if we are not too far right
2616 if (cols < columns) {
2617 InsetText & inset = loctab->getCellInset(cell);
2618 LyXFont const font = inset.getLyXText(bv)->
2619 getFont(inset.paragraphs.begin(), 0);
2620 inset.setText(buf.substr(op, p - op), font);
2626 // we can only set this if we are not too far right
2627 if (cols < columns) {
2628 InsetText & inset = tabular.getCellInset(cell);
2629 LyXFont const font = inset.getLyXText(bv)->
2630 getFont(inset.paragraphs.begin(), 0);
2631 inset.setText(buf.substr(op, p - op), font);
2636 cell = loctab->getCellNumber(row, cols);
2642 // check for the last cell if there is no trailing '\n'
2643 if (cell < cells && op < len) {
2644 InsetText & inset = loctab->getCellInset(cell);
2645 LyXFont const font = inset.getLyXText(bv)->
2646 getFont(inset.paragraphs.begin(), 0);
2647 inset.setText(buf.substr(op, len - op), font);
2654 void InsetTabular::addPreview(PreviewLoader & loader) const
2656 int const rows = tabular.rows();
2657 int const columns = tabular.columns();
2658 for (int i = 0; i < rows; ++i)
2659 for (int j = 0; j < columns; ++j)
2660 tabular.getCellInset(i, j).addPreview(loader);
2664 string const InsetTabularMailer::name_("tabular");
2666 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2667 : inset_(const_cast<InsetTabular &>(inset))
2671 string const InsetTabularMailer::inset2string(Buffer const &) const
2673 return params2string(inset_);
2677 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2679 istringstream data(in);
2681 lex.setStream(data);
2683 #warning CHECK verify that this is a sane value to return.
2689 string const token = lex.getString();
2697 string const token = lex.getString();
2698 if (token != "\\active_cell")
2701 cell = lex.getInteger();
2704 // This is part of the inset proper that is usually swallowed
2705 // by Buffer::readInset
2708 string const token = lex.getString();
2709 if (token != "Tabular")
2716 Buffer const & buffer = inset.buffer();
2717 inset.read(buffer, lex);
2719 // We can't set the active cell, but we can tell the frontend
2725 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2727 Buffer const & buffer = inset.buffer();
2730 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2731 inset.write(buffer, data);
2732 data << "\\end_inset\n";