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"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
35 #include "frontends/Alert.h"
36 #include "frontends/font_metrics.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
40 #include "support/std_sstream.h"
44 using lyx::graphics::PreviewLoader;
46 using lyx::support::ltrim;
47 using lyx::support::strToInt;
48 using lyx::support::strToDbl;
54 using std::istringstream;
56 using std::ostringstream;
63 int const ADD_TO_HEIGHT = 2;
64 int const ADD_TO_TABULAR_WIDTH = 2;
67 boost::scoped_ptr<LyXTabular> paste_tabular;
70 struct TabularFeature {
71 LyXTabular::Feature action;
76 TabularFeature tabularFeature[] =
78 { LyXTabular::APPEND_ROW, "append-row" },
79 { LyXTabular::APPEND_COLUMN, "append-column" },
80 { LyXTabular::DELETE_ROW, "delete-row" },
81 { LyXTabular::DELETE_COLUMN, "delete-column" },
82 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
83 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
84 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
85 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
86 { LyXTabular::ALIGN_LEFT, "align-left" },
87 { LyXTabular::ALIGN_RIGHT, "align-right" },
88 { LyXTabular::ALIGN_CENTER, "align-center" },
89 { LyXTabular::ALIGN_BLOCK, "align-block" },
90 { LyXTabular::VALIGN_TOP, "valign-top" },
91 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
92 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
93 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
94 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
95 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
96 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
97 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
98 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
99 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
100 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
101 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
102 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
103 { LyXTabular::MULTICOLUMN, "multicolumn" },
104 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
105 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
106 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
107 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
108 { LyXTabular::SET_PWIDTH, "set-pwidth" },
109 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
110 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
111 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
112 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
113 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
114 { LyXTabular::SET_USEBOX, "set-usebox" },
115 { LyXTabular::SET_LTHEAD, "set-lthead" },
116 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
117 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
118 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
119 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
120 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
121 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
122 { LyXTabular::LAST_ACTION, "" }
126 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
128 FeatureEqual(LyXTabular::Feature feature)
129 : feature_(feature) {}
130 bool operator()(TabularFeature const & tf) const {
131 return tf.action == feature_;
134 LyXTabular::Feature feature_;
140 string const featureAsString(LyXTabular::Feature feature)
142 TabularFeature * end = tabularFeature +
143 sizeof(tabularFeature) / sizeof(TabularFeature);
144 TabularFeature * it = std::find_if(tabularFeature, end,
145 FeatureEqual(feature));
146 return (it == end) ? string() : it->feature;
150 bool InsetTabular::hasPasteBuffer() const
152 return (paste_tabular.get() != 0);
156 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
157 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
158 buffer_(&buf), cursorx_(0), cursory_(0), tablemode(false)
160 tabular.setOwner(this);
166 InsetTabular::InsetTabular(InsetTabular const & tab)
167 : UpdatableInset(tab), tabular(tab.tabular),
168 buffer_(tab.buffer_), cursorx_(0), cursory_(0), tablemode(false)
170 tabular.setOwner(this);
176 InsetTabular::~InsetTabular()
178 InsetTabularMailer(*this).hideDialog();
182 auto_ptr<InsetBase> InsetTabular::clone() const
184 return auto_ptr<InsetBase>(new InsetTabular(*this));
188 Buffer const & InsetTabular::buffer() const
194 void InsetTabular::buffer(Buffer * b)
200 void InsetTabular::write(Buffer const & buf, ostream & os) const
202 os << "Tabular" << endl;
203 tabular.write(buf, os);
207 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
209 bool const old_format = (lex.getString() == "\\LyXTable");
211 tabular.read(buf, lex);
217 string token = lex.getString();
218 while (lex.isOK() && (token != "\\end_inset")) {
220 token = lex.getString();
222 if (token != "\\end_inset") {
223 lex.printError("Missing \\end_inset at this point. "
229 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
231 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
232 // mi.base.textwidth << "\n";
234 lyxerr << "InsetTabular::metrics: need bv" << endl;
238 calculate_dimensions_of_cells(mi);
240 dim.asc = tabular.getAscentOfRow(0);
241 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
242 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
247 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
249 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
251 BufferView * bv = pi.base.bv;
258 x += ADD_TO_TABULAR_WIDTH;
261 first_visible_cell = -1;
262 for (int i = 0; i < tabular.rows(); ++i) {
264 cell = tabular.getCellNumber(i, 0);
265 if (y + tabular.getDescentOfRow(i) <= 0 &&
266 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
268 y += tabular.getDescentOfRow(i) +
269 tabular.getAscentOfRow(i + 1) +
270 tabular.getAdditionalHeight(i + 1);
273 for (int j = 0; j < tabular.columns(); ++j) {
274 if (nx > bv->workWidth())
276 if (tabular.isPartOfMultiColumn(i, j))
278 if (first_visible_cell < 0)
279 first_visible_cell = cell;
281 drawCellSelection(pi.pain, nx, y, i, j, cell);
283 int const cx = nx + tabular.getBeginningOfTextInCell(cell);
284 tabular.getCellInset(cell).draw(pi, cx, y);
285 drawCellLines(pi.pain, nx, y, i, cell);
286 nx += tabular.getWidthOfColumn(cell);
290 // Would be nice, but for some completely unfathomable reason,
291 // on a col resize to a new fixed width, even though the insettexts
292 // are resized, the cell isn't, but drawing all cells in a tall table
293 // has the desired effect somehow. Complete dark magic.
295 // avoiding drawing the rest of a long table is
296 // a pretty big speedup
297 if (y > bv->workHeight())
301 y += tabular.getDescentOfRow(i) +
302 tabular.getAscentOfRow(i + 1) +
303 tabular.getAdditionalHeight(i + 1);
308 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
309 int row, int cell) const
311 int x2 = x + tabular.getWidthOfColumn(cell);
314 if (!tabular.topAlreadyDrawn(cell)) {
315 on_off = !tabular.topLine(cell);
316 pain.line(x, y - tabular.getAscentOfRow(row),
317 x2, y - tabular.getAscentOfRow(row),
318 on_off ? LColor::tabularonoffline : LColor::tabularline,
319 on_off ? Painter::line_onoffdash : Painter::line_solid);
321 on_off = !tabular.bottomLine(cell);
322 pain.line(x, y + tabular.getDescentOfRow(row),
323 x2, y + tabular.getDescentOfRow(row),
324 on_off ? LColor::tabularonoffline : LColor::tabularline,
325 on_off ? Painter::line_onoffdash : Painter::line_solid);
326 if (!tabular.leftAlreadyDrawn(cell)) {
327 on_off = !tabular.leftLine(cell);
328 pain.line(x, y - tabular.getAscentOfRow(row),
329 x, y + tabular.getDescentOfRow(row),
330 on_off ? LColor::tabularonoffline : LColor::tabularline,
331 on_off ? Painter::line_onoffdash : Painter::line_solid);
333 on_off = !tabular.rightLine(cell);
334 pain.line(x2 - tabular.getAdditionalWidth(cell),
335 y - tabular.getAscentOfRow(row),
336 x2 - tabular.getAdditionalWidth(cell),
337 y + tabular.getDescentOfRow(row),
338 on_off ? LColor::tabularonoffline : LColor::tabularline,
339 on_off ? Painter::line_onoffdash : Painter::line_solid);
343 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
344 int row, int column, int cell) const
349 BOOST_ASSERT(hasSelection());
350 int cs = tabular.column_of_cell(sel_cell_start);
351 int ce = tabular.column_of_cell(sel_cell_end);
354 cs = tabular.column_of_cell(sel_cell_end);
356 ce = tabular.right_column_of_cell(sel_cell_end);
359 int rs = tabular.row_of_cell(sel_cell_start);
360 int re = tabular.row_of_cell(sel_cell_end);
364 if (column >= cs && column <= ce && row >= rs && row <= re) {
365 int w = tabular.getWidthOfColumn(cell);
366 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
367 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
368 w, h, LColor::selection);
373 string const InsetTabular::editMessage() const
375 return _("Opened table");
379 void InsetTabular::updateLocal(LCursor & cur) const
386 extern CursorBase theTempCursor;
389 void InsetTabular::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
391 if (hasSelection() && cmd.button() == mouse_button::button3)
394 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
397 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
400 cur.cursor_ = theTempCursor;
405 setPos(cur.bv(), cmd.x, cmd.y);
406 cur.cursor_ = theTempCursor;
410 lyxerr << cur << endl;
412 if (cmd.button() == mouse_button::button2)
413 dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
417 void InsetTabular::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
419 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
420 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
422 setPos(cur.bv(), cmd.x, cmd.y);
423 if (!hasSelection()) {
424 setSelection(actcell, actcell);
428 setSelection(sel_cell_start, actcell);
429 tablemode = (sel_cell_start != actcell);
434 void InsetTabular::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
436 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
437 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
438 if (cmd.button() == mouse_button::button3)
439 InsetTabularMailer(*this).showDialog(&cur.bv());
443 void InsetTabular::edit(LCursor & cur, bool left)
445 lyxerr << "InsetTabular::edit: " << this << endl;
450 if (isRightToLeft(cur))
451 cell = tabular.getLastCellInRow(0);
455 if (isRightToLeft(cur))
456 cell = tabular.getFirstCellInRow(tabular.rows()-1);
458 cell = tabular.getNumberOfCells() - 1;
462 cur.bv().fitCursor();
468 void InsetTabular::edit(LCursor & cur, int x, int y)
470 lyxerr << "InsetTabular::edit: " << this << " first cell "
471 << &tabular.cell_info[0][0].inset << endl;
474 setPos(cur.bv(), x, y);
477 //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
480 // activateCellInset(bv, cell, x - xx, y - cursory_);
485 InsetTabular::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
487 lyxerr << "# InsetTabular::dispatch: " << cmd
488 << " tablemode: " << tablemode << endl;
490 DispatchResult result(true, true);
491 switch (cmd.action) {
493 case LFUN_MOUSE_PRESS:
494 lfunMousePress(cur, cmd);
495 return DispatchResult(true, true);
497 case LFUN_MOUSE_MOTION:
498 lfunMouseMotion(cur, cmd);
499 return DispatchResult(true, true);
501 case LFUN_MOUSE_RELEASE:
502 lfunMouseRelease(cur, cmd);
503 return DispatchResult(true, true);
511 int cell = cur.idx();
512 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
513 result = tabular.getCellInset(cell).dispatch(cur, cmd);
515 switch (result.val()) {
517 if (movePrevCell(cur))
518 result = DispatchResult(true, true);
520 result = DispatchResult(false, FINISHED);
524 if (moveNextCell(cur))
525 result = DispatchResult(true, true);
527 result = DispatchResult(false, FINISHED_RIGHT);
532 result = DispatchResult(true, true);
534 result = DispatchResult(false, FINISHED_UP);
538 if (moveDownLock(cur))
539 result = DispatchResult(true, true);
541 result = DispatchResult(false, FINISHED_UP);
545 lyxerr << "# don't handle dispatch" << endl;
549 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
552 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
553 switch (cmd.action) {
555 case LFUN_CELL_BACKWARD:
556 case LFUN_CELL_FORWARD:
557 if (cmd.action == LFUN_CELL_FORWARD)
564 case LFUN_SCROLL_INSET:
565 if (!cmd.argument.empty()) {
566 if (cmd.argument.find('.') != cmd.argument.npos)
567 scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
569 scroll(cur.bv(), strToInt(cmd.argument));
571 return DispatchResult(true, true);
574 case LFUN_RIGHTSEL: {
575 int const start = hasSelection() ? sel_cell_start : cur.idx();
576 if (tabular.isLastCellInRow(cur.idx())) {
577 setSelection(start, cur.idx());
582 // if we are starting a selection, only select
583 // the current cell at the beginning
584 if (hasSelection()) {
588 setSelection(start, end);
593 if (!moveRightLock(cur))
594 result = DispatchResult(false, FINISHED_RIGHT);
599 int const start = hasSelection() ? sel_cell_start : cur.idx();
600 if (tabular.isFirstCellInRow(cur.idx())) {
601 setSelection(start, cur.idx());
606 // if we are starting a selection, only select
607 // the current cell at the beginning
608 if (hasSelection()) {
612 setSelection(start, end);
617 if (!moveLeftLock(cur))
618 result = DispatchResult(false, FINISHED);
623 int const start = hasSelection() ? sel_cell_start : cur.idx();
624 int const ocell = cur.idx();
625 // if we are starting a selection, only select
626 // the current cell at the beginning
627 if (hasSelection()) {
629 if (ocell == sel_cell_end ||
630 tabular.column_of_cell(ocell) >
631 tabular.column_of_cell(cur.idx()))
632 setSelection(start, tabular.getCellBelow(sel_cell_end));
634 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
636 setSelection(start, start);
643 result = DispatchResult(false, FINISHED_DOWN);
648 int const start = hasSelection() ? sel_cell_start : cur.idx();
649 int const ocell = cur.idx();
650 // if we are starting a selection, only select
651 // the current cell at the beginning
652 if (hasSelection()) {
654 if (ocell == sel_cell_end ||
655 tabular.column_of_cell(ocell) >
656 tabular.column_of_cell(cur.idx()))
657 setSelection(start, tabular.getCellAbove(sel_cell_end));
659 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
661 setSelection(start, start);
668 result = DispatchResult(false, FINISHED_DOWN);
675 int actcell = cur.idx();
676 int actcol = tabular.column_of_cell(actcell);
678 if (cur.bv().top_y() + cur.bv().painter().paperHeight()
679 < yo_ + tabular.getHeightOfTabular())
681 cur.bv().scrollDocView(
682 cur.bv().top_y() + cur.bv().painter().paperHeight());
683 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
685 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
694 int column = tabular.column_of_cell(cur.idx());
696 cur.bv().scrollDocView(
697 cur.bv().top_y() - cur.bv().painter().paperHeight());
701 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
709 // none of these make sense for insettabular,
710 // but we must catch them to prevent any
711 // selection from being confused
715 case LFUN_WORDLEFTSEL:
717 case LFUN_WORDRIGHTSEL:
719 case LFUN_DOWN_PARAGRAPH:
720 case LFUN_DOWN_PARAGRAPHSEL:
721 case LFUN_UP_PARAGRAPH:
722 case LFUN_UP_PARAGRAPHSEL:
728 case LFUN_BEGINNINGBUF:
729 case LFUN_BEGINNINGBUFSEL:
734 case LFUN_LAYOUT_TABULAR:
735 InsetTabularMailer(*this).showDialog(&cur.bv());
738 case LFUN_INSET_DIALOG_UPDATE:
739 InsetTabularMailer(*this).updateDialog(&cur.bv());
742 case LFUN_TABULAR_FEATURE:
743 if (!tabularFeatures(cur, cmd.argument))
744 result = DispatchResult(false);
747 // insert file functions
748 case LFUN_FILE_INSERT_ASCII_PARA:
749 case LFUN_FILE_INSERT_ASCII: {
750 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
751 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
752 result = DispatchResult(false);
766 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
770 if (copySelection(cur.bv())) {
771 recordUndo(cur, Undo::DELETE);
772 cutSelection(cur.bv().buffer()->params());
777 recordUndo(cur, Undo::DELETE);
778 cutSelection(cur.bv().buffer()->params());
785 copySelection(cur.bv());
788 case LFUN_PASTESELECTION: {
789 string const clip = cur.bv().getClipboard();
792 if (clip.find('\t') != string::npos) {
796 string::size_type len = clip.length();
797 string::size_type p = 0;
800 (p = clip.find_first_of("\t\n", p)) != string::npos) {
808 maxCols = max(cols, maxCols);
814 maxCols = max(cols, maxCols);
817 new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
819 string::size_type op = 0;
821 int cells = paste_tabular->getNumberOfCells();
825 while (cell < cells && p < len &&
826 (p = clip.find_first_of("\t\n", p)) != string::npos) {
831 paste_tabular->getCellInset(cell).
832 setText(clip.substr(op, p-op), font);
837 paste_tabular->getCellInset(cell).
838 setText(clip.substr(op, p-op), font);
839 while (cols++ < maxCols)
847 // check for the last cell if there is no trailing '\n'
848 if (cell < cells && op < len)
849 paste_tabular->getCellInset(cell).
850 setText(clip.substr(op, len-op), font);
851 } else if (!insertAsciiString(cur.bv(), clip, true))
853 // so that the clipboard is used and it goes on
855 // and executes LFUN_PASTESELECTION in insettext!
856 paste_tabular.reset();
862 if (hasPasteBuffer()) {
863 recordUndo(cur, Undo::INSERT);
864 pasteSelection(cur.bv());
869 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
872 // handle font changing stuff on selection before we lock the inset
873 // in the default part!
874 result = DispatchResult(false);
875 // we try to activate the actual inset and put this event down to
876 // the insets dispatch function.
881 InsetTabularMailer(*this).updateDialog(&cur.bv());
884 if (cmd.action == LFUN_INSET_TOGGLE) {
885 tablemode = !tablemode;
886 result = DispatchResult(true, true);
893 int InsetTabular::latex(Buffer const & buf, ostream & os,
894 OutputParams const & runparams) const
896 return tabular.latex(buf, os, runparams);
900 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
901 OutputParams const & runparams) const
903 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
904 return tabular.plaintext(buf, os, runparams, dp, false, 0);
908 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
909 OutputParams const & runparams) const
911 return tabular.linuxdoc(buf,os, runparams);
915 int InsetTabular::docbook(Buffer const & buf, ostream & os,
916 OutputParams const & runparams) const
921 // if the table is inside a float it doesn't need the informaltable
922 // wrapper. Search for it.
923 for (master = owner(); master; master = master->owner())
924 if (master->lyxCode() == InsetOld::FLOAT_CODE)
928 os << "<informaltable>";
929 if (runparams.mixed_content)
933 ret += tabular.docbook(buf, os, runparams);
935 os << "</informaltable>";
936 if (runparams.mixed_content)
944 void InsetTabular::validate(LaTeXFeatures & features) const
946 tabular.validate(features);
950 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
952 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
955 for (int j = 0; j < tabular.columns(); ++j) {
956 if (tabular.isPartOfMultiColumn(i, j))
962 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
963 tabular.getCellInset(cell).metrics(m, dim);
964 maxAsc = max(maxAsc, dim.asc);
965 maxDesc = max(maxDesc, dim.des);
966 tabular.setWidthOfCell(cell, dim.wid);
968 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
969 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
974 void InsetTabular::getCursorPos(int cell, int & x, int & y) const
976 InsetText const & inset = tabular.getCellInset(cell);
977 inset.getCursorPos(cell, x, y);
978 x += inset.x() - xo_;
979 y += inset.y() - yo_;
983 void InsetTabular::setPos(BufferView & bv, int x, int y) const
985 int const cell = getCell(x + xo_, y + yo_);
986 lyxerr << "# InsetTabular::setPos() cell: " << cell << endl;
987 InsetText const & inset = tabular.getCellInset(cell);
988 inset.text_.setCursorFromCoordinates(x, y);
994 int ly = tabular.getDescentOfRow(actrow);
996 // first search the right row
997 while (ly < y && actrow + 1 < tabular.rows()) {
998 cursory_ += tabular.getDescentOfRow(actrow) +
999 tabular.getAscentOfRow(actrow + 1) +
1000 tabular.getAdditionalHeight(actrow + 1);
1002 ly = cursory_ + tabular.getDescentOfRow(actrow);
1004 actcell = tabular.getCellNumber(actrow, actcol);
1006 // now search the right column
1007 int lx = tabular.getWidthOfColumn(actcell) -
1008 tabular.getAdditionalWidth(actcell);
1010 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1011 lx += tabular.getWidthOfColumn(actcell + 1)
1012 + tabular.getAdditionalWidth(actcell);
1014 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1016 resetPos(bv.cursor());
1020 int InsetTabular::getCellXPos(int cell) const
1024 for (; !tabular.isFirstCellInRow(c); --c)
1026 int lx = tabular.getWidthOfColumn(cell);
1027 for (; c < cell; ++c)
1028 lx += tabular.getWidthOfColumn(c);
1030 return lx - tabular.getWidthOfColumn(cell) + xo_;
1034 void InsetTabular::resetPos(LCursor &) const
1037 #ifdef WITH_WARNINGS
1038 #warning This should be fixed in the right manner (20011128 Jug)
1040 // fast hack to fix infinite repaintings!
1041 if (in_reset_pos > 0)
1044 BufferView & bv = cur.bv();
1046 int actcell = cur.cell();
1047 int actcol = tabular.column_of_cell(actcell);
1050 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1051 if (tabular.isLastCellInRow(cell)) {
1052 cursory_ += tabular.getDescentOfRow(actrow) +
1053 tabular.getAscentOfRow(actrow + 1) +
1054 tabular.getAdditionalHeight(actrow + 1);
1059 // we need this only from here on!!!
1061 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1062 int new_x = getCellXPos(actcell) + offset;
1063 int old_x = cursorx_;
1065 // cursor.x(getCellXPos(actcell) + offset);
1066 if (actcol < tabular.columns() - 1 && scroll(false) &&
1067 tabular.getWidthOfTabular() < bv->workWidth()-20)
1071 } else if (cursorx_ - offset > 20 &&
1072 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1073 > bv.workWidth() - 20) {
1074 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1076 } else if (cursorx_ - offset < 20) {
1077 scroll(&bv, 20 - cursorx_ + offset);
1079 } else if (scroll() && xo_ > 20 &&
1080 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1081 scroll(&bv, old_x - cursorx_);
1084 InsetTabularMailer(*this).updateDialog(bv);
1090 bool InsetTabular::moveRight(LCursor & cur)
1092 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1100 bool InsetTabular::moveRightLock(LCursor & cur)
1102 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1105 activateCellInset(cur, cur.idx(), false);
1110 bool InsetTabular::moveLeft(LCursor & cur)
1112 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1120 bool InsetTabular::moveLeftLock(LCursor & cur)
1122 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1125 activateCellInset(cur, cur.idx(), true);
1130 bool InsetTabular::moveUp(LCursor & cur)
1132 if (tabular.row_of_cell(cur.idx()) == 0)
1134 cur.idx() = tabular.getCellAbove(cur.idx());
1140 bool InsetTabular::moveUpLock(LCursor & cur)
1142 if (tabular.row_of_cell(cur.idx()) == 0)
1144 cur.idx() = tabular.getCellAbove(cur.idx());
1146 activateCellInset(cur, cur.idx(), cur.x_target(), 0);
1151 bool InsetTabular::moveDown(LCursor & cur)
1153 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1155 cur.idx() = tabular.getCellBelow(cur.idx());
1161 bool InsetTabular::moveDownLock(LCursor & cur)
1163 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1165 cur.idx() = tabular.getCellBelow(cur.idx());
1167 activateCellInset(cur, cur.idx(), cur.x_target());
1172 bool InsetTabular::moveNextCell(LCursor & cur)
1174 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1175 if (isRightToLeft(cur)) {
1176 if (tabular.isFirstCellInRow(cur.idx())) {
1177 int row = tabular.row_of_cell(cur.idx());
1178 if (row == tabular.rows() - 1)
1180 cur.idx() = tabular.getLastCellInRow(row);
1181 cur.idx() = tabular.getCellBelow(cur.idx());
1188 if (tabular.isLastCell(cur.idx()))
1194 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1200 bool InsetTabular::movePrevCell(LCursor & cur)
1202 if (isRightToLeft(cur)) {
1203 if (tabular.isLastCellInRow(cur.idx())) {
1204 int row = tabular.row_of_cell(cur.idx());
1207 cur.idx() = tabular.getFirstCellInRow(row);
1208 cur.idx() = tabular.getCellAbove(cur.idx());
1210 if (tabular.isLastCell(cur.idx()))
1215 if (cur.idx() == 0) // first cell
1226 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1228 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1231 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1232 string const tmp = tabularFeature[i].feature;
1234 if (tmp == what.substr(0, tmp.length())) {
1235 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1236 //tabularFeatures[i].feature.length())) {
1237 action = tabularFeature[i].action;
1241 if (action == LyXTabular::LAST_ACTION)
1245 ltrim(what.substr(tabularFeature[i].feature.length()));
1246 tabularFeatures(cur, action, val);
1253 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1254 string const & special, bool & flag)
1256 if (special == "dl_above") {
1259 } else if (special == "dl_below") {
1260 ltt.bottomDL = flag;
1262 } else if (special == "empty") {
1274 void InsetTabular::tabularFeatures(LCursor & cur,
1275 LyXTabular::Feature feature, string const & value)
1277 BufferView & bv = cur.bv();
1278 int actcell = cur.idx();
1283 bool setLines = false;
1284 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1285 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1289 case LyXTabular::M_ALIGN_LEFT:
1290 case LyXTabular::ALIGN_LEFT:
1291 setAlign = LYX_ALIGN_LEFT;
1294 case LyXTabular::M_ALIGN_RIGHT:
1295 case LyXTabular::ALIGN_RIGHT:
1296 setAlign = LYX_ALIGN_RIGHT;
1299 case LyXTabular::M_ALIGN_CENTER:
1300 case LyXTabular::ALIGN_CENTER:
1301 setAlign = LYX_ALIGN_CENTER;
1304 case LyXTabular::ALIGN_BLOCK:
1305 setAlign = LYX_ALIGN_BLOCK;
1308 case LyXTabular::M_VALIGN_TOP:
1309 case LyXTabular::VALIGN_TOP:
1310 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1313 case LyXTabular::M_VALIGN_BOTTOM:
1314 case LyXTabular::VALIGN_BOTTOM:
1315 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1318 case LyXTabular::M_VALIGN_MIDDLE:
1319 case LyXTabular::VALIGN_MIDDLE:
1320 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1327 if (hasSelection()) {
1328 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1330 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1331 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1333 recordUndo(cur, Undo::ATOMIC);
1335 int row = tabular.row_of_cell(actcell);
1336 int column = tabular.column_of_cell(actcell);
1338 LyXTabular::ltType ltt;
1342 case LyXTabular::SET_PWIDTH: {
1343 LyXLength const len(value);
1344 tabular.setColumnPWidth(actcell, len);
1346 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1347 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1348 else if (!len.zero()
1349 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1350 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1354 case LyXTabular::SET_MPWIDTH:
1355 tabular.setMColumnPWidth(actcell, LyXLength(value));
1358 case LyXTabular::SET_SPECIAL_COLUMN:
1359 case LyXTabular::SET_SPECIAL_MULTI:
1360 tabular.setAlignSpecial(actcell,value,feature);
1363 case LyXTabular::APPEND_ROW:
1364 // append the row into the tabular
1365 tabular.appendRow(bv.buffer()->params(), actcell);
1366 tabular.setOwner(this);
1369 case LyXTabular::APPEND_COLUMN:
1370 // append the column into the tabular
1371 tabular.appendColumn(bv.buffer()->params(), actcell);
1372 tabular.setOwner(this);
1373 actcell = tabular.getCellNumber(row, column);
1376 case LyXTabular::DELETE_ROW:
1377 for (int i = sel_row_start; i <= sel_row_end; ++i)
1378 tabular.deleteRow(sel_row_start);
1379 if (sel_row_start >= tabular.rows())
1381 actcell = tabular.getCellNumber(sel_row_start, column);
1385 case LyXTabular::DELETE_COLUMN:
1386 for (int i = sel_col_start; i <= sel_col_end; ++i)
1387 tabular.deleteColumn(sel_col_start);
1388 if (sel_col_start >= tabular.columns())
1390 actcell = tabular.getCellNumber(row, sel_col_start);
1394 case LyXTabular::M_TOGGLE_LINE_TOP:
1396 case LyXTabular::TOGGLE_LINE_TOP: {
1397 bool lineSet = !tabular.topLine(actcell, flag);
1398 for (int i = sel_row_start; i <= sel_row_end; ++i)
1399 for (int j = sel_col_start; j <= sel_col_end; ++j)
1401 tabular.getCellNumber(i, j),
1406 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1408 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1409 bool lineSet = !tabular.bottomLine(actcell, flag);
1410 for (int i = sel_row_start; i <= sel_row_end; ++i)
1411 for (int j = sel_col_start; j <= sel_col_end; ++j)
1412 tabular.setBottomLine(
1413 tabular.getCellNumber(i, j),
1419 case LyXTabular::M_TOGGLE_LINE_LEFT:
1421 case LyXTabular::TOGGLE_LINE_LEFT: {
1422 bool lineSet = !tabular.leftLine(actcell, flag);
1423 for (int i = sel_row_start; i <= sel_row_end; ++i)
1424 for (int j = sel_col_start; j <= sel_col_end; ++j)
1425 tabular.setLeftLine(
1426 tabular.getCellNumber(i,j),
1432 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1434 case LyXTabular::TOGGLE_LINE_RIGHT: {
1435 bool lineSet = !tabular.rightLine(actcell, flag);
1436 for (int i = sel_row_start; i <= sel_row_end; ++i)
1437 for (int j = sel_col_start; j <= sel_col_end; ++j)
1438 tabular.setRightLine(
1439 tabular.getCellNumber(i,j),
1445 case LyXTabular::M_ALIGN_LEFT:
1446 case LyXTabular::M_ALIGN_RIGHT:
1447 case LyXTabular::M_ALIGN_CENTER:
1449 case LyXTabular::ALIGN_LEFT:
1450 case LyXTabular::ALIGN_RIGHT:
1451 case LyXTabular::ALIGN_CENTER:
1452 case LyXTabular::ALIGN_BLOCK:
1453 for (int i = sel_row_start; i <= sel_row_end; ++i)
1454 for (int j = sel_col_start; j <= sel_col_end; ++j)
1455 tabular.setAlignment(
1456 tabular.getCellNumber(i, j),
1461 case LyXTabular::M_VALIGN_TOP:
1462 case LyXTabular::M_VALIGN_BOTTOM:
1463 case LyXTabular::M_VALIGN_MIDDLE:
1465 case LyXTabular::VALIGN_TOP:
1466 case LyXTabular::VALIGN_BOTTOM:
1467 case LyXTabular::VALIGN_MIDDLE:
1468 for (int i = sel_row_start; i <= sel_row_end; ++i)
1469 for (int j = sel_col_start; j <= sel_col_end; ++j)
1470 tabular.setVAlignment(
1471 tabular.getCellNumber(i, j),
1475 case LyXTabular::MULTICOLUMN: {
1476 if (sel_row_start != sel_row_end) {
1477 #ifdef WITH_WARNINGS
1478 #warning Need I say it ? This is horrible.
1480 Alert::error(_("Error setting multicolumn"),
1481 _("You cannot set multicolumn vertically."));
1484 // just multicol for one Single Cell
1485 if (!hasSelection()) {
1486 // check wether we are completly in a multicol
1487 if (tabular.isMultiColumn(actcell))
1488 tabular.unsetMultiColumn(actcell);
1490 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1493 // we have a selection so this means we just add all this
1494 // cells to form a multicolumn cell
1498 if (sel_cell_start > sel_cell_end) {
1499 s_start = sel_cell_end;
1500 s_end = sel_cell_start;
1502 s_start = sel_cell_start;
1503 s_end = sel_cell_end;
1505 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1511 case LyXTabular::SET_ALL_LINES:
1513 case LyXTabular::UNSET_ALL_LINES:
1514 for (int i = sel_row_start; i <= sel_row_end; ++i)
1515 for (int j = sel_col_start; j <= sel_col_end; ++j)
1516 tabular.setAllLines(
1517 tabular.getCellNumber(i,j), setLines);
1520 case LyXTabular::SET_LONGTABULAR:
1521 tabular.setLongTabular(true);
1524 case LyXTabular::UNSET_LONGTABULAR:
1525 tabular.setLongTabular(false);
1528 case LyXTabular::SET_ROTATE_TABULAR:
1529 tabular.setRotateTabular(true);
1532 case LyXTabular::UNSET_ROTATE_TABULAR:
1533 tabular.setRotateTabular(false);
1536 case LyXTabular::SET_ROTATE_CELL:
1537 for (int i = sel_row_start; i <= sel_row_end; ++i)
1538 for (int j = sel_col_start; j <= sel_col_end; ++j)
1539 tabular.setRotateCell(
1540 tabular.getCellNumber(i, j), true);
1543 case LyXTabular::UNSET_ROTATE_CELL:
1544 for (int i = sel_row_start; i <= sel_row_end; ++i)
1545 for (int j = sel_col_start; j <= sel_col_end; ++j)
1546 tabular.setRotateCell(
1547 tabular.getCellNumber(i, j), false);
1550 case LyXTabular::SET_USEBOX: {
1551 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1552 if (val == tabular.getUsebox(actcell))
1553 val = LyXTabular::BOX_NONE;
1554 for (int i = sel_row_start; i <= sel_row_end; ++i)
1555 for (int j = sel_col_start; j <= sel_col_end; ++j)
1556 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1560 case LyXTabular::UNSET_LTFIRSTHEAD:
1562 case LyXTabular::SET_LTFIRSTHEAD:
1563 tabular.getRowOfLTFirstHead(row, ltt);
1564 checkLongtableSpecial(ltt, value, flag);
1565 tabular.setLTHead(row, flag, ltt, true);
1568 case LyXTabular::UNSET_LTHEAD:
1570 case LyXTabular::SET_LTHEAD:
1571 tabular.getRowOfLTHead(row, ltt);
1572 checkLongtableSpecial(ltt, value, flag);
1573 tabular.setLTHead(row, flag, ltt, false);
1576 case LyXTabular::UNSET_LTFOOT:
1578 case LyXTabular::SET_LTFOOT:
1579 tabular.getRowOfLTFoot(row, ltt);
1580 checkLongtableSpecial(ltt, value, flag);
1581 tabular.setLTFoot(row, flag, ltt, false);
1584 case LyXTabular::UNSET_LTLASTFOOT:
1586 case LyXTabular::SET_LTLASTFOOT:
1587 tabular.getRowOfLTLastFoot(row, ltt);
1588 checkLongtableSpecial(ltt, value, flag);
1589 tabular.setLTFoot(row, flag, ltt, true);
1592 case LyXTabular::SET_LTNEWPAGE:
1593 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1596 // dummy stuff just to avoid warnings
1597 case LyXTabular::LAST_ACTION:
1602 InsetTabularMailer(*this).updateDialog(&bv);
1606 void InsetTabular::activateCellInset(LCursor & cur, int cell, int x, int y)
1608 tabular.getCellInset(cell).edit(cur, x, y);
1614 void InsetTabular::activateCellInset(LCursor & cur, int cell, bool behind)
1616 tabular.getCellInset(cell).edit(cur, behind);
1622 bool InsetTabular::showInsetDialog(BufferView * bv) const
1624 InsetTabularMailer(*this).showDialog(bv);
1629 void InsetTabular::openLayoutDialog(BufferView * bv) const
1631 InsetTabularMailer(*this).showDialog(bv);
1636 // function returns an object as defined in func_status.h:
1637 // states OK, Unknown, Disabled, On, Off.
1639 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1642 int action = LyXTabular::LAST_ACTION;
1645 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1646 string const tmp = tabularFeature[i].feature;
1647 if (tmp == what.substr(0, tmp.length())) {
1648 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1649 // tabularFeatures[i].feature.length()))
1650 action = tabularFeature[i].action;
1654 if (action == LyXTabular::LAST_ACTION) {
1656 status.unknown(true);
1660 string const argument
1661 = ltrim(what.substr(tabularFeature[i].feature.length()));
1666 LyXTabular::ltType dummyltt;
1670 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1672 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1675 case LyXTabular::SET_PWIDTH:
1676 case LyXTabular::SET_MPWIDTH:
1677 case LyXTabular::SET_SPECIAL_COLUMN:
1678 case LyXTabular::SET_SPECIAL_MULTI:
1679 case LyXTabular::APPEND_ROW:
1680 case LyXTabular::APPEND_COLUMN:
1681 case LyXTabular::DELETE_ROW:
1682 case LyXTabular::DELETE_COLUMN:
1683 case LyXTabular::SET_ALL_LINES:
1684 case LyXTabular::UNSET_ALL_LINES:
1688 case LyXTabular::MULTICOLUMN:
1689 status.setOnOff(tabular.isMultiColumn(actcell));
1692 case LyXTabular::M_TOGGLE_LINE_TOP:
1694 case LyXTabular::TOGGLE_LINE_TOP:
1695 status.setOnOff(tabular.topLine(actcell, flag));
1698 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1700 case LyXTabular::TOGGLE_LINE_BOTTOM:
1701 status.setOnOff(tabular.bottomLine(actcell, flag));
1704 case LyXTabular::M_TOGGLE_LINE_LEFT:
1706 case LyXTabular::TOGGLE_LINE_LEFT:
1707 status.setOnOff(tabular.leftLine(actcell, flag));
1710 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1712 case LyXTabular::TOGGLE_LINE_RIGHT:
1713 status.setOnOff(tabular.rightLine(actcell, flag));
1716 case LyXTabular::M_ALIGN_LEFT:
1718 case LyXTabular::ALIGN_LEFT:
1719 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1722 case LyXTabular::M_ALIGN_RIGHT:
1724 case LyXTabular::ALIGN_RIGHT:
1725 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1728 case LyXTabular::M_ALIGN_CENTER:
1730 case LyXTabular::ALIGN_CENTER:
1731 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1734 case LyXTabular::ALIGN_BLOCK:
1735 status.disabled(tabular.getPWidth(actcell).zero());
1736 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1739 case LyXTabular::M_VALIGN_TOP:
1741 case LyXTabular::VALIGN_TOP:
1743 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1746 case LyXTabular::M_VALIGN_BOTTOM:
1748 case LyXTabular::VALIGN_BOTTOM:
1750 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1753 case LyXTabular::M_VALIGN_MIDDLE:
1755 case LyXTabular::VALIGN_MIDDLE:
1757 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1760 case LyXTabular::SET_LONGTABULAR:
1761 status.setOnOff(tabular.isLongTabular());
1764 case LyXTabular::UNSET_LONGTABULAR:
1765 status.setOnOff(!tabular.isLongTabular());
1768 case LyXTabular::SET_ROTATE_TABULAR:
1769 status.setOnOff(tabular.getRotateTabular());
1772 case LyXTabular::UNSET_ROTATE_TABULAR:
1773 status.setOnOff(!tabular.getRotateTabular());
1776 case LyXTabular::SET_ROTATE_CELL:
1777 status.setOnOff(tabular.getRotateCell(actcell));
1780 case LyXTabular::UNSET_ROTATE_CELL:
1781 status.setOnOff(!tabular.getRotateCell(actcell));
1784 case LyXTabular::SET_USEBOX:
1785 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1788 case LyXTabular::SET_LTFIRSTHEAD:
1789 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1792 case LyXTabular::SET_LTHEAD:
1793 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1796 case LyXTabular::SET_LTFOOT:
1797 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1800 case LyXTabular::SET_LTLASTFOOT:
1801 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1804 case LyXTabular::SET_LTNEWPAGE:
1805 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1810 status.disabled(true);
1817 void InsetTabular::getLabelList(Buffer const & buffer,
1818 vector<string> & list) const
1820 tabular.getLabelList(buffer, list);
1824 bool InsetTabular::copySelection(BufferView & bv)
1826 if (!hasSelection())
1829 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1830 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1831 if (sel_col_start > sel_col_end) {
1832 sel_col_start = sel_col_end;
1833 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1835 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1838 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1839 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1840 if (sel_row_start > sel_row_end)
1841 swap(sel_row_start, sel_row_end);
1843 paste_tabular.reset(new LyXTabular(tabular));
1844 paste_tabular->setOwner(this);
1846 for (int i = 0; i < sel_row_start; ++i)
1847 paste_tabular->deleteRow(0);
1849 int const rows = sel_row_end - sel_row_start + 1;
1850 while (paste_tabular->rows() > rows)
1851 paste_tabular->deleteRow(rows);
1853 paste_tabular->setTopLine(0, true, true);
1854 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1857 for (int i = 0; i < sel_col_start; ++i)
1858 paste_tabular->deleteColumn(0);
1860 int const columns = sel_col_end - sel_col_start + 1;
1861 while (paste_tabular->columns() > columns)
1862 paste_tabular->deleteColumn(columns);
1864 paste_tabular->setLeftLine(0, true, true);
1865 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1869 OutputParams const runparams;
1870 paste_tabular->plaintext(*bv.buffer(), os, runparams,
1871 ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1872 bv.stuffClipboard(os.str());
1877 bool InsetTabular::pasteSelection(BufferView & bv)
1881 int actcell = bv.cursor().idx();
1882 int actcol = tabular.column_of_cell(actcell);
1883 int actrow = tabular.row_of_cell(actcell);
1884 for (int r1 = 0, r2 = actrow;
1885 r1 < paste_tabular->rows() && r2 < tabular.rows();
1887 for (int c1 = 0, c2 = actcol;
1888 c1 < paste_tabular->columns() && c2 < tabular.columns();
1890 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1891 tabular.isPartOfMultiColumn(r2, c2))
1893 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1897 if (tabular.isPartOfMultiColumn(r2, c2)) {
1901 InsetText & inset = tabular.getCellInset(r2, c2);
1902 inset = paste_tabular->getCellInset(r1, c1);
1903 inset.setOwner(this);
1911 bool InsetTabular::cutSelection(BufferParams const & bp)
1913 if (!hasSelection())
1916 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1917 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1918 if (sel_col_start > sel_col_end) {
1919 sel_col_start = sel_col_end;
1920 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1922 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1925 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1926 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1928 if (sel_row_start > sel_row_end)
1929 swap(sel_row_start, sel_row_end);
1931 if (sel_cell_start > sel_cell_end)
1932 swap(sel_cell_start, sel_cell_end);
1934 for (int i = sel_row_start; i <= sel_row_end; ++i)
1935 for (int j = sel_col_start; j <= sel_col_end; ++j)
1936 tabular.getCellInset(tabular.getCellNumber(i, j))
1937 .clear(bp.tracking_changes);
1942 bool InsetTabular::isRightToLeft(LCursor & cur)
1944 return cur.bv().getParentLanguage(this)->RightToLeft();
1948 void InsetTabular::getSelection(int actcell,
1949 int & srow, int & erow, int & scol, int & ecol) const
1951 int const start = hasSelection() ? sel_cell_start : actcell;
1952 int const end = hasSelection() ? sel_cell_end : actcell;
1954 srow = tabular.row_of_cell(start);
1955 erow = tabular.row_of_cell(end);
1959 scol = tabular.column_of_cell(start);
1960 ecol = tabular.column_of_cell(end);
1964 ecol = tabular.right_column_of_cell(end);
1968 int InsetTabular::numParagraphs() const
1970 return tabular.getNumberOfCells();
1974 LyXText * InsetTabular::getText(int i) const
1976 return i < tabular.getNumberOfCells()
1977 ? tabular.getCellInset(i).getText(0)
1982 void InsetTabular::markErased()
1984 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1985 tabular.getCellInset(cell).markErased();
1989 bool InsetTabular::forceDefaultParagraphs(InsetBase const * in) const
1991 const int cell = tabular.getCellFromInset(in);
1994 return tabular.getPWidth(cell).zero();
1996 // this is a workaround for a crash (New, Insert->Tabular,
1997 // Insert->FootNote)
2001 // well we didn't obviously find it so maybe our owner knows more
2002 BOOST_ASSERT(owner());
2003 return owner()->forceDefaultParagraphs(in);
2007 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2010 if (buf.length() <= 0)
2016 string::size_type len = buf.length();
2017 string::size_type p = 0;
2019 int actcell = bv.cursor().idx();
2020 int actcol = tabular.column_of_cell(actcell);
2021 int actrow = tabular.row_of_cell(actcell);
2023 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2031 maxCols = max(cols, maxCols);
2037 maxCols = max(cols, maxCols);
2038 LyXTabular * loctab;
2043 paste_tabular.reset(
2044 new LyXTabular(bv.buffer()->params(), rows, maxCols));
2045 paste_tabular->setOwner(this);
2046 loctab = paste_tabular.get();
2055 string::size_type op = 0;
2056 int cells = loctab->getNumberOfCells();
2059 rows = loctab->rows();
2060 int const columns = loctab->columns();
2062 while (cell < cells && p < len && row < rows &&
2063 (p = buf.find_first_of("\t\n", p)) != string::npos)
2069 // we can only set this if we are not too far right
2070 if (cols < columns) {
2071 InsetText & inset = loctab->getCellInset(cell);
2072 LyXFont const font = inset.text_.
2073 getFont(inset.paragraphs().begin(), 0);
2074 inset.setText(buf.substr(op, p - op), font);
2080 // we can only set this if we are not too far right
2081 if (cols < columns) {
2082 InsetText & inset = tabular.getCellInset(cell);
2083 LyXFont const font = inset.text_.
2084 getFont(inset.paragraphs().begin(), 0);
2085 inset.setText(buf.substr(op, p - op), font);
2090 cell = loctab->getCellNumber(row, cols);
2096 // check for the last cell if there is no trailing '\n'
2097 if (cell < cells && op < len) {
2098 InsetText & inset = loctab->getCellInset(cell);
2099 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2100 inset.setText(buf.substr(op, len - op), font);
2106 void InsetTabular::addPreview(PreviewLoader & loader) const
2108 int const rows = tabular.rows();
2109 int const columns = tabular.columns();
2110 for (int i = 0; i < rows; ++i) {
2111 for (int j = 0; j < columns; ++j)
2112 tabular.getCellInset(i, j).addPreview(loader);
2118 void InsetTabular::clearSelection() const
2122 has_selection = false;
2126 void InsetTabular::setSelection(int start, int end) const
2128 sel_cell_start = start;
2130 has_selection = true;
2134 string const InsetTabularMailer::name_("tabular");
2136 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2137 : inset_(const_cast<InsetTabular &>(inset))
2141 string const InsetTabularMailer::inset2string(Buffer const &) const
2143 return params2string(inset_);
2147 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2149 istringstream data(in);
2151 lex.setStream(data);
2153 #warning CHECK verify that this is a sane value to return.
2159 string const token = lex.getString();
2167 string const token = lex.getString();
2168 if (token != "\\active_cell")
2171 cell = lex.getInteger();
2174 // This is part of the inset proper that is usually swallowed
2175 // by Buffer::readInset
2178 string const token = lex.getString();
2179 if (token != "Tabular")
2186 Buffer const & buffer = inset.buffer();
2187 inset.read(buffer, lex);
2189 // We can't set the active cell, but we can tell the frontend
2195 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2199 //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2200 data << name_ << " \\active_cell " << 0 << '\n';
2201 inset.write(inset.buffer(), data);
2202 data << "\\end_inset\n";