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;
252 setPosCache(pi, x, y);
257 x += ADD_TO_TABULAR_WIDTH;
260 first_visible_cell = -1;
261 for (int i = 0; i < tabular.rows(); ++i) {
263 cell = tabular.getCellNumber(i, 0);
264 if (y + tabular.getDescentOfRow(i) <= 0 &&
265 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
267 y += tabular.getDescentOfRow(i) +
268 tabular.getAscentOfRow(i + 1) +
269 tabular.getAdditionalHeight(i + 1);
272 for (int j = 0; j < tabular.columns(); ++j) {
273 if (nx > bv->workWidth())
275 if (tabular.isPartOfMultiColumn(i, j))
277 if (first_visible_cell < 0)
278 first_visible_cell = cell;
280 drawCellSelection(pi.pain, nx, y, i, j, cell);
282 int const cx = nx + tabular.getBeginningOfTextInCell(cell);
283 tabular.getCellInset(cell).draw(pi, cx, y);
284 drawCellLines(pi.pain, nx, y, i, cell);
285 nx += tabular.getWidthOfColumn(cell);
289 // Would be nice, but for some completely unfathomable reason,
290 // on a col resize to a new fixed width, even though the insettexts
291 // are resized, the cell isn't, but drawing all cells in a tall table
292 // has the desired effect somehow. Complete dark magic.
294 // avoiding drawing the rest of a long table is
295 // a pretty big speedup
296 if (y > bv->workHeight())
300 y += tabular.getDescentOfRow(i) +
301 tabular.getAscentOfRow(i + 1) +
302 tabular.getAdditionalHeight(i + 1);
307 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
308 int row, int cell) const
310 int x2 = x + tabular.getWidthOfColumn(cell);
313 if (!tabular.topAlreadyDrawn(cell)) {
314 on_off = !tabular.topLine(cell);
315 pain.line(x, y - tabular.getAscentOfRow(row),
316 x2, y - tabular.getAscentOfRow(row),
317 on_off ? LColor::tabularonoffline : LColor::tabularline,
318 on_off ? Painter::line_onoffdash : Painter::line_solid);
320 on_off = !tabular.bottomLine(cell);
321 pain.line(x, y + tabular.getDescentOfRow(row),
322 x2, y + tabular.getDescentOfRow(row),
323 on_off ? LColor::tabularonoffline : LColor::tabularline,
324 on_off ? Painter::line_onoffdash : Painter::line_solid);
325 if (!tabular.leftAlreadyDrawn(cell)) {
326 on_off = !tabular.leftLine(cell);
327 pain.line(x, y - tabular.getAscentOfRow(row),
328 x, y + tabular.getDescentOfRow(row),
329 on_off ? LColor::tabularonoffline : LColor::tabularline,
330 on_off ? Painter::line_onoffdash : Painter::line_solid);
332 on_off = !tabular.rightLine(cell);
333 pain.line(x2 - tabular.getAdditionalWidth(cell),
334 y - tabular.getAscentOfRow(row),
335 x2 - tabular.getAdditionalWidth(cell),
336 y + tabular.getDescentOfRow(row),
337 on_off ? LColor::tabularonoffline : LColor::tabularline,
338 on_off ? Painter::line_onoffdash : Painter::line_solid);
342 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
343 int row, int column, int cell) const
348 BOOST_ASSERT(hasSelection());
349 int cs = tabular.column_of_cell(sel_cell_start);
350 int ce = tabular.column_of_cell(sel_cell_end);
353 cs = tabular.column_of_cell(sel_cell_end);
355 ce = tabular.right_column_of_cell(sel_cell_end);
358 int rs = tabular.row_of_cell(sel_cell_start);
359 int re = tabular.row_of_cell(sel_cell_end);
363 if (column >= cs && column <= ce && row >= rs && row <= re) {
364 int w = tabular.getWidthOfColumn(cell);
365 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
366 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
367 w, h, LColor::selection);
372 string const InsetTabular::editMessage() const
374 return _("Opened table");
378 void InsetTabular::updateLocal(LCursor & cur) const
385 void InsetTabular::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
387 if (hasSelection() && cmd.button() == mouse_button::button3)
390 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
393 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
396 //cur.cursor_ = theTempCursor;
401 setPos(cur, cmd.x, cmd.y);
402 //cur.cursor_ = theTempCursor;
406 lyxerr << cur << endl;
408 if (cmd.button() == mouse_button::button2)
409 dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
413 void InsetTabular::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
415 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
416 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
418 setPos(cur, cmd.x, cmd.y);
419 if (!hasSelection()) {
420 setSelection(actcell, actcell);
424 setSelection(sel_cell_start, actcell);
425 tablemode = (sel_cell_start != actcell);
430 void InsetTabular::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
432 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
433 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
434 if (cmd.button() == mouse_button::button3)
435 InsetTabularMailer(*this).showDialog(&cur.bv());
439 void InsetTabular::edit(LCursor & cur, bool left)
441 lyxerr << "InsetTabular::edit: " << this << endl;
446 if (isRightToLeft(cur))
447 cell = tabular.getLastCellInRow(0);
451 if (isRightToLeft(cur))
452 cell = tabular.getFirstCellInRow(tabular.rows()-1);
454 cell = tabular.getNumberOfCells() - 1;
458 cur.bv().fitCursor();
464 void InsetTabular::edit(LCursor & cur, int x, int y)
466 lyxerr << "InsetTabular::edit: " << this << " first cell "
467 << &tabular.cell_info[0][0].inset << endl;
473 //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
476 // activateCellInset(bv, cell, x - xx, y - cursory_);
481 InsetTabular::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
483 lyxerr << "# InsetTabular::dispatch: " << cmd
484 << " tablemode: " << tablemode << endl;
486 DispatchResult result(true, true);
487 switch (cmd.action) {
489 case LFUN_MOUSE_PRESS:
490 lfunMousePress(cur, cmd);
491 return DispatchResult(true, true);
493 case LFUN_MOUSE_MOTION:
494 lfunMouseMotion(cur, cmd);
495 return DispatchResult(true, true);
497 case LFUN_MOUSE_RELEASE:
498 lfunMouseRelease(cur, cmd);
499 return DispatchResult(true, true);
506 int const cell = cur.idx();
507 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
508 result = tabular.getCellInset(cell).dispatch(cur, cmd);
510 switch (result.val()) {
512 if (movePrevCell(cur))
513 result = DispatchResult(true, true);
515 result = DispatchResult(false, FINISHED);
519 if (moveNextCell(cur))
520 result = DispatchResult(true, true);
522 result = DispatchResult(false, FINISHED_RIGHT);
527 result = DispatchResult(true, true);
529 result = DispatchResult(false, FINISHED_UP);
533 if (moveDownLock(cur))
534 result = DispatchResult(true, true);
536 result = DispatchResult(false, FINISHED_UP);
540 lyxerr << "# don't handle dispatch" << endl;
544 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
547 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
548 switch (cmd.action) {
550 case LFUN_CELL_BACKWARD:
551 case LFUN_CELL_FORWARD:
552 if (cmd.action == LFUN_CELL_FORWARD)
559 case LFUN_SCROLL_INSET:
560 if (!cmd.argument.empty()) {
561 if (cmd.argument.find('.') != cmd.argument.npos)
562 scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
564 scroll(cur.bv(), strToInt(cmd.argument));
566 return DispatchResult(true, true);
569 case LFUN_RIGHTSEL: {
570 int const start = hasSelection() ? sel_cell_start : cur.idx();
571 if (tabular.isLastCellInRow(cur.idx())) {
572 setSelection(start, cur.idx());
577 // if we are starting a selection, only select
578 // the current cell at the beginning
579 if (hasSelection()) {
583 setSelection(start, end);
588 if (!moveRightLock(cur))
589 result = DispatchResult(false, FINISHED_RIGHT);
594 int const start = hasSelection() ? sel_cell_start : cur.idx();
595 if (tabular.isFirstCellInRow(cur.idx())) {
596 setSelection(start, cur.idx());
601 // if we are starting a selection, only select
602 // the current cell at the beginning
603 if (hasSelection()) {
607 setSelection(start, end);
612 if (!moveLeftLock(cur))
613 result = DispatchResult(false, FINISHED);
618 int const start = hasSelection() ? sel_cell_start : cur.idx();
619 int const ocell = cur.idx();
620 // if we are starting a selection, only select
621 // the current cell at the beginning
622 if (hasSelection()) {
624 if (ocell == sel_cell_end ||
625 tabular.column_of_cell(ocell) >
626 tabular.column_of_cell(cur.idx()))
627 setSelection(start, tabular.getCellBelow(sel_cell_end));
629 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
631 setSelection(start, start);
638 result = DispatchResult(false, FINISHED_DOWN);
643 int const start = hasSelection() ? sel_cell_start : cur.idx();
644 int const ocell = cur.idx();
645 // if we are starting a selection, only select
646 // the current cell at the beginning
647 if (hasSelection()) {
649 if (ocell == sel_cell_end ||
650 tabular.column_of_cell(ocell) >
651 tabular.column_of_cell(cur.idx()))
652 setSelection(start, tabular.getCellAbove(sel_cell_end));
654 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
656 setSelection(start, start);
663 result = DispatchResult(false, FINISHED_DOWN);
670 int actcell = cur.idx();
671 int actcol = tabular.column_of_cell(actcell);
673 if (cur.bv().top_y() + cur.bv().painter().paperHeight()
674 < yo_ + tabular.getHeightOfTabular())
676 cur.bv().scrollDocView(
677 cur.bv().top_y() + cur.bv().painter().paperHeight());
678 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
680 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
689 int column = tabular.column_of_cell(cur.idx());
691 cur.bv().scrollDocView(
692 cur.bv().top_y() - cur.bv().painter().paperHeight());
696 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
704 // none of these make sense for insettabular,
705 // but we must catch them to prevent any
706 // selection from being confused
710 case LFUN_WORDLEFTSEL:
712 case LFUN_WORDRIGHTSEL:
714 case LFUN_DOWN_PARAGRAPH:
715 case LFUN_DOWN_PARAGRAPHSEL:
716 case LFUN_UP_PARAGRAPH:
717 case LFUN_UP_PARAGRAPHSEL:
723 case LFUN_BEGINNINGBUF:
724 case LFUN_BEGINNINGBUFSEL:
729 case LFUN_LAYOUT_TABULAR:
730 InsetTabularMailer(*this).showDialog(&cur.bv());
733 case LFUN_INSET_DIALOG_UPDATE:
734 InsetTabularMailer(*this).updateDialog(&cur.bv());
737 case LFUN_TABULAR_FEATURE:
738 if (!tabularFeatures(cur, cmd.argument))
739 result = DispatchResult(false);
742 // insert file functions
743 case LFUN_FILE_INSERT_ASCII_PARA:
744 case LFUN_FILE_INSERT_ASCII: {
745 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
746 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
747 result = DispatchResult(false);
761 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
765 if (copySelection(cur.bv())) {
766 recordUndo(cur, Undo::DELETE);
767 cutSelection(cur.bv().buffer()->params());
772 recordUndo(cur, Undo::DELETE);
773 cutSelection(cur.bv().buffer()->params());
780 copySelection(cur.bv());
783 case LFUN_PASTESELECTION: {
784 string const clip = cur.bv().getClipboard();
787 if (clip.find('\t') != string::npos) {
791 string::size_type len = clip.length();
792 string::size_type p = 0;
795 (p = clip.find_first_of("\t\n", p)) != string::npos) {
803 maxCols = max(cols, maxCols);
809 maxCols = max(cols, maxCols);
812 new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
814 string::size_type op = 0;
816 int cells = paste_tabular->getNumberOfCells();
820 while (cell < cells && p < len &&
821 (p = clip.find_first_of("\t\n", p)) != string::npos) {
826 paste_tabular->getCellInset(cell).
827 setText(clip.substr(op, p-op), font);
832 paste_tabular->getCellInset(cell).
833 setText(clip.substr(op, p-op), font);
834 while (cols++ < maxCols)
842 // check for the last cell if there is no trailing '\n'
843 if (cell < cells && op < len)
844 paste_tabular->getCellInset(cell).
845 setText(clip.substr(op, len-op), font);
846 } else if (!insertAsciiString(cur.bv(), clip, true))
848 // so that the clipboard is used and it goes on
850 // and executes LFUN_PASTESELECTION in insettext!
851 paste_tabular.reset();
857 if (hasPasteBuffer()) {
858 recordUndo(cur, Undo::INSERT);
859 pasteSelection(cur.bv());
864 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
867 // handle font changing stuff on selection before we lock the inset
868 // in the default part!
869 result = DispatchResult(false);
870 // we try to activate the actual inset and put this event down to
871 // the insets dispatch function.
876 InsetTabularMailer(*this).updateDialog(&cur.bv());
879 if (cmd.action == LFUN_INSET_TOGGLE) {
880 tablemode = !tablemode;
881 result = DispatchResult(true, true);
888 int InsetTabular::latex(Buffer const & buf, ostream & os,
889 OutputParams const & runparams) const
891 return tabular.latex(buf, os, runparams);
895 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
896 OutputParams const & runparams) const
898 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
899 return tabular.plaintext(buf, os, runparams, dp, false, 0);
903 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
904 OutputParams const & runparams) const
906 return tabular.linuxdoc(buf,os, runparams);
910 int InsetTabular::docbook(Buffer const & buf, ostream & os,
911 OutputParams const & runparams) const
916 // if the table is inside a float it doesn't need the informaltable
917 // wrapper. Search for it.
918 for (master = owner(); master; master = master->owner())
919 if (master->lyxCode() == InsetOld::FLOAT_CODE)
923 os << "<informaltable>";
924 if (runparams.mixed_content)
928 ret += tabular.docbook(buf, os, runparams);
930 os << "</informaltable>";
931 if (runparams.mixed_content)
939 void InsetTabular::validate(LaTeXFeatures & features) const
941 tabular.validate(features);
945 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
947 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
950 for (int j = 0; j < tabular.columns(); ++j) {
951 if (tabular.isPartOfMultiColumn(i, j))
957 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
958 tabular.getCellInset(cell).metrics(m, dim);
959 maxAsc = max(maxAsc, dim.asc);
960 maxDesc = max(maxDesc, dim.des);
961 tabular.setWidthOfCell(cell, dim.wid);
963 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
964 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
969 void InsetTabular::getCursorPos(CursorSlice const & cur, int & x, int & y) const
971 tabular.getCellInset(cur.idx()).getCursorPos(cur, x, y);
975 void InsetTabular::setPos(LCursor & cur, int x, int y) const
977 //lyxerr << "# InsetTabular::setPos() cursor: " << cur << endl;
978 int const cell = getCell(x + xo_, y + yo_);
979 InsetText const & inset = tabular.getCellInset(cell);
980 inset.text_.setCursorFromCoordinates(cur.current(), x, y);
985 int ly = tabular.getDescentOfRow(actrow);
987 // first search the right row
988 while (ly < y && actrow + 1 < tabular.rows()) {
989 cursory_ += tabular.getDescentOfRow(actrow) +
990 tabular.getAscentOfRow(actrow + 1) +
991 tabular.getAdditionalHeight(actrow + 1);
993 ly = cursory_ + tabular.getDescentOfRow(actrow);
995 actcell = tabular.getCellNumber(actrow, actcol);
997 // now search the right column
998 int lx = tabular.getWidthOfColumn(actcell) -
999 tabular.getAdditionalWidth(actcell);
1001 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1002 lx += tabular.getWidthOfColumn(actcell + 1)
1003 + tabular.getAdditionalWidth(actcell);
1005 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1010 int InsetTabular::getCellXPos(int cell) const
1014 for (; !tabular.isFirstCellInRow(c); --c)
1016 int lx = tabular.getWidthOfColumn(cell);
1017 for (; c < cell; ++c)
1018 lx += tabular.getWidthOfColumn(c);
1020 return lx - tabular.getWidthOfColumn(cell) + xo_;
1024 void InsetTabular::resetPos(LCursor & cur) const
1027 #ifdef WITH_WARNINGS
1028 #warning This should be fixed in the right manner (20011128 Jug)
1030 // fast hack to fix infinite repaintings!
1031 if (in_reset_pos > 0)
1034 BufferView & bv = cur.bv();
1036 int actcell = cur.cell();
1037 int actcol = tabular.column_of_cell(actcell);
1040 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1041 if (tabular.isLastCellInRow(cell)) {
1042 cursory_ += tabular.getDescentOfRow(actrow) +
1043 tabular.getAscentOfRow(actrow + 1) +
1044 tabular.getAdditionalHeight(actrow + 1);
1049 // we need this only from here on!!!
1051 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1052 int new_x = getCellXPos(actcell) + offset;
1053 int old_x = cursorx_;
1055 // cursor.x(getCellXPos(actcell) + offset);
1056 if (actcol < tabular.columns() - 1 && scroll(false) &&
1057 tabular.getWidthOfTabular() < bv->workWidth()-20)
1061 } else if (cursorx_ - offset > 20 &&
1062 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1063 > bv.workWidth() - 20) {
1064 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1066 } else if (cursorx_ - offset < 20) {
1067 scroll(&bv, 20 - cursorx_ + offset);
1069 } else if (scroll() && xo_ > 20 &&
1070 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1071 scroll(&bv, old_x - cursorx_);
1074 InsetTabularMailer(*this).updateDialog(bv);
1080 bool InsetTabular::moveRight(LCursor & cur)
1082 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1090 bool InsetTabular::moveRightLock(LCursor & cur)
1092 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1095 activateCellInset(cur, cur.idx(), false);
1100 bool InsetTabular::moveLeft(LCursor & cur)
1102 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1110 bool InsetTabular::moveLeftLock(LCursor & cur)
1112 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1115 activateCellInset(cur, cur.idx(), true);
1120 bool InsetTabular::moveUp(LCursor & cur)
1122 if (tabular.row_of_cell(cur.idx()) == 0)
1124 cur.idx() = tabular.getCellAbove(cur.idx());
1130 bool InsetTabular::moveUpLock(LCursor & cur)
1132 if (tabular.row_of_cell(cur.idx()) == 0)
1134 cur.idx() = tabular.getCellAbove(cur.idx());
1136 activateCellInset(cur, cur.idx(), cur.x_target(), 0);
1141 bool InsetTabular::moveDown(LCursor & cur)
1143 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1145 cur.idx() = tabular.getCellBelow(cur.idx());
1151 bool InsetTabular::moveDownLock(LCursor & cur)
1153 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1155 cur.idx() = tabular.getCellBelow(cur.idx());
1157 activateCellInset(cur, cur.idx(), cur.x_target());
1162 bool InsetTabular::moveNextCell(LCursor & cur)
1164 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1165 if (isRightToLeft(cur)) {
1166 if (tabular.isFirstCellInRow(cur.idx())) {
1167 int row = tabular.row_of_cell(cur.idx());
1168 if (row == tabular.rows() - 1)
1170 cur.idx() = tabular.getLastCellInRow(row);
1171 cur.idx() = tabular.getCellBelow(cur.idx());
1178 if (tabular.isLastCell(cur.idx()))
1184 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1190 bool InsetTabular::movePrevCell(LCursor & cur)
1192 if (isRightToLeft(cur)) {
1193 if (tabular.isLastCellInRow(cur.idx())) {
1194 int row = tabular.row_of_cell(cur.idx());
1197 cur.idx() = tabular.getFirstCellInRow(row);
1198 cur.idx() = tabular.getCellAbove(cur.idx());
1200 if (tabular.isLastCell(cur.idx()))
1205 if (cur.idx() == 0) // first cell
1216 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1218 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1221 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1222 string const tmp = tabularFeature[i].feature;
1224 if (tmp == what.substr(0, tmp.length())) {
1225 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1226 //tabularFeatures[i].feature.length())) {
1227 action = tabularFeature[i].action;
1231 if (action == LyXTabular::LAST_ACTION)
1235 ltrim(what.substr(tabularFeature[i].feature.length()));
1236 tabularFeatures(cur, action, val);
1243 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1244 string const & special, bool & flag)
1246 if (special == "dl_above") {
1249 } else if (special == "dl_below") {
1250 ltt.bottomDL = flag;
1252 } else if (special == "empty") {
1264 void InsetTabular::tabularFeatures(LCursor & cur,
1265 LyXTabular::Feature feature, string const & value)
1267 BufferView & bv = cur.bv();
1268 int actcell = cur.idx();
1273 bool setLines = false;
1274 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1275 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1279 case LyXTabular::M_ALIGN_LEFT:
1280 case LyXTabular::ALIGN_LEFT:
1281 setAlign = LYX_ALIGN_LEFT;
1284 case LyXTabular::M_ALIGN_RIGHT:
1285 case LyXTabular::ALIGN_RIGHT:
1286 setAlign = LYX_ALIGN_RIGHT;
1289 case LyXTabular::M_ALIGN_CENTER:
1290 case LyXTabular::ALIGN_CENTER:
1291 setAlign = LYX_ALIGN_CENTER;
1294 case LyXTabular::ALIGN_BLOCK:
1295 setAlign = LYX_ALIGN_BLOCK;
1298 case LyXTabular::M_VALIGN_TOP:
1299 case LyXTabular::VALIGN_TOP:
1300 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1303 case LyXTabular::M_VALIGN_BOTTOM:
1304 case LyXTabular::VALIGN_BOTTOM:
1305 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1308 case LyXTabular::M_VALIGN_MIDDLE:
1309 case LyXTabular::VALIGN_MIDDLE:
1310 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1317 if (hasSelection()) {
1318 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1320 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1321 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1323 recordUndo(cur, Undo::ATOMIC);
1325 int row = tabular.row_of_cell(actcell);
1326 int column = tabular.column_of_cell(actcell);
1328 LyXTabular::ltType ltt;
1332 case LyXTabular::SET_PWIDTH: {
1333 LyXLength const len(value);
1334 tabular.setColumnPWidth(actcell, len);
1336 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1337 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1338 else if (!len.zero()
1339 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1340 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1344 case LyXTabular::SET_MPWIDTH:
1345 tabular.setMColumnPWidth(actcell, LyXLength(value));
1348 case LyXTabular::SET_SPECIAL_COLUMN:
1349 case LyXTabular::SET_SPECIAL_MULTI:
1350 tabular.setAlignSpecial(actcell,value,feature);
1353 case LyXTabular::APPEND_ROW:
1354 // append the row into the tabular
1355 tabular.appendRow(bv.buffer()->params(), actcell);
1356 tabular.setOwner(this);
1359 case LyXTabular::APPEND_COLUMN:
1360 // append the column into the tabular
1361 tabular.appendColumn(bv.buffer()->params(), actcell);
1362 tabular.setOwner(this);
1363 actcell = tabular.getCellNumber(row, column);
1366 case LyXTabular::DELETE_ROW:
1367 for (int i = sel_row_start; i <= sel_row_end; ++i)
1368 tabular.deleteRow(sel_row_start);
1369 if (sel_row_start >= tabular.rows())
1371 actcell = tabular.getCellNumber(sel_row_start, column);
1375 case LyXTabular::DELETE_COLUMN:
1376 for (int i = sel_col_start; i <= sel_col_end; ++i)
1377 tabular.deleteColumn(sel_col_start);
1378 if (sel_col_start >= tabular.columns())
1380 actcell = tabular.getCellNumber(row, sel_col_start);
1384 case LyXTabular::M_TOGGLE_LINE_TOP:
1386 case LyXTabular::TOGGLE_LINE_TOP: {
1387 bool lineSet = !tabular.topLine(actcell, flag);
1388 for (int i = sel_row_start; i <= sel_row_end; ++i)
1389 for (int j = sel_col_start; j <= sel_col_end; ++j)
1391 tabular.getCellNumber(i, j),
1396 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1398 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1399 bool lineSet = !tabular.bottomLine(actcell, flag);
1400 for (int i = sel_row_start; i <= sel_row_end; ++i)
1401 for (int j = sel_col_start; j <= sel_col_end; ++j)
1402 tabular.setBottomLine(
1403 tabular.getCellNumber(i, j),
1409 case LyXTabular::M_TOGGLE_LINE_LEFT:
1411 case LyXTabular::TOGGLE_LINE_LEFT: {
1412 bool lineSet = !tabular.leftLine(actcell, flag);
1413 for (int i = sel_row_start; i <= sel_row_end; ++i)
1414 for (int j = sel_col_start; j <= sel_col_end; ++j)
1415 tabular.setLeftLine(
1416 tabular.getCellNumber(i,j),
1422 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1424 case LyXTabular::TOGGLE_LINE_RIGHT: {
1425 bool lineSet = !tabular.rightLine(actcell, flag);
1426 for (int i = sel_row_start; i <= sel_row_end; ++i)
1427 for (int j = sel_col_start; j <= sel_col_end; ++j)
1428 tabular.setRightLine(
1429 tabular.getCellNumber(i,j),
1435 case LyXTabular::M_ALIGN_LEFT:
1436 case LyXTabular::M_ALIGN_RIGHT:
1437 case LyXTabular::M_ALIGN_CENTER:
1439 case LyXTabular::ALIGN_LEFT:
1440 case LyXTabular::ALIGN_RIGHT:
1441 case LyXTabular::ALIGN_CENTER:
1442 case LyXTabular::ALIGN_BLOCK:
1443 for (int i = sel_row_start; i <= sel_row_end; ++i)
1444 for (int j = sel_col_start; j <= sel_col_end; ++j)
1445 tabular.setAlignment(
1446 tabular.getCellNumber(i, j),
1451 case LyXTabular::M_VALIGN_TOP:
1452 case LyXTabular::M_VALIGN_BOTTOM:
1453 case LyXTabular::M_VALIGN_MIDDLE:
1455 case LyXTabular::VALIGN_TOP:
1456 case LyXTabular::VALIGN_BOTTOM:
1457 case LyXTabular::VALIGN_MIDDLE:
1458 for (int i = sel_row_start; i <= sel_row_end; ++i)
1459 for (int j = sel_col_start; j <= sel_col_end; ++j)
1460 tabular.setVAlignment(
1461 tabular.getCellNumber(i, j),
1465 case LyXTabular::MULTICOLUMN: {
1466 if (sel_row_start != sel_row_end) {
1467 #ifdef WITH_WARNINGS
1468 #warning Need I say it ? This is horrible.
1470 Alert::error(_("Error setting multicolumn"),
1471 _("You cannot set multicolumn vertically."));
1474 // just multicol for one Single Cell
1475 if (!hasSelection()) {
1476 // check wether we are completly in a multicol
1477 if (tabular.isMultiColumn(actcell))
1478 tabular.unsetMultiColumn(actcell);
1480 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1483 // we have a selection so this means we just add all this
1484 // cells to form a multicolumn cell
1488 if (sel_cell_start > sel_cell_end) {
1489 s_start = sel_cell_end;
1490 s_end = sel_cell_start;
1492 s_start = sel_cell_start;
1493 s_end = sel_cell_end;
1495 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1501 case LyXTabular::SET_ALL_LINES:
1503 case LyXTabular::UNSET_ALL_LINES:
1504 for (int i = sel_row_start; i <= sel_row_end; ++i)
1505 for (int j = sel_col_start; j <= sel_col_end; ++j)
1506 tabular.setAllLines(
1507 tabular.getCellNumber(i,j), setLines);
1510 case LyXTabular::SET_LONGTABULAR:
1511 tabular.setLongTabular(true);
1514 case LyXTabular::UNSET_LONGTABULAR:
1515 tabular.setLongTabular(false);
1518 case LyXTabular::SET_ROTATE_TABULAR:
1519 tabular.setRotateTabular(true);
1522 case LyXTabular::UNSET_ROTATE_TABULAR:
1523 tabular.setRotateTabular(false);
1526 case LyXTabular::SET_ROTATE_CELL:
1527 for (int i = sel_row_start; i <= sel_row_end; ++i)
1528 for (int j = sel_col_start; j <= sel_col_end; ++j)
1529 tabular.setRotateCell(
1530 tabular.getCellNumber(i, j), true);
1533 case LyXTabular::UNSET_ROTATE_CELL:
1534 for (int i = sel_row_start; i <= sel_row_end; ++i)
1535 for (int j = sel_col_start; j <= sel_col_end; ++j)
1536 tabular.setRotateCell(
1537 tabular.getCellNumber(i, j), false);
1540 case LyXTabular::SET_USEBOX: {
1541 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1542 if (val == tabular.getUsebox(actcell))
1543 val = LyXTabular::BOX_NONE;
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.setUsebox(tabular.getCellNumber(i, j), val);
1550 case LyXTabular::UNSET_LTFIRSTHEAD:
1552 case LyXTabular::SET_LTFIRSTHEAD:
1553 tabular.getRowOfLTFirstHead(row, ltt);
1554 checkLongtableSpecial(ltt, value, flag);
1555 tabular.setLTHead(row, flag, ltt, true);
1558 case LyXTabular::UNSET_LTHEAD:
1560 case LyXTabular::SET_LTHEAD:
1561 tabular.getRowOfLTHead(row, ltt);
1562 checkLongtableSpecial(ltt, value, flag);
1563 tabular.setLTHead(row, flag, ltt, false);
1566 case LyXTabular::UNSET_LTFOOT:
1568 case LyXTabular::SET_LTFOOT:
1569 tabular.getRowOfLTFoot(row, ltt);
1570 checkLongtableSpecial(ltt, value, flag);
1571 tabular.setLTFoot(row, flag, ltt, false);
1574 case LyXTabular::UNSET_LTLASTFOOT:
1576 case LyXTabular::SET_LTLASTFOOT:
1577 tabular.getRowOfLTLastFoot(row, ltt);
1578 checkLongtableSpecial(ltt, value, flag);
1579 tabular.setLTFoot(row, flag, ltt, true);
1582 case LyXTabular::SET_LTNEWPAGE:
1583 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1586 // dummy stuff just to avoid warnings
1587 case LyXTabular::LAST_ACTION:
1592 InsetTabularMailer(*this).updateDialog(&bv);
1596 void InsetTabular::activateCellInset(LCursor & cur, int cell, int x, int y)
1598 tabular.getCellInset(cell).edit(cur, x, y);
1604 void InsetTabular::activateCellInset(LCursor & cur, int cell, bool behind)
1606 tabular.getCellInset(cell).edit(cur, behind);
1612 bool InsetTabular::showInsetDialog(BufferView * bv) const
1614 InsetTabularMailer(*this).showDialog(bv);
1619 void InsetTabular::openLayoutDialog(BufferView * bv) const
1621 InsetTabularMailer(*this).showDialog(bv);
1626 // function returns an object as defined in func_status.h:
1627 // states OK, Unknown, Disabled, On, Off.
1629 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1632 int action = LyXTabular::LAST_ACTION;
1635 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1636 string const tmp = tabularFeature[i].feature;
1637 if (tmp == what.substr(0, tmp.length())) {
1638 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1639 // tabularFeatures[i].feature.length()))
1640 action = tabularFeature[i].action;
1644 if (action == LyXTabular::LAST_ACTION) {
1646 status.unknown(true);
1650 string const argument
1651 = ltrim(what.substr(tabularFeature[i].feature.length()));
1656 LyXTabular::ltType dummyltt;
1660 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1662 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1665 case LyXTabular::SET_PWIDTH:
1666 case LyXTabular::SET_MPWIDTH:
1667 case LyXTabular::SET_SPECIAL_COLUMN:
1668 case LyXTabular::SET_SPECIAL_MULTI:
1669 case LyXTabular::APPEND_ROW:
1670 case LyXTabular::APPEND_COLUMN:
1671 case LyXTabular::DELETE_ROW:
1672 case LyXTabular::DELETE_COLUMN:
1673 case LyXTabular::SET_ALL_LINES:
1674 case LyXTabular::UNSET_ALL_LINES:
1678 case LyXTabular::MULTICOLUMN:
1679 status.setOnOff(tabular.isMultiColumn(actcell));
1682 case LyXTabular::M_TOGGLE_LINE_TOP:
1684 case LyXTabular::TOGGLE_LINE_TOP:
1685 status.setOnOff(tabular.topLine(actcell, flag));
1688 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1690 case LyXTabular::TOGGLE_LINE_BOTTOM:
1691 status.setOnOff(tabular.bottomLine(actcell, flag));
1694 case LyXTabular::M_TOGGLE_LINE_LEFT:
1696 case LyXTabular::TOGGLE_LINE_LEFT:
1697 status.setOnOff(tabular.leftLine(actcell, flag));
1700 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1702 case LyXTabular::TOGGLE_LINE_RIGHT:
1703 status.setOnOff(tabular.rightLine(actcell, flag));
1706 case LyXTabular::M_ALIGN_LEFT:
1708 case LyXTabular::ALIGN_LEFT:
1709 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1712 case LyXTabular::M_ALIGN_RIGHT:
1714 case LyXTabular::ALIGN_RIGHT:
1715 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1718 case LyXTabular::M_ALIGN_CENTER:
1720 case LyXTabular::ALIGN_CENTER:
1721 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1724 case LyXTabular::ALIGN_BLOCK:
1725 status.disabled(tabular.getPWidth(actcell).zero());
1726 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1729 case LyXTabular::M_VALIGN_TOP:
1731 case LyXTabular::VALIGN_TOP:
1733 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1736 case LyXTabular::M_VALIGN_BOTTOM:
1738 case LyXTabular::VALIGN_BOTTOM:
1740 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1743 case LyXTabular::M_VALIGN_MIDDLE:
1745 case LyXTabular::VALIGN_MIDDLE:
1747 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1750 case LyXTabular::SET_LONGTABULAR:
1751 status.setOnOff(tabular.isLongTabular());
1754 case LyXTabular::UNSET_LONGTABULAR:
1755 status.setOnOff(!tabular.isLongTabular());
1758 case LyXTabular::SET_ROTATE_TABULAR:
1759 status.setOnOff(tabular.getRotateTabular());
1762 case LyXTabular::UNSET_ROTATE_TABULAR:
1763 status.setOnOff(!tabular.getRotateTabular());
1766 case LyXTabular::SET_ROTATE_CELL:
1767 status.setOnOff(tabular.getRotateCell(actcell));
1770 case LyXTabular::UNSET_ROTATE_CELL:
1771 status.setOnOff(!tabular.getRotateCell(actcell));
1774 case LyXTabular::SET_USEBOX:
1775 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1778 case LyXTabular::SET_LTFIRSTHEAD:
1779 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1782 case LyXTabular::SET_LTHEAD:
1783 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1786 case LyXTabular::SET_LTFOOT:
1787 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1790 case LyXTabular::SET_LTLASTFOOT:
1791 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1794 case LyXTabular::SET_LTNEWPAGE:
1795 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1800 status.disabled(true);
1807 void InsetTabular::getLabelList(Buffer const & buffer,
1808 vector<string> & list) const
1810 tabular.getLabelList(buffer, list);
1814 bool InsetTabular::copySelection(BufferView & bv)
1816 if (!hasSelection())
1819 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1820 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1821 if (sel_col_start > sel_col_end) {
1822 sel_col_start = sel_col_end;
1823 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1825 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1828 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1829 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1830 if (sel_row_start > sel_row_end)
1831 swap(sel_row_start, sel_row_end);
1833 paste_tabular.reset(new LyXTabular(tabular));
1834 paste_tabular->setOwner(this);
1836 for (int i = 0; i < sel_row_start; ++i)
1837 paste_tabular->deleteRow(0);
1839 int const rows = sel_row_end - sel_row_start + 1;
1840 while (paste_tabular->rows() > rows)
1841 paste_tabular->deleteRow(rows);
1843 paste_tabular->setTopLine(0, true, true);
1844 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1847 for (int i = 0; i < sel_col_start; ++i)
1848 paste_tabular->deleteColumn(0);
1850 int const columns = sel_col_end - sel_col_start + 1;
1851 while (paste_tabular->columns() > columns)
1852 paste_tabular->deleteColumn(columns);
1854 paste_tabular->setLeftLine(0, true, true);
1855 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1859 OutputParams const runparams;
1860 paste_tabular->plaintext(*bv.buffer(), os, runparams,
1861 ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1862 bv.stuffClipboard(os.str());
1867 bool InsetTabular::pasteSelection(BufferView & bv)
1871 int actcell = bv.cursor().idx();
1872 int actcol = tabular.column_of_cell(actcell);
1873 int actrow = tabular.row_of_cell(actcell);
1874 for (int r1 = 0, r2 = actrow;
1875 r1 < paste_tabular->rows() && r2 < tabular.rows();
1877 for (int c1 = 0, c2 = actcol;
1878 c1 < paste_tabular->columns() && c2 < tabular.columns();
1880 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1881 tabular.isPartOfMultiColumn(r2, c2))
1883 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1887 if (tabular.isPartOfMultiColumn(r2, c2)) {
1891 InsetText & inset = tabular.getCellInset(r2, c2);
1892 inset = paste_tabular->getCellInset(r1, c1);
1893 inset.setOwner(this);
1901 bool InsetTabular::cutSelection(BufferParams const & bp)
1903 if (!hasSelection())
1906 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1907 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1908 if (sel_col_start > sel_col_end) {
1909 sel_col_start = sel_col_end;
1910 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1912 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1915 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1916 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1918 if (sel_row_start > sel_row_end)
1919 swap(sel_row_start, sel_row_end);
1921 if (sel_cell_start > sel_cell_end)
1922 swap(sel_cell_start, sel_cell_end);
1924 for (int i = sel_row_start; i <= sel_row_end; ++i)
1925 for (int j = sel_col_start; j <= sel_col_end; ++j)
1926 tabular.getCellInset(tabular.getCellNumber(i, j))
1927 .clear(bp.tracking_changes);
1932 bool InsetTabular::isRightToLeft(LCursor & cur)
1934 return cur.bv().getParentLanguage(this)->RightToLeft();
1938 void InsetTabular::getSelection(int actcell,
1939 int & srow, int & erow, int & scol, int & ecol) const
1941 int const start = hasSelection() ? sel_cell_start : actcell;
1942 int const end = hasSelection() ? sel_cell_end : actcell;
1944 srow = tabular.row_of_cell(start);
1945 erow = tabular.row_of_cell(end);
1949 scol = tabular.column_of_cell(start);
1950 ecol = tabular.column_of_cell(end);
1954 ecol = tabular.right_column_of_cell(end);
1958 int InsetTabular::numParagraphs() const
1960 return tabular.getNumberOfCells();
1964 LyXText * InsetTabular::getText(int i) const
1966 return i < tabular.getNumberOfCells()
1967 ? tabular.getCellInset(i).getText(0)
1972 void InsetTabular::markErased()
1974 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1975 tabular.getCellInset(cell).markErased();
1979 bool InsetTabular::forceDefaultParagraphs(InsetBase const * in) const
1981 const int cell = tabular.getCellFromInset(in);
1984 return tabular.getPWidth(cell).zero();
1986 // this is a workaround for a crash (New, Insert->Tabular,
1987 // Insert->FootNote)
1991 // well we didn't obviously find it so maybe our owner knows more
1992 BOOST_ASSERT(owner());
1993 return owner()->forceDefaultParagraphs(in);
1997 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2000 if (buf.length() <= 0)
2006 string::size_type len = buf.length();
2007 string::size_type p = 0;
2009 int actcell = bv.cursor().idx();
2010 int actcol = tabular.column_of_cell(actcell);
2011 int actrow = tabular.row_of_cell(actcell);
2013 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2021 maxCols = max(cols, maxCols);
2027 maxCols = max(cols, maxCols);
2028 LyXTabular * loctab;
2033 paste_tabular.reset(
2034 new LyXTabular(bv.buffer()->params(), rows, maxCols));
2035 paste_tabular->setOwner(this);
2036 loctab = paste_tabular.get();
2045 string::size_type op = 0;
2046 int cells = loctab->getNumberOfCells();
2049 rows = loctab->rows();
2050 int const columns = loctab->columns();
2052 while (cell < cells && p < len && row < rows &&
2053 (p = buf.find_first_of("\t\n", p)) != string::npos)
2059 // we can only set this if we are not too far right
2060 if (cols < columns) {
2061 InsetText & inset = loctab->getCellInset(cell);
2062 LyXFont const font = inset.text_.
2063 getFont(inset.paragraphs().begin(), 0);
2064 inset.setText(buf.substr(op, p - op), font);
2070 // we can only set this if we are not too far right
2071 if (cols < columns) {
2072 InsetText & inset = tabular.getCellInset(cell);
2073 LyXFont const font = inset.text_.
2074 getFont(inset.paragraphs().begin(), 0);
2075 inset.setText(buf.substr(op, p - op), font);
2080 cell = loctab->getCellNumber(row, cols);
2086 // check for the last cell if there is no trailing '\n'
2087 if (cell < cells && op < len) {
2088 InsetText & inset = loctab->getCellInset(cell);
2089 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2090 inset.setText(buf.substr(op, len - op), font);
2096 void InsetTabular::addPreview(PreviewLoader & loader) const
2098 int const rows = tabular.rows();
2099 int const columns = tabular.columns();
2100 for (int i = 0; i < rows; ++i) {
2101 for (int j = 0; j < columns; ++j)
2102 tabular.getCellInset(i, j).addPreview(loader);
2108 void InsetTabular::clearSelection() const
2112 has_selection = false;
2116 void InsetTabular::setSelection(int start, int end) const
2118 sel_cell_start = start;
2120 has_selection = true;
2124 string const InsetTabularMailer::name_("tabular");
2126 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2127 : inset_(const_cast<InsetTabular &>(inset))
2131 string const InsetTabularMailer::inset2string(Buffer const &) const
2133 return params2string(inset_);
2137 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2139 istringstream data(in);
2141 lex.setStream(data);
2143 #warning CHECK verify that this is a sane value to return.
2149 string const token = lex.getString();
2157 string const token = lex.getString();
2158 if (token != "\\active_cell")
2161 cell = lex.getInteger();
2164 // This is part of the inset proper that is usually swallowed
2165 // by Buffer::readInset
2168 string const token = lex.getString();
2169 if (token != "Tabular")
2176 Buffer const & buffer = inset.buffer();
2177 inset.read(buffer, lex);
2179 // We can't set the active cell, but we can tell the frontend
2185 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2189 //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2190 data << name_ << " \\active_cell " << 0 << '\n';
2191 inset.write(inset.buffer(), data);
2192 data << "\\end_inset\n";