3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
27 #include "metricsinfo.h"
28 #include "outputparams.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "ParagraphParameters.h"
34 #include "frontends/Alert.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/Painter.h"
39 #include "support/std_sstream.h"
42 using lyx::graphics::PreviewLoader;
44 using lyx::support::ltrim;
45 using lyx::support::strToInt;
52 using std::istringstream;
54 using std::ostringstream;
59 int const ADD_TO_HEIGHT = 2;
60 int const ADD_TO_TABULAR_WIDTH = 2;
63 boost::scoped_ptr<LyXTabular> paste_tabular;
66 struct TabularFeature {
67 LyXTabular::Feature action;
72 TabularFeature tabularFeature[] =
74 { LyXTabular::APPEND_ROW, "append-row" },
75 { LyXTabular::APPEND_COLUMN, "append-column" },
76 { LyXTabular::DELETE_ROW, "delete-row" },
77 { LyXTabular::DELETE_COLUMN, "delete-column" },
78 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
79 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
80 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
81 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
82 { LyXTabular::ALIGN_LEFT, "align-left" },
83 { LyXTabular::ALIGN_RIGHT, "align-right" },
84 { LyXTabular::ALIGN_CENTER, "align-center" },
85 { LyXTabular::ALIGN_BLOCK, "align-block" },
86 { LyXTabular::VALIGN_TOP, "valign-top" },
87 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
88 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
89 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
90 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
91 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
92 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
93 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
94 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
95 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
96 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
97 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
98 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
99 { LyXTabular::MULTICOLUMN, "multicolumn" },
100 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
101 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
102 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
103 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
104 { LyXTabular::SET_PWIDTH, "set-pwidth" },
105 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
106 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
107 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
108 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
109 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
110 { LyXTabular::SET_USEBOX, "set-usebox" },
111 { LyXTabular::SET_LTHEAD, "set-lthead" },
112 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
113 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
114 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
115 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
116 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
117 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
118 { LyXTabular::LAST_ACTION, "" }
122 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
123 bool operator()(TabularFeature & tf)
125 return tf.action == feature_;
128 LyXTabular::Feature feature_;
134 string const featureAsString(LyXTabular::Feature feature)
136 TabularFeature * it = tabularFeature;
137 TabularFeature * end = it +
138 sizeof(tabularFeature) / sizeof(TabularFeature);
139 it = std::find_if(it, end, FindFeature(feature));
140 return (it == end) ? string() : it->feature;
144 bool InsetTabular::hasPasteBuffer() const
146 return (paste_tabular.get() != 0);
150 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
151 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
152 buffer_(&buf), cursorx_(0), cursory_(0)
154 tabular.setOwner(this);
155 // for now make it always display as display() inset
157 the_locking_inset = 0;
158 old_locking_inset = 0;
161 actrow = actcell = 0;
169 InsetTabular::InsetTabular(InsetTabular const & tab)
170 : UpdatableInset(tab), tabular(tab.tabular),
171 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
173 tabular.setOwner(this);
174 the_locking_inset = 0;
175 old_locking_inset = 0;
178 actrow = actcell = 0;
186 InsetTabular::~InsetTabular()
188 InsetTabularMailer(*this).hideDialog();
192 auto_ptr<InsetBase> InsetTabular::clone() const
194 return auto_ptr<InsetBase>(new InsetTabular(*this));
198 Buffer const & InsetTabular::buffer() const
204 BufferView * InsetTabular::view() const
211 void InsetTabular::buffer(Buffer * b)
217 void InsetTabular::write(Buffer const & buf, ostream & os) const
219 os << "Tabular" << endl;
220 tabular.write(buf, os);
224 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
226 bool const old_format = (lex.getString() == "\\LyXTable");
228 tabular.read(buf, lex);
234 string token = lex.getString();
235 while (lex.isOK() && (token != "\\end_inset")) {
237 token = lex.getString();
239 if (token != "\\end_inset") {
240 lex.printError("Missing \\end_inset at this point. "
246 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
248 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
249 // mi.base.textwidth << "\n";
251 lyxerr << "InsetTabular::metrics: need bv" << endl;
255 calculate_dimensions_of_cells(mi);
257 dim.asc = tabular.getAscentOfRow(0);
258 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
259 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
264 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
266 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
268 BufferView * bv = pi.base.bv;
271 UpdatableInset::draw(pi, x, y);
279 x += ADD_TO_TABULAR_WIDTH;
282 first_visible_cell = -1;
283 for (int i = 0; i < tabular.rows(); ++i) {
285 cell = tabular.getCellNumber(i, 0);
286 if (y + tabular.getDescentOfRow(i) <= 0 &&
287 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
289 y += tabular.getDescentOfRow(i) +
290 tabular.getAscentOfRow(i + 1) +
291 tabular.getAdditionalHeight(i + 1);
294 for (int j = 0; j < tabular.columns(); ++j) {
295 if (nx > bv->workWidth())
297 if (tabular.isPartOfMultiColumn(i, j))
299 int cx = nx + tabular.getBeginningOfTextInCell(cell);
300 if (first_visible_cell < 0)
301 first_visible_cell = cell;
302 if (hasSelection()) {
303 drawCellSelection(pi.pain, nx, y, i, j, cell);
306 tabular.getCellInset(cell).draw(pi, cx, y);
307 drawCellLines(pi.pain, nx, y, i, cell);
308 nx += tabular.getWidthOfColumn(cell);
312 // Would be nice, but for some completely unfathomable reason,
313 // on a col resize to a new fixed width, even though the insettexts
314 // are resized, the cell isn't, but drawing all cells in a tall table
315 // has the desired effect somehow. Complete dark magic.
317 // avoiding drawing the rest of a long table is
318 // a pretty big speedup
319 if (y > bv->workHeight())
323 y += tabular.getDescentOfRow(i) +
324 tabular.getAscentOfRow(i + 1) +
325 tabular.getAdditionalHeight(i + 1);
330 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
331 int row, int cell) const
333 int x2 = x + tabular.getWidthOfColumn(cell);
336 if (!tabular.topAlreadyDrawn(cell)) {
337 on_off = !tabular.topLine(cell);
338 pain.line(x, y - tabular.getAscentOfRow(row),
339 x2, y - tabular.getAscentOfRow(row),
340 on_off ? LColor::tabularonoffline : LColor::tabularline,
341 on_off ? Painter::line_onoffdash : Painter::line_solid);
343 on_off = !tabular.bottomLine(cell);
344 pain.line(x, y + tabular.getDescentOfRow(row),
345 x2, y + tabular.getDescentOfRow(row),
346 on_off ? LColor::tabularonoffline : LColor::tabularline,
347 on_off ? Painter::line_onoffdash : Painter::line_solid);
348 if (!tabular.leftAlreadyDrawn(cell)) {
349 on_off = !tabular.leftLine(cell);
350 pain.line(x, y - tabular.getAscentOfRow(row),
351 x, y + tabular.getDescentOfRow(row),
352 on_off ? LColor::tabularonoffline : LColor::tabularline,
353 on_off ? Painter::line_onoffdash : Painter::line_solid);
355 on_off = !tabular.rightLine(cell);
356 pain.line(x2 - tabular.getAdditionalWidth(cell),
357 y - tabular.getAscentOfRow(row),
358 x2 - tabular.getAdditionalWidth(cell),
359 y + tabular.getDescentOfRow(row),
360 on_off ? LColor::tabularonoffline : LColor::tabularline,
361 on_off ? Painter::line_onoffdash : Painter::line_solid);
365 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
366 int row, int column, int cell) const
368 BOOST_ASSERT(hasSelection());
369 int cs = tabular.column_of_cell(sel_cell_start);
370 int ce = tabular.column_of_cell(sel_cell_end);
373 cs = tabular.column_of_cell(sel_cell_end);
375 ce = tabular.right_column_of_cell(sel_cell_end);
378 int rs = tabular.row_of_cell(sel_cell_start);
379 int re = tabular.row_of_cell(sel_cell_end);
383 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
384 int w = tabular.getWidthOfColumn(cell);
385 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
386 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
387 w, h, LColor::selection);
392 string const InsetTabular::editMessage() const
394 return _("Opened table");
398 void InsetTabular::insetUnlock(BufferView * bv)
400 if (the_locking_inset) {
401 the_locking_inset->insetUnlock(bv);
403 the_locking_inset = 0;
408 if (scroll(false) || hasSelection()) {
417 void InsetTabular::updateLocal(BufferView * bv) const
419 bv->updateInset(this);
425 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
427 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
434 if (inset == &tabular.getCellInset(actcell)) {
435 lyxerr[Debug::INSETTEXT] << "OK" << endl;
436 the_locking_inset = &tabular.getCellInset(actcell);
441 if (!the_locking_inset) {
442 int const n = tabular.getNumberOfCells();
443 for (int i = 0; i < n; ++i) {
444 InsetText * in = &tabular.getCellInset(i);
447 the_locking_inset = in;
452 if (in->lockInsetInInset(bv, inset)) {
454 the_locking_inset = in;
464 if (the_locking_inset && (the_locking_inset == inset)) {
465 lyxerr[Debug::INSETTEXT] << "OK" << endl;
470 if (the_locking_inset) {
471 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
472 return the_locking_inset->lockInsetInInset(bv, inset);
475 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
480 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
483 if (!the_locking_inset)
485 if (the_locking_inset == inset) {
486 the_locking_inset->insetUnlock(bv);
488 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
495 // this has to be here otherwise we don't redraw the cell!
496 the_locking_inset = 0;
499 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
500 if (inset->lyxCode() == TABULAR_CODE &&
501 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
502 InsetTabularMailer(*this).updateDialog(bv);
511 int InsetTabular::insetInInsetY() const
513 if (!the_locking_inset)
515 return inset_y + the_locking_inset->insetInInsetY();
519 UpdatableInset * InsetTabular::getLockingInset() const
521 return the_locking_inset ? the_locking_inset->getLockingInset() :
522 const_cast<InsetTabular *>(this);
526 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
530 if (the_locking_inset)
531 return the_locking_inset->getFirstLockingInsetOfType(c);
536 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
538 if (the_locking_inset)
539 return the_locking_inset->insertInset(bv, inset);
544 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
546 if (hasSelection() && cmd.button() == mouse_button::button3)
549 if (hasSelection()) {
551 updateLocal(cmd.view());
554 int const ocell = actcell;
555 BufferView * bv = cmd.view();
559 the_locking_inset = 0;
563 setPos(bv, cmd.x, cmd.y);
566 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
568 if (ocell == actcell && the_locking_inset && inset_hit) {
570 FuncRequest cmd1 = cmd;
573 the_locking_inset->dispatch(cmd1);
577 if (the_locking_inset) {
578 the_locking_inset->insetUnlock(bv);
580 the_locking_inset = 0;
583 if (cmd.button() == mouse_button::button2) {
584 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
588 if (inset_hit && bv->theLockingInset()) {
589 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
590 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
593 FuncRequest cmd1 = cmd;
596 the_locking_inset->dispatch(cmd1);
601 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
603 DispatchResult ret(false);
604 if (the_locking_inset) {
605 FuncRequest cmd1 = cmd;
608 ret = the_locking_inset->dispatch(cmd1);
610 if (cmd.button() == mouse_button::button3 && ret == DispatchResult(false)) {
611 InsetTabularMailer(*this).showDialog(cmd.view());
614 return ret.dispatched() || ret.val() > FINISHED;
618 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
620 if (the_locking_inset) {
621 FuncRequest cmd1 = cmd;
624 the_locking_inset->dispatch(cmd1);
628 BufferView * bv = cmd.view();
629 int const old_cell = actcell;
631 setPos(bv, cmd.x, cmd.y);
632 if (!hasSelection()) {
633 setSelection(actcell, actcell);
635 } else if (old_cell != actcell) {
636 setSelection(sel_cell_start, actcell);
642 void InsetTabular::edit(BufferView * bv, bool left)
644 lyxerr << "InsetTabular::edit: " << this << " first cell: "
645 << &tabular.cell_info[0][0].inset << endl;
647 if (!bv->lockInset(this)) {
648 lyxerr << "InsetTabular::Cannot lock inset" << endl;
654 the_locking_inset = 0;
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 void InsetTabular::edit(BufferView * bv, int x, int y)
677 lyxerr << "InsetTabular::edit: " << this << " first cell "
678 << &tabular.cell_info[0][0].inset << endl;
680 if (!bv->lockInset(this)) {
681 lyxerr << "InsetTabular::Cannot lock inset" << endl;
687 the_locking_inset = 0;
694 if (insetHit(bv, x, y)) {
695 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
697 activateCellInset(bv, x - inset_x, y - inset_y);
703 InsetTabular::priv_dispatch(FuncRequest const & cmd,
704 idx_type & idx, pos_type & pos)
706 // We need to save the value of the_locking_inset as the call to
707 // the_locking_inset->localDispatch might unlock it.
708 old_locking_inset = the_locking_inset;
709 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
710 BufferView * bv = cmd.view();
712 if (result.dispatched()) {
717 if (cmd.action < 0 && cmd.argument.empty())
718 return DispatchResult(false, FINISHED);
720 bool hs = hasSelection();
722 result = DispatchResult(true, true);
723 // this one have priority over the locked InsetText, if we're not already
724 // inside another tabular then that one get's priority!
725 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
726 switch (cmd.action) {
727 case LFUN_MOUSE_PRESS:
729 return DispatchResult(true, true);
731 case LFUN_MOUSE_MOTION:
732 lfunMouseMotion(cmd);
733 return DispatchResult(true, true);
735 case LFUN_MOUSE_RELEASE:
736 return DispatchResult(lfunMouseRelease(cmd));
738 case LFUN_CELL_BACKWARD:
739 case LFUN_CELL_FORWARD:
740 unlockInsetInInset(bv, the_locking_inset);
741 if (cmd.action == LFUN_CELL_FORWARD)
742 moveNextCell(bv, old_locking_inset != 0);
744 movePrevCell(bv, old_locking_inset != 0);
748 if (!the_locking_inset)
749 return DispatchResult(true);
751 // this to avoid compiler warnings.
757 kb_action action = cmd.action;
758 string arg = cmd.argument;
759 if (the_locking_inset) {
760 result = the_locking_inset->dispatch(cmd);
761 if (result.dispatched()) {
762 if (result.update()) {
765 int const sc = scroll();
767 if (sc != scroll()) {
768 // inset has been scrolled
773 } else if (result.val() == FINISHED_UP) {
775 // Make sure to reset status message after
776 // exiting, e.g. math inset
777 bv->owner()->clearMessage();
778 } else if (result.val() == FINISHED_DOWN) {
780 bv->owner()->clearMessage();
781 } else if (result.val() == FINISHED_RIGHT) {
783 bv->owner()->clearMessage();
784 } else if (result.val() == FINISHED) {
785 bv->owner()->clearMessage();
789 result = DispatchResult(true, true);
791 // --- Cursor Movements ----------------------------------
792 case LFUN_RIGHTSEL: {
793 int const start = hasSelection() ? sel_cell_start : actcell;
794 if (tabular.isLastCellInRow(actcell)) {
795 setSelection(start, actcell);
800 // if we are starting a selection, only select
801 // the current cell at the beginning
802 if (hasSelection()) {
803 moveRight(bv, false);
806 setSelection(start, end);
811 result = moveRight(bv);
817 int const start = hasSelection() ? sel_cell_start : actcell;
818 if (tabular.isFirstCellInRow(actcell)) {
819 setSelection(start, actcell);
824 // if we are starting a selection, only select
825 // the current cell at the beginning
826 if (hasSelection()) {
830 setSelection(start, end);
835 result = moveLeft(bv);
841 int const start = hasSelection() ? sel_cell_start : actcell;
842 int const ocell = actcell;
843 // if we are starting a selection, only select
844 // the current cell at the beginning
845 if (hasSelection()) {
847 if (ocell == sel_cell_end ||
848 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
849 setSelection(start, tabular.getCellBelow(sel_cell_end));
851 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
853 setSelection(start, start);
859 result = moveDown(bv, old_locking_inset != 0);
865 int const start = hasSelection() ? sel_cell_start : actcell;
866 int const ocell = actcell;
867 // if we are starting a selection, only select
868 // the current cell at the beginning
869 if (hasSelection()) {
871 if ((ocell == sel_cell_end) ||
872 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
873 setSelection(start, tabular.getCellAbove(sel_cell_end));
875 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
877 setSelection(start, start);
883 result = moveUp(bv, old_locking_inset != 0);
892 unlockInsetInInset(bv, the_locking_inset);
893 if (bv->top_y() + bv->painter().paperHeight() <
894 top_baseline + tabular.getHeightOfTabular())
896 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
897 actcell = tabular.getCellBelow(first_visible_cell) + column;
899 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
909 unlockInsetInInset(bv, the_locking_inset);
910 if (top_baseline < 0) {
911 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
912 if (top_baseline > 0)
915 actcell = tabular.getCellBelow(first_visible_cell) + column;
923 // none of these make sense for insettabular,
924 // but we must catch them to prevent any
925 // selection from being confused
929 case LFUN_WORDLEFTSEL:
931 case LFUN_WORDRIGHTSEL:
933 case LFUN_DOWN_PARAGRAPH:
934 case LFUN_DOWN_PARAGRAPHSEL:
935 case LFUN_UP_PARAGRAPH:
936 case LFUN_UP_PARAGRAPHSEL:
942 case LFUN_BEGINNINGBUF:
943 case LFUN_BEGINNINGBUFSEL:
947 case LFUN_LAYOUT_TABULAR:
948 InsetTabularMailer(*this).showDialog(bv);
950 case LFUN_INSET_DIALOG_UPDATE:
951 InsetTabularMailer(*this).updateDialog(bv);
953 case LFUN_TABULAR_FEATURE:
954 if (!tabularFeatures(bv, arg))
955 result = DispatchResult(false);
957 // insert file functions
958 case LFUN_FILE_INSERT_ASCII_PARA:
959 case LFUN_FILE_INSERT_ASCII:
961 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
964 if (insertAsciiString(bv, tmpstr, false))
967 result = DispatchResult(false);
970 // cut and paste functions
972 if (!copySelection(bv))
976 recordUndo(bv, Undo::DELETE);
977 cutSelection(bv->buffer()->params());
986 case LFUN_PASTESELECTION:
988 string const clip = bv->getClipboard();
992 if (clip.find('\t') != string::npos) {
996 string::size_type len = clip.length();
997 string::size_type p = 0;
1000 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1008 maxCols = max(cols, maxCols);
1014 maxCols = max(cols, maxCols);
1016 paste_tabular.reset(
1017 new LyXTabular(bv->buffer()->params(),
1018 this, rows, maxCols)
1021 string::size_type op = 0;
1023 int cells = paste_tabular->getNumberOfCells();
1025 while ((cell < cells) && (p < len) &&
1026 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1031 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1036 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1037 while (cols++ < maxCols)
1045 // check for the last cell if there is no trailing '\n'
1046 if ((cell < cells) && (op < len))
1047 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1050 if (!insertAsciiString(bv, clip, true))
1053 // so that the clipboard is used and it goes on
1055 // and executes LFUN_PASTESELECTION in insettext!
1056 paste_tabular.reset();
1060 if (hasPasteBuffer()) {
1061 recordUndo(bv, Undo::INSERT);
1066 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1068 // handle font changing stuff on selection before we lock the inset
1069 // in the default part!
1070 result = DispatchResult(false);
1081 case LFUN_UNDERLINE:
1082 case LFUN_FONT_SIZE:
1083 if (bv->dispatch(FuncRequest(bv, action, arg)))
1084 result = DispatchResult(true, true);
1090 // we try to activate the actual inset and put this event down to
1091 // the insets dispatch function.
1092 if (result.dispatched() || the_locking_inset)
1094 if (activateCellInset(bv)) {
1095 result = the_locking_inset->dispatch(FuncRequest(bv, action, arg));
1096 if (!result.dispatched()) {
1097 unlockInsetInInset(bv, the_locking_inset);
1098 // we need to update if this was requested before
1100 return DispatchResult(false);
1110 if (result.val() >= FINISHED)
1111 bv->unlockInset(this);
1112 else if (!the_locking_inset && bv->fitCursor())
1119 int InsetTabular::latex(Buffer const & buf, ostream & os,
1120 OutputParams const & runparams) const
1122 return tabular.latex(buf, os, runparams);
1126 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1127 OutputParams const & runparams) const
1129 if (runparams.linelen > 0)
1130 return tabular.plaintext(buf, os, runparams,
1131 ownerPar(buf, this).params().depth(),
1133 return tabular.plaintext(buf, os, runparams, 0, false, 0);
1137 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1138 OutputParams const & runparams) const
1140 return tabular.linuxdoc(buf,os, runparams);
1144 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1145 OutputParams const & runparams) const
1150 // if the table is inside a float it doesn't need the informaltable
1151 // wrapper. Search for it.
1152 for (master = owner();
1153 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1154 master = master->owner());
1157 os << "<informaltable>";
1158 if (runparams.mixed_content)
1162 ret += tabular.docbook(buf, os, runparams);
1164 os << "</informaltable>";
1165 if (runparams.mixed_content)
1173 void InsetTabular::validate(LaTeXFeatures & features) const
1175 tabular.validate(features);
1179 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1182 // if we have a locking_inset we should have to check only this cell for
1183 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1184 // This is _really_ important (André)
1185 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1188 for (int j = 0; j < tabular.columns(); ++j) {
1192 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1193 tabular.getCellInset(actrow, j).metrics(m, dim);
1194 maxAsc = max(dim.asc, maxAsc);
1195 maxDesc = max(dim.des, maxDesc);
1197 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1198 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1199 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1205 for (int i = 0; i < tabular.rows(); ++i) {
1208 for (int j = 0; j < tabular.columns(); ++j) {
1209 if (tabular.isPartOfMultiColumn(i, j))
1215 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1216 tabular.getCellInset(cell).metrics(m, dim);
1217 maxAsc = max(maxAsc, dim.asc);
1218 maxDesc = max(maxDesc, dim.des);
1219 tabular.setWidthOfCell(cell, dim.wid);
1221 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1222 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1227 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1229 if (the_locking_inset) {
1230 the_locking_inset->getCursor(bv, x, y);
1235 y = cursory_ + InsetTabular::y();
1238 int desc = tabular.getDescentOfRow(actrow);
1240 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1242 y += ADD_TO_HEIGHT * 2;
1243 y += TEXT_TO_INSET_OFFSET;
1247 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1249 if (the_locking_inset) {
1250 the_locking_inset->getCursorPos(bv, x, y);
1253 x = cursorx_ - top_x;
1258 void InsetTabular::fitInsetCursor(BufferView * bv) const
1260 if (the_locking_inset) {
1261 the_locking_inset->fitInsetCursor(bv);
1266 int const asc = font_metrics::maxAscent(font);
1267 int const desc = font_metrics::maxDescent(font);
1270 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1274 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1277 actcell = actrow = actcol = 0;
1278 int ly = tabular.getDescentOfRow(actrow);
1280 // first search the right row
1281 while (ly < y && actrow + 1 < tabular.rows()) {
1282 cursory_ += tabular.getDescentOfRow(actrow) +
1283 tabular.getAscentOfRow(actrow + 1) +
1284 tabular.getAdditionalHeight(actrow + 1);
1286 ly = cursory_ + tabular.getDescentOfRow(actrow);
1288 actcell = tabular.getCellNumber(actrow, actcol);
1290 // now search the right column
1291 int lx = tabular.getWidthOfColumn(actcell) -
1292 tabular.getAdditionalWidth(actcell);
1294 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1295 lx += tabular.getWidthOfColumn(actcell + 1)
1296 + tabular.getAdditionalWidth(actcell);
1298 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1303 int InsetTabular::getCellXPos(int cell) const
1307 for (; !tabular.isFirstCellInRow(c); --c)
1309 int lx = tabular.getWidthOfColumn(cell);
1310 for (; c < cell; ++c)
1311 lx += tabular.getWidthOfColumn(c);
1313 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1317 void InsetTabular::resetPos(BufferView * bv) const
1319 #ifdef WITH_WARNINGS
1320 #warning This should be fixed in the right manner (20011128 Jug)
1322 // fast hack to fix infinite repaintings!
1323 if (in_reset_pos > 0)
1327 actcol = tabular.column_of_cell(actcell);
1330 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1331 if (tabular.isLastCellInRow(cell)) {
1332 cursory_ += tabular.getDescentOfRow(actrow) +
1333 tabular.getAscentOfRow(actrow + 1) +
1334 tabular.getAdditionalHeight(actrow + 1);
1339 if (the_locking_inset)
1343 // we need this only from here on!!!
1345 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1346 int new_x = getCellXPos(actcell);
1347 int old_x = cursorx_;
1350 // cursor.x(getCellXPos(actcell) + offset);
1351 if (actcol < tabular.columns() - 1 && scroll(false) &&
1352 tabular.getWidthOfTabular() < bv->workWidth()-20)
1356 } else if (the_locking_inset &&
1357 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1359 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1360 if (xx > bv->workWidth()-20) {
1361 scroll(bv, -(xx - bv->workWidth() + 60));
1363 } else if (xx < 20) {
1371 } else if (cursorx_ - offset > 20 &&
1372 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1373 > bv->workWidth() - 20) {
1374 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1376 } else if (cursorx_ - offset < 20) {
1377 scroll(bv, 20 - cursorx_ + offset);
1379 } else if (scroll() && top_x > 20 &&
1380 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1381 scroll(bv, old_x - cursorx_);
1384 if (the_locking_inset) {
1385 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1388 if ((!the_locking_inset ||
1389 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1390 actcell != oldcell) {
1391 InsetTabularMailer(*this).updateDialog(bv);
1398 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1400 if (lock && !old_locking_inset) {
1401 if (activateCellInset(bv))
1402 return DispatchResult(true, true);
1404 bool moved = isRightToLeft(bv)
1405 ? movePrevCell(bv) : moveNextCell(bv);
1407 return DispatchResult(false, FINISHED_RIGHT);
1408 if (lock && activateCellInset(bv))
1409 return DispatchResult(true, true);
1412 return DispatchResult(true);
1416 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1418 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1420 return DispatchResult(false, FINISHED);
1422 if (lock && activateCellInset(bv, 0, 0, true))
1423 return DispatchResult(true, true);
1425 return DispatchResult(true);
1429 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1431 int const ocell = actcell;
1432 actcell = tabular.getCellAbove(actcell);
1433 if (actcell == ocell) // we moved out of the inset
1434 return DispatchResult(false, FINISHED_UP);
1439 if (old_locking_inset) {
1440 old_locking_inset->getCursorPos(bv, x, y);
1441 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1443 if (activateCellInset(bv, x, 0))
1444 return DispatchResult(true, true);
1446 return DispatchResult(true);
1450 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1452 int const ocell = actcell;
1453 actcell = tabular.getCellBelow(actcell);
1454 if (actcell == ocell) // we moved out of the inset
1455 return DispatchResult(false, FINISHED_DOWN);
1460 if (old_locking_inset) {
1461 old_locking_inset->getCursorPos(bv, x, y);
1462 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1464 if (activateCellInset(bv, x, 0))
1465 return DispatchResult(true, true);
1467 return DispatchResult(true);
1471 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1473 if (isRightToLeft(bv)) {
1474 if (tabular.isFirstCellInRow(actcell)) {
1475 int row = tabular.row_of_cell(actcell);
1476 if (row == tabular.rows() - 1)
1478 actcell = tabular.getLastCellInRow(row);
1479 actcell = tabular.getCellBelow(actcell);
1486 if (tabular.isLastCell(actcell))
1491 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1492 isRightToLeftPar(bv->buffer()->params());
1493 activateCellInset(bv, 0, 0, !rtl);
1500 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1502 if (isRightToLeft(bv)) {
1503 if (tabular.isLastCellInRow(actcell)) {
1504 int row = tabular.row_of_cell(actcell);
1507 actcell = tabular.getFirstCellInRow(row);
1508 actcell = tabular.getCellAbove(actcell);
1510 if (tabular.isLastCell(actcell))
1515 if (!actcell) // first cell
1520 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1521 isRightToLeftPar(bv->buffer()->params());
1522 activateCellInset(bv, 0, 0, !rtl);
1529 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1533 setSelection(0, tabular.getNumberOfCells() - 1);
1535 if (hasSelection()) {
1536 recordUndo(bv, Undo::ATOMIC);
1537 bool const frozen = undo_frozen;
1540 // apply the fontchange on the whole selection
1545 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1546 for (int i = sel_row_start; i <= sel_row_end; ++i)
1547 for (int j = sel_col_start; j <= sel_col_end; ++j)
1548 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1556 if (the_locking_inset)
1557 the_locking_inset->setFont(bv, font, tall);
1561 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1563 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1566 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1567 string const tmp = tabularFeature[i].feature;
1569 if (tmp == what.substr(0, tmp.length())) {
1570 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1571 //tabularFeatures[i].feature.length())) {
1572 action = tabularFeature[i].action;
1576 if (action == LyXTabular::LAST_ACTION)
1580 ltrim(what.substr(tabularFeature[i].feature.length()));
1581 tabularFeatures(bv, action, val);
1587 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1588 string const & special, bool & flag)
1590 if (special == "dl_above") {
1593 } else if (special == "dl_below") {
1594 ltt.bottomDL = flag;
1596 } else if (special == "empty") {
1608 void InsetTabular::tabularFeatures(BufferView * bv,
1609 LyXTabular::Feature feature, string const & value)
1615 bool setLines = false;
1616 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1617 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1621 case LyXTabular::M_ALIGN_LEFT:
1622 case LyXTabular::ALIGN_LEFT:
1623 setAlign = LYX_ALIGN_LEFT;
1626 case LyXTabular::M_ALIGN_RIGHT:
1627 case LyXTabular::ALIGN_RIGHT:
1628 setAlign = LYX_ALIGN_RIGHT;
1631 case LyXTabular::M_ALIGN_CENTER:
1632 case LyXTabular::ALIGN_CENTER:
1633 setAlign = LYX_ALIGN_CENTER;
1636 case LyXTabular::ALIGN_BLOCK:
1637 setAlign = LYX_ALIGN_BLOCK;
1640 case LyXTabular::M_VALIGN_TOP:
1641 case LyXTabular::VALIGN_TOP:
1642 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1645 case LyXTabular::M_VALIGN_BOTTOM:
1646 case LyXTabular::VALIGN_BOTTOM:
1647 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1650 case LyXTabular::M_VALIGN_MIDDLE:
1651 case LyXTabular::VALIGN_MIDDLE:
1652 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1659 if (hasSelection()) {
1660 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1662 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1663 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1665 recordUndo(bv, Undo::ATOMIC);
1667 int row = tabular.row_of_cell(actcell);
1668 int column = tabular.column_of_cell(actcell);
1670 LyXTabular::ltType ltt;
1674 case LyXTabular::SET_PWIDTH: {
1675 LyXLength const len(value);
1676 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1678 tabular.setColumnPWidth(actcell, len);
1679 if (oldlen != len) {
1680 // We need this otherwise we won't resize
1681 // the insettext of the active cell (if any)
1682 // until later (see InsetText::do_resize)
1683 unlockInsetInInset(bv, the_locking_inset);
1688 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1689 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1690 else if (!len.zero()
1691 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1692 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1696 case LyXTabular::SET_MPWIDTH:
1698 LyXLength const len(value);
1699 LyXLength const & oldlen = tabular.getPWidth(actcell);
1700 tabular.setMColumnPWidth(actcell, len);
1701 if (oldlen != len) {
1702 // We need this otherwise we won't resize
1703 // the insettext of the active cell (if any)
1704 // until later (see InsetText::do_resize)
1705 unlockInsetInInset(bv, the_locking_inset);
1711 case LyXTabular::SET_SPECIAL_COLUMN:
1712 case LyXTabular::SET_SPECIAL_MULTI:
1713 tabular.setAlignSpecial(actcell,value,feature);
1717 case LyXTabular::APPEND_ROW:
1718 // append the row into the tabular
1719 unlockInsetInInset(bv, the_locking_inset);
1720 tabular.appendRow(bv->buffer()->params(), actcell);
1721 tabular.setOwner(this);
1725 case LyXTabular::APPEND_COLUMN:
1726 // append the column into the tabular
1727 unlockInsetInInset(bv, the_locking_inset);
1728 tabular.appendColumn(bv->buffer()->params(), actcell);
1729 tabular.setOwner(this);
1730 actcell = tabular.getCellNumber(row, column);
1734 case LyXTabular::DELETE_ROW:
1735 unlockInsetInInset(bv, the_locking_inset);
1736 for (int i = sel_row_start; i <= sel_row_end; ++i)
1737 tabular.deleteRow(sel_row_start);
1738 if (sel_row_start >= tabular.rows())
1740 actcell = tabular.getCellNumber(sel_row_start, column);
1745 case LyXTabular::DELETE_COLUMN:
1746 unlockInsetInInset(bv, the_locking_inset);
1747 for (int i = sel_col_start; i <= sel_col_end; ++i)
1748 tabular.deleteColumn(sel_col_start);
1749 if (sel_col_start >= tabular.columns())
1751 actcell = tabular.getCellNumber(row, sel_col_start);
1756 case LyXTabular::M_TOGGLE_LINE_TOP:
1758 case LyXTabular::TOGGLE_LINE_TOP: {
1759 bool lineSet = !tabular.topLine(actcell, flag);
1760 for (int i = sel_row_start; i <= sel_row_end; ++i)
1761 for (int j = sel_col_start; j <= sel_col_end; ++j)
1763 tabular.getCellNumber(i, j),
1769 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1771 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1772 bool lineSet = !tabular.bottomLine(actcell, flag);
1773 for (int i = sel_row_start; i <= sel_row_end; ++i)
1774 for (int j = sel_col_start; j <= sel_col_end; ++j)
1775 tabular.setBottomLine(
1776 tabular.getCellNumber(i, j),
1783 case LyXTabular::M_TOGGLE_LINE_LEFT:
1785 case LyXTabular::TOGGLE_LINE_LEFT: {
1786 bool lineSet = !tabular.leftLine(actcell, flag);
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.setLeftLine(
1790 tabular.getCellNumber(i,j),
1797 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1799 case LyXTabular::TOGGLE_LINE_RIGHT: {
1800 bool lineSet = !tabular.rightLine(actcell, flag);
1801 for (int i = sel_row_start; i <= sel_row_end; ++i)
1802 for (int j = sel_col_start; j <= sel_col_end; ++j)
1803 tabular.setRightLine(
1804 tabular.getCellNumber(i,j),
1811 case LyXTabular::M_ALIGN_LEFT:
1812 case LyXTabular::M_ALIGN_RIGHT:
1813 case LyXTabular::M_ALIGN_CENTER:
1815 case LyXTabular::ALIGN_LEFT:
1816 case LyXTabular::ALIGN_RIGHT:
1817 case LyXTabular::ALIGN_CENTER:
1818 case LyXTabular::ALIGN_BLOCK:
1819 for (int i = sel_row_start; i <= sel_row_end; ++i)
1820 for (int j = sel_col_start; j <= sel_col_end; ++j)
1821 tabular.setAlignment(
1822 tabular.getCellNumber(i, j),
1828 case LyXTabular::M_VALIGN_TOP:
1829 case LyXTabular::M_VALIGN_BOTTOM:
1830 case LyXTabular::M_VALIGN_MIDDLE:
1832 case LyXTabular::VALIGN_TOP:
1833 case LyXTabular::VALIGN_BOTTOM:
1834 case LyXTabular::VALIGN_MIDDLE:
1835 for (int i = sel_row_start; i <= sel_row_end; ++i)
1836 for (int j = sel_col_start; j <= sel_col_end; ++j)
1837 tabular.setVAlignment(
1838 tabular.getCellNumber(i, j),
1843 case LyXTabular::MULTICOLUMN: {
1844 if (sel_row_start != sel_row_end) {
1845 #ifdef WITH_WARNINGS
1846 #warning Need I say it ? This is horrible.
1848 Alert::error(_("Error setting multicolumn"),
1849 _("You cannot set multicolumn vertically."));
1852 // just multicol for one Single Cell
1853 if (!hasSelection()) {
1854 // check wether we are completly in a multicol
1855 if (tabular.isMultiColumn(actcell))
1856 tabular.unsetMultiColumn(actcell);
1858 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1862 // we have a selection so this means we just add all this
1863 // cells to form a multicolumn cell
1867 if (sel_cell_start > sel_cell_end) {
1868 s_start = sel_cell_end;
1869 s_end = sel_cell_start;
1871 s_start = sel_cell_start;
1872 s_end = sel_cell_end;
1874 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1881 case LyXTabular::SET_ALL_LINES:
1883 case LyXTabular::UNSET_ALL_LINES:
1884 for (int i = sel_row_start; i <= sel_row_end; ++i)
1885 for (int j = sel_col_start; j <= sel_col_end; ++j)
1886 tabular.setAllLines(
1887 tabular.getCellNumber(i,j), setLines);
1891 case LyXTabular::SET_LONGTABULAR:
1892 tabular.setLongTabular(true);
1893 updateLocal(bv); // because this toggles displayed
1896 case LyXTabular::UNSET_LONGTABULAR:
1897 tabular.setLongTabular(false);
1898 updateLocal(bv); // because this toggles displayed
1901 case LyXTabular::SET_ROTATE_TABULAR:
1902 tabular.setRotateTabular(true);
1905 case LyXTabular::UNSET_ROTATE_TABULAR:
1906 tabular.setRotateTabular(false);
1909 case LyXTabular::SET_ROTATE_CELL:
1910 for (int i = sel_row_start; i <= sel_row_end; ++i)
1911 for (int j = sel_col_start; j <= sel_col_end; ++j)
1912 tabular.setRotateCell(
1913 tabular.getCellNumber(i, j), true);
1916 case LyXTabular::UNSET_ROTATE_CELL:
1917 for (int i = sel_row_start; i <= sel_row_end; ++i)
1918 for (int j = sel_col_start; j <= sel_col_end; ++j)
1919 tabular.setRotateCell(
1920 tabular.getCellNumber(i, j), false);
1923 case LyXTabular::SET_USEBOX: {
1924 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1925 if (val == tabular.getUsebox(actcell))
1926 val = LyXTabular::BOX_NONE;
1927 for (int i = sel_row_start; i <= sel_row_end; ++i)
1928 for (int j = sel_col_start; j <= sel_col_end; ++j)
1929 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1933 case LyXTabular::UNSET_LTFIRSTHEAD:
1935 case LyXTabular::SET_LTFIRSTHEAD:
1936 tabular.getRowOfLTFirstHead(row, ltt);
1937 checkLongtableSpecial(ltt, value, flag);
1938 tabular.setLTHead(row, flag, ltt, true);
1941 case LyXTabular::UNSET_LTHEAD:
1943 case LyXTabular::SET_LTHEAD:
1944 tabular.getRowOfLTHead(row, ltt);
1945 checkLongtableSpecial(ltt, value, flag);
1946 tabular.setLTHead(row, flag, ltt, false);
1949 case LyXTabular::UNSET_LTFOOT:
1951 case LyXTabular::SET_LTFOOT:
1952 tabular.getRowOfLTFoot(row, ltt);
1953 checkLongtableSpecial(ltt, value, flag);
1954 tabular.setLTFoot(row, flag, ltt, false);
1957 case LyXTabular::UNSET_LTLASTFOOT:
1959 case LyXTabular::SET_LTLASTFOOT:
1960 tabular.getRowOfLTLastFoot(row, ltt);
1961 checkLongtableSpecial(ltt, value, flag);
1962 tabular.setLTFoot(row, flag, ltt, true);
1965 case LyXTabular::SET_LTNEWPAGE:
1966 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1969 // dummy stuff just to avoid warnings
1970 case LyXTabular::LAST_ACTION:
1974 InsetTabularMailer(*this).updateDialog(bv);
1978 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1980 UpdatableInset & inset = tabular.getCellInset(actcell);
1983 x = inset.x() + inset.width();
1984 y = inset.descent();
1986 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1987 //inset_y = cursory_;
1988 inset.edit(bv, x, y);
1989 if (!the_locking_inset)
1992 return the_locking_inset;
1996 bool InsetTabular::insetHit(BufferView *, int x, int) const
1998 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
2002 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2004 //resizeLyXText(bv, recursive);
2008 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2009 bool const recursive) const
2011 if (the_locking_inset)
2012 return the_locking_inset->getLyXText(bv, recursive);
2013 return InsetOld::getLyXText(bv, recursive);
2017 bool InsetTabular::showInsetDialog(BufferView * bv) const
2019 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2020 InsetTabularMailer(*this).showDialog(bv);
2025 void InsetTabular::openLayoutDialog(BufferView * bv) const
2027 if (the_locking_inset) {
2028 InsetTabular * inset = static_cast<InsetTabular *>
2029 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2031 inset->openLayoutDialog(bv);
2035 InsetTabularMailer(*this).showDialog(bv);
2040 // function returns an object as defined in func_status.h:
2041 // states OK, Unknown, Disabled, On, Off.
2043 FuncStatus InsetTabular::getStatus(string const & what) const
2045 int action = LyXTabular::LAST_ACTION;
2049 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2050 string const tmp = tabularFeature[i].feature;
2051 if (tmp == what.substr(0, tmp.length())) {
2052 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2053 // tabularFeatures[i].feature.length())) {
2054 action = tabularFeature[i].action;
2058 if (action == LyXTabular::LAST_ACTION) {
2060 status.unknown(true);
2064 string const argument
2065 = ltrim(what.substr(tabularFeature[i].feature.length()));
2070 LyXTabular::ltType dummyltt;
2074 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2076 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2079 case LyXTabular::SET_PWIDTH:
2080 case LyXTabular::SET_MPWIDTH:
2081 case LyXTabular::SET_SPECIAL_COLUMN:
2082 case LyXTabular::SET_SPECIAL_MULTI:
2083 case LyXTabular::APPEND_ROW:
2084 case LyXTabular::APPEND_COLUMN:
2085 case LyXTabular::DELETE_ROW:
2086 case LyXTabular::DELETE_COLUMN:
2087 case LyXTabular::SET_ALL_LINES:
2088 case LyXTabular::UNSET_ALL_LINES:
2092 case LyXTabular::MULTICOLUMN:
2093 status.setOnOff(tabular.isMultiColumn(actcell));
2096 case LyXTabular::M_TOGGLE_LINE_TOP:
2098 case LyXTabular::TOGGLE_LINE_TOP:
2099 status.setOnOff(tabular.topLine(actcell, flag));
2102 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2104 case LyXTabular::TOGGLE_LINE_BOTTOM:
2105 status.setOnOff(tabular.bottomLine(actcell, flag));
2108 case LyXTabular::M_TOGGLE_LINE_LEFT:
2110 case LyXTabular::TOGGLE_LINE_LEFT:
2111 status.setOnOff(tabular.leftLine(actcell, flag));
2114 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2116 case LyXTabular::TOGGLE_LINE_RIGHT:
2117 status.setOnOff(tabular.rightLine(actcell, flag));
2120 case LyXTabular::M_ALIGN_LEFT:
2122 case LyXTabular::ALIGN_LEFT:
2123 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2126 case LyXTabular::M_ALIGN_RIGHT:
2128 case LyXTabular::ALIGN_RIGHT:
2129 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2132 case LyXTabular::M_ALIGN_CENTER:
2134 case LyXTabular::ALIGN_CENTER:
2135 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2138 case LyXTabular::ALIGN_BLOCK:
2139 status.disabled(tabular.getPWidth(actcell).zero());
2140 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2143 case LyXTabular::M_VALIGN_TOP:
2145 case LyXTabular::VALIGN_TOP:
2146 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2149 case LyXTabular::M_VALIGN_BOTTOM:
2151 case LyXTabular::VALIGN_BOTTOM:
2152 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2155 case LyXTabular::M_VALIGN_MIDDLE:
2157 case LyXTabular::VALIGN_MIDDLE:
2158 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2161 case LyXTabular::SET_LONGTABULAR:
2162 status.setOnOff(tabular.isLongTabular());
2165 case LyXTabular::UNSET_LONGTABULAR:
2166 status.setOnOff(!tabular.isLongTabular());
2169 case LyXTabular::SET_ROTATE_TABULAR:
2170 status.setOnOff(tabular.getRotateTabular());
2173 case LyXTabular::UNSET_ROTATE_TABULAR:
2174 status.setOnOff(!tabular.getRotateTabular());
2177 case LyXTabular::SET_ROTATE_CELL:
2178 status.setOnOff(tabular.getRotateCell(actcell));
2181 case LyXTabular::UNSET_ROTATE_CELL:
2182 status.setOnOff(!tabular.getRotateCell(actcell));
2185 case LyXTabular::SET_USEBOX:
2186 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2189 case LyXTabular::SET_LTFIRSTHEAD:
2190 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2193 case LyXTabular::SET_LTHEAD:
2194 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2197 case LyXTabular::SET_LTFOOT:
2198 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2201 case LyXTabular::SET_LTLASTFOOT:
2202 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2205 case LyXTabular::SET_LTNEWPAGE:
2206 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2211 status.disabled(true);
2218 void InsetTabular::getLabelList(Buffer const & buffer,
2219 std::vector<string> & list) const
2221 tabular.getLabelList(buffer, list);
2225 bool InsetTabular::copySelection(BufferView * bv)
2227 if (!hasSelection())
2230 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2231 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2232 if (sel_col_start > sel_col_end) {
2233 sel_col_start = sel_col_end;
2234 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2236 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2239 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2240 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2241 if (sel_row_start > sel_row_end)
2242 swap(sel_row_start, sel_row_end);
2244 paste_tabular.reset(new LyXTabular(tabular));
2245 paste_tabular->setOwner(this);
2247 for (int i = 0; i < sel_row_start; ++i)
2248 paste_tabular->deleteRow(0);
2250 int const rows = sel_row_end - sel_row_start + 1;
2251 while (paste_tabular->rows() > rows)
2252 paste_tabular->deleteRow(rows);
2254 paste_tabular->setTopLine(0, true, true);
2255 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2258 for (int i = 0; i < sel_col_start; ++i)
2259 paste_tabular->deleteColumn(0);
2261 int const columns = sel_col_end - sel_col_start + 1;
2262 while (paste_tabular->columns() > columns)
2263 paste_tabular->deleteColumn(columns);
2265 paste_tabular->setLeftLine(0, true, true);
2266 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2270 OutputParams const runparams;
2271 paste_tabular->plaintext(*bv->buffer(), os, runparams,
2272 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2273 bv->stuffClipboard(os.str());
2278 bool InsetTabular::pasteSelection(BufferView * bv)
2283 for (int r1 = 0, r2 = actrow;
2284 r1 < paste_tabular->rows() && r2 < tabular.rows();
2286 for (int c1 = 0, c2 = actcol;
2287 c1 < paste_tabular->columns() && c2 < tabular.columns();
2289 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2290 tabular.isPartOfMultiColumn(r2, c2))
2292 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2296 if (tabular.isPartOfMultiColumn(r2, c2)) {
2300 InsetText & inset = tabular.getCellInset(r2, c2);
2301 inset = paste_tabular->getCellInset(r1, c1);
2302 inset.setOwner(this);
2303 inset.deleteLyXText(bv);
2311 bool InsetTabular::cutSelection(BufferParams const & bp)
2313 if (!hasSelection())
2316 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2317 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2318 if (sel_col_start > sel_col_end) {
2319 sel_col_start = sel_col_end;
2320 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2322 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2325 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2326 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2328 if (sel_row_start > sel_row_end)
2329 swap(sel_row_start, sel_row_end);
2331 if (sel_cell_start > sel_cell_end)
2332 swap(sel_cell_start, sel_cell_end);
2334 for (int i = sel_row_start; i <= sel_row_end; ++i)
2335 for (int j = sel_col_start; j <= sel_col_end; ++j)
2336 tabular.getCellInset(tabular.getCellNumber(i, j))
2337 .clear(bp.tracking_changes);
2342 bool InsetTabular::isRightToLeft(BufferView * bv)
2344 return bv->getParentLanguage(this)->RightToLeft();
2348 int InsetTabular::scroll(bool recursive) const
2350 int sx = UpdatableInset::scroll(false);
2352 if (recursive && the_locking_inset)
2353 sx += the_locking_inset->scroll(recursive);
2359 void InsetTabular::getSelection(int & srow, int & erow,
2360 int & scol, int & ecol) const
2362 int const start = hasSelection() ? sel_cell_start : actcell;
2363 int const end = hasSelection() ? sel_cell_end : actcell;
2365 srow = tabular.row_of_cell(start);
2366 erow = tabular.row_of_cell(end);
2370 scol = tabular.column_of_cell(start);
2371 ecol = tabular.column_of_cell(end);
2375 ecol = tabular.right_column_of_cell(end);
2379 ParagraphList * InsetTabular::getParagraphs(int i) const
2381 return i < tabular.getNumberOfCells()
2382 ? tabular.getCellInset(i).getParagraphs(0)
2387 int InsetTabular::numParagraphs() const
2389 return tabular.getNumberOfCells();
2393 LyXText * InsetTabular::getText(int i) const
2395 return i < tabular.getNumberOfCells()
2396 ? tabular.getCellInset(i).getText(0)
2401 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2403 if (the_locking_inset)
2404 return the_locking_inset->cursor(bv);
2405 return InsetOld::cursor(bv);
2409 void InsetTabular::markErased()
2411 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2412 tabular.getCellInset(cell).markErased();
2416 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2418 if (the_locking_inset)
2419 return the_locking_inset->insetAllowed(code);
2420 // we return true here because if the inset is not locked someone
2421 // wants to insert something in one of our insettexts and we generally
2427 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2429 const int cell = tabular.getCellFromInset(in);
2432 return tabular.getPWidth(cell).zero();
2434 // this is a workaround for a crash (New, Insert->Tabular,
2435 // Insert->FootNote)
2439 // well we didn't obviously find it so maybe our owner knows more
2440 BOOST_ASSERT(owner());
2441 return owner()->forceDefaultParagraphs(in);
2445 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2448 if (buf.length() <= 0)
2454 string::size_type len = buf.length();
2455 string::size_type p = 0;
2457 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2465 maxCols = max(cols, maxCols);
2471 maxCols = max(cols, maxCols);
2472 LyXTabular * loctab;
2477 paste_tabular.reset(
2478 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2481 paste_tabular->setOwner(this);
2482 loctab = paste_tabular.get();
2491 string::size_type op = 0;
2492 int cells = loctab->getNumberOfCells();
2495 rows = loctab->rows();
2496 int const columns = loctab->columns();
2498 while (cell < cells && p < len && row < rows &&
2499 (p = buf.find_first_of("\t\n", p)) != string::npos)
2505 // we can only set this if we are not too far right
2506 if (cols < columns) {
2507 InsetText & inset = loctab->getCellInset(cell);
2508 LyXFont const font = inset.getLyXText(bv)->
2509 getFont(inset.paragraphs.begin(), 0);
2510 inset.setText(buf.substr(op, p - op), font);
2516 // we can only set this if we are not too far right
2517 if (cols < columns) {
2518 InsetText & inset = tabular.getCellInset(cell);
2519 LyXFont const font = inset.getLyXText(bv)->
2520 getFont(inset.paragraphs.begin(), 0);
2521 inset.setText(buf.substr(op, p - op), font);
2526 cell = loctab->getCellNumber(row, cols);
2532 // check for the last cell if there is no trailing '\n'
2533 if (cell < cells && op < len) {
2534 InsetText & inset = loctab->getCellInset(cell);
2535 LyXFont const font = inset.getLyXText(bv)->
2536 getFont(inset.paragraphs.begin(), 0);
2537 inset.setText(buf.substr(op, len - op), font);
2544 void InsetTabular::addPreview(PreviewLoader & loader) const
2546 int const rows = tabular.rows();
2547 int const columns = tabular.columns();
2548 for (int i = 0; i < rows; ++i)
2549 for (int j = 0; j < columns; ++j)
2550 tabular.getCellInset(i, j).addPreview(loader);
2554 string const InsetTabularMailer::name_("tabular");
2556 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2557 : inset_(const_cast<InsetTabular &>(inset))
2561 string const InsetTabularMailer::inset2string(Buffer const &) const
2563 return params2string(inset_);
2567 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2569 istringstream data(in);
2571 lex.setStream(data);
2573 #warning CHECK verify that this is a sane value to return.
2579 string const token = lex.getString();
2587 string const token = lex.getString();
2588 if (token != "\\active_cell")
2591 cell = lex.getInteger();
2594 // This is part of the inset proper that is usually swallowed
2595 // by Buffer::readInset
2598 string const token = lex.getString();
2599 if (token != "Tabular")
2606 Buffer const & buffer = inset.buffer();
2607 inset.read(buffer, lex);
2609 // We can't set the active cell, but we can tell the frontend
2615 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2617 Buffer const & buffer = inset.buffer();
2620 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2621 inset.write(buffer, data);
2622 data << "\\end_inset\n";