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"
14 #include "insettext.h"
17 #include "BufferView.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
28 #include "lyxlength.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "metricsinfo.h"
36 #include "frontends/Alert.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/Painter.h"
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
50 using namespace lyx::support;
51 using namespace lyx::graphics;
65 int const ADD_TO_HEIGHT = 2;
66 int const ADD_TO_TABULAR_WIDTH = 2;
69 LyXTabular * paste_tabular = 0;
72 struct TabularFeature {
73 LyXTabular::Feature action;
78 TabularFeature tabularFeature[] =
80 { LyXTabular::APPEND_ROW, "append-row" },
81 { LyXTabular::APPEND_COLUMN, "append-column" },
82 { LyXTabular::DELETE_ROW, "delete-row" },
83 { LyXTabular::DELETE_COLUMN, "delete-column" },
84 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
85 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
86 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
87 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
88 { LyXTabular::ALIGN_LEFT, "align-left" },
89 { LyXTabular::ALIGN_RIGHT, "align-right" },
90 { LyXTabular::ALIGN_CENTER, "align-center" },
91 { LyXTabular::ALIGN_BLOCK, "align-block" },
92 { LyXTabular::VALIGN_TOP, "valign-top" },
93 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
94 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
95 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
96 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
97 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
98 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
99 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
100 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
101 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
102 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
103 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
104 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
105 { LyXTabular::MULTICOLUMN, "multicolumn" },
106 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
107 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
108 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
109 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
110 { LyXTabular::SET_PWIDTH, "set-pwidth" },
111 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
112 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
113 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
114 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
115 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
116 { LyXTabular::SET_USEBOX, "set-usebox" },
117 { LyXTabular::SET_LTHEAD, "set-lthead" },
118 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
119 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
120 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
121 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
122 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
123 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
124 { LyXTabular::LAST_ACTION, "" }
128 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
129 bool operator()(TabularFeature & tf)
131 return tf.action == feature_;
134 LyXTabular::Feature feature_;
140 string const featureAsString(LyXTabular::Feature feature)
142 TabularFeature * it = tabularFeature;
143 TabularFeature * end = it +
144 sizeof(tabularFeature) / sizeof(TabularFeature);
145 it = std::find_if(it, end, FindFeature(feature));
146 return (it == end) ? string() : it->feature;
150 bool InsetTabular::hasPasteBuffer() const
152 return (paste_tabular != 0);
156 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
157 : tabular(buf.params, this, max(rows, 1), max(columns, 1)),
158 buffer_(&buf), cursorx_(0), cursory_(0)
160 // for now make it always display as display() inset
162 the_locking_inset = 0;
163 old_locking_inset = 0;
166 actrow = actcell = 0;
174 InsetTabular::InsetTabular(InsetTabular const & tab)
175 : UpdatableInset(tab),
176 tabular(tab.buffer_->params, this, tab.tabular),
177 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
179 the_locking_inset = 0;
180 old_locking_inset = 0;
183 actrow = actcell = 0;
191 InsetTabular::~InsetTabular()
193 InsetTabularMailer(*this).hideDialog();
197 auto_ptr<InsetBase> InsetTabular::clone() const
199 return auto_ptr<InsetBase>(new InsetTabular(*this));
203 Buffer const * InsetTabular::buffer() const
209 BufferView * InsetTabular::view() const
216 void InsetTabular::buffer(Buffer * b)
222 void InsetTabular::write(Buffer const * buf, ostream & os) const
224 os << "Tabular" << endl;
225 tabular.write(buf, os);
229 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
231 bool const old_format = (lex.getString() == "\\LyXTable");
233 tabular.read(buf, lex);
239 string token = lex.getString();
240 while (lex.isOK() && (token != "\\end_inset")) {
242 token = lex.getString();
244 if (token != "\\end_inset") {
245 lex.printError("Missing \\end_inset at this point. "
251 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
253 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
254 // mi.base.textwidth << "\n";
256 lyxerr << "InsetTabular::metrics: need bv" << endl;
260 calculate_dimensions_of_cells(mi);
262 dim.asc = tabular.getAscentOfRow(0);
263 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
264 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
269 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
271 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
273 BufferView * bv = pi.base.bv;
276 UpdatableInset::draw(pi, x, y);
284 x += ADD_TO_TABULAR_WIDTH;
287 first_visible_cell = -1;
288 for (int i = 0; i < tabular.rows(); ++i) {
290 cell = tabular.getCellNumber(i, 0);
291 if (y + tabular.getDescentOfRow(i) <= 0 &&
292 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
294 y += tabular.getDescentOfRow(i) +
295 tabular.getAscentOfRow(i + 1) +
296 tabular.getAdditionalHeight(i + 1);
299 for (int j = 0; j < tabular.columns(); ++j) {
300 if (nx > bv->workWidth())
302 if (tabular.isPartOfMultiColumn(i, j))
304 int cx = nx + tabular.getBeginningOfTextInCell(cell);
305 if (first_visible_cell < 0)
306 first_visible_cell = cell;
307 if (hasSelection()) {
308 drawCellSelection(pi.pain, nx, y, i, j, cell);
311 tabular.getCellInset(cell).draw(pi, cx, y);
312 drawCellLines(pi.pain, nx, y, i, cell);
313 nx += tabular.getWidthOfColumn(cell);
317 // Would be nice, but for some completely unfathomable reason,
318 // on a col resize to a new fixed width, even though the insettexts
319 // are resized, the cell isn't, but drawing all cells in a tall table
320 // has the desired effect somehow. Complete dark magic.
322 // avoiding drawing the rest of a long table is
323 // a pretty big speedup
324 if (y > bv->workHeight())
328 y += tabular.getDescentOfRow(i) +
329 tabular.getAscentOfRow(i + 1) +
330 tabular.getAdditionalHeight(i + 1);
335 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
336 int row, int cell) const
338 int x2 = x + tabular.getWidthOfColumn(cell);
341 if (!tabular.topAlreadyDrawn(cell)) {
342 on_off = !tabular.topLine(cell);
343 pain.line(x, y - tabular.getAscentOfRow(row),
344 x2, y - tabular.getAscentOfRow(row),
345 on_off ? LColor::tabularonoffline : LColor::tabularline,
346 on_off ? Painter::line_onoffdash : Painter::line_solid);
348 on_off = !tabular.bottomLine(cell);
349 pain.line(x, y + tabular.getDescentOfRow(row),
350 x2, y + tabular.getDescentOfRow(row),
351 on_off ? LColor::tabularonoffline : LColor::tabularline,
352 on_off ? Painter::line_onoffdash : Painter::line_solid);
353 if (!tabular.leftAlreadyDrawn(cell)) {
354 on_off = !tabular.leftLine(cell);
355 pain.line(x, y - tabular.getAscentOfRow(row),
356 x, y + tabular.getDescentOfRow(row),
357 on_off ? LColor::tabularonoffline : LColor::tabularline,
358 on_off ? Painter::line_onoffdash : Painter::line_solid);
360 on_off = !tabular.rightLine(cell);
361 pain.line(x2 - tabular.getAdditionalWidth(cell),
362 y - tabular.getAscentOfRow(row),
363 x2 - tabular.getAdditionalWidth(cell),
364 y + tabular.getDescentOfRow(row),
365 on_off ? LColor::tabularonoffline : LColor::tabularline,
366 on_off ? Painter::line_onoffdash : Painter::line_solid);
370 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
371 int row, int column, int cell) const
373 Assert(hasSelection());
374 int cs = tabular.column_of_cell(sel_cell_start);
375 int ce = tabular.column_of_cell(sel_cell_end);
378 cs = tabular.column_of_cell(sel_cell_end);
380 ce = tabular.right_column_of_cell(sel_cell_end);
383 int rs = tabular.row_of_cell(sel_cell_start);
384 int re = tabular.row_of_cell(sel_cell_end);
388 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
389 int w = tabular.getWidthOfColumn(cell);
390 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
391 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
392 w, h, LColor::selection);
397 string const InsetTabular::editMessage() const
399 return _("Opened table");
403 void InsetTabular::insetUnlock(BufferView * bv)
405 if (the_locking_inset) {
406 the_locking_inset->insetUnlock(bv);
408 the_locking_inset = 0;
413 if (scroll(false) || hasSelection()) {
422 void InsetTabular::updateLocal(BufferView * bv) const
430 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
432 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
437 if (inset == &tabular.getCellInset(actcell)) {
438 lyxerr[Debug::INSETTEXT] << "OK" << endl;
439 the_locking_inset = &tabular.getCellInset(actcell);
442 } else if (!the_locking_inset) {
443 int const n = tabular.getNumberOfCells();
444 int const id = inset->id();
445 for (int i = 0; i < n; ++i) {
446 InsetText * in = &tabular.getCellInset(i);
449 the_locking_inset = in;
454 if (in->getInsetFromID(id)) {
456 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
457 return the_locking_inset->lockInsetInInset(bv, inset);
460 } else if (the_locking_inset && (the_locking_inset == inset)) {
461 lyxerr[Debug::INSETTEXT] << "OK" << endl;
463 } else if (the_locking_inset) {
464 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
465 return the_locking_inset->lockInsetInInset(bv, inset);
467 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
472 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
475 if (!the_locking_inset)
477 if (the_locking_inset == inset) {
478 the_locking_inset->insetUnlock(bv);
480 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
487 // this has to be here otherwise we don't redraw the cell!
488 the_locking_inset = 0;
491 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
492 if (inset->lyxCode() == TABULAR_CODE &&
493 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
494 InsetTabularMailer(*this).updateDialog(bv);
503 int InsetTabular::insetInInsetY() const
505 if (!the_locking_inset)
507 return inset_y + the_locking_inset->insetInInsetY();
511 UpdatableInset * InsetTabular::getLockingInset() const
513 return the_locking_inset ? the_locking_inset->getLockingInset() :
514 const_cast<InsetTabular *>(this);
518 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
522 if (the_locking_inset)
523 return the_locking_inset->getFirstLockingInsetOfType(c);
528 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
530 if (the_locking_inset)
531 return the_locking_inset->insertInset(bv, inset);
536 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
538 if (hasSelection() && cmd.button() == mouse_button::button3)
541 if (hasSelection()) {
543 updateLocal(cmd.view());
546 int const ocell = actcell;
547 BufferView * bv = cmd.view();
551 the_locking_inset = 0;
555 setPos(bv, cmd.x, cmd.y);
558 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
560 if ((ocell == actcell) && the_locking_inset && inset_hit) {
562 FuncRequest cmd1 = cmd;
565 the_locking_inset->localDispatch(cmd1);
569 if (the_locking_inset) {
570 the_locking_inset->insetUnlock(bv);
572 the_locking_inset = 0;
575 if (cmd.button() == mouse_button::button2) {
576 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
580 if (inset_hit && bv->theLockingInset()) {
581 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
582 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
585 FuncRequest cmd1 = cmd;
588 the_locking_inset->localDispatch(cmd1);
593 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
596 if (the_locking_inset) {
597 FuncRequest cmd1 = cmd;
600 ret = the_locking_inset->localDispatch(cmd1);
602 if (cmd.button() == mouse_button::button3 && !ret) {
603 InsetTabularMailer(*this).showDialog(cmd.view());
610 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
612 if (the_locking_inset) {
613 FuncRequest cmd1 = cmd;
616 the_locking_inset->localDispatch(cmd1);
620 BufferView * bv = cmd.view();
621 int const old_cell = actcell;
623 setPos(bv, cmd.x, cmd.y);
624 if (!hasSelection()) {
625 setSelection(actcell, actcell);
627 } else if (old_cell != actcell) {
628 setSelection(sel_cell_start, actcell);
634 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
636 // We need to save the value of the_locking_inset as the call to
637 // the_locking_inset->localDispatch might unlock it.
638 old_locking_inset = the_locking_inset;
639 RESULT result = UpdatableInset::localDispatch(cmd);
640 BufferView * bv = cmd.view();
642 if (cmd.action == LFUN_INSET_EDIT) {
644 if (!bv->lockInset(this)) {
645 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
651 the_locking_inset = 0;
655 if (cmd.argument.size()) {
656 if (cmd.argument == "left") {
657 if (isRightToLeft(bv))
658 actcell = tabular.getLastCellInRow(0);
662 if (isRightToLeft(bv))
663 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
665 actcell = tabular.getNumberOfCells() - 1;
673 setPos(bv, cmd.x, cmd.y);
676 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
677 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
683 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
688 if (cmd.action < 0 && cmd.argument.empty())
691 bool hs = hasSelection();
694 // this one have priority over the locked InsetText, if we're not already
695 // inside another tabular then that one get's priority!
696 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
697 switch (cmd.action) {
698 case LFUN_MOUSE_PRESS:
702 case LFUN_MOUSE_MOTION:
703 lfunMouseMotion(cmd);
706 case LFUN_MOUSE_RELEASE:
707 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
709 case LFUN_CELL_BACKWARD:
710 case LFUN_CELL_FORWARD:
711 unlockInsetInInset(bv, the_locking_inset);
712 if (cmd.action == LFUN_CELL_FORWARD)
713 moveNextCell(bv, old_locking_inset != 0);
715 movePrevCell(bv, old_locking_inset != 0);
719 if (!the_locking_inset)
720 return DISPATCHED_NOUPDATE;
722 // this to avoid compiler warnings.
728 kb_action action = cmd.action;
729 string arg = cmd.argument;
730 if (the_locking_inset) {
731 result = the_locking_inset->localDispatch(cmd);
732 if (result == DISPATCHED_NOUPDATE) {
735 if (sc != scroll()) { // inset has been scrolled
739 } else if (result == DISPATCHED) {
742 } else if (result == FINISHED_UP) {
744 // Make sure to reset status message after
745 // exiting, e.g. math inset
746 bv->owner()->clearMessage();
747 } else if (result == FINISHED_DOWN) {
749 bv->owner()->clearMessage();
750 } else if (result == FINISHED_RIGHT) {
752 bv->owner()->clearMessage();
753 } else if (result == FINISHED) {
754 bv->owner()->clearMessage();
760 // --- Cursor Movements ----------------------------------
761 case LFUN_RIGHTSEL: {
762 int const start = hasSelection() ? sel_cell_start : actcell;
763 if (tabular.isLastCellInRow(actcell)) {
764 setSelection(start, actcell);
769 // if we are starting a selection, only select
770 // the current cell at the beginning
771 if (hasSelection()) {
772 moveRight(bv, false);
775 setSelection(start, end);
780 result = moveRight(bv);
786 int const start = hasSelection() ? sel_cell_start : actcell;
787 if (tabular.isFirstCellInRow(actcell)) {
788 setSelection(start, actcell);
793 // if we are starting a selection, only select
794 // the current cell at the beginning
795 if (hasSelection()) {
799 setSelection(start, end);
804 result = moveLeft(bv);
810 int const start = hasSelection() ? sel_cell_start : actcell;
811 int const ocell = actcell;
812 // if we are starting a selection, only select
813 // the current cell at the beginning
814 if (hasSelection()) {
816 if (ocell == sel_cell_end ||
817 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
818 setSelection(start, tabular.getCellBelow(sel_cell_end));
820 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
822 setSelection(start, start);
828 result = moveDown(bv, old_locking_inset != 0);
834 int const start = hasSelection() ? sel_cell_start : actcell;
835 int const ocell = actcell;
836 // if we are starting a selection, only select
837 // the current cell at the beginning
838 if (hasSelection()) {
840 if ((ocell == sel_cell_end) ||
841 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
842 setSelection(start, tabular.getCellAbove(sel_cell_end));
844 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
846 setSelection(start, start);
852 result = moveUp(bv, old_locking_inset != 0);
861 unlockInsetInInset(bv, the_locking_inset);
862 if (bv->text->top_y() + bv->painter().paperHeight() <
863 top_baseline + tabular.getHeightOfTabular())
865 bv->scrollDocView(bv->text->top_y() + bv->painter().paperHeight());
866 actcell = tabular.getCellBelow(first_visible_cell) + column;
868 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
878 unlockInsetInInset(bv, the_locking_inset);
879 if (top_baseline < 0) {
880 bv->scrollDocView(bv->text->top_y() - bv->painter().paperHeight());
881 if (top_baseline > 0)
884 actcell = tabular.getCellBelow(first_visible_cell) + column;
892 // none of these make sense for insettabular,
893 // but we must catch them to prevent any
894 // selection from being confused
898 case LFUN_WORDLEFTSEL:
900 case LFUN_WORDRIGHTSEL:
902 case LFUN_DOWN_PARAGRAPH:
903 case LFUN_DOWN_PARAGRAPHSEL:
904 case LFUN_UP_PARAGRAPH:
905 case LFUN_UP_PARAGRAPHSEL:
911 case LFUN_BEGINNINGBUF:
912 case LFUN_BEGINNINGBUFSEL:
916 case LFUN_LAYOUT_TABULAR: {
917 InsetTabularMailer(*this).showDialog(bv);
920 case LFUN_INSET_DIALOG_UPDATE: {
921 InsetTabularMailer(*this).updateDialog(bv);
924 case LFUN_TABULAR_FEATURE:
925 if (!tabularFeatures(bv, arg))
926 result = UNDISPATCHED;
928 // insert file functions
929 case LFUN_FILE_INSERT_ASCII_PARA:
930 case LFUN_FILE_INSERT_ASCII:
932 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
935 if (insertAsciiString(bv, tmpstr, false))
938 result = UNDISPATCHED;
941 // cut and paste functions
943 if (!copySelection(bv))
947 recordUndo(bv, Undo::DELETE);
948 cutSelection(bv->buffer()->params);
957 case LFUN_PASTESELECTION:
959 string const clip(bv->getClipboard());
963 if (clip.find('\t') != string::npos) {
967 string::size_type len = clip.length();
968 string::size_type p = 0;
971 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
979 maxCols = max(cols, maxCols);
985 maxCols = max(cols, maxCols);
986 delete paste_tabular;
987 paste_tabular = new LyXTabular(bv->buffer()->params,
988 this, rows, maxCols);
989 string::size_type op = 0;
991 int cells = paste_tabular->getNumberOfCells();
993 while ((cell < cells) && (p < len) &&
994 (p = clip.find_first_of("\t\n", p)) != string::npos) {
999 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1004 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1005 while (cols++ < maxCols)
1013 // check for the last cell if there is no trailing '\n'
1014 if ((cell < cells) && (op < len))
1015 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1018 if (!insertAsciiString(bv, clip, true))
1021 // so that the clipboard is used and it goes on
1023 // and executes LFUN_PASTESELECTION in insettext!
1024 delete paste_tabular;
1029 if (hasPasteBuffer()) {
1030 recordUndo(bv, Undo::INSERT);
1035 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1037 // handle font changing stuff on selection before we lock the inset
1038 // in the default part!
1039 result = UNDISPATCHED;
1050 case LFUN_UNDERLINE:
1051 case LFUN_FONT_SIZE:
1052 if (bv->dispatch(FuncRequest(bv, action, arg)))
1053 result = DISPATCHED;
1059 // we try to activate the actual inset and put this event down to
1060 // the insets dispatch function.
1061 if (result == DISPATCHED || the_locking_inset)
1063 if (activateCellInset(bv)) {
1064 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1065 if (result == UNDISPATCHED || result >= FINISHED) {
1066 unlockInsetInInset(bv, the_locking_inset);
1067 // we need to update if this was requested before
1069 return UNDISPATCHED;
1078 if (result < FINISHED) {
1079 if (!the_locking_inset && bv->fitCursor())
1082 bv->unlockInset(this);
1087 int InsetTabular::latex(Buffer const * buf, ostream & os,
1088 LatexRunParams const & runparams) const
1090 return tabular.latex(buf, os, runparams);
1094 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1097 return tabular.ascii(buf, os, (int)parOwner()->params().depth(),
1099 return tabular.ascii(buf, os, 0, false, 0);
1103 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1105 return tabular.linuxdoc(buf,os);
1109 int InsetTabular::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1114 // if the table is inside a float it doesn't need the informaltable
1115 // wrapper. Search for it.
1116 for (master = owner();
1117 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1118 master = master->owner());
1121 os << "<informaltable>";
1126 ret += tabular.docbook(buf, os, mixcont);
1128 os << "</informaltable>";
1137 void InsetTabular::validate(LaTeXFeatures & features) const
1139 tabular.validate(features);
1143 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1146 // if we have a locking_inset we should have to check only this cell for
1147 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1148 // This is _really_ important (André)
1149 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1152 for (int j = 0; j < tabular.columns(); ++j) {
1156 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1157 tabular.getCellInset(actrow, j).metrics(m, dim);
1158 maxAsc = max(dim.asc, maxAsc);
1159 maxDesc = max(dim.des, maxDesc);
1161 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1162 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1163 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1169 for (int i = 0; i < tabular.rows(); ++i) {
1172 for (int j = 0; j < tabular.columns(); ++j) {
1173 if (tabular.isPartOfMultiColumn(i, j))
1179 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1180 tabular.getCellInset(cell).metrics(m, dim);
1181 maxAsc = max(maxAsc, dim.asc);
1182 maxDesc = max(maxDesc, dim.des);
1183 tabular.setWidthOfCell(cell, dim.wid);
1185 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1186 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1191 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1193 if (the_locking_inset) {
1194 the_locking_inset->getCursor(bv, x, y);
1199 y = cursory_ + InsetTabular::y();
1202 int desc = tabular.getDescentOfRow(actrow);
1204 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1206 y += ADD_TO_HEIGHT * 2;
1207 y += TEXT_TO_INSET_OFFSET;
1211 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1213 if (the_locking_inset) {
1214 the_locking_inset->getCursorPos(bv, x, y);
1217 x = cursorx_ - top_x;
1222 void InsetTabular::fitInsetCursor(BufferView * bv) const
1224 if (the_locking_inset) {
1225 the_locking_inset->fitInsetCursor(bv);
1230 int const asc = font_metrics::maxAscent(font);
1231 int const desc = font_metrics::maxDescent(font);
1234 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1238 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1241 actcell = actrow = actcol = 0;
1242 int ly = tabular.getDescentOfRow(actrow);
1244 // first search the right row
1245 while (ly < y && actrow + 1 < tabular.rows()) {
1246 cursory_ += tabular.getDescentOfRow(actrow) +
1247 tabular.getAscentOfRow(actrow + 1) +
1248 tabular.getAdditionalHeight(actrow + 1);
1250 ly = cursory_ + tabular.getDescentOfRow(actrow);
1252 actcell = tabular.getCellNumber(actrow, actcol);
1254 // now search the right column
1255 int lx = tabular.getWidthOfColumn(actcell) -
1256 tabular.getAdditionalWidth(actcell);
1258 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1259 lx += tabular.getWidthOfColumn(actcell + 1)
1260 + tabular.getAdditionalWidth(actcell);
1262 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1267 int InsetTabular::getCellXPos(int cell) const
1271 for (; !tabular.isFirstCellInRow(c); --c)
1273 int lx = tabular.getWidthOfColumn(cell);
1274 for (; c < cell; ++c)
1275 lx += tabular.getWidthOfColumn(c);
1277 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1281 void InsetTabular::resetPos(BufferView * bv) const
1283 #ifdef WITH_WARNINGS
1284 #warning This should be fixed in the right manner (20011128 Jug)
1286 // fast hack to fix infinite repaintings!
1287 if (in_reset_pos > 0)
1291 actcol = tabular.column_of_cell(actcell);
1294 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1295 if (tabular.isLastCellInRow(cell)) {
1296 cursory_ += tabular.getDescentOfRow(actrow) +
1297 tabular.getAscentOfRow(actrow + 1) +
1298 tabular.getAdditionalHeight(actrow + 1);
1303 if (the_locking_inset)
1307 // we need this only from here on!!!
1309 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1310 int new_x = getCellXPos(actcell);
1311 int old_x = cursorx_;
1314 // cursor.x(getCellXPos(actcell) + offset);
1315 if (actcol < tabular.columns() - 1 && scroll(false) &&
1316 tabular.getWidthOfTabular() < bv->workWidth()-20)
1320 } else if (the_locking_inset &&
1321 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1323 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1324 if (xx > bv->workWidth()-20) {
1325 scroll(bv, -(xx - bv->workWidth() + 60));
1327 } else if (xx < 20) {
1335 } else if (cursorx_ - offset > 20 &&
1336 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1337 > bv->workWidth() - 20) {
1338 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1340 } else if (cursorx_ - offset < 20) {
1341 scroll(bv, 20 - cursorx_ + offset);
1343 } else if (scroll() && top_x > 20 &&
1344 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1345 scroll(bv, old_x - cursorx_);
1348 if (the_locking_inset) {
1349 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1352 if ((!the_locking_inset ||
1353 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1354 actcell != oldcell) {
1355 InsetTabularMailer(*this).updateDialog(bv);
1362 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1364 if (lock && !old_locking_inset) {
1365 if (activateCellInset(bv))
1368 bool moved = isRightToLeft(bv)
1369 ? movePrevCell(bv) : moveNextCell(bv);
1371 return FINISHED_RIGHT;
1372 if (lock && activateCellInset(bv))
1376 return DISPATCHED_NOUPDATE;
1380 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1382 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1385 if (lock) { // behind the inset
1386 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1390 return DISPATCHED_NOUPDATE;
1394 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1396 int const ocell = actcell;
1397 actcell = tabular.getCellAbove(actcell);
1398 if (actcell == ocell) // we moved out of the inset
1404 if (old_locking_inset) {
1405 old_locking_inset->getCursorPos(bv, x, y);
1406 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1408 if (activateCellInset(bv, x, 0))
1411 return DISPATCHED_NOUPDATE;
1415 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1417 int const ocell = actcell;
1418 actcell = tabular.getCellBelow(actcell);
1419 if (actcell == ocell) // we moved out of the inset
1420 return FINISHED_DOWN;
1425 if (old_locking_inset) {
1426 old_locking_inset->getCursorPos(bv, x, y);
1427 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1429 if (activateCellInset(bv, x, 0))
1432 return DISPATCHED_NOUPDATE;
1436 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1438 if (isRightToLeft(bv)) {
1439 if (tabular.isFirstCellInRow(actcell)) {
1440 int row = tabular.row_of_cell(actcell);
1441 if (row == tabular.rows() - 1)
1443 actcell = tabular.getLastCellInRow(row);
1444 actcell = tabular.getCellBelow(actcell);
1451 if (tabular.isLastCell(actcell))
1456 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1457 isRightToLeftPar(bv->buffer()->params);
1458 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1465 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1467 if (isRightToLeft(bv)) {
1468 if (tabular.isLastCellInRow(actcell)) {
1469 int row = tabular.row_of_cell(actcell);
1472 actcell = tabular.getFirstCellInRow(row);
1473 actcell = tabular.getCellAbove(actcell);
1475 if (tabular.isLastCell(actcell))
1480 if (!actcell) // first cell
1485 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1486 isRightToLeftPar(bv->buffer()->params);
1487 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1494 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1498 setSelection(0, tabular.getNumberOfCells() - 1);
1500 if (hasSelection()) {
1501 recordUndo(bv, Undo::ATOMIC);
1502 bool const frozen = undo_frozen;
1505 // apply the fontchange on the whole selection
1510 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1511 for(int i = sel_row_start; i <= sel_row_end; ++i)
1512 for(int j = sel_col_start; j <= sel_col_end; ++j)
1513 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1521 if (the_locking_inset)
1522 the_locking_inset->setFont(bv, font, tall);
1526 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1528 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1531 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1532 string const tmp = tabularFeature[i].feature;
1534 if (tmp == what.substr(0, tmp.length())) {
1535 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1536 //tabularFeatures[i].feature.length())) {
1537 action = tabularFeature[i].action;
1541 if (action == LyXTabular::LAST_ACTION)
1545 ltrim(what.substr(tabularFeature[i].feature.length()));
1546 tabularFeatures(bv, action, val);
1552 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1553 string const & special, bool & flag)
1555 if (special == "dl_above") {
1558 } else if (special == "dl_below") {
1559 ltt.bottomDL = flag;
1561 } else if (special == "empty") {
1573 void InsetTabular::tabularFeatures(BufferView * bv,
1574 LyXTabular::Feature feature,
1575 string const & value)
1581 bool setLines = false;
1582 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1583 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1587 case LyXTabular::M_ALIGN_LEFT:
1588 case LyXTabular::ALIGN_LEFT:
1589 setAlign = LYX_ALIGN_LEFT;
1592 case LyXTabular::M_ALIGN_RIGHT:
1593 case LyXTabular::ALIGN_RIGHT:
1594 setAlign = LYX_ALIGN_RIGHT;
1597 case LyXTabular::M_ALIGN_CENTER:
1598 case LyXTabular::ALIGN_CENTER:
1599 setAlign = LYX_ALIGN_CENTER;
1602 case LyXTabular::ALIGN_BLOCK:
1603 setAlign = LYX_ALIGN_BLOCK;
1606 case LyXTabular::M_VALIGN_TOP:
1607 case LyXTabular::VALIGN_TOP:
1608 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1611 case LyXTabular::M_VALIGN_BOTTOM:
1612 case LyXTabular::VALIGN_BOTTOM:
1613 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1616 case LyXTabular::M_VALIGN_MIDDLE:
1617 case LyXTabular::VALIGN_MIDDLE:
1618 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1625 if (hasSelection()) {
1626 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1628 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1629 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1631 recordUndo(bv, Undo::ATOMIC);
1633 int row = tabular.row_of_cell(actcell);
1634 int column = tabular.column_of_cell(actcell);
1636 LyXTabular::ltType ltt;
1640 case LyXTabular::SET_PWIDTH:
1642 LyXLength const vallen(value);
1643 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1645 bool const update = (tmplen != vallen);
1646 tabular.setColumnPWidth(actcell, vallen);
1648 // We need this otherwise we won't resize
1649 // the insettext of the active cell (if any)
1650 // until later (see InsetText::do_resize)
1651 unlockInsetInInset(bv, the_locking_inset);
1656 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1657 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1658 else if (!vallen.zero()
1659 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1660 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1664 case LyXTabular::SET_MPWIDTH:
1666 LyXLength const vallen(value);
1667 LyXLength const & tmplen = tabular.getPWidth(actcell);
1669 bool const update = (tmplen != vallen);
1670 tabular.setMColumnPWidth(actcell, vallen);
1672 // We need this otherwise we won't resize
1673 // the insettext of the active cell (if any)
1674 // until later (see InsetText::do_resize)
1675 unlockInsetInInset(bv, the_locking_inset);
1680 case LyXTabular::SET_SPECIAL_COLUMN:
1681 case LyXTabular::SET_SPECIAL_MULTI:
1682 tabular.setAlignSpecial(actcell,value,feature);
1685 case LyXTabular::APPEND_ROW:
1686 // append the row into the tabular
1687 unlockInsetInInset(bv, the_locking_inset);
1688 tabular.appendRow(bv->buffer()->params, actcell);
1691 case LyXTabular::APPEND_COLUMN:
1692 // append the column into the tabular
1693 unlockInsetInInset(bv, the_locking_inset);
1694 tabular.appendColumn(bv->buffer()->params, actcell);
1695 actcell = tabular.getCellNumber(row, column);
1698 case LyXTabular::DELETE_ROW:
1699 unlockInsetInInset(bv, the_locking_inset);
1700 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1701 tabular.deleteRow(sel_row_start);
1703 if (sel_row_start >= tabular.rows())
1705 actcell = tabular.getCellNumber(sel_row_start, column);
1709 case LyXTabular::DELETE_COLUMN:
1710 unlockInsetInInset(bv, the_locking_inset);
1711 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1712 tabular.deleteColumn(sel_col_start);
1714 if (sel_col_start >= tabular.columns())
1716 actcell = tabular.getCellNumber(row, sel_col_start);
1720 case LyXTabular::M_TOGGLE_LINE_TOP:
1722 case LyXTabular::TOGGLE_LINE_TOP:
1724 bool lineSet = !tabular.topLine(actcell, flag);
1725 for (int i = sel_row_start; i <= sel_row_end; ++i)
1726 for (int j = sel_col_start; j <= sel_col_end; ++j)
1728 tabular.getCellNumber(i, j),
1734 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1736 case LyXTabular::TOGGLE_LINE_BOTTOM:
1738 bool lineSet = !tabular.bottomLine(actcell, flag);
1739 for (int i = sel_row_start; i <= sel_row_end; ++i)
1740 for (int j = sel_col_start; j <= sel_col_end; ++j)
1741 tabular.setBottomLine(
1742 tabular.getCellNumber(i, j),
1749 case LyXTabular::M_TOGGLE_LINE_LEFT:
1751 case LyXTabular::TOGGLE_LINE_LEFT:
1753 bool lineSet = !tabular.leftLine(actcell, flag);
1754 for (int i = sel_row_start; i <= sel_row_end; ++i)
1755 for (int j = sel_col_start; j <= sel_col_end; ++j)
1756 tabular.setLeftLine(
1757 tabular.getCellNumber(i,j),
1764 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1766 case LyXTabular::TOGGLE_LINE_RIGHT:
1768 bool lineSet = !tabular.rightLine(actcell, flag);
1769 for (int i = sel_row_start; i <= sel_row_end; ++i)
1770 for (int j = sel_col_start; j <= sel_col_end; ++j)
1771 tabular.setRightLine(
1772 tabular.getCellNumber(i,j),
1779 case LyXTabular::M_ALIGN_LEFT:
1780 case LyXTabular::M_ALIGN_RIGHT:
1781 case LyXTabular::M_ALIGN_CENTER:
1783 case LyXTabular::ALIGN_LEFT:
1784 case LyXTabular::ALIGN_RIGHT:
1785 case LyXTabular::ALIGN_CENTER:
1786 case LyXTabular::ALIGN_BLOCK:
1787 for (int i = sel_row_start; i <= sel_row_end; ++i)
1788 for (int j = sel_col_start; j <= sel_col_end; ++j)
1789 tabular.setAlignment(
1790 tabular.getCellNumber(i, j),
1796 case LyXTabular::M_VALIGN_TOP:
1797 case LyXTabular::M_VALIGN_BOTTOM:
1798 case LyXTabular::M_VALIGN_MIDDLE:
1800 case LyXTabular::VALIGN_TOP:
1801 case LyXTabular::VALIGN_BOTTOM:
1802 case LyXTabular::VALIGN_MIDDLE:
1803 for (int i = sel_row_start; i <= sel_row_end; ++i)
1804 for (int j = sel_col_start; j <= sel_col_end; ++j)
1805 tabular.setVAlignment(
1806 tabular.getCellNumber(i, j),
1811 case LyXTabular::MULTICOLUMN: {
1812 if (sel_row_start != sel_row_end) {
1813 #ifdef WITH_WARNINGS
1814 #warning Need I say it ? This is horrible.
1816 Alert::error(_("Error setting multicolumn"),
1817 _("You cannot set multicolumn vertically."));
1820 // just multicol for one Single Cell
1821 if (!hasSelection()) {
1822 // check wether we are completly in a multicol
1823 if (tabular.isMultiColumn(actcell))
1824 tabular.unsetMultiColumn(actcell);
1826 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1830 // we have a selection so this means we just add all this
1831 // cells to form a multicolumn cell
1835 if (sel_cell_start > sel_cell_end) {
1836 s_start = sel_cell_end;
1837 s_end = sel_cell_start;
1839 s_start = sel_cell_start;
1840 s_end = sel_cell_end;
1842 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1849 case LyXTabular::SET_ALL_LINES:
1851 case LyXTabular::UNSET_ALL_LINES:
1852 for (int i = sel_row_start; i <= sel_row_end; ++i)
1853 for (int j = sel_col_start; j <= sel_col_end; ++j)
1854 tabular.setAllLines(
1855 tabular.getCellNumber(i,j), setLines);
1859 case LyXTabular::SET_LONGTABULAR:
1860 tabular.setLongTabular(true);
1861 updateLocal(bv); // because this toggles displayed
1864 case LyXTabular::UNSET_LONGTABULAR:
1865 tabular.setLongTabular(false);
1866 updateLocal(bv); // because this toggles displayed
1869 case LyXTabular::SET_ROTATE_TABULAR:
1870 tabular.setRotateTabular(true);
1873 case LyXTabular::UNSET_ROTATE_TABULAR:
1874 tabular.setRotateTabular(false);
1877 case LyXTabular::SET_ROTATE_CELL:
1878 for (int i = sel_row_start; i <= sel_row_end; ++i)
1879 for (int j = sel_col_start; j<=sel_col_end; ++j)
1880 tabular.setRotateCell(
1881 tabular.getCellNumber(i, j), true);
1884 case LyXTabular::UNSET_ROTATE_CELL:
1885 for (int i = sel_row_start; i <= sel_row_end; ++i)
1886 for (int j = sel_col_start; j <= sel_col_end; ++j)
1887 tabular.setRotateCell(
1888 tabular.getCellNumber(i, j), false);
1891 case LyXTabular::SET_USEBOX: {
1892 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1893 if (val == tabular.getUsebox(actcell))
1894 val = LyXTabular::BOX_NONE;
1895 for (int i = sel_row_start; i <= sel_row_end; ++i)
1896 for (int j = sel_col_start; j <= sel_col_end; ++j)
1897 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1901 case LyXTabular::UNSET_LTFIRSTHEAD:
1903 case LyXTabular::SET_LTFIRSTHEAD:
1904 tabular.getRowOfLTFirstHead(row, ltt);
1905 checkLongtableSpecial(ltt, value, flag);
1906 tabular.setLTHead(row, flag, ltt, true);
1909 case LyXTabular::UNSET_LTHEAD:
1911 case LyXTabular::SET_LTHEAD:
1912 tabular.getRowOfLTHead(row, ltt);
1913 checkLongtableSpecial(ltt, value, flag);
1914 tabular.setLTHead(row, flag, ltt, false);
1917 case LyXTabular::UNSET_LTFOOT:
1919 case LyXTabular::SET_LTFOOT:
1920 tabular.getRowOfLTFoot(row, ltt);
1921 checkLongtableSpecial(ltt, value, flag);
1922 tabular.setLTFoot(row, flag, ltt, false);
1925 case LyXTabular::UNSET_LTLASTFOOT:
1927 case LyXTabular::SET_LTLASTFOOT:
1928 tabular.getRowOfLTLastFoot(row, ltt);
1929 checkLongtableSpecial(ltt, value, flag);
1930 tabular.setLTFoot(row, flag, ltt, true);
1933 case LyXTabular::SET_LTNEWPAGE: {
1934 bool what = !tabular.getLTNewPage(row);
1935 tabular.setLTNewPage(row, what);
1939 // dummy stuff just to avoid warnings
1940 case LyXTabular::LAST_ACTION:
1944 InsetTabularMailer(*this).updateDialog(bv);
1948 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1949 mouse_button::state button, bool behind)
1951 UpdatableInset & inset = tabular.getCellInset(actcell);
1954 x = inset.x() + inset.width();
1955 y = inset.descent();
1957 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1958 //inset_y = cursory_;
1959 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1960 if (!the_locking_inset)
1963 return the_locking_inset;
1967 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1968 mouse_button::state button)
1970 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1972 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1976 bool InsetTabular::insetHit(BufferView *, int x, int) const
1978 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1982 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1984 //resizeLyXText(bv, recursive);
1988 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1989 bool const recursive) const
1991 if (the_locking_inset)
1992 return the_locking_inset->getLyXText(bv, recursive);
1993 return InsetOld::getLyXText(bv, recursive);
1997 bool InsetTabular::showInsetDialog(BufferView * bv) const
1999 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2000 InsetTabularMailer(*this).showDialog(bv);
2005 void InsetTabular::openLayoutDialog(BufferView * bv) const
2007 if (the_locking_inset) {
2008 InsetTabular * inset = static_cast<InsetTabular *>
2009 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2011 inset->openLayoutDialog(bv);
2015 InsetTabularMailer(*this).showDialog(bv);
2020 // function returns an object as defined in func_status.h:
2021 // states OK, Unknown, Disabled, On, Off.
2023 FuncStatus InsetTabular::getStatus(string const & what) const
2025 int action = LyXTabular::LAST_ACTION;
2029 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2030 string const tmp = tabularFeature[i].feature;
2031 if (tmp == what.substr(0, tmp.length())) {
2032 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2033 // tabularFeatures[i].feature.length())) {
2034 action = tabularFeature[i].action;
2038 if (action == LyXTabular::LAST_ACTION) {
2040 return status.unknown(true);
2043 string const argument
2044 = ltrim(what.substr(tabularFeature[i].feature.length()));
2049 LyXTabular::ltType dummyltt;
2053 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2055 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2058 case LyXTabular::SET_PWIDTH:
2059 case LyXTabular::SET_MPWIDTH:
2060 case LyXTabular::SET_SPECIAL_COLUMN:
2061 case LyXTabular::SET_SPECIAL_MULTI:
2062 case LyXTabular::APPEND_ROW:
2063 case LyXTabular::APPEND_COLUMN:
2064 case LyXTabular::DELETE_ROW:
2065 case LyXTabular::DELETE_COLUMN:
2066 case LyXTabular::SET_ALL_LINES:
2067 case LyXTabular::UNSET_ALL_LINES:
2068 return status.clear();
2070 case LyXTabular::MULTICOLUMN:
2071 status.setOnOff(tabular.isMultiColumn(actcell));
2074 case LyXTabular::M_TOGGLE_LINE_TOP:
2076 case LyXTabular::TOGGLE_LINE_TOP:
2077 status.setOnOff(tabular.topLine(actcell, flag));
2080 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2082 case LyXTabular::TOGGLE_LINE_BOTTOM:
2083 status.setOnOff(tabular.bottomLine(actcell, flag));
2086 case LyXTabular::M_TOGGLE_LINE_LEFT:
2088 case LyXTabular::TOGGLE_LINE_LEFT:
2089 status.setOnOff(tabular.leftLine(actcell, flag));
2092 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2094 case LyXTabular::TOGGLE_LINE_RIGHT:
2095 status.setOnOff(tabular.rightLine(actcell, flag));
2098 case LyXTabular::M_ALIGN_LEFT:
2100 case LyXTabular::ALIGN_LEFT:
2101 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2104 case LyXTabular::M_ALIGN_RIGHT:
2106 case LyXTabular::ALIGN_RIGHT:
2107 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2110 case LyXTabular::M_ALIGN_CENTER:
2112 case LyXTabular::ALIGN_CENTER:
2113 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2116 case LyXTabular::ALIGN_BLOCK:
2117 status.disabled(tabular.getPWidth(actcell).zero());
2118 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2121 case LyXTabular::M_VALIGN_TOP:
2123 case LyXTabular::VALIGN_TOP:
2124 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2127 case LyXTabular::M_VALIGN_BOTTOM:
2129 case LyXTabular::VALIGN_BOTTOM:
2130 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2133 case LyXTabular::M_VALIGN_MIDDLE:
2135 case LyXTabular::VALIGN_MIDDLE:
2136 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2139 case LyXTabular::SET_LONGTABULAR:
2140 status.setOnOff(tabular.isLongTabular());
2143 case LyXTabular::UNSET_LONGTABULAR:
2144 status.setOnOff(!tabular.isLongTabular());
2147 case LyXTabular::SET_ROTATE_TABULAR:
2148 status.setOnOff(tabular.getRotateTabular());
2151 case LyXTabular::UNSET_ROTATE_TABULAR:
2152 status.setOnOff(!tabular.getRotateTabular());
2155 case LyXTabular::SET_ROTATE_CELL:
2156 status.setOnOff(tabular.getRotateCell(actcell));
2159 case LyXTabular::UNSET_ROTATE_CELL:
2160 status.setOnOff(!tabular.getRotateCell(actcell));
2163 case LyXTabular::SET_USEBOX:
2164 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2167 case LyXTabular::SET_LTFIRSTHEAD:
2168 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2171 case LyXTabular::SET_LTHEAD:
2172 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2175 case LyXTabular::SET_LTFOOT:
2176 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2179 case LyXTabular::SET_LTLASTFOOT:
2180 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2183 case LyXTabular::SET_LTNEWPAGE:
2184 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2189 status.disabled(true);
2196 void InsetTabular::getLabelList(std::vector<string> & list) const
2198 tabular.getLabelList(list);
2202 bool InsetTabular::copySelection(BufferView * bv)
2204 if (!hasSelection())
2207 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2208 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2209 if (sel_col_start > sel_col_end) {
2210 sel_col_start = sel_col_end;
2211 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2213 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2216 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2217 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2218 if (sel_row_start > sel_row_end)
2219 swap(sel_row_start, sel_row_end);
2221 delete paste_tabular;
2222 paste_tabular = new LyXTabular(bv->buffer()->params, this, tabular);
2224 for (int i = 0; i < sel_row_start; ++i)
2225 paste_tabular->deleteRow(0);
2227 int const rows = sel_row_end - sel_row_start + 1;
2228 while (paste_tabular->rows() > rows)
2229 paste_tabular->deleteRow(rows);
2231 paste_tabular->setTopLine(0, true, true);
2232 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2235 for (int i = 0; i < sel_col_start; ++i)
2236 paste_tabular->deleteColumn(0);
2238 int const columns = sel_col_end - sel_col_start + 1;
2239 while (paste_tabular->columns() > columns)
2240 paste_tabular->deleteColumn(columns);
2242 paste_tabular->setLeftLine(0, true, true);
2243 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2247 paste_tabular->ascii(bv->buffer(), os,
2248 (int)parOwner()->params().depth(), true, '\t');
2249 bv->stuffClipboard(STRCONV(os.str()));
2254 bool InsetTabular::pasteSelection(BufferView * bv)
2259 for (int r1 = 0, r2 = actrow;
2260 r1 < paste_tabular->rows() && r2 < tabular.rows();
2262 for (int c1 = 0, c2 = actcol;
2263 c1 < paste_tabular->columns() && c2 < tabular.columns();
2265 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2266 tabular.isPartOfMultiColumn(r2, c2))
2268 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2272 if (tabular.isPartOfMultiColumn(r2, c2)) {
2276 InsetText & inset = tabular.getCellInset(r2, c2);
2277 inset = paste_tabular->getCellInset(r1, c1);
2278 inset.setOwner(this);
2279 inset.deleteLyXText(bv);
2287 bool InsetTabular::cutSelection(BufferParams const & bp)
2289 if (!hasSelection())
2292 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2293 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2294 if (sel_col_start > sel_col_end) {
2295 sel_col_start = sel_col_end;
2296 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2298 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2301 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2302 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2304 if (sel_row_start > sel_row_end)
2305 swap(sel_row_start, sel_row_end);
2307 if (sel_cell_start > sel_cell_end)
2308 swap(sel_cell_start, sel_cell_end);
2310 for (int i = sel_row_start; i <= sel_row_end; ++i)
2311 for (int j = sel_col_start; j <= sel_col_end; ++j)
2312 tabular.getCellInset(tabular.getCellNumber(i, j)).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 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, actcell);
2543 return tabular.getPWidth(cell).zero();
2545 // well we didn't obviously find it so maybe our owner knows more
2547 return owner()->forceDefaultParagraphs(in);
2549 lyxerr << "If we're here there is really something strange going on!"
2555 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2558 if (buf.length() <= 0)
2564 string::size_type len = buf.length();
2565 string::size_type p = 0;
2567 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2575 maxCols = max(cols, maxCols);
2581 maxCols = max(cols, maxCols);
2582 LyXTabular * loctab;
2587 delete paste_tabular;
2588 paste_tabular = new LyXTabular(bv->buffer()->params,
2589 this, rows, maxCols);
2590 loctab = paste_tabular;
2599 string::size_type op = 0;
2600 int cells = loctab->getNumberOfCells();
2603 rows = loctab->rows();
2604 int const columns = loctab->columns();
2606 while (cell < cells && p < len && row < rows &&
2607 (p = buf.find_first_of("\t\n", p)) != string::npos)
2613 // we can only set this if we are not too far right
2614 if (cols < columns) {
2615 InsetText & inset = loctab->getCellInset(cell);
2616 LyXFont const font = inset.getLyXText(bv)->
2617 getFont(inset.paragraphs.begin(), 0);
2618 inset.setText(buf.substr(op, p - op), font);
2624 // we can only set this if we are not too far right
2625 if (cols < columns) {
2626 InsetText & inset = tabular.getCellInset(cell);
2627 LyXFont const font = inset.getLyXText(bv)->
2628 getFont(inset.paragraphs.begin(), 0);
2629 inset.setText(buf.substr(op, p - op), font);
2634 cell = loctab->getCellNumber(row, cols);
2640 // check for the last cell if there is no trailing '\n'
2641 if (cell < cells && op < len) {
2642 InsetText & inset = loctab->getCellInset(cell);
2643 LyXFont const font = inset.getLyXText(bv)->
2644 getFont(inset.paragraphs.begin(), 0);
2645 inset.setText(buf.substr(op, len - op), font);
2652 void InsetTabular::addPreview(PreviewLoader & loader) const
2654 int const rows = tabular.rows();
2655 int const columns = tabular.columns();
2656 for (int i = 0; i < rows; ++i)
2657 for (int j = 0; j < columns; ++j)
2658 tabular.getCellInset(i, j).addPreview(loader);
2662 string const InsetTabularMailer::name_("tabular");
2664 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2665 : inset_(const_cast<InsetTabular &>(inset))
2669 string const InsetTabularMailer::inset2string(Buffer const &) const
2671 return params2string(inset_);
2675 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2677 istringstream data(STRCONV(in));
2679 lex.setStream(data);
2681 #warning CHECK verify that this is a sane value to return.
2687 string const token = lex.getString();
2695 string const token = lex.getString();
2696 if (token != "\\active_cell")
2699 cell = lex.getInteger();
2702 // This is part of the inset proper that is usually swallowed
2703 // by Buffer::readInset
2706 string const token = lex.getString();
2707 if (token != "Tabular")
2714 Buffer const * const buffer = inset.buffer();
2716 inset.read(buffer, lex);
2718 // We can't set the active cell, but we can tell the frontend
2724 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2726 Buffer const * const buffer = inset.buffer();
2731 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2732 inset.write(buffer, data);
2733 data << "\\end_inset\n";
2734 return STRCONV(data.str());