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"
22 #include "FuncStatus.h"
25 #include "LaTeXFeatures.h"
29 #include "lyxlength.h"
32 #include "ParagraphParameters.h"
33 #include "undo_funcs.h"
34 #include "WordLangTuple.h"
35 #include "metricsinfo.h"
36 #include "paragraph_funcs.h"
38 #include "frontends/Alert.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
44 #include "support/LAssert.h"
45 #include "support/lstrings.h"
52 using namespace lyx::support;
53 using namespace lyx::graphics;
67 int const ADD_TO_HEIGHT = 2;
68 int const ADD_TO_TABULAR_WIDTH = 2;
71 LyXTabular * paste_tabular = 0;
74 struct TabularFeature {
75 LyXTabular::Feature action;
80 TabularFeature tabularFeature[] =
82 { LyXTabular::APPEND_ROW, "append-row" },
83 { LyXTabular::APPEND_COLUMN, "append-column" },
84 { LyXTabular::DELETE_ROW, "delete-row" },
85 { LyXTabular::DELETE_COLUMN, "delete-column" },
86 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
87 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
88 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
89 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
90 { LyXTabular::ALIGN_LEFT, "align-left" },
91 { LyXTabular::ALIGN_RIGHT, "align-right" },
92 { LyXTabular::ALIGN_CENTER, "align-center" },
93 { LyXTabular::ALIGN_BLOCK, "align-block" },
94 { LyXTabular::VALIGN_TOP, "valign-top" },
95 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
96 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
97 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
98 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
99 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
100 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
101 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
102 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
103 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
104 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
105 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
106 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
107 { LyXTabular::MULTICOLUMN, "multicolumn" },
108 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
109 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
110 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
111 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
112 { LyXTabular::SET_PWIDTH, "set-pwidth" },
113 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
114 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
115 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
116 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
117 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
118 { LyXTabular::SET_USEBOX, "set-usebox" },
119 { LyXTabular::SET_LTHEAD, "set-lthead" },
120 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
121 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
122 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
123 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
124 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
125 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
126 { LyXTabular::LAST_ACTION, "" }
130 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
131 bool operator()(TabularFeature & tf)
133 return tf.action == feature_;
136 LyXTabular::Feature feature_;
142 string const featureAsString(LyXTabular::Feature feature)
144 TabularFeature * it = tabularFeature;
145 TabularFeature * end = it +
146 sizeof(tabularFeature) / sizeof(TabularFeature);
147 it = std::find_if(it, end, FindFeature(feature));
148 return (it == end) ? string() : it->feature;
152 bool InsetTabular::hasPasteBuffer() const
154 return (paste_tabular != 0);
158 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
159 : tabular(buf.params, this, max(rows, 1), max(columns, 1)),
160 buffer_(&buf), cursorx_(0), cursory_(0)
162 // for now make it always display as display() inset
164 the_locking_inset = 0;
165 old_locking_inset = 0;
168 actrow = actcell = 0;
176 InsetTabular::InsetTabular(InsetTabular const & tab)
177 : UpdatableInset(tab),
178 tabular(tab.buffer_->params, this, tab.tabular),
179 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
181 the_locking_inset = 0;
182 old_locking_inset = 0;
185 actrow = actcell = 0;
193 InsetTabular::~InsetTabular()
195 InsetTabularMailer(*this).hideDialog();
199 auto_ptr<InsetBase> InsetTabular::clone() const
201 return auto_ptr<InsetBase>(new InsetTabular(*this));
205 Buffer const & InsetTabular::buffer() const
211 BufferView * InsetTabular::view() const
218 void InsetTabular::buffer(Buffer * b)
224 void InsetTabular::write(Buffer const & buf, ostream & os) const
226 os << "Tabular" << endl;
227 tabular.write(buf, os);
231 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
233 bool const old_format = (lex.getString() == "\\LyXTable");
235 tabular.read(buf, lex);
241 string token = lex.getString();
242 while (lex.isOK() && (token != "\\end_inset")) {
244 token = lex.getString();
246 if (token != "\\end_inset") {
247 lex.printError("Missing \\end_inset at this point. "
253 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
255 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
256 // mi.base.textwidth << "\n";
258 lyxerr << "InsetTabular::metrics: need bv" << endl;
262 calculate_dimensions_of_cells(mi);
264 dim.asc = tabular.getAscentOfRow(0);
265 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
266 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
271 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
273 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
275 BufferView * bv = pi.base.bv;
278 UpdatableInset::draw(pi, x, y);
286 x += ADD_TO_TABULAR_WIDTH;
289 first_visible_cell = -1;
290 for (int i = 0; i < tabular.rows(); ++i) {
292 cell = tabular.getCellNumber(i, 0);
293 if (y + tabular.getDescentOfRow(i) <= 0 &&
294 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
296 y += tabular.getDescentOfRow(i) +
297 tabular.getAscentOfRow(i + 1) +
298 tabular.getAdditionalHeight(i + 1);
301 for (int j = 0; j < tabular.columns(); ++j) {
302 if (nx > bv->workWidth())
304 if (tabular.isPartOfMultiColumn(i, j))
306 int cx = nx + tabular.getBeginningOfTextInCell(cell);
307 if (first_visible_cell < 0)
308 first_visible_cell = cell;
309 if (hasSelection()) {
310 drawCellSelection(pi.pain, nx, y, i, j, cell);
313 tabular.getCellInset(cell).draw(pi, cx, y);
314 drawCellLines(pi.pain, nx, y, i, cell);
315 nx += tabular.getWidthOfColumn(cell);
319 // Would be nice, but for some completely unfathomable reason,
320 // on a col resize to a new fixed width, even though the insettexts
321 // are resized, the cell isn't, but drawing all cells in a tall table
322 // has the desired effect somehow. Complete dark magic.
324 // avoiding drawing the rest of a long table is
325 // a pretty big speedup
326 if (y > bv->workHeight())
330 y += tabular.getDescentOfRow(i) +
331 tabular.getAscentOfRow(i + 1) +
332 tabular.getAdditionalHeight(i + 1);
337 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
338 int row, int cell) const
340 int x2 = x + tabular.getWidthOfColumn(cell);
343 if (!tabular.topAlreadyDrawn(cell)) {
344 on_off = !tabular.topLine(cell);
345 pain.line(x, y - tabular.getAscentOfRow(row),
346 x2, y - tabular.getAscentOfRow(row),
347 on_off ? LColor::tabularonoffline : LColor::tabularline,
348 on_off ? Painter::line_onoffdash : Painter::line_solid);
350 on_off = !tabular.bottomLine(cell);
351 pain.line(x, y + tabular.getDescentOfRow(row),
352 x2, y + tabular.getDescentOfRow(row),
353 on_off ? LColor::tabularonoffline : LColor::tabularline,
354 on_off ? Painter::line_onoffdash : Painter::line_solid);
355 if (!tabular.leftAlreadyDrawn(cell)) {
356 on_off = !tabular.leftLine(cell);
357 pain.line(x, y - tabular.getAscentOfRow(row),
358 x, y + tabular.getDescentOfRow(row),
359 on_off ? LColor::tabularonoffline : LColor::tabularline,
360 on_off ? Painter::line_onoffdash : Painter::line_solid);
362 on_off = !tabular.rightLine(cell);
363 pain.line(x2 - tabular.getAdditionalWidth(cell),
364 y - tabular.getAscentOfRow(row),
365 x2 - tabular.getAdditionalWidth(cell),
366 y + tabular.getDescentOfRow(row),
367 on_off ? LColor::tabularonoffline : LColor::tabularline,
368 on_off ? Painter::line_onoffdash : Painter::line_solid);
372 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
373 int row, int column, int cell) const
375 Assert(hasSelection());
376 int cs = tabular.column_of_cell(sel_cell_start);
377 int ce = tabular.column_of_cell(sel_cell_end);
380 cs = tabular.column_of_cell(sel_cell_end);
382 ce = tabular.right_column_of_cell(sel_cell_end);
385 int rs = tabular.row_of_cell(sel_cell_start);
386 int re = tabular.row_of_cell(sel_cell_end);
390 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
391 int w = tabular.getWidthOfColumn(cell);
392 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
393 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
394 w, h, LColor::selection);
399 string const InsetTabular::editMessage() const
401 return _("Opened table");
405 void InsetTabular::insetUnlock(BufferView * bv)
407 if (the_locking_inset) {
408 the_locking_inset->insetUnlock(bv);
410 the_locking_inset = 0;
415 if (scroll(false) || hasSelection()) {
424 void InsetTabular::updateLocal(BufferView * bv) const
426 bv->updateInset(this);
432 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
434 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
439 if (inset == &tabular.getCellInset(actcell)) {
440 lyxerr[Debug::INSETTEXT] << "OK" << endl;
441 the_locking_inset = &tabular.getCellInset(actcell);
444 } else if (!the_locking_inset) {
445 int const n = tabular.getNumberOfCells();
446 int const id = inset->id();
447 for (int i = 0; i < n; ++i) {
448 InsetText * in = &tabular.getCellInset(i);
451 the_locking_inset = in;
456 if (in->getInsetFromID(id)) {
458 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
459 return the_locking_inset->lockInsetInInset(bv, inset);
462 } else if (the_locking_inset && (the_locking_inset == inset)) {
463 lyxerr[Debug::INSETTEXT] << "OK" << endl;
465 } else if (the_locking_inset) {
466 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
467 return the_locking_inset->lockInsetInInset(bv, inset);
469 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
474 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
477 if (!the_locking_inset)
479 if (the_locking_inset == inset) {
480 the_locking_inset->insetUnlock(bv);
482 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
489 // this has to be here otherwise we don't redraw the cell!
490 the_locking_inset = 0;
493 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
494 if (inset->lyxCode() == TABULAR_CODE &&
495 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
496 InsetTabularMailer(*this).updateDialog(bv);
505 int InsetTabular::insetInInsetY() const
507 if (!the_locking_inset)
509 return inset_y + the_locking_inset->insetInInsetY();
513 UpdatableInset * InsetTabular::getLockingInset() const
515 return the_locking_inset ? the_locking_inset->getLockingInset() :
516 const_cast<InsetTabular *>(this);
520 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
524 if (the_locking_inset)
525 return the_locking_inset->getFirstLockingInsetOfType(c);
530 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
532 if (the_locking_inset)
533 return the_locking_inset->insertInset(bv, inset);
538 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
540 if (hasSelection() && cmd.button() == mouse_button::button3)
543 if (hasSelection()) {
545 updateLocal(cmd.view());
548 int const ocell = actcell;
549 BufferView * bv = cmd.view();
553 the_locking_inset = 0;
557 setPos(bv, cmd.x, cmd.y);
560 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
562 if ((ocell == actcell) && the_locking_inset && inset_hit) {
564 FuncRequest cmd1 = cmd;
567 the_locking_inset->localDispatch(cmd1);
571 if (the_locking_inset) {
572 the_locking_inset->insetUnlock(bv);
574 the_locking_inset = 0;
577 if (cmd.button() == mouse_button::button2) {
578 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
582 if (inset_hit && bv->theLockingInset()) {
583 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
584 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
587 FuncRequest cmd1 = cmd;
590 the_locking_inset->localDispatch(cmd1);
595 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
598 if (the_locking_inset) {
599 FuncRequest cmd1 = cmd;
602 ret = the_locking_inset->localDispatch(cmd1);
604 if (cmd.button() == mouse_button::button3 && !ret) {
605 InsetTabularMailer(*this).showDialog(cmd.view());
612 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
614 if (the_locking_inset) {
615 FuncRequest cmd1 = cmd;
618 the_locking_inset->localDispatch(cmd1);
622 BufferView * bv = cmd.view();
623 int const old_cell = actcell;
625 setPos(bv, cmd.x, cmd.y);
626 if (!hasSelection()) {
627 setSelection(actcell, actcell);
629 } else if (old_cell != actcell) {
630 setSelection(sel_cell_start, actcell);
636 InsetOld::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
638 // We need to save the value of the_locking_inset as the call to
639 // the_locking_inset->localDispatch might unlock it.
640 old_locking_inset = the_locking_inset;
641 RESULT result = UpdatableInset::localDispatch(cmd);
642 BufferView * bv = cmd.view();
644 if (cmd.action == LFUN_INSET_EDIT) {
646 if (!bv->lockInset(this)) {
647 lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
653 the_locking_inset = 0;
657 if (cmd.argument.size()) {
658 if (cmd.argument == "left") {
659 if (isRightToLeft(bv))
660 actcell = tabular.getLastCellInRow(0);
664 if (isRightToLeft(bv))
665 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
667 actcell = tabular.getNumberOfCells() - 1;
675 setPos(bv, cmd.x, cmd.y);
678 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
679 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
685 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
690 if (cmd.action < 0 && cmd.argument.empty())
693 bool hs = hasSelection();
696 // this one have priority over the locked InsetText, if we're not already
697 // inside another tabular then that one get's priority!
698 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
699 switch (cmd.action) {
700 case LFUN_MOUSE_PRESS:
704 case LFUN_MOUSE_MOTION:
705 lfunMouseMotion(cmd);
708 case LFUN_MOUSE_RELEASE:
709 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
711 case LFUN_CELL_BACKWARD:
712 case LFUN_CELL_FORWARD:
713 unlockInsetInInset(bv, the_locking_inset);
714 if (cmd.action == LFUN_CELL_FORWARD)
715 moveNextCell(bv, old_locking_inset != 0);
717 movePrevCell(bv, old_locking_inset != 0);
721 if (!the_locking_inset)
722 return DISPATCHED_NOUPDATE;
724 // this to avoid compiler warnings.
730 kb_action action = cmd.action;
731 string arg = cmd.argument;
732 if (the_locking_inset) {
733 result = the_locking_inset->localDispatch(cmd);
734 if (result == DISPATCHED_NOUPDATE) {
737 if (sc != scroll()) { // inset has been scrolled
741 } else if (result == DISPATCHED) {
744 } else if (result == FINISHED_UP) {
746 // Make sure to reset status message after
747 // exiting, e.g. math inset
748 bv->owner()->clearMessage();
749 } else if (result == FINISHED_DOWN) {
751 bv->owner()->clearMessage();
752 } else if (result == FINISHED_RIGHT) {
754 bv->owner()->clearMessage();
755 } else if (result == FINISHED) {
756 bv->owner()->clearMessage();
762 // --- Cursor Movements ----------------------------------
763 case LFUN_RIGHTSEL: {
764 int const start = hasSelection() ? sel_cell_start : actcell;
765 if (tabular.isLastCellInRow(actcell)) {
766 setSelection(start, actcell);
771 // if we are starting a selection, only select
772 // the current cell at the beginning
773 if (hasSelection()) {
774 moveRight(bv, false);
777 setSelection(start, end);
782 result = moveRight(bv);
788 int const start = hasSelection() ? sel_cell_start : actcell;
789 if (tabular.isFirstCellInRow(actcell)) {
790 setSelection(start, actcell);
795 // if we are starting a selection, only select
796 // the current cell at the beginning
797 if (hasSelection()) {
801 setSelection(start, end);
806 result = moveLeft(bv);
812 int const start = hasSelection() ? sel_cell_start : actcell;
813 int const ocell = actcell;
814 // if we are starting a selection, only select
815 // the current cell at the beginning
816 if (hasSelection()) {
818 if (ocell == sel_cell_end ||
819 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
820 setSelection(start, tabular.getCellBelow(sel_cell_end));
822 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
824 setSelection(start, start);
830 result = moveDown(bv, old_locking_inset != 0);
836 int const start = hasSelection() ? sel_cell_start : actcell;
837 int const ocell = actcell;
838 // if we are starting a selection, only select
839 // the current cell at the beginning
840 if (hasSelection()) {
842 if ((ocell == sel_cell_end) ||
843 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
844 setSelection(start, tabular.getCellAbove(sel_cell_end));
846 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
848 setSelection(start, start);
854 result = moveUp(bv, old_locking_inset != 0);
863 unlockInsetInInset(bv, the_locking_inset);
864 if (bv->top_y() + bv->painter().paperHeight() <
865 top_baseline + tabular.getHeightOfTabular())
867 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
868 actcell = tabular.getCellBelow(first_visible_cell) + column;
870 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
880 unlockInsetInInset(bv, the_locking_inset);
881 if (top_baseline < 0) {
882 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
883 if (top_baseline > 0)
886 actcell = tabular.getCellBelow(first_visible_cell) + column;
894 // none of these make sense for insettabular,
895 // but we must catch them to prevent any
896 // selection from being confused
900 case LFUN_WORDLEFTSEL:
902 case LFUN_WORDRIGHTSEL:
904 case LFUN_DOWN_PARAGRAPH:
905 case LFUN_DOWN_PARAGRAPHSEL:
906 case LFUN_UP_PARAGRAPH:
907 case LFUN_UP_PARAGRAPHSEL:
913 case LFUN_BEGINNINGBUF:
914 case LFUN_BEGINNINGBUFSEL:
918 case LFUN_LAYOUT_TABULAR: {
919 InsetTabularMailer(*this).showDialog(bv);
922 case LFUN_INSET_DIALOG_UPDATE: {
923 InsetTabularMailer(*this).updateDialog(bv);
926 case LFUN_TABULAR_FEATURE:
927 if (!tabularFeatures(bv, arg))
928 result = UNDISPATCHED;
930 // insert file functions
931 case LFUN_FILE_INSERT_ASCII_PARA:
932 case LFUN_FILE_INSERT_ASCII:
934 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
937 if (insertAsciiString(bv, tmpstr, false))
940 result = UNDISPATCHED;
943 // cut and paste functions
945 if (!copySelection(bv))
949 recordUndo(bv, Undo::DELETE);
950 cutSelection(bv->buffer()->params);
959 case LFUN_PASTESELECTION:
961 string const clip(bv->getClipboard());
965 if (clip.find('\t') != string::npos) {
969 string::size_type len = clip.length();
970 string::size_type p = 0;
973 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
981 maxCols = max(cols, maxCols);
987 maxCols = max(cols, maxCols);
988 delete paste_tabular;
989 paste_tabular = new LyXTabular(bv->buffer()->params,
990 this, rows, maxCols);
991 string::size_type op = 0;
993 int cells = paste_tabular->getNumberOfCells();
995 while ((cell < cells) && (p < len) &&
996 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1001 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1006 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1007 while (cols++ < maxCols)
1015 // check for the last cell if there is no trailing '\n'
1016 if ((cell < cells) && (op < len))
1017 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1020 if (!insertAsciiString(bv, clip, true))
1023 // so that the clipboard is used and it goes on
1025 // and executes LFUN_PASTESELECTION in insettext!
1026 delete paste_tabular;
1031 if (hasPasteBuffer()) {
1032 recordUndo(bv, Undo::INSERT);
1037 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1039 // handle font changing stuff on selection before we lock the inset
1040 // in the default part!
1041 result = UNDISPATCHED;
1052 case LFUN_UNDERLINE:
1053 case LFUN_FONT_SIZE:
1054 if (bv->dispatch(FuncRequest(bv, action, arg)))
1055 result = DISPATCHED;
1061 // we try to activate the actual inset and put this event down to
1062 // the insets dispatch function.
1063 if (result == DISPATCHED || the_locking_inset)
1065 if (activateCellInset(bv)) {
1066 result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1067 if (result == UNDISPATCHED || result >= FINISHED) {
1068 unlockInsetInInset(bv, the_locking_inset);
1069 // we need to update if this was requested before
1071 return UNDISPATCHED;
1080 if (result < FINISHED) {
1081 if (!the_locking_inset && bv->fitCursor())
1084 bv->unlockInset(this);
1089 int InsetTabular::latex(Buffer const & buf, ostream & os,
1090 LatexRunParams const & runparams) const
1092 return tabular.latex(buf, os, runparams);
1096 int InsetTabular::ascii(Buffer const & buf, ostream & os, int ll) const
1099 return tabular.ascii(buf, os, ownerPar(buf, this).params().depth(),
1101 return tabular.ascii(buf, os, 0, false, 0);
1105 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os) const
1107 return tabular.linuxdoc(buf,os);
1111 int InsetTabular::docbook(Buffer const & buf, ostream & os, bool mixcont) const
1116 // if the table is inside a float it doesn't need the informaltable
1117 // wrapper. Search for it.
1118 for (master = owner();
1119 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1120 master = master->owner());
1123 os << "<informaltable>";
1128 ret += tabular.docbook(buf, os, mixcont);
1130 os << "</informaltable>";
1139 void InsetTabular::validate(LaTeXFeatures & features) const
1141 tabular.validate(features);
1145 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1148 // if we have a locking_inset we should have to check only this cell for
1149 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1150 // This is _really_ important (André)
1151 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1154 for (int j = 0; j < tabular.columns(); ++j) {
1158 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1159 tabular.getCellInset(actrow, j).metrics(m, dim);
1160 maxAsc = max(dim.asc, maxAsc);
1161 maxDesc = max(dim.des, maxDesc);
1163 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1164 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1165 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1171 for (int i = 0; i < tabular.rows(); ++i) {
1174 for (int j = 0; j < tabular.columns(); ++j) {
1175 if (tabular.isPartOfMultiColumn(i, j))
1181 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1182 tabular.getCellInset(cell).metrics(m, dim);
1183 maxAsc = max(maxAsc, dim.asc);
1184 maxDesc = max(maxDesc, dim.des);
1185 tabular.setWidthOfCell(cell, dim.wid);
1187 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1188 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1193 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1195 if (the_locking_inset) {
1196 the_locking_inset->getCursor(bv, x, y);
1201 y = cursory_ + InsetTabular::y();
1204 int desc = tabular.getDescentOfRow(actrow);
1206 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1208 y += ADD_TO_HEIGHT * 2;
1209 y += TEXT_TO_INSET_OFFSET;
1213 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1215 if (the_locking_inset) {
1216 the_locking_inset->getCursorPos(bv, x, y);
1219 x = cursorx_ - top_x;
1224 void InsetTabular::fitInsetCursor(BufferView * bv) const
1226 if (the_locking_inset) {
1227 the_locking_inset->fitInsetCursor(bv);
1232 int const asc = font_metrics::maxAscent(font);
1233 int const desc = font_metrics::maxDescent(font);
1236 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1240 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1243 actcell = actrow = actcol = 0;
1244 int ly = tabular.getDescentOfRow(actrow);
1246 // first search the right row
1247 while (ly < y && actrow + 1 < tabular.rows()) {
1248 cursory_ += tabular.getDescentOfRow(actrow) +
1249 tabular.getAscentOfRow(actrow + 1) +
1250 tabular.getAdditionalHeight(actrow + 1);
1252 ly = cursory_ + tabular.getDescentOfRow(actrow);
1254 actcell = tabular.getCellNumber(actrow, actcol);
1256 // now search the right column
1257 int lx = tabular.getWidthOfColumn(actcell) -
1258 tabular.getAdditionalWidth(actcell);
1260 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1261 lx += tabular.getWidthOfColumn(actcell + 1)
1262 + tabular.getAdditionalWidth(actcell);
1264 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1269 int InsetTabular::getCellXPos(int cell) const
1273 for (; !tabular.isFirstCellInRow(c); --c)
1275 int lx = tabular.getWidthOfColumn(cell);
1276 for (; c < cell; ++c)
1277 lx += tabular.getWidthOfColumn(c);
1279 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1283 void InsetTabular::resetPos(BufferView * bv) const
1285 #ifdef WITH_WARNINGS
1286 #warning This should be fixed in the right manner (20011128 Jug)
1288 // fast hack to fix infinite repaintings!
1289 if (in_reset_pos > 0)
1293 actcol = tabular.column_of_cell(actcell);
1296 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1297 if (tabular.isLastCellInRow(cell)) {
1298 cursory_ += tabular.getDescentOfRow(actrow) +
1299 tabular.getAscentOfRow(actrow + 1) +
1300 tabular.getAdditionalHeight(actrow + 1);
1305 if (the_locking_inset)
1309 // we need this only from here on!!!
1311 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1312 int new_x = getCellXPos(actcell);
1313 int old_x = cursorx_;
1316 // cursor.x(getCellXPos(actcell) + offset);
1317 if (actcol < tabular.columns() - 1 && scroll(false) &&
1318 tabular.getWidthOfTabular() < bv->workWidth()-20)
1322 } else if (the_locking_inset &&
1323 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1325 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1326 if (xx > bv->workWidth()-20) {
1327 scroll(bv, -(xx - bv->workWidth() + 60));
1329 } else if (xx < 20) {
1337 } else if (cursorx_ - offset > 20 &&
1338 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1339 > bv->workWidth() - 20) {
1340 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1342 } else if (cursorx_ - offset < 20) {
1343 scroll(bv, 20 - cursorx_ + offset);
1345 } else if (scroll() && top_x > 20 &&
1346 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1347 scroll(bv, old_x - cursorx_);
1350 if (the_locking_inset) {
1351 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1354 if ((!the_locking_inset ||
1355 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1356 actcell != oldcell) {
1357 InsetTabularMailer(*this).updateDialog(bv);
1364 InsetOld::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1366 if (lock && !old_locking_inset) {
1367 if (activateCellInset(bv))
1370 bool moved = isRightToLeft(bv)
1371 ? movePrevCell(bv) : moveNextCell(bv);
1373 return FINISHED_RIGHT;
1374 if (lock && activateCellInset(bv))
1378 return DISPATCHED_NOUPDATE;
1382 InsetOld::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1384 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1387 if (lock) { // behind the inset
1388 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1392 return DISPATCHED_NOUPDATE;
1396 InsetOld::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1398 int const ocell = actcell;
1399 actcell = tabular.getCellAbove(actcell);
1400 if (actcell == ocell) // we moved out of the inset
1406 if (old_locking_inset) {
1407 old_locking_inset->getCursorPos(bv, x, y);
1408 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1410 if (activateCellInset(bv, x, 0))
1413 return DISPATCHED_NOUPDATE;
1417 InsetOld::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1419 int const ocell = actcell;
1420 actcell = tabular.getCellBelow(actcell);
1421 if (actcell == ocell) // we moved out of the inset
1422 return FINISHED_DOWN;
1427 if (old_locking_inset) {
1428 old_locking_inset->getCursorPos(bv, x, y);
1429 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1431 if (activateCellInset(bv, x, 0))
1434 return DISPATCHED_NOUPDATE;
1438 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1440 if (isRightToLeft(bv)) {
1441 if (tabular.isFirstCellInRow(actcell)) {
1442 int row = tabular.row_of_cell(actcell);
1443 if (row == tabular.rows() - 1)
1445 actcell = tabular.getLastCellInRow(row);
1446 actcell = tabular.getCellBelow(actcell);
1453 if (tabular.isLastCell(actcell))
1458 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1459 isRightToLeftPar(bv->buffer()->params);
1460 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1467 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1469 if (isRightToLeft(bv)) {
1470 if (tabular.isLastCellInRow(actcell)) {
1471 int row = tabular.row_of_cell(actcell);
1474 actcell = tabular.getFirstCellInRow(row);
1475 actcell = tabular.getCellAbove(actcell);
1477 if (tabular.isLastCell(actcell))
1482 if (!actcell) // first cell
1487 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1488 isRightToLeftPar(bv->buffer()->params);
1489 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1496 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1500 setSelection(0, tabular.getNumberOfCells() - 1);
1502 if (hasSelection()) {
1503 recordUndo(bv, Undo::ATOMIC);
1504 bool const frozen = undo_frozen;
1507 // apply the fontchange on the whole selection
1512 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1513 for(int i = sel_row_start; i <= sel_row_end; ++i)
1514 for(int j = sel_col_start; j <= sel_col_end; ++j)
1515 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1523 if (the_locking_inset)
1524 the_locking_inset->setFont(bv, font, tall);
1528 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1530 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1533 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1534 string const tmp = tabularFeature[i].feature;
1536 if (tmp == what.substr(0, tmp.length())) {
1537 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1538 //tabularFeatures[i].feature.length())) {
1539 action = tabularFeature[i].action;
1543 if (action == LyXTabular::LAST_ACTION)
1547 ltrim(what.substr(tabularFeature[i].feature.length()));
1548 tabularFeatures(bv, action, val);
1554 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1555 string const & special, bool & flag)
1557 if (special == "dl_above") {
1560 } else if (special == "dl_below") {
1561 ltt.bottomDL = flag;
1563 } else if (special == "empty") {
1575 void InsetTabular::tabularFeatures(BufferView * bv,
1576 LyXTabular::Feature feature,
1577 string const & value)
1583 bool setLines = false;
1584 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1585 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1589 case LyXTabular::M_ALIGN_LEFT:
1590 case LyXTabular::ALIGN_LEFT:
1591 setAlign = LYX_ALIGN_LEFT;
1594 case LyXTabular::M_ALIGN_RIGHT:
1595 case LyXTabular::ALIGN_RIGHT:
1596 setAlign = LYX_ALIGN_RIGHT;
1599 case LyXTabular::M_ALIGN_CENTER:
1600 case LyXTabular::ALIGN_CENTER:
1601 setAlign = LYX_ALIGN_CENTER;
1604 case LyXTabular::ALIGN_BLOCK:
1605 setAlign = LYX_ALIGN_BLOCK;
1608 case LyXTabular::M_VALIGN_TOP:
1609 case LyXTabular::VALIGN_TOP:
1610 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1613 case LyXTabular::M_VALIGN_BOTTOM:
1614 case LyXTabular::VALIGN_BOTTOM:
1615 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1618 case LyXTabular::M_VALIGN_MIDDLE:
1619 case LyXTabular::VALIGN_MIDDLE:
1620 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1627 if (hasSelection()) {
1628 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1630 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1631 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1633 recordUndo(bv, Undo::ATOMIC);
1635 int row = tabular.row_of_cell(actcell);
1636 int column = tabular.column_of_cell(actcell);
1638 LyXTabular::ltType ltt;
1642 case LyXTabular::SET_PWIDTH:
1644 LyXLength const vallen(value);
1645 LyXLength const & tmplen = tabular.getColumnPWidth(actcell);
1647 bool const update = (tmplen != vallen);
1648 tabular.setColumnPWidth(actcell, vallen);
1650 // We need this otherwise we won't resize
1651 // the insettext of the active cell (if any)
1652 // until later (see InsetText::do_resize)
1653 unlockInsetInInset(bv, the_locking_inset);
1658 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1659 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1660 else if (!vallen.zero()
1661 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1662 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1666 case LyXTabular::SET_MPWIDTH:
1668 LyXLength const vallen(value);
1669 LyXLength const & tmplen = tabular.getPWidth(actcell);
1671 bool const update = (tmplen != vallen);
1672 tabular.setMColumnPWidth(actcell, vallen);
1674 // We need this otherwise we won't resize
1675 // the insettext of the active cell (if any)
1676 // until later (see InsetText::do_resize)
1677 unlockInsetInInset(bv, the_locking_inset);
1682 case LyXTabular::SET_SPECIAL_COLUMN:
1683 case LyXTabular::SET_SPECIAL_MULTI:
1684 tabular.setAlignSpecial(actcell,value,feature);
1687 case LyXTabular::APPEND_ROW:
1688 // append the row into the tabular
1689 unlockInsetInInset(bv, the_locking_inset);
1690 tabular.appendRow(bv->buffer()->params, actcell);
1693 case LyXTabular::APPEND_COLUMN:
1694 // append the column into the tabular
1695 unlockInsetInInset(bv, the_locking_inset);
1696 tabular.appendColumn(bv->buffer()->params, actcell);
1697 actcell = tabular.getCellNumber(row, column);
1700 case LyXTabular::DELETE_ROW:
1701 unlockInsetInInset(bv, the_locking_inset);
1702 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1703 tabular.deleteRow(sel_row_start);
1705 if (sel_row_start >= tabular.rows())
1707 actcell = tabular.getCellNumber(sel_row_start, column);
1711 case LyXTabular::DELETE_COLUMN:
1712 unlockInsetInInset(bv, the_locking_inset);
1713 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1714 tabular.deleteColumn(sel_col_start);
1716 if (sel_col_start >= tabular.columns())
1718 actcell = tabular.getCellNumber(row, sel_col_start);
1722 case LyXTabular::M_TOGGLE_LINE_TOP:
1724 case LyXTabular::TOGGLE_LINE_TOP:
1726 bool lineSet = !tabular.topLine(actcell, flag);
1727 for (int i = sel_row_start; i <= sel_row_end; ++i)
1728 for (int j = sel_col_start; j <= sel_col_end; ++j)
1730 tabular.getCellNumber(i, j),
1736 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1738 case LyXTabular::TOGGLE_LINE_BOTTOM:
1740 bool lineSet = !tabular.bottomLine(actcell, flag);
1741 for (int i = sel_row_start; i <= sel_row_end; ++i)
1742 for (int j = sel_col_start; j <= sel_col_end; ++j)
1743 tabular.setBottomLine(
1744 tabular.getCellNumber(i, j),
1751 case LyXTabular::M_TOGGLE_LINE_LEFT:
1753 case LyXTabular::TOGGLE_LINE_LEFT:
1755 bool lineSet = !tabular.leftLine(actcell, flag);
1756 for (int i = sel_row_start; i <= sel_row_end; ++i)
1757 for (int j = sel_col_start; j <= sel_col_end; ++j)
1758 tabular.setLeftLine(
1759 tabular.getCellNumber(i,j),
1766 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1768 case LyXTabular::TOGGLE_LINE_RIGHT:
1770 bool lineSet = !tabular.rightLine(actcell, flag);
1771 for (int i = sel_row_start; i <= sel_row_end; ++i)
1772 for (int j = sel_col_start; j <= sel_col_end; ++j)
1773 tabular.setRightLine(
1774 tabular.getCellNumber(i,j),
1781 case LyXTabular::M_ALIGN_LEFT:
1782 case LyXTabular::M_ALIGN_RIGHT:
1783 case LyXTabular::M_ALIGN_CENTER:
1785 case LyXTabular::ALIGN_LEFT:
1786 case LyXTabular::ALIGN_RIGHT:
1787 case LyXTabular::ALIGN_CENTER:
1788 case LyXTabular::ALIGN_BLOCK:
1789 for (int i = sel_row_start; i <= sel_row_end; ++i)
1790 for (int j = sel_col_start; j <= sel_col_end; ++j)
1791 tabular.setAlignment(
1792 tabular.getCellNumber(i, j),
1798 case LyXTabular::M_VALIGN_TOP:
1799 case LyXTabular::M_VALIGN_BOTTOM:
1800 case LyXTabular::M_VALIGN_MIDDLE:
1802 case LyXTabular::VALIGN_TOP:
1803 case LyXTabular::VALIGN_BOTTOM:
1804 case LyXTabular::VALIGN_MIDDLE:
1805 for (int i = sel_row_start; i <= sel_row_end; ++i)
1806 for (int j = sel_col_start; j <= sel_col_end; ++j)
1807 tabular.setVAlignment(
1808 tabular.getCellNumber(i, j),
1813 case LyXTabular::MULTICOLUMN: {
1814 if (sel_row_start != sel_row_end) {
1815 #ifdef WITH_WARNINGS
1816 #warning Need I say it ? This is horrible.
1818 Alert::error(_("Error setting multicolumn"),
1819 _("You cannot set multicolumn vertically."));
1822 // just multicol for one Single Cell
1823 if (!hasSelection()) {
1824 // check wether we are completly in a multicol
1825 if (tabular.isMultiColumn(actcell))
1826 tabular.unsetMultiColumn(actcell);
1828 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1832 // we have a selection so this means we just add all this
1833 // cells to form a multicolumn cell
1837 if (sel_cell_start > sel_cell_end) {
1838 s_start = sel_cell_end;
1839 s_end = sel_cell_start;
1841 s_start = sel_cell_start;
1842 s_end = sel_cell_end;
1844 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1851 case LyXTabular::SET_ALL_LINES:
1853 case LyXTabular::UNSET_ALL_LINES:
1854 for (int i = sel_row_start; i <= sel_row_end; ++i)
1855 for (int j = sel_col_start; j <= sel_col_end; ++j)
1856 tabular.setAllLines(
1857 tabular.getCellNumber(i,j), setLines);
1861 case LyXTabular::SET_LONGTABULAR:
1862 tabular.setLongTabular(true);
1863 updateLocal(bv); // because this toggles displayed
1866 case LyXTabular::UNSET_LONGTABULAR:
1867 tabular.setLongTabular(false);
1868 updateLocal(bv); // because this toggles displayed
1871 case LyXTabular::SET_ROTATE_TABULAR:
1872 tabular.setRotateTabular(true);
1875 case LyXTabular::UNSET_ROTATE_TABULAR:
1876 tabular.setRotateTabular(false);
1879 case LyXTabular::SET_ROTATE_CELL:
1880 for (int i = sel_row_start; i <= sel_row_end; ++i)
1881 for (int j = sel_col_start; j<=sel_col_end; ++j)
1882 tabular.setRotateCell(
1883 tabular.getCellNumber(i, j), true);
1886 case LyXTabular::UNSET_ROTATE_CELL:
1887 for (int i = sel_row_start; i <= sel_row_end; ++i)
1888 for (int j = sel_col_start; j <= sel_col_end; ++j)
1889 tabular.setRotateCell(
1890 tabular.getCellNumber(i, j), false);
1893 case LyXTabular::SET_USEBOX: {
1894 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1895 if (val == tabular.getUsebox(actcell))
1896 val = LyXTabular::BOX_NONE;
1897 for (int i = sel_row_start; i <= sel_row_end; ++i)
1898 for (int j = sel_col_start; j <= sel_col_end; ++j)
1899 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1903 case LyXTabular::UNSET_LTFIRSTHEAD:
1905 case LyXTabular::SET_LTFIRSTHEAD:
1906 tabular.getRowOfLTFirstHead(row, ltt);
1907 checkLongtableSpecial(ltt, value, flag);
1908 tabular.setLTHead(row, flag, ltt, true);
1911 case LyXTabular::UNSET_LTHEAD:
1913 case LyXTabular::SET_LTHEAD:
1914 tabular.getRowOfLTHead(row, ltt);
1915 checkLongtableSpecial(ltt, value, flag);
1916 tabular.setLTHead(row, flag, ltt, false);
1919 case LyXTabular::UNSET_LTFOOT:
1921 case LyXTabular::SET_LTFOOT:
1922 tabular.getRowOfLTFoot(row, ltt);
1923 checkLongtableSpecial(ltt, value, flag);
1924 tabular.setLTFoot(row, flag, ltt, false);
1927 case LyXTabular::UNSET_LTLASTFOOT:
1929 case LyXTabular::SET_LTLASTFOOT:
1930 tabular.getRowOfLTLastFoot(row, ltt);
1931 checkLongtableSpecial(ltt, value, flag);
1932 tabular.setLTFoot(row, flag, ltt, true);
1935 case LyXTabular::SET_LTNEWPAGE: {
1936 bool what = !tabular.getLTNewPage(row);
1937 tabular.setLTNewPage(row, what);
1941 // dummy stuff just to avoid warnings
1942 case LyXTabular::LAST_ACTION:
1946 InsetTabularMailer(*this).updateDialog(bv);
1950 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1951 mouse_button::state button, bool behind)
1953 UpdatableInset & inset = tabular.getCellInset(actcell);
1956 x = inset.x() + inset.width();
1957 y = inset.descent();
1959 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1960 //inset_y = cursory_;
1961 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1962 if (!the_locking_inset)
1965 return the_locking_inset;
1969 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1970 mouse_button::state button)
1972 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1974 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1978 bool InsetTabular::insetHit(BufferView *, int x, int) const
1980 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1984 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
1986 //resizeLyXText(bv, recursive);
1990 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1991 bool const recursive) const
1993 if (the_locking_inset)
1994 return the_locking_inset->getLyXText(bv, recursive);
1995 return InsetOld::getLyXText(bv, recursive);
1999 bool InsetTabular::showInsetDialog(BufferView * bv) const
2001 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2002 InsetTabularMailer(*this).showDialog(bv);
2007 void InsetTabular::openLayoutDialog(BufferView * bv) const
2009 if (the_locking_inset) {
2010 InsetTabular * inset = static_cast<InsetTabular *>
2011 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2013 inset->openLayoutDialog(bv);
2017 InsetTabularMailer(*this).showDialog(bv);
2022 // function returns an object as defined in func_status.h:
2023 // states OK, Unknown, Disabled, On, Off.
2025 FuncStatus InsetTabular::getStatus(string const & what) const
2027 int action = LyXTabular::LAST_ACTION;
2031 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2032 string const tmp = tabularFeature[i].feature;
2033 if (tmp == what.substr(0, tmp.length())) {
2034 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2035 // tabularFeatures[i].feature.length())) {
2036 action = tabularFeature[i].action;
2040 if (action == LyXTabular::LAST_ACTION) {
2042 return status.unknown(true);
2045 string const argument
2046 = ltrim(what.substr(tabularFeature[i].feature.length()));
2051 LyXTabular::ltType dummyltt;
2055 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2057 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2060 case LyXTabular::SET_PWIDTH:
2061 case LyXTabular::SET_MPWIDTH:
2062 case LyXTabular::SET_SPECIAL_COLUMN:
2063 case LyXTabular::SET_SPECIAL_MULTI:
2064 case LyXTabular::APPEND_ROW:
2065 case LyXTabular::APPEND_COLUMN:
2066 case LyXTabular::DELETE_ROW:
2067 case LyXTabular::DELETE_COLUMN:
2068 case LyXTabular::SET_ALL_LINES:
2069 case LyXTabular::UNSET_ALL_LINES:
2070 return status.clear();
2072 case LyXTabular::MULTICOLUMN:
2073 status.setOnOff(tabular.isMultiColumn(actcell));
2076 case LyXTabular::M_TOGGLE_LINE_TOP:
2078 case LyXTabular::TOGGLE_LINE_TOP:
2079 status.setOnOff(tabular.topLine(actcell, flag));
2082 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2084 case LyXTabular::TOGGLE_LINE_BOTTOM:
2085 status.setOnOff(tabular.bottomLine(actcell, flag));
2088 case LyXTabular::M_TOGGLE_LINE_LEFT:
2090 case LyXTabular::TOGGLE_LINE_LEFT:
2091 status.setOnOff(tabular.leftLine(actcell, flag));
2094 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2096 case LyXTabular::TOGGLE_LINE_RIGHT:
2097 status.setOnOff(tabular.rightLine(actcell, flag));
2100 case LyXTabular::M_ALIGN_LEFT:
2102 case LyXTabular::ALIGN_LEFT:
2103 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2106 case LyXTabular::M_ALIGN_RIGHT:
2108 case LyXTabular::ALIGN_RIGHT:
2109 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2112 case LyXTabular::M_ALIGN_CENTER:
2114 case LyXTabular::ALIGN_CENTER:
2115 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2118 case LyXTabular::ALIGN_BLOCK:
2119 status.disabled(tabular.getPWidth(actcell).zero());
2120 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2123 case LyXTabular::M_VALIGN_TOP:
2125 case LyXTabular::VALIGN_TOP:
2126 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2129 case LyXTabular::M_VALIGN_BOTTOM:
2131 case LyXTabular::VALIGN_BOTTOM:
2132 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2135 case LyXTabular::M_VALIGN_MIDDLE:
2137 case LyXTabular::VALIGN_MIDDLE:
2138 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2141 case LyXTabular::SET_LONGTABULAR:
2142 status.setOnOff(tabular.isLongTabular());
2145 case LyXTabular::UNSET_LONGTABULAR:
2146 status.setOnOff(!tabular.isLongTabular());
2149 case LyXTabular::SET_ROTATE_TABULAR:
2150 status.setOnOff(tabular.getRotateTabular());
2153 case LyXTabular::UNSET_ROTATE_TABULAR:
2154 status.setOnOff(!tabular.getRotateTabular());
2157 case LyXTabular::SET_ROTATE_CELL:
2158 status.setOnOff(tabular.getRotateCell(actcell));
2161 case LyXTabular::UNSET_ROTATE_CELL:
2162 status.setOnOff(!tabular.getRotateCell(actcell));
2165 case LyXTabular::SET_USEBOX:
2166 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2169 case LyXTabular::SET_LTFIRSTHEAD:
2170 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2173 case LyXTabular::SET_LTHEAD:
2174 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2177 case LyXTabular::SET_LTFOOT:
2178 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2181 case LyXTabular::SET_LTLASTFOOT:
2182 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2185 case LyXTabular::SET_LTNEWPAGE:
2186 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2191 status.disabled(true);
2198 void InsetTabular::getLabelList(std::vector<string> & list) const
2200 tabular.getLabelList(list);
2204 bool InsetTabular::copySelection(BufferView * bv)
2206 if (!hasSelection())
2209 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2210 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2211 if (sel_col_start > sel_col_end) {
2212 sel_col_start = sel_col_end;
2213 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2215 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2218 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2219 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2220 if (sel_row_start > sel_row_end)
2221 swap(sel_row_start, sel_row_end);
2223 delete paste_tabular;
2224 paste_tabular = new LyXTabular(bv->buffer()->params, this, tabular);
2226 for (int i = 0; i < sel_row_start; ++i)
2227 paste_tabular->deleteRow(0);
2229 int const rows = sel_row_end - sel_row_start + 1;
2230 while (paste_tabular->rows() > rows)
2231 paste_tabular->deleteRow(rows);
2233 paste_tabular->setTopLine(0, true, true);
2234 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2237 for (int i = 0; i < sel_col_start; ++i)
2238 paste_tabular->deleteColumn(0);
2240 int const columns = sel_col_end - sel_col_start + 1;
2241 while (paste_tabular->columns() > columns)
2242 paste_tabular->deleteColumn(columns);
2244 paste_tabular->setLeftLine(0, true, true);
2245 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2249 paste_tabular->ascii(*bv->buffer(), os,
2250 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2251 bv->stuffClipboard(STRCONV(os.str()));
2256 bool InsetTabular::pasteSelection(BufferView * bv)
2261 for (int r1 = 0, r2 = actrow;
2262 r1 < paste_tabular->rows() && r2 < tabular.rows();
2264 for (int c1 = 0, c2 = actcol;
2265 c1 < paste_tabular->columns() && c2 < tabular.columns();
2267 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2268 tabular.isPartOfMultiColumn(r2, c2))
2270 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2274 if (tabular.isPartOfMultiColumn(r2, c2)) {
2278 InsetText & inset = tabular.getCellInset(r2, c2);
2279 inset = paste_tabular->getCellInset(r1, c1);
2280 inset.setOwner(this);
2281 inset.deleteLyXText(bv);
2289 bool InsetTabular::cutSelection(BufferParams const & bp)
2291 if (!hasSelection())
2294 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2295 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2296 if (sel_col_start > sel_col_end) {
2297 sel_col_start = sel_col_end;
2298 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2300 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2303 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2304 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2306 if (sel_row_start > sel_row_end)
2307 swap(sel_row_start, sel_row_end);
2309 if (sel_cell_start > sel_cell_end)
2310 swap(sel_cell_start, sel_cell_end);
2312 for (int i = sel_row_start; i <= sel_row_end; ++i)
2313 for (int j = sel_col_start; j <= sel_col_end; ++j)
2314 tabular.getCellInset(tabular.getCellNumber(i, j)).clear(bp.tracking_changes);
2319 bool InsetTabular::isRightToLeft(BufferView * bv)
2321 return bv->getParentLanguage(this)->RightToLeft();
2325 int InsetTabular::scroll(bool recursive) const
2327 int sx = UpdatableInset::scroll(false);
2329 if (recursive && the_locking_inset)
2330 sx += the_locking_inset->scroll(recursive);
2336 void InsetTabular::getSelection(int & srow, int & erow,
2337 int & scol, int & ecol) const
2339 int const start = hasSelection() ? sel_cell_start : actcell;
2340 int const end = hasSelection() ? sel_cell_end : actcell;
2342 srow = tabular.row_of_cell(start);
2343 erow = tabular.row_of_cell(end);
2347 scol = tabular.column_of_cell(start);
2348 ecol = tabular.column_of_cell(end);
2352 ecol = tabular.right_column_of_cell(end);
2356 ParagraphList * InsetTabular::getParagraphs(int i) const
2358 return (i < tabular.getNumberOfCells())
2359 ? tabular.getCellInset(i).getParagraphs(0)
2364 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2366 if (the_locking_inset)
2367 return the_locking_inset->cursor(bv);
2368 return InsetOld::cursor(bv);
2372 InsetOld * InsetTabular::getInsetFromID(int id_arg) const
2375 return const_cast<InsetTabular *>(this);
2377 for (int i = 0; i < tabular.rows(); ++i) {
2378 for (int j = 0; j < tabular.columns(); ++j) {
2379 InsetOld * inset = tabular.getCellInset(i, j).getInsetFromID(id_arg);
2389 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2391 if (the_locking_inset) {
2392 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2393 if (!word.word().empty())
2395 if (tabular.isLastCell(actcell)) {
2396 bv->unlockInset(const_cast<InsetTabular *>(this));
2397 return WordLangTuple();
2401 // otherwise we have to lock the next inset and ask for it's selecttion
2402 tabular.getCellInset(actcell)
2403 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2404 WordLangTuple word(selectNextWordInt(bv, value));
2405 if (!word.word().empty())
2411 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2413 // when entering this function the inset should be ALWAYS locked!
2414 Assert(the_locking_inset);
2416 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2417 if (!word.word().empty())
2420 if (tabular.isLastCell(actcell)) {
2421 bv->unlockInset(const_cast<InsetTabular *>(this));
2422 return WordLangTuple();
2425 // otherwise we have to lock the next inset and ask for it's selecttion
2427 tabular.getCellInset(actcell)
2428 .localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2429 return selectNextWordInt(bv, value);
2433 void InsetTabular::selectSelectedWord(BufferView * bv)
2435 if (the_locking_inset)
2436 the_locking_inset->selectSelectedWord(bv);
2440 void InsetTabular::markErased()
2442 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2443 tabular.getCellInset(cell).markErased();
2447 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2449 if (the_locking_inset) {
2450 if (the_locking_inset->nextChange(bv, length)) {
2454 if (tabular.isLastCell(actcell))
2458 InsetText & inset = tabular.getCellInset(actcell);
2459 if (inset.nextChange(bv, length)) {
2463 while (!tabular.isLastCell(actcell)) {
2465 InsetText & inset = tabular.getCellInset(actcell);
2466 if (inset.nextChange(bv, length)) {
2475 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2479 if (the_locking_inset) {
2480 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2484 if (tabular.isLastCell(actcell))
2488 InsetText & inset = tabular.getCellInset(cell);
2489 if (inset.searchForward(bv, str, cs, mw)) {
2493 while (!tabular.isLastCell(cell)) {
2495 InsetText & inset = tabular.getCellInset(cell);
2496 if (inset.searchForward(bv, str, cs, mw)) {
2505 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2508 int cell = tabular.getNumberOfCells();
2509 if (the_locking_inset) {
2510 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2519 InsetText & inset = tabular.getCellInset(cell);
2520 if (inset.searchBackward(bv, str, cs, mw)) {
2529 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2531 if (the_locking_inset)
2532 return the_locking_inset->insetAllowed(code);
2533 // we return true here because if the inset is not locked someone
2534 // wants to insert something in one of our insettexts and we generally
2540 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2542 const int cell = tabular.getCellFromInset(in, actcell);
2545 return tabular.getPWidth(cell).zero();
2547 // well we didn't obviously find it so maybe our owner knows more
2549 return owner()->forceDefaultParagraphs(in);
2551 lyxerr << "If we're here there is really something strange going on!"
2557 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2560 if (buf.length() <= 0)
2566 string::size_type len = buf.length();
2567 string::size_type p = 0;
2569 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2577 maxCols = max(cols, maxCols);
2583 maxCols = max(cols, maxCols);
2584 LyXTabular * loctab;
2589 delete paste_tabular;
2590 paste_tabular = new LyXTabular(bv->buffer()->params,
2591 this, rows, maxCols);
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(STRCONV(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";
2733 return STRCONV(data.str());