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;
55 using std::istringstream;
57 using std::ostringstream;
62 int const ADD_TO_HEIGHT = 2;
63 int const ADD_TO_TABULAR_WIDTH = 2;
66 boost::scoped_ptr<LyXTabular> paste_tabular;
69 struct TabularFeature {
70 LyXTabular::Feature action;
75 TabularFeature tabularFeature[] =
77 { LyXTabular::APPEND_ROW, "append-row" },
78 { LyXTabular::APPEND_COLUMN, "append-column" },
79 { LyXTabular::DELETE_ROW, "delete-row" },
80 { LyXTabular::DELETE_COLUMN, "delete-column" },
81 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
82 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
83 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
84 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
85 { LyXTabular::ALIGN_LEFT, "align-left" },
86 { LyXTabular::ALIGN_RIGHT, "align-right" },
87 { LyXTabular::ALIGN_CENTER, "align-center" },
88 { LyXTabular::ALIGN_BLOCK, "align-block" },
89 { LyXTabular::VALIGN_TOP, "valign-top" },
90 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
91 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
92 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
93 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
94 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
95 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
96 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
97 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
98 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
99 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
100 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
101 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
102 { LyXTabular::MULTICOLUMN, "multicolumn" },
103 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
104 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
105 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
106 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
107 { LyXTabular::SET_PWIDTH, "set-pwidth" },
108 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
109 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
110 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
111 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
112 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
113 { LyXTabular::SET_USEBOX, "set-usebox" },
114 { LyXTabular::SET_LTHEAD, "set-lthead" },
115 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
116 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
117 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
118 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
119 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
120 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
121 { LyXTabular::LAST_ACTION, "" }
125 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
127 FeatureEqual(LyXTabular::Feature feature)
128 : feature_(feature) {}
129 bool operator()(TabularFeature const & tf) const {
130 return tf.action == feature_;
133 LyXTabular::Feature feature_;
139 string const featureAsString(LyXTabular::Feature feature)
141 TabularFeature * end = tabularFeature +
142 sizeof(tabularFeature) / sizeof(TabularFeature);
143 TabularFeature * it = std::find_if(tabularFeature, end,
144 FeatureEqual(feature));
145 return (it == end) ? string() : it->feature;
149 bool InsetTabular::hasPasteBuffer() const
151 return (paste_tabular.get() != 0);
155 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
156 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
157 buffer_(&buf), cursorx_(0), cursory_(0), tablemode(false)
159 tabular.setOwner(this);
165 InsetTabular::InsetTabular(InsetTabular const & tab)
166 : UpdatableInset(tab), tabular(tab.tabular),
167 buffer_(tab.buffer_), cursorx_(0), cursory_(0), tablemode(false)
169 tabular.setOwner(this);
175 InsetTabular::~InsetTabular()
177 InsetTabularMailer(*this).hideDialog();
181 auto_ptr<InsetBase> InsetTabular::clone() const
183 return auto_ptr<InsetBase>(new InsetTabular(*this));
187 Buffer const & InsetTabular::buffer() const
193 void InsetTabular::buffer(Buffer * b)
199 void InsetTabular::write(Buffer const & buf, ostream & os) const
201 os << "Tabular" << endl;
202 tabular.write(buf, os);
206 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
208 bool const old_format = (lex.getString() == "\\LyXTable");
210 tabular.read(buf, lex);
216 string token = lex.getString();
217 while (lex.isOK() && (token != "\\end_inset")) {
219 token = lex.getString();
221 if (token != "\\end_inset") {
222 lex.printError("Missing \\end_inset at this point. "
228 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
230 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
231 // mi.base.textwidth << "\n";
233 lyxerr << "InsetTabular::metrics: need bv" << endl;
237 calculate_dimensions_of_cells(mi);
239 dim.asc = tabular.getAscentOfRow(0);
240 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
241 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
246 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
248 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
250 BufferView * bv = pi.base.bv;
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 extern CursorBase theTempCursor;
388 void InsetTabular::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
390 if (hasSelection() && cmd.button() == mouse_button::button3)
393 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
396 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
399 cur.cursor_ = theTempCursor;
404 setPos(cur.bv(), cmd.x, cmd.y);
405 cur.cursor_ = theTempCursor;
409 lyxerr << cur << endl;
411 if (cmd.button() == mouse_button::button2)
412 dispatch(cur, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
416 void InsetTabular::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
418 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
419 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << actcell << endl;
421 setPos(cur.bv(), cmd.x, cmd.y);
422 if (!hasSelection()) {
423 setSelection(actcell, actcell);
427 setSelection(sel_cell_start, actcell);
428 tablemode = (sel_cell_start != actcell);
433 void InsetTabular::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
435 int const actcell = getCell(cmd.x + xo_, cmd.y + yo_);
436 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << actcell << endl;
437 if (cmd.button() == mouse_button::button3)
438 InsetTabularMailer(*this).showDialog(&cur.bv());
442 void InsetTabular::edit(LCursor & cur, bool left)
444 lyxerr << "InsetTabular::edit: " << this << endl;
449 if (isRightToLeft(cur))
450 cell = tabular.getLastCellInRow(0);
454 if (isRightToLeft(cur))
455 cell = tabular.getFirstCellInRow(tabular.rows()-1);
457 cell = tabular.getNumberOfCells() - 1;
461 cur.bv().fitCursor();
467 void InsetTabular::edit(LCursor & cur, int x, int y)
469 lyxerr << "InsetTabular::edit: " << this << " first cell "
470 << &tabular.cell_info[0][0].inset << endl;
473 setPos(cur.bv(), x, y);
476 //int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
479 // activateCellInset(bv, cell, x - xx, y - cursory_);
484 InsetTabular::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
486 lyxerr << "# InsetTabular::dispatch: " << cmd
487 << " tablemode: " << tablemode << endl;
489 DispatchResult result(true, true);
490 switch (cmd.action) {
492 case LFUN_MOUSE_PRESS:
493 lfunMousePress(cur, cmd);
494 return DispatchResult(true, true);
496 case LFUN_MOUSE_MOTION:
497 lfunMouseMotion(cur, cmd);
498 return DispatchResult(true, true);
500 case LFUN_MOUSE_RELEASE:
501 lfunMouseRelease(cur, cmd);
502 return DispatchResult(true, true);
510 int cell = cur.idx();
511 lyxerr << "# InsetTabular::dispatch: A " << cur << endl;
512 result = tabular.getCellInset(cell).dispatch(cur, cmd);
514 switch (result.val()) {
516 if (movePrevCell(cur))
517 result = DispatchResult(true, true);
519 result = DispatchResult(false, FINISHED);
523 if (moveNextCell(cur))
524 result = DispatchResult(true, true);
526 result = DispatchResult(false, FINISHED_RIGHT);
531 result = DispatchResult(true, true);
533 result = DispatchResult(false, FINISHED_UP);
537 if (moveDownLock(cur))
538 result = DispatchResult(true, true);
540 result = DispatchResult(false, FINISHED_UP);
544 lyxerr << "# don't handle dispatch" << endl;
548 lyxerr << "# InsetTabular::dispatch: B " << cur << endl;
551 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
552 switch (cmd.action) {
554 case LFUN_CELL_BACKWARD:
555 case LFUN_CELL_FORWARD:
556 if (cmd.action == LFUN_CELL_FORWARD)
563 case LFUN_SCROLL_INSET:
564 if (!cmd.argument.empty()) {
565 if (cmd.argument.find('.') != cmd.argument.npos)
566 scroll(cur.bv(), static_cast<float>(strToDbl(cmd.argument)));
568 scroll(cur.bv(), strToInt(cmd.argument));
570 return DispatchResult(true, true);
573 case LFUN_RIGHTSEL: {
574 int const start = hasSelection() ? sel_cell_start : cur.idx();
575 if (tabular.isLastCellInRow(cur.idx())) {
576 setSelection(start, cur.idx());
581 // if we are starting a selection, only select
582 // the current cell at the beginning
583 if (hasSelection()) {
587 setSelection(start, end);
592 if (!moveRightLock(cur))
593 result = DispatchResult(false, FINISHED_RIGHT);
598 int const start = hasSelection() ? sel_cell_start : cur.idx();
599 if (tabular.isFirstCellInRow(cur.idx())) {
600 setSelection(start, cur.idx());
605 // if we are starting a selection, only select
606 // the current cell at the beginning
607 if (hasSelection()) {
611 setSelection(start, end);
616 if (!moveLeftLock(cur))
617 result = DispatchResult(false, FINISHED);
622 int const start = hasSelection() ? sel_cell_start : cur.idx();
623 int const ocell = cur.idx();
624 // if we are starting a selection, only select
625 // the current cell at the beginning
626 if (hasSelection()) {
628 if (ocell == sel_cell_end ||
629 tabular.column_of_cell(ocell) >
630 tabular.column_of_cell(cur.idx()))
631 setSelection(start, tabular.getCellBelow(sel_cell_end));
633 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
635 setSelection(start, start);
642 result = DispatchResult(false, FINISHED_DOWN);
647 int const start = hasSelection() ? sel_cell_start : cur.idx();
648 int const ocell = cur.idx();
649 // if we are starting a selection, only select
650 // the current cell at the beginning
651 if (hasSelection()) {
653 if (ocell == sel_cell_end ||
654 tabular.column_of_cell(ocell) >
655 tabular.column_of_cell(cur.idx()))
656 setSelection(start, tabular.getCellAbove(sel_cell_end));
658 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
660 setSelection(start, start);
667 result = DispatchResult(false, FINISHED_DOWN);
674 int actcell = cur.idx();
675 int actcol = tabular.column_of_cell(actcell);
677 if (cur.bv().top_y() + cur.bv().painter().paperHeight()
678 < yo_ + tabular.getHeightOfTabular())
680 cur.bv().scrollDocView(
681 cur.bv().top_y() + cur.bv().painter().paperHeight());
682 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
684 cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
693 int column = tabular.column_of_cell(cur.idx());
695 cur.bv().scrollDocView(
696 cur.bv().top_y() - cur.bv().painter().paperHeight());
700 cur.idx() = tabular.getCellBelow(first_visible_cell) + column;
708 // none of these make sense for insettabular,
709 // but we must catch them to prevent any
710 // selection from being confused
714 case LFUN_WORDLEFTSEL:
716 case LFUN_WORDRIGHTSEL:
718 case LFUN_DOWN_PARAGRAPH:
719 case LFUN_DOWN_PARAGRAPHSEL:
720 case LFUN_UP_PARAGRAPH:
721 case LFUN_UP_PARAGRAPHSEL:
727 case LFUN_BEGINNINGBUF:
728 case LFUN_BEGINNINGBUFSEL:
733 case LFUN_LAYOUT_TABULAR:
734 InsetTabularMailer(*this).showDialog(&cur.bv());
737 case LFUN_INSET_DIALOG_UPDATE:
738 InsetTabularMailer(*this).updateDialog(&cur.bv());
741 case LFUN_TABULAR_FEATURE:
742 if (!tabularFeatures(cur, cmd.argument))
743 result = DispatchResult(false);
746 // insert file functions
747 case LFUN_FILE_INSERT_ASCII_PARA:
748 case LFUN_FILE_INSERT_ASCII: {
749 string tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
750 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
751 result = DispatchResult(false);
765 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
769 if (copySelection(cur.bv())) {
770 recordUndo(cur, Undo::DELETE);
771 cutSelection(cur.bv().buffer()->params());
776 recordUndo(cur, Undo::DELETE);
777 cutSelection(cur.bv().buffer()->params());
784 copySelection(cur.bv());
787 case LFUN_PASTESELECTION: {
788 string const clip = cur.bv().getClipboard();
791 if (clip.find('\t') != string::npos) {
795 string::size_type len = clip.length();
796 string::size_type p = 0;
799 (p = clip.find_first_of("\t\n", p)) != string::npos) {
807 maxCols = max(cols, maxCols);
813 maxCols = max(cols, maxCols);
816 new LyXTabular(cur.bv().buffer()->params(), rows, maxCols));
818 string::size_type op = 0;
820 int cells = paste_tabular->getNumberOfCells();
824 while (cell < cells && p < len &&
825 (p = clip.find_first_of("\t\n", p)) != string::npos) {
830 paste_tabular->getCellInset(cell).
831 setText(clip.substr(op, p-op), font);
836 paste_tabular->getCellInset(cell).
837 setText(clip.substr(op, p-op), font);
838 while (cols++ < maxCols)
846 // check for the last cell if there is no trailing '\n'
847 if (cell < cells && op < len)
848 paste_tabular->getCellInset(cell).
849 setText(clip.substr(op, len-op), font);
850 } else if (!insertAsciiString(cur.bv(), clip, true))
852 // so that the clipboard is used and it goes on
854 // and executes LFUN_PASTESELECTION in insettext!
855 paste_tabular.reset();
861 if (hasPasteBuffer()) {
862 recordUndo(cur, Undo::INSERT);
863 pasteSelection(cur.bv());
868 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
871 // handle font changing stuff on selection before we lock the inset
872 // in the default part!
873 result = DispatchResult(false);
874 // we try to activate the actual inset and put this event down to
875 // the insets dispatch function.
880 InsetTabularMailer(*this).updateDialog(&cur.bv());
883 if (cmd.action == LFUN_INSET_TOGGLE) {
884 tablemode = !tablemode;
885 result = DispatchResult(true, true);
892 int InsetTabular::latex(Buffer const & buf, ostream & os,
893 OutputParams const & runparams) const
895 return tabular.latex(buf, os, runparams);
899 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
900 OutputParams const & runparams) const
902 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
903 return tabular.plaintext(buf, os, runparams, dp, false, 0);
907 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
908 OutputParams const & runparams) const
910 return tabular.linuxdoc(buf,os, runparams);
914 int InsetTabular::docbook(Buffer const & buf, ostream & os,
915 OutputParams const & runparams) const
920 // if the table is inside a float it doesn't need the informaltable
921 // wrapper. Search for it.
922 for (master = owner(); master; master = master->owner())
923 if (master->lyxCode() == InsetOld::FLOAT_CODE)
927 os << "<informaltable>";
928 if (runparams.mixed_content)
932 ret += tabular.docbook(buf, os, runparams);
934 os << "</informaltable>";
935 if (runparams.mixed_content)
943 void InsetTabular::validate(LaTeXFeatures & features) const
945 tabular.validate(features);
949 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
951 for (int i = 0, cell = -1; i < tabular.rows(); ++i) {
954 for (int j = 0; j < tabular.columns(); ++j) {
955 if (tabular.isPartOfMultiColumn(i, j))
961 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
962 tabular.getCellInset(cell).metrics(m, dim);
963 maxAsc = max(maxAsc, dim.asc);
964 maxDesc = max(maxDesc, dim.des);
965 tabular.setWidthOfCell(cell, dim.wid);
967 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
968 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
973 void InsetTabular::getCursorPos(int cell, int & x, int & y) const
975 InsetText const & inset = tabular.getCellInset(cell);
976 inset.getCursorPos(cell, x, y);
977 x += inset.x() - xo_;
978 y += inset.y() - yo_;
982 void InsetTabular::setPos(BufferView & bv, int x, int y) const
984 int const cell = getCell(x + xo_, y + yo_);
985 lyxerr << "# InsetTabular::setPos() cell: " << cell << endl;
986 InsetText const & inset = tabular.getCellInset(cell);
987 inset.text_.setCursorFromCoordinates(x, y);
993 int ly = tabular.getDescentOfRow(actrow);
995 // first search the right row
996 while (ly < y && actrow + 1 < tabular.rows()) {
997 cursory_ += tabular.getDescentOfRow(actrow) +
998 tabular.getAscentOfRow(actrow + 1) +
999 tabular.getAdditionalHeight(actrow + 1);
1001 ly = cursory_ + tabular.getDescentOfRow(actrow);
1003 actcell = tabular.getCellNumber(actrow, actcol);
1005 // now search the right column
1006 int lx = tabular.getWidthOfColumn(actcell) -
1007 tabular.getAdditionalWidth(actcell);
1009 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1010 lx += tabular.getWidthOfColumn(actcell + 1)
1011 + tabular.getAdditionalWidth(actcell);
1013 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + xo_ + 2;
1015 resetPos(bv.cursor());
1019 int InsetTabular::getCellXPos(int cell) const
1023 for (; !tabular.isFirstCellInRow(c); --c)
1025 int lx = tabular.getWidthOfColumn(cell);
1026 for (; c < cell; ++c)
1027 lx += tabular.getWidthOfColumn(c);
1029 return lx - tabular.getWidthOfColumn(cell) + xo_;
1033 void InsetTabular::resetPos(LCursor &) const
1036 #ifdef WITH_WARNINGS
1037 #warning This should be fixed in the right manner (20011128 Jug)
1039 // fast hack to fix infinite repaintings!
1040 if (in_reset_pos > 0)
1043 BufferView & bv = cur.bv();
1045 int actcell = cur.cell();
1046 int actcol = tabular.column_of_cell(actcell);
1049 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1050 if (tabular.isLastCellInRow(cell)) {
1051 cursory_ += tabular.getDescentOfRow(actrow) +
1052 tabular.getAscentOfRow(actrow + 1) +
1053 tabular.getAdditionalHeight(actrow + 1);
1058 // we need this only from here on!!!
1060 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1061 int new_x = getCellXPos(actcell) + offset;
1062 int old_x = cursorx_;
1064 // cursor.x(getCellXPos(actcell) + offset);
1065 if (actcol < tabular.columns() - 1 && scroll(false) &&
1066 tabular.getWidthOfTabular() < bv->workWidth()-20)
1070 } else if (cursorx_ - offset > 20 &&
1071 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1072 > bv.workWidth() - 20) {
1073 scroll(&bv, - tabular.getWidthOfColumn(actcell) - 20);
1075 } else if (cursorx_ - offset < 20) {
1076 scroll(&bv, 20 - cursorx_ + offset);
1078 } else if (scroll() && xo_ > 20 &&
1079 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1080 scroll(&bv, old_x - cursorx_);
1083 InsetTabularMailer(*this).updateDialog(bv);
1089 bool InsetTabular::moveRight(LCursor & cur)
1091 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1099 bool InsetTabular::moveRightLock(LCursor & cur)
1101 bool moved = isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
1104 activateCellInset(cur, cur.idx(), false);
1109 bool InsetTabular::moveLeft(LCursor & cur)
1111 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1119 bool InsetTabular::moveLeftLock(LCursor & cur)
1121 bool moved = isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
1124 activateCellInset(cur, cur.idx(), true);
1129 bool InsetTabular::moveUp(LCursor & cur)
1131 if (tabular.row_of_cell(cur.idx()) == 0)
1133 cur.idx() = tabular.getCellAbove(cur.idx());
1139 bool InsetTabular::moveUpLock(LCursor & cur)
1141 if (tabular.row_of_cell(cur.idx()) == 0)
1143 cur.idx() = tabular.getCellAbove(cur.idx());
1145 activateCellInset(cur, cur.idx(), cur.x_target(), 0);
1150 bool InsetTabular::moveDown(LCursor & cur)
1152 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1154 cur.idx() = tabular.getCellBelow(cur.idx());
1160 bool InsetTabular::moveDownLock(LCursor & cur)
1162 if (tabular.row_of_cell(cur.idx()) == tabular.rows() - 1)
1164 cur.idx() = tabular.getCellBelow(cur.idx());
1166 activateCellInset(cur, cur.idx(), cur.x_target());
1171 bool InsetTabular::moveNextCell(LCursor & cur)
1173 lyxerr << "InsetTabular::moveNextCell 1 cur: " << cur << endl;
1174 if (isRightToLeft(cur)) {
1175 if (tabular.isFirstCellInRow(cur.idx())) {
1176 int row = tabular.row_of_cell(cur.idx());
1177 if (row == tabular.rows() - 1)
1179 cur.idx() = tabular.getLastCellInRow(row);
1180 cur.idx() = tabular.getCellBelow(cur.idx());
1187 if (tabular.isLastCell(cur.idx()))
1193 lyxerr << "InsetTabular::moveNextCell 2 cur: " << cur << endl;
1199 bool InsetTabular::movePrevCell(LCursor & cur)
1201 if (isRightToLeft(cur)) {
1202 if (tabular.isLastCellInRow(cur.idx())) {
1203 int row = tabular.row_of_cell(cur.idx());
1206 cur.idx() = tabular.getFirstCellInRow(row);
1207 cur.idx() = tabular.getCellAbove(cur.idx());
1209 if (tabular.isLastCell(cur.idx()))
1214 if (cur.idx() == 0) // first cell
1225 bool InsetTabular::tabularFeatures(LCursor & cur, std::string const & what)
1227 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1230 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1231 string const tmp = tabularFeature[i].feature;
1233 if (tmp == what.substr(0, tmp.length())) {
1234 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1235 //tabularFeatures[i].feature.length())) {
1236 action = tabularFeature[i].action;
1240 if (action == LyXTabular::LAST_ACTION)
1244 ltrim(what.substr(tabularFeature[i].feature.length()));
1245 tabularFeatures(cur, action, val);
1252 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1253 string const & special, bool & flag)
1255 if (special == "dl_above") {
1258 } else if (special == "dl_below") {
1259 ltt.bottomDL = flag;
1261 } else if (special == "empty") {
1273 void InsetTabular::tabularFeatures(LCursor & cur,
1274 LyXTabular::Feature feature, string const & value)
1276 BufferView & bv = cur.bv();
1277 int actcell = cur.idx();
1282 bool setLines = false;
1283 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1284 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1288 case LyXTabular::M_ALIGN_LEFT:
1289 case LyXTabular::ALIGN_LEFT:
1290 setAlign = LYX_ALIGN_LEFT;
1293 case LyXTabular::M_ALIGN_RIGHT:
1294 case LyXTabular::ALIGN_RIGHT:
1295 setAlign = LYX_ALIGN_RIGHT;
1298 case LyXTabular::M_ALIGN_CENTER:
1299 case LyXTabular::ALIGN_CENTER:
1300 setAlign = LYX_ALIGN_CENTER;
1303 case LyXTabular::ALIGN_BLOCK:
1304 setAlign = LYX_ALIGN_BLOCK;
1307 case LyXTabular::M_VALIGN_TOP:
1308 case LyXTabular::VALIGN_TOP:
1309 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1312 case LyXTabular::M_VALIGN_BOTTOM:
1313 case LyXTabular::VALIGN_BOTTOM:
1314 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1317 case LyXTabular::M_VALIGN_MIDDLE:
1318 case LyXTabular::VALIGN_MIDDLE:
1319 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1326 if (hasSelection()) {
1327 getSelection(actcell, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1329 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1330 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1332 recordUndo(cur, Undo::ATOMIC);
1334 int row = tabular.row_of_cell(actcell);
1335 int column = tabular.column_of_cell(actcell);
1337 LyXTabular::ltType ltt;
1341 case LyXTabular::SET_PWIDTH: {
1342 LyXLength const len(value);
1343 tabular.setColumnPWidth(actcell, len);
1345 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1346 tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1347 else if (!len.zero()
1348 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1349 tabularFeatures(cur, LyXTabular::ALIGN_BLOCK, string());
1353 case LyXTabular::SET_MPWIDTH:
1354 tabular.setMColumnPWidth(actcell, LyXLength(value));
1357 case LyXTabular::SET_SPECIAL_COLUMN:
1358 case LyXTabular::SET_SPECIAL_MULTI:
1359 tabular.setAlignSpecial(actcell,value,feature);
1362 case LyXTabular::APPEND_ROW:
1363 // append the row into the tabular
1364 tabular.appendRow(bv.buffer()->params(), actcell);
1365 tabular.setOwner(this);
1368 case LyXTabular::APPEND_COLUMN:
1369 // append the column into the tabular
1370 tabular.appendColumn(bv.buffer()->params(), actcell);
1371 tabular.setOwner(this);
1372 actcell = tabular.getCellNumber(row, column);
1375 case LyXTabular::DELETE_ROW:
1376 for (int i = sel_row_start; i <= sel_row_end; ++i)
1377 tabular.deleteRow(sel_row_start);
1378 if (sel_row_start >= tabular.rows())
1380 actcell = tabular.getCellNumber(sel_row_start, column);
1384 case LyXTabular::DELETE_COLUMN:
1385 for (int i = sel_col_start; i <= sel_col_end; ++i)
1386 tabular.deleteColumn(sel_col_start);
1387 if (sel_col_start >= tabular.columns())
1389 actcell = tabular.getCellNumber(row, sel_col_start);
1393 case LyXTabular::M_TOGGLE_LINE_TOP:
1395 case LyXTabular::TOGGLE_LINE_TOP: {
1396 bool lineSet = !tabular.topLine(actcell, flag);
1397 for (int i = sel_row_start; i <= sel_row_end; ++i)
1398 for (int j = sel_col_start; j <= sel_col_end; ++j)
1400 tabular.getCellNumber(i, j),
1405 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1407 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1408 bool lineSet = !tabular.bottomLine(actcell, flag);
1409 for (int i = sel_row_start; i <= sel_row_end; ++i)
1410 for (int j = sel_col_start; j <= sel_col_end; ++j)
1411 tabular.setBottomLine(
1412 tabular.getCellNumber(i, j),
1418 case LyXTabular::M_TOGGLE_LINE_LEFT:
1420 case LyXTabular::TOGGLE_LINE_LEFT: {
1421 bool lineSet = !tabular.leftLine(actcell, flag);
1422 for (int i = sel_row_start; i <= sel_row_end; ++i)
1423 for (int j = sel_col_start; j <= sel_col_end; ++j)
1424 tabular.setLeftLine(
1425 tabular.getCellNumber(i,j),
1431 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1433 case LyXTabular::TOGGLE_LINE_RIGHT: {
1434 bool lineSet = !tabular.rightLine(actcell, flag);
1435 for (int i = sel_row_start; i <= sel_row_end; ++i)
1436 for (int j = sel_col_start; j <= sel_col_end; ++j)
1437 tabular.setRightLine(
1438 tabular.getCellNumber(i,j),
1444 case LyXTabular::M_ALIGN_LEFT:
1445 case LyXTabular::M_ALIGN_RIGHT:
1446 case LyXTabular::M_ALIGN_CENTER:
1448 case LyXTabular::ALIGN_LEFT:
1449 case LyXTabular::ALIGN_RIGHT:
1450 case LyXTabular::ALIGN_CENTER:
1451 case LyXTabular::ALIGN_BLOCK:
1452 for (int i = sel_row_start; i <= sel_row_end; ++i)
1453 for (int j = sel_col_start; j <= sel_col_end; ++j)
1454 tabular.setAlignment(
1455 tabular.getCellNumber(i, j),
1460 case LyXTabular::M_VALIGN_TOP:
1461 case LyXTabular::M_VALIGN_BOTTOM:
1462 case LyXTabular::M_VALIGN_MIDDLE:
1464 case LyXTabular::VALIGN_TOP:
1465 case LyXTabular::VALIGN_BOTTOM:
1466 case LyXTabular::VALIGN_MIDDLE:
1467 for (int i = sel_row_start; i <= sel_row_end; ++i)
1468 for (int j = sel_col_start; j <= sel_col_end; ++j)
1469 tabular.setVAlignment(
1470 tabular.getCellNumber(i, j),
1474 case LyXTabular::MULTICOLUMN: {
1475 if (sel_row_start != sel_row_end) {
1476 #ifdef WITH_WARNINGS
1477 #warning Need I say it ? This is horrible.
1479 Alert::error(_("Error setting multicolumn"),
1480 _("You cannot set multicolumn vertically."));
1483 // just multicol for one Single Cell
1484 if (!hasSelection()) {
1485 // check wether we are completly in a multicol
1486 if (tabular.isMultiColumn(actcell))
1487 tabular.unsetMultiColumn(actcell);
1489 tabular.setMultiColumn(bv.buffer(), actcell, 1);
1492 // we have a selection so this means we just add all this
1493 // cells to form a multicolumn cell
1497 if (sel_cell_start > sel_cell_end) {
1498 s_start = sel_cell_end;
1499 s_end = sel_cell_start;
1501 s_start = sel_cell_start;
1502 s_end = sel_cell_end;
1504 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1510 case LyXTabular::SET_ALL_LINES:
1512 case LyXTabular::UNSET_ALL_LINES:
1513 for (int i = sel_row_start; i <= sel_row_end; ++i)
1514 for (int j = sel_col_start; j <= sel_col_end; ++j)
1515 tabular.setAllLines(
1516 tabular.getCellNumber(i,j), setLines);
1519 case LyXTabular::SET_LONGTABULAR:
1520 tabular.setLongTabular(true);
1523 case LyXTabular::UNSET_LONGTABULAR:
1524 tabular.setLongTabular(false);
1527 case LyXTabular::SET_ROTATE_TABULAR:
1528 tabular.setRotateTabular(true);
1531 case LyXTabular::UNSET_ROTATE_TABULAR:
1532 tabular.setRotateTabular(false);
1535 case LyXTabular::SET_ROTATE_CELL:
1536 for (int i = sel_row_start; i <= sel_row_end; ++i)
1537 for (int j = sel_col_start; j <= sel_col_end; ++j)
1538 tabular.setRotateCell(
1539 tabular.getCellNumber(i, j), true);
1542 case LyXTabular::UNSET_ROTATE_CELL:
1543 for (int i = sel_row_start; i <= sel_row_end; ++i)
1544 for (int j = sel_col_start; j <= sel_col_end; ++j)
1545 tabular.setRotateCell(
1546 tabular.getCellNumber(i, j), false);
1549 case LyXTabular::SET_USEBOX: {
1550 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1551 if (val == tabular.getUsebox(actcell))
1552 val = LyXTabular::BOX_NONE;
1553 for (int i = sel_row_start; i <= sel_row_end; ++i)
1554 for (int j = sel_col_start; j <= sel_col_end; ++j)
1555 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1559 case LyXTabular::UNSET_LTFIRSTHEAD:
1561 case LyXTabular::SET_LTFIRSTHEAD:
1562 tabular.getRowOfLTFirstHead(row, ltt);
1563 checkLongtableSpecial(ltt, value, flag);
1564 tabular.setLTHead(row, flag, ltt, true);
1567 case LyXTabular::UNSET_LTHEAD:
1569 case LyXTabular::SET_LTHEAD:
1570 tabular.getRowOfLTHead(row, ltt);
1571 checkLongtableSpecial(ltt, value, flag);
1572 tabular.setLTHead(row, flag, ltt, false);
1575 case LyXTabular::UNSET_LTFOOT:
1577 case LyXTabular::SET_LTFOOT:
1578 tabular.getRowOfLTFoot(row, ltt);
1579 checkLongtableSpecial(ltt, value, flag);
1580 tabular.setLTFoot(row, flag, ltt, false);
1583 case LyXTabular::UNSET_LTLASTFOOT:
1585 case LyXTabular::SET_LTLASTFOOT:
1586 tabular.getRowOfLTLastFoot(row, ltt);
1587 checkLongtableSpecial(ltt, value, flag);
1588 tabular.setLTFoot(row, flag, ltt, true);
1591 case LyXTabular::SET_LTNEWPAGE:
1592 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1595 // dummy stuff just to avoid warnings
1596 case LyXTabular::LAST_ACTION:
1601 InsetTabularMailer(*this).updateDialog(&bv);
1605 void InsetTabular::activateCellInset(LCursor & cur, int cell, int x, int y)
1607 tabular.getCellInset(cell).edit(cur, x, y);
1613 void InsetTabular::activateCellInset(LCursor & cur, int cell, bool behind)
1615 tabular.getCellInset(cell).edit(cur, behind);
1621 bool InsetTabular::showInsetDialog(BufferView * bv) const
1623 InsetTabularMailer(*this).showDialog(bv);
1628 void InsetTabular::openLayoutDialog(BufferView * bv) const
1630 InsetTabularMailer(*this).showDialog(bv);
1635 // function returns an object as defined in func_status.h:
1636 // states OK, Unknown, Disabled, On, Off.
1638 FuncStatus InsetTabular::getStatus(string const & what, int actcell) const
1641 int action = LyXTabular::LAST_ACTION;
1644 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1645 string const tmp = tabularFeature[i].feature;
1646 if (tmp == what.substr(0, tmp.length())) {
1647 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1648 // tabularFeatures[i].feature.length()))
1649 action = tabularFeature[i].action;
1653 if (action == LyXTabular::LAST_ACTION) {
1655 status.unknown(true);
1659 string const argument
1660 = ltrim(what.substr(tabularFeature[i].feature.length()));
1665 LyXTabular::ltType dummyltt;
1669 getSelection(actcell, sel_row_start, sel_row_end, dummy, dummy);
1671 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1674 case LyXTabular::SET_PWIDTH:
1675 case LyXTabular::SET_MPWIDTH:
1676 case LyXTabular::SET_SPECIAL_COLUMN:
1677 case LyXTabular::SET_SPECIAL_MULTI:
1678 case LyXTabular::APPEND_ROW:
1679 case LyXTabular::APPEND_COLUMN:
1680 case LyXTabular::DELETE_ROW:
1681 case LyXTabular::DELETE_COLUMN:
1682 case LyXTabular::SET_ALL_LINES:
1683 case LyXTabular::UNSET_ALL_LINES:
1687 case LyXTabular::MULTICOLUMN:
1688 status.setOnOff(tabular.isMultiColumn(actcell));
1691 case LyXTabular::M_TOGGLE_LINE_TOP:
1693 case LyXTabular::TOGGLE_LINE_TOP:
1694 status.setOnOff(tabular.topLine(actcell, flag));
1697 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1699 case LyXTabular::TOGGLE_LINE_BOTTOM:
1700 status.setOnOff(tabular.bottomLine(actcell, flag));
1703 case LyXTabular::M_TOGGLE_LINE_LEFT:
1705 case LyXTabular::TOGGLE_LINE_LEFT:
1706 status.setOnOff(tabular.leftLine(actcell, flag));
1709 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1711 case LyXTabular::TOGGLE_LINE_RIGHT:
1712 status.setOnOff(tabular.rightLine(actcell, flag));
1715 case LyXTabular::M_ALIGN_LEFT:
1717 case LyXTabular::ALIGN_LEFT:
1718 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1721 case LyXTabular::M_ALIGN_RIGHT:
1723 case LyXTabular::ALIGN_RIGHT:
1724 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1727 case LyXTabular::M_ALIGN_CENTER:
1729 case LyXTabular::ALIGN_CENTER:
1730 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1733 case LyXTabular::ALIGN_BLOCK:
1734 status.disabled(tabular.getPWidth(actcell).zero());
1735 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1738 case LyXTabular::M_VALIGN_TOP:
1740 case LyXTabular::VALIGN_TOP:
1742 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1745 case LyXTabular::M_VALIGN_BOTTOM:
1747 case LyXTabular::VALIGN_BOTTOM:
1749 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1752 case LyXTabular::M_VALIGN_MIDDLE:
1754 case LyXTabular::VALIGN_MIDDLE:
1756 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1759 case LyXTabular::SET_LONGTABULAR:
1760 status.setOnOff(tabular.isLongTabular());
1763 case LyXTabular::UNSET_LONGTABULAR:
1764 status.setOnOff(!tabular.isLongTabular());
1767 case LyXTabular::SET_ROTATE_TABULAR:
1768 status.setOnOff(tabular.getRotateTabular());
1771 case LyXTabular::UNSET_ROTATE_TABULAR:
1772 status.setOnOff(!tabular.getRotateTabular());
1775 case LyXTabular::SET_ROTATE_CELL:
1776 status.setOnOff(tabular.getRotateCell(actcell));
1779 case LyXTabular::UNSET_ROTATE_CELL:
1780 status.setOnOff(!tabular.getRotateCell(actcell));
1783 case LyXTabular::SET_USEBOX:
1784 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1787 case LyXTabular::SET_LTFIRSTHEAD:
1788 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1791 case LyXTabular::SET_LTHEAD:
1792 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1795 case LyXTabular::SET_LTFOOT:
1796 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1799 case LyXTabular::SET_LTLASTFOOT:
1800 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1803 case LyXTabular::SET_LTNEWPAGE:
1804 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1809 status.disabled(true);
1816 void InsetTabular::getLabelList(Buffer const & buffer,
1817 std::vector<string> & list) const
1819 tabular.getLabelList(buffer, list);
1823 bool InsetTabular::copySelection(BufferView & bv)
1825 if (!hasSelection())
1828 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1829 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1830 if (sel_col_start > sel_col_end) {
1831 sel_col_start = sel_col_end;
1832 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1834 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1837 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1838 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1839 if (sel_row_start > sel_row_end)
1840 swap(sel_row_start, sel_row_end);
1842 paste_tabular.reset(new LyXTabular(tabular));
1843 paste_tabular->setOwner(this);
1845 for (int i = 0; i < sel_row_start; ++i)
1846 paste_tabular->deleteRow(0);
1848 int const rows = sel_row_end - sel_row_start + 1;
1849 while (paste_tabular->rows() > rows)
1850 paste_tabular->deleteRow(rows);
1852 paste_tabular->setTopLine(0, true, true);
1853 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1856 for (int i = 0; i < sel_col_start; ++i)
1857 paste_tabular->deleteColumn(0);
1859 int const columns = sel_col_end - sel_col_start + 1;
1860 while (paste_tabular->columns() > columns)
1861 paste_tabular->deleteColumn(columns);
1863 paste_tabular->setLeftLine(0, true, true);
1864 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1868 OutputParams const runparams;
1869 paste_tabular->plaintext(*bv.buffer(), os, runparams,
1870 ownerPar(*bv.buffer(), this).params().depth(), true, '\t');
1871 bv.stuffClipboard(os.str());
1876 bool InsetTabular::pasteSelection(BufferView & bv)
1880 int actcell = bv.cursor().idx();
1881 int actcol = tabular.column_of_cell(actcell);
1882 int actrow = tabular.row_of_cell(actcell);
1883 for (int r1 = 0, r2 = actrow;
1884 r1 < paste_tabular->rows() && r2 < tabular.rows();
1886 for (int c1 = 0, c2 = actcol;
1887 c1 < paste_tabular->columns() && c2 < tabular.columns();
1889 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1890 tabular.isPartOfMultiColumn(r2, c2))
1892 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1896 if (tabular.isPartOfMultiColumn(r2, c2)) {
1900 InsetText & inset = tabular.getCellInset(r2, c2);
1901 inset = paste_tabular->getCellInset(r1, c1);
1902 inset.setOwner(this);
1910 bool InsetTabular::cutSelection(BufferParams const & bp)
1912 if (!hasSelection())
1915 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1916 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1917 if (sel_col_start > sel_col_end) {
1918 sel_col_start = sel_col_end;
1919 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1921 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1924 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1925 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1927 if (sel_row_start > sel_row_end)
1928 swap(sel_row_start, sel_row_end);
1930 if (sel_cell_start > sel_cell_end)
1931 swap(sel_cell_start, sel_cell_end);
1933 for (int i = sel_row_start; i <= sel_row_end; ++i)
1934 for (int j = sel_col_start; j <= sel_col_end; ++j)
1935 tabular.getCellInset(tabular.getCellNumber(i, j))
1936 .clear(bp.tracking_changes);
1941 bool InsetTabular::isRightToLeft(LCursor & cur)
1943 return cur.bv().getParentLanguage(this)->RightToLeft();
1947 void InsetTabular::getSelection(int actcell,
1948 int & srow, int & erow, int & scol, int & ecol) const
1950 int const start = hasSelection() ? sel_cell_start : actcell;
1951 int const end = hasSelection() ? sel_cell_end : actcell;
1953 srow = tabular.row_of_cell(start);
1954 erow = tabular.row_of_cell(end);
1958 scol = tabular.column_of_cell(start);
1959 ecol = tabular.column_of_cell(end);
1963 ecol = tabular.right_column_of_cell(end);
1967 int InsetTabular::numParagraphs() const
1969 return tabular.getNumberOfCells();
1973 LyXText * InsetTabular::getText(int i) const
1975 return i < tabular.getNumberOfCells()
1976 ? tabular.getCellInset(i).getText(0)
1981 void InsetTabular::markErased()
1983 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1984 tabular.getCellInset(cell).markErased();
1988 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1990 const int cell = tabular.getCellFromInset(in);
1993 return tabular.getPWidth(cell).zero();
1995 // this is a workaround for a crash (New, Insert->Tabular,
1996 // Insert->FootNote)
2000 // well we didn't obviously find it so maybe our owner knows more
2001 BOOST_ASSERT(owner());
2002 return owner()->forceDefaultParagraphs(in);
2006 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
2009 if (buf.length() <= 0)
2015 string::size_type len = buf.length();
2016 string::size_type p = 0;
2018 int actcell = bv.cursor().idx();
2019 int actcol = tabular.column_of_cell(actcell);
2020 int actrow = tabular.row_of_cell(actcell);
2022 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2030 maxCols = max(cols, maxCols);
2036 maxCols = max(cols, maxCols);
2037 LyXTabular * loctab;
2042 paste_tabular.reset(
2043 new LyXTabular(bv.buffer()->params(), rows, maxCols));
2044 paste_tabular->setOwner(this);
2045 loctab = paste_tabular.get();
2054 string::size_type op = 0;
2055 int cells = loctab->getNumberOfCells();
2058 rows = loctab->rows();
2059 int const columns = loctab->columns();
2061 while (cell < cells && p < len && row < rows &&
2062 (p = buf.find_first_of("\t\n", p)) != string::npos)
2068 // we can only set this if we are not too far right
2069 if (cols < columns) {
2070 InsetText & inset = loctab->getCellInset(cell);
2071 LyXFont const font = inset.text_.
2072 getFont(inset.paragraphs().begin(), 0);
2073 inset.setText(buf.substr(op, p - op), font);
2079 // we can only set this if we are not too far right
2080 if (cols < columns) {
2081 InsetText & inset = tabular.getCellInset(cell);
2082 LyXFont const font = inset.text_.
2083 getFont(inset.paragraphs().begin(), 0);
2084 inset.setText(buf.substr(op, p - op), font);
2089 cell = loctab->getCellNumber(row, cols);
2095 // check for the last cell if there is no trailing '\n'
2096 if (cell < cells && op < len) {
2097 InsetText & inset = loctab->getCellInset(cell);
2098 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2099 inset.setText(buf.substr(op, len - op), font);
2105 void InsetTabular::addPreview(PreviewLoader & loader) const
2107 int const rows = tabular.rows();
2108 int const columns = tabular.columns();
2109 for (int i = 0; i < rows; ++i) {
2110 for (int j = 0; j < columns; ++j)
2111 tabular.getCellInset(i, j).addPreview(loader);
2117 void InsetTabular::clearSelection() const
2121 has_selection = false;
2125 void InsetTabular::setSelection(int start, int end) const
2127 sel_cell_start = start;
2129 has_selection = true;
2133 string const InsetTabularMailer::name_("tabular");
2135 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2136 : inset_(const_cast<InsetTabular &>(inset))
2140 string const InsetTabularMailer::inset2string(Buffer const &) const
2142 return params2string(inset_);
2146 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2148 istringstream data(in);
2150 lex.setStream(data);
2152 #warning CHECK verify that this is a sane value to return.
2158 string const token = lex.getString();
2166 string const token = lex.getString();
2167 if (token != "\\active_cell")
2170 cell = lex.getInteger();
2173 // This is part of the inset proper that is usually swallowed
2174 // by Buffer::readInset
2177 string const token = lex.getString();
2178 if (token != "Tabular")
2185 Buffer const & buffer = inset.buffer();
2186 inset.read(buffer, lex);
2188 // We can't set the active cell, but we can tell the frontend
2194 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2198 //data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2199 data << name_ << " \\active_cell " << 0 << '\n';
2200 inset.write(inset.buffer(), data);
2201 data << "\\end_inset\n";