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"
43 using lyx::graphics::PreviewLoader;
45 using lyx::support::ltrim;
46 using lyx::support::strToInt;
47 using lyx::support::strToDbl;
54 using std::istringstream;
56 using std::ostringstream;
61 int const ADD_TO_HEIGHT = 2;
62 int const ADD_TO_TABULAR_WIDTH = 2;
65 boost::scoped_ptr<LyXTabular> paste_tabular;
68 struct TabularFeature {
69 LyXTabular::Feature action;
74 TabularFeature tabularFeature[] =
76 { LyXTabular::APPEND_ROW, "append-row" },
77 { LyXTabular::APPEND_COLUMN, "append-column" },
78 { LyXTabular::DELETE_ROW, "delete-row" },
79 { LyXTabular::DELETE_COLUMN, "delete-column" },
80 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
81 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
82 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
83 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
84 { LyXTabular::ALIGN_LEFT, "align-left" },
85 { LyXTabular::ALIGN_RIGHT, "align-right" },
86 { LyXTabular::ALIGN_CENTER, "align-center" },
87 { LyXTabular::ALIGN_BLOCK, "align-block" },
88 { LyXTabular::VALIGN_TOP, "valign-top" },
89 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
90 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
91 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
92 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
93 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
94 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
95 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
96 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
97 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
98 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
99 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
100 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
101 { LyXTabular::MULTICOLUMN, "multicolumn" },
102 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
103 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
104 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
105 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
106 { LyXTabular::SET_PWIDTH, "set-pwidth" },
107 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
108 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
109 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
110 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
111 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
112 { LyXTabular::SET_USEBOX, "set-usebox" },
113 { LyXTabular::SET_LTHEAD, "set-lthead" },
114 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
115 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
116 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
117 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
118 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
119 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
120 { LyXTabular::LAST_ACTION, "" }
125 FindFeature(LyXTabular::Feature feature)
129 bool operator()(TabularFeature & tf)
131 return tf.action == feature_;
135 LyXTabular::Feature feature_;
141 string const featureAsString(LyXTabular::Feature feature)
143 TabularFeature * it = tabularFeature;
144 TabularFeature * end = it +
145 sizeof(tabularFeature) / sizeof(TabularFeature);
146 it = std::find_if(it, end, FindFeature(feature));
147 return (it == end) ? string() : it->feature;
151 bool InsetTabular::hasPasteBuffer() const
153 return (paste_tabular.get() != 0);
157 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
158 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
159 buffer_(&buf), cursorx_(0), cursory_(0)
161 tabular.setOwner(this);
170 InsetTabular::InsetTabular(InsetTabular const & tab)
171 : UpdatableInset(tab), tabular(tab.tabular),
172 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
174 tabular.setOwner(this);
183 InsetTabular::~InsetTabular()
185 InsetTabularMailer(*this).hideDialog();
189 auto_ptr<InsetBase> InsetTabular::clone() const
191 return auto_ptr<InsetBase>(new InsetTabular(*this));
195 Buffer const & InsetTabular::buffer() const
201 void InsetTabular::buffer(Buffer * b)
207 void InsetTabular::write(Buffer const & buf, ostream & os) const
209 os << "Tabular" << endl;
210 tabular.write(buf, os);
214 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
216 bool const old_format = (lex.getString() == "\\LyXTable");
218 tabular.read(buf, lex);
224 string token = lex.getString();
225 while (lex.isOK() && (token != "\\end_inset")) {
227 token = lex.getString();
229 if (token != "\\end_inset") {
230 lex.printError("Missing \\end_inset at this point. "
236 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
238 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
239 // mi.base.textwidth << "\n";
241 lyxerr << "InsetTabular::metrics: need bv" << endl;
245 calculate_dimensions_of_cells(mi);
247 dim.asc = tabular.getAscentOfRow(0);
248 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
249 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
254 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
256 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
258 BufferView * bv = pi.base.bv;
265 x += ADD_TO_TABULAR_WIDTH;
268 first_visible_cell = -1;
269 for (int i = 0; i < tabular.rows(); ++i) {
271 cell = tabular.getCellNumber(i, 0);
272 if (y + tabular.getDescentOfRow(i) <= 0 &&
273 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
275 y += tabular.getDescentOfRow(i) +
276 tabular.getAscentOfRow(i + 1) +
277 tabular.getAdditionalHeight(i + 1);
280 for (int j = 0; j < tabular.columns(); ++j) {
281 if (nx > bv->workWidth())
283 if (tabular.isPartOfMultiColumn(i, j))
285 if (first_visible_cell < 0)
286 first_visible_cell = cell;
288 drawCellSelection(pi.pain, nx, y, i, j, cell);
290 int const cx = nx + tabular.getBeginningOfTextInCell(cell);
291 tabular.getCellInset(cell).draw(pi, cx, y);
292 drawCellLines(pi.pain, nx, y, i, cell);
293 nx += tabular.getWidthOfColumn(cell);
297 // Would be nice, but for some completely unfathomable reason,
298 // on a col resize to a new fixed width, even though the insettexts
299 // are resized, the cell isn't, but drawing all cells in a tall table
300 // has the desired effect somehow. Complete dark magic.
302 // avoiding drawing the rest of a long table is
303 // a pretty big speedup
304 if (y > bv->workHeight())
308 y += tabular.getDescentOfRow(i) +
309 tabular.getAscentOfRow(i + 1) +
310 tabular.getAdditionalHeight(i + 1);
315 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
316 int row, int cell) const
318 int x2 = x + tabular.getWidthOfColumn(cell);
321 if (!tabular.topAlreadyDrawn(cell)) {
322 on_off = !tabular.topLine(cell);
323 pain.line(x, y - tabular.getAscentOfRow(row),
324 x2, y - tabular.getAscentOfRow(row),
325 on_off ? LColor::tabularonoffline : LColor::tabularline,
326 on_off ? Painter::line_onoffdash : Painter::line_solid);
328 on_off = !tabular.bottomLine(cell);
329 pain.line(x, y + tabular.getDescentOfRow(row),
330 x2, y + tabular.getDescentOfRow(row),
331 on_off ? LColor::tabularonoffline : LColor::tabularline,
332 on_off ? Painter::line_onoffdash : Painter::line_solid);
333 if (!tabular.leftAlreadyDrawn(cell)) {
334 on_off = !tabular.leftLine(cell);
335 pain.line(x, y - tabular.getAscentOfRow(row),
336 x, y + tabular.getDescentOfRow(row),
337 on_off ? LColor::tabularonoffline : LColor::tabularline,
338 on_off ? Painter::line_onoffdash : Painter::line_solid);
340 on_off = !tabular.rightLine(cell);
341 pain.line(x2 - tabular.getAdditionalWidth(cell),
342 y - tabular.getAscentOfRow(row),
343 x2 - tabular.getAdditionalWidth(cell),
344 y + tabular.getDescentOfRow(row),
345 on_off ? LColor::tabularonoffline : LColor::tabularline,
346 on_off ? Painter::line_onoffdash : Painter::line_solid);
350 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
351 int row, int column, int cell) const
353 if (locked_cell != -1)
356 BOOST_ASSERT(hasSelection());
357 int cs = tabular.column_of_cell(sel_cell_start);
358 int ce = tabular.column_of_cell(sel_cell_end);
361 cs = tabular.column_of_cell(sel_cell_end);
363 ce = tabular.right_column_of_cell(sel_cell_end);
366 int rs = tabular.row_of_cell(sel_cell_start);
367 int re = tabular.row_of_cell(sel_cell_end);
371 if (column >= cs && column <= ce && row >= rs && row <= re) {
372 int w = tabular.getWidthOfColumn(cell);
373 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
374 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
375 w, h, LColor::selection);
380 string const InsetTabular::editMessage() const
382 return _("Opened table");
386 void InsetTabular::updateLocal(BufferView * bv) const
392 extern LCursor theTempCursor;
396 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
398 if (hasSelection() && cmd.button() == mouse_button::button3)
404 BufferView * bv = cmd.view();
406 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
407 lyxerr << "# InsetTabular::lfunMousePress cell: " << cell << endl;
409 bv->cursor() = theTempCursor;
410 bv->cursor().data_.push_back(CursorItem(this));
411 bv->cursor().data_.back().idx_ = cell;
413 setPos(bv, cmd.x, cmd.y);
415 bv->cursor() = theTempCursor;
416 bv->cursor().data_.back().idx_ = cell;
419 lyxerr << bv->cursor() << endl;
421 if (cmd.button() == mouse_button::button2)
422 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
426 void InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
428 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
429 lyxerr << "# InsetTabular::lfunMouseRelease cell: " << cell << endl;
430 if (cmd.button() == mouse_button::button3)
431 InsetTabularMailer(*this).showDialog(cmd.view());
435 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
437 int cell = getCell(cmd.x + xo_, cmd.y + yo_);
438 lyxerr << "# InsetTabular::lfunMouseMotion cell: " << cell << endl;
439 BufferView * bv = cmd.view();
440 int const old_cell = actcell;
442 setPos(bv, cmd.x, cmd.y);
444 setSelection(actcell, actcell);
445 else if (old_cell != actcell)
446 setSelection(sel_cell_start, actcell);
450 void InsetTabular::edit(BufferView * bv, bool left)
452 lyxerr << "InsetTabular::edit: " << this << endl;
455 if (isRightToLeft(bv))
456 actcell = tabular.getLastCellInRow(0);
460 if (isRightToLeft(bv))
461 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
463 actcell = tabular.getNumberOfCells() - 1;
468 bv->cursor().push(this);
469 bv->cursor().data_.back().idx_ = actcell;
470 locked_cell = actcell;
471 lyxerr << bv->cursor() << endl;
475 void InsetTabular::edit(BufferView * bv, int x, int y)
477 lyxerr << "InsetTabular::edit: " << this << " first cell "
478 << &tabular.cell_info[0][0].inset << endl;
484 int xx = cursorx_ - xo_ + tabular.getBeginningOfTextInCell(actcell);
485 bv->cursor().push(this);
487 activateCellInset(bv, x - xx, y - cursory_, false);
492 InsetTabular::priv_dispatch(FuncRequest const & cmd,
493 idx_type & idx, pos_type & pos)
495 lyxerr << "# InsetTabular::dispatch: " << cmd << endl;
497 DispatchResult result(true, true);
498 BufferView * bv = cmd.view();
499 lyxerr << "# cursor: " << bv->cursor() << endl;
501 switch (cmd.action) {
503 case LFUN_MOUSE_PRESS:
505 return DispatchResult(true, true);
507 case LFUN_MOUSE_MOTION:
508 lfunMouseMotion(cmd);
509 return DispatchResult(true, true);
511 case LFUN_MOUSE_RELEASE:
512 lfunMouseRelease(cmd);
513 return DispatchResult(true, true);
520 int cell = bv->cursor().data_.back().idx_;
522 lyxerr << "# InsetTabular::dispatch 1: " << cmd << endl;
523 result = tabular.getCellInset(cell).dispatch(cmd, idx, pos);
525 switch (result.val()) {
526 case LFUN_FINISHED_LEFT:
527 lyxerr << "handle LFUN_FINISHED_LEFT, act: " << actcell << endl;
528 if (movePrevCell(bv, false))
529 result = DispatchResult(true, true);
531 result = DispatchResult(false, FINISHED);
534 case LFUN_FINISHED_RIGHT:
535 lyxerr << "handle LFUN_FINISHED_RIGHT, act: " << actcell << endl;
536 if (moveNextCell(bv, false))
537 result = DispatchResult(true, true);
539 result = DispatchResult(false, FINISHED_RIGHT);
542 case LFUN_FINISHED_UP:
543 lyxerr << "handle LFUN_FINISHED_UP, act: " << actcell << endl;
544 result = moveUp(bv, true);
547 case LFUN_FINISHED_DOWN:
548 lyxerr << "handle LFUN_FINISHED_DOWN, act: " << actcell << endl;
549 result = moveDown(bv, true);
556 lyxerr << "# InsetTabular::dispatch 2: " << cmd << endl;
560 lyxerr << "# InsetTabular::dispatch 3: " << cmd << endl;
561 switch (cmd.action) {
563 case LFUN_CELL_BACKWARD:
564 case LFUN_CELL_FORWARD:
565 if (cmd.action == LFUN_CELL_FORWARD)
566 moveNextCell(bv, false);
568 movePrevCell(bv, false);
572 case LFUN_SCROLL_INSET:
573 if (!cmd.argument.empty()) {
574 if (cmd.argument.find('.') != cmd.argument.npos)
575 scroll(cmd.view(), static_cast<float>(strToDbl(cmd.argument)));
577 scroll(cmd.view(), strToInt(cmd.argument));
578 cmd.view()->update();
579 return DispatchResult(true, true);
582 case LFUN_RIGHTSEL: {
583 int const start = hasSelection() ? sel_cell_start : actcell;
584 if (tabular.isLastCellInRow(actcell)) {
585 setSelection(start, actcell);
590 // if we are starting a selection, only select
591 // the current cell at the beginning
592 if (hasSelection()) {
593 moveRight(bv, false);
596 setSelection(start, end);
601 result = moveRight(bv, true);
606 int const start = hasSelection() ? sel_cell_start : actcell;
607 if (tabular.isFirstCellInRow(actcell)) {
608 setSelection(start, actcell);
613 // if we are starting a selection, only select
614 // the current cell at the beginning
615 if (hasSelection()) {
619 setSelection(start, end);
624 result = moveLeft(bv, true);
629 int const start = hasSelection() ? sel_cell_start : actcell;
630 int const ocell = actcell;
631 // if we are starting a selection, only select
632 // the current cell at the beginning
633 if (hasSelection()) {
635 if (ocell == sel_cell_end ||
636 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
637 setSelection(start, tabular.getCellBelow(sel_cell_end));
639 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
641 setSelection(start, start);
647 result = moveDown(bv, false);
652 int const start = hasSelection() ? sel_cell_start : actcell;
653 int const ocell = actcell;
654 // if we are starting a selection, only select
655 // the current cell at the beginning
656 if (hasSelection()) {
658 if (ocell == sel_cell_end ||
659 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
660 setSelection(start, tabular.getCellAbove(sel_cell_end));
662 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
664 setSelection(start, start);
670 result = moveUp(bv, false);
678 if (bv->top_y() + bv->painter().paperHeight()
679 < yo_ + tabular.getHeightOfTabular())
681 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
682 actcell = tabular.getCellBelow(first_visible_cell) + column;
684 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
695 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
699 actcell = tabular.getCellBelow(first_visible_cell) + column;
707 // none of these make sense for insettabular,
708 // but we must catch them to prevent any
709 // selection from being confused
713 case LFUN_WORDLEFTSEL:
715 case LFUN_WORDRIGHTSEL:
717 case LFUN_DOWN_PARAGRAPH:
718 case LFUN_DOWN_PARAGRAPHSEL:
719 case LFUN_UP_PARAGRAPH:
720 case LFUN_UP_PARAGRAPHSEL:
726 case LFUN_BEGINNINGBUF:
727 case LFUN_BEGINNINGBUFSEL:
732 case LFUN_LAYOUT_TABULAR:
733 InsetTabularMailer(*this).showDialog(bv);
736 case LFUN_INSET_DIALOG_UPDATE:
737 InsetTabularMailer(*this).updateDialog(bv);
740 case LFUN_TABULAR_FEATURE:
741 if (!tabularFeatures(bv, cmd.argument))
742 result = DispatchResult(false);
745 // insert file functions
746 case LFUN_FILE_INSERT_ASCII_PARA:
747 case LFUN_FILE_INSERT_ASCII: {
748 string tmpstr = getContentsOfAsciiFile(bv, cmd.argument, false);
749 if (!tmpstr.empty() && !insertAsciiString(bv, tmpstr, false))
750 result = DispatchResult(false);
764 lyxerr << "font changes not re-implemented for tables after LOCK" << endl;
768 if (copySelection(bv)) {
769 recordUndo(bv, Undo::DELETE);
770 cutSelection(bv->buffer()->params());
775 recordUndo(bv, Undo::DELETE);
776 cutSelection(bv->buffer()->params());
786 case LFUN_PASTESELECTION: {
787 string const clip = bv->getClipboard();
790 if (clip.find('\t') != string::npos) {
794 string::size_type len = clip.length();
795 string::size_type p = 0;
798 (p = clip.find_first_of("\t\n", p)) != string::npos) {
806 maxCols = max(cols, maxCols);
812 maxCols = max(cols, maxCols);
815 new LyXTabular(bv->buffer()->params(), rows, maxCols));
817 string::size_type op = 0;
819 int cells = paste_tabular->getNumberOfCells();
823 while (cell < cells && p < len &&
824 (p = clip.find_first_of("\t\n", p)) != string::npos) {
829 paste_tabular->getCellInset(cell).
830 setText(clip.substr(op, p-op), font);
835 paste_tabular->getCellInset(cell).
836 setText(clip.substr(op, p-op), font);
837 while (cols++ < maxCols)
845 // check for the last cell if there is no trailing '\n'
846 if (cell < cells && op < len)
847 paste_tabular->getCellInset(cell).
848 setText(clip.substr(op, len-op), font);
849 } else if (!insertAsciiString(bv, clip, true))
851 // so that the clipboard is used and it goes on
853 // and executes LFUN_PASTESELECTION in insettext!
854 paste_tabular.reset();
860 if (hasPasteBuffer()) {
861 recordUndo(bv, Undo::INSERT);
867 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
870 // handle font changing stuff on selection before we lock the inset
871 // in the default part!
872 result = DispatchResult(false);
873 // we try to activate the actual inset and put this event down to
874 // the insets dispatch function.
879 InsetTabularMailer(*this).updateDialog(bv);
884 int InsetTabular::latex(Buffer const & buf, ostream & os,
885 OutputParams const & runparams) const
887 return tabular.latex(buf, os, runparams);
891 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
892 OutputParams const & runparams) const
894 int dp = runparams.linelen ? ownerPar(buf, this).params().depth() : 0;
895 return tabular.plaintext(buf, os, runparams, dp, false, 0);
899 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
900 OutputParams const & runparams) const
902 return tabular.linuxdoc(buf,os, runparams);
906 int InsetTabular::docbook(Buffer const & buf, ostream & os,
907 OutputParams const & runparams) const
912 // if the table is inside a float it doesn't need the informaltable
913 // wrapper. Search for it.
914 for (master = owner();
915 master && master->lyxCode() != InsetOld::FLOAT_CODE;
916 master = master->owner())
920 os << "<informaltable>";
921 if (runparams.mixed_content)
925 ret += tabular.docbook(buf, os, runparams);
927 os << "</informaltable>";
928 if (runparams.mixed_content)
936 void InsetTabular::validate(LaTeXFeatures & features) const
938 tabular.validate(features);
942 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
945 for (int i = 0; i < tabular.rows(); ++i) {
948 for (int j = 0; j < tabular.columns(); ++j) {
949 if (tabular.isPartOfMultiColumn(i, j))
955 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
956 tabular.getCellInset(cell).metrics(m, dim);
957 maxAsc = max(maxAsc, dim.asc);
958 maxDesc = max(maxDesc, dim.des);
959 tabular.setWidthOfCell(cell, dim.wid);
961 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
962 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
967 void InsetTabular::getCursorPos(int & x, int & y) const
969 if (locked_cell == -1) {
970 x = TEXT_TO_INSET_OFFSET + cursorx_ - xo_;
971 y = TEXT_TO_INSET_OFFSET + cursory_;
973 tabular.getCellInset(locked_cell).getCursorPos(x, y);
974 x = TEXT_TO_INSET_OFFSET;
979 void InsetTabular::setPos(BufferView * bv, int x, int y) const
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(BufferView * bv) const
1026 #ifdef WITH_WARNINGS
1027 #warning This should be fixed in the right manner (20011128 Jug)
1029 // fast hack to fix infinite repaintings!
1030 if (in_reset_pos > 0)
1034 actcol = tabular.column_of_cell(actcell);
1037 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1038 if (tabular.isLastCellInRow(cell)) {
1039 cursory_ += tabular.getDescentOfRow(actrow) +
1040 tabular.getAscentOfRow(actrow + 1) +
1041 tabular.getAdditionalHeight(actrow + 1);
1046 // we need this only from here on!!!
1048 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1049 int new_x = getCellXPos(actcell) + offset;
1050 int old_x = cursorx_;
1052 // cursor.x(getCellXPos(actcell) + offset);
1053 if (actcol < tabular.columns() - 1 && scroll(false) &&
1054 tabular.getWidthOfTabular() < bv->workWidth()-20)
1058 } else if (cursorx_ - offset > 20 &&
1059 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1060 > bv->workWidth() - 20) {
1061 scroll(bv, - tabular.getWidthOfColumn(actcell) - 20);
1063 } else if (cursorx_ - offset < 20) {
1064 scroll(bv, 20 - cursorx_ + offset);
1066 } else if (scroll() && xo_ > 20 &&
1067 xo_ + tabular.getWidthOfTabular() > bv->workWidth() - 20) {
1068 scroll(bv, old_x - cursorx_);
1071 InsetTabularMailer(*this).updateDialog(bv);
1076 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1078 bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1080 return DispatchResult(false, FINISHED_RIGHT);
1082 activateCellInset(bv, 0, 0, false);
1083 return DispatchResult(true, true);
1086 return DispatchResult(true);
1090 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1092 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1094 return DispatchResult(false, FINISHED);
1097 activateCellInset(bv, 0, 0, true);
1098 return DispatchResult(true, true);
1101 return DispatchResult(true);
1105 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1107 int const ocell = actcell;
1108 actcell = tabular.getCellAbove(actcell);
1109 if (actcell == ocell) // we moved out of the inset
1110 return DispatchResult(false, FINISHED_UP);
1113 activateCellInset(bv, bv->x_target(), 0, false);
1114 return DispatchResult(true, true);
1118 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1120 int const ocell = actcell;
1121 actcell = tabular.getCellBelow(actcell);
1122 if (actcell == ocell) // we moved out of the inset
1123 return DispatchResult(false, FINISHED_DOWN);
1126 activateCellInset(bv, bv->x_target(), 0, false);
1127 return DispatchResult(true, true);
1131 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1133 if (isRightToLeft(bv)) {
1134 if (tabular.isFirstCellInRow(actcell)) {
1135 int row = tabular.row_of_cell(actcell);
1136 if (row == tabular.rows() - 1)
1138 actcell = tabular.getLastCellInRow(row);
1139 actcell = tabular.getCellBelow(actcell);
1146 if (tabular.isLastCell(actcell))
1151 bool rtl = tabular.getCellInset(actcell).paragraphs().begin()->
1152 isRightToLeftPar(bv->buffer()->params());
1153 activateCellInset(bv, 0, 0, !rtl);
1160 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1162 lyxerr << "move prevcell 1" << endl;
1163 if (isRightToLeft(bv)) {
1164 lyxerr << "move prevcell a" << endl;
1165 if (tabular.isLastCellInRow(actcell)) {
1166 int row = tabular.row_of_cell(actcell);
1169 actcell = tabular.getFirstCellInRow(row);
1170 actcell = tabular.getCellAbove(actcell);
1172 if (tabular.isLastCell(actcell))
1177 lyxerr << "move prevcell b" << endl;
1178 if (actcell == 0) // first cell
1182 lyxerr << "move prevcell 2" << endl;
1184 bool rtl = tabular.getCellInset(actcell).paragraphs().begin()->
1185 isRightToLeftPar(bv->buffer()->params());
1186 activateCellInset(bv, 0, 0, !rtl);
1188 lyxerr << "move prevcell 3" << endl;
1190 lyxerr << "move prevcell 4" << endl;
1195 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1197 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1200 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1201 string const tmp = tabularFeature[i].feature;
1203 if (tmp == what.substr(0, tmp.length())) {
1204 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1205 //tabularFeatures[i].feature.length())) {
1206 action = tabularFeature[i].action;
1210 if (action == LyXTabular::LAST_ACTION)
1214 ltrim(what.substr(tabularFeature[i].feature.length()));
1215 tabularFeatures(bv, action, val);
1221 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1222 string const & special, bool & flag)
1224 if (special == "dl_above") {
1227 } else if (special == "dl_below") {
1228 ltt.bottomDL = flag;
1230 } else if (special == "empty") {
1242 void InsetTabular::tabularFeatures(BufferView * bv,
1243 LyXTabular::Feature feature, string const & value)
1249 bool setLines = false;
1250 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1251 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1255 case LyXTabular::M_ALIGN_LEFT:
1256 case LyXTabular::ALIGN_LEFT:
1257 setAlign = LYX_ALIGN_LEFT;
1260 case LyXTabular::M_ALIGN_RIGHT:
1261 case LyXTabular::ALIGN_RIGHT:
1262 setAlign = LYX_ALIGN_RIGHT;
1265 case LyXTabular::M_ALIGN_CENTER:
1266 case LyXTabular::ALIGN_CENTER:
1267 setAlign = LYX_ALIGN_CENTER;
1270 case LyXTabular::ALIGN_BLOCK:
1271 setAlign = LYX_ALIGN_BLOCK;
1274 case LyXTabular::M_VALIGN_TOP:
1275 case LyXTabular::VALIGN_TOP:
1276 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1279 case LyXTabular::M_VALIGN_BOTTOM:
1280 case LyXTabular::VALIGN_BOTTOM:
1281 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1284 case LyXTabular::M_VALIGN_MIDDLE:
1285 case LyXTabular::VALIGN_MIDDLE:
1286 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1293 if (hasSelection()) {
1294 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1296 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1297 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1299 recordUndo(bv, Undo::ATOMIC);
1301 int row = tabular.row_of_cell(actcell);
1302 int column = tabular.column_of_cell(actcell);
1304 LyXTabular::ltType ltt;
1308 case LyXTabular::SET_PWIDTH: {
1309 LyXLength const len(value);
1310 tabular.setColumnPWidth(actcell, len);
1312 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1313 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1314 else if (!len.zero()
1315 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1316 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1320 case LyXTabular::SET_MPWIDTH:
1321 tabular.setMColumnPWidth(actcell, LyXLength(value));
1324 case LyXTabular::SET_SPECIAL_COLUMN:
1325 case LyXTabular::SET_SPECIAL_MULTI:
1326 tabular.setAlignSpecial(actcell,value,feature);
1329 case LyXTabular::APPEND_ROW:
1330 // append the row into the tabular
1331 tabular.appendRow(bv->buffer()->params(), actcell);
1332 tabular.setOwner(this);
1335 case LyXTabular::APPEND_COLUMN:
1336 // append the column into the tabular
1337 tabular.appendColumn(bv->buffer()->params(), actcell);
1338 tabular.setOwner(this);
1339 actcell = tabular.getCellNumber(row, column);
1342 case LyXTabular::DELETE_ROW:
1343 for (int i = sel_row_start; i <= sel_row_end; ++i)
1344 tabular.deleteRow(sel_row_start);
1345 if (sel_row_start >= tabular.rows())
1347 actcell = tabular.getCellNumber(sel_row_start, column);
1351 case LyXTabular::DELETE_COLUMN:
1352 for (int i = sel_col_start; i <= sel_col_end; ++i)
1353 tabular.deleteColumn(sel_col_start);
1354 if (sel_col_start >= tabular.columns())
1356 actcell = tabular.getCellNumber(row, sel_col_start);
1360 case LyXTabular::M_TOGGLE_LINE_TOP:
1362 case LyXTabular::TOGGLE_LINE_TOP: {
1363 bool lineSet = !tabular.topLine(actcell, flag);
1364 for (int i = sel_row_start; i <= sel_row_end; ++i)
1365 for (int j = sel_col_start; j <= sel_col_end; ++j)
1367 tabular.getCellNumber(i, j),
1372 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1374 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1375 bool lineSet = !tabular.bottomLine(actcell, flag);
1376 for (int i = sel_row_start; i <= sel_row_end; ++i)
1377 for (int j = sel_col_start; j <= sel_col_end; ++j)
1378 tabular.setBottomLine(
1379 tabular.getCellNumber(i, j),
1385 case LyXTabular::M_TOGGLE_LINE_LEFT:
1387 case LyXTabular::TOGGLE_LINE_LEFT: {
1388 bool lineSet = !tabular.leftLine(actcell, flag);
1389 for (int i = sel_row_start; i <= sel_row_end; ++i)
1390 for (int j = sel_col_start; j <= sel_col_end; ++j)
1391 tabular.setLeftLine(
1392 tabular.getCellNumber(i,j),
1398 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1400 case LyXTabular::TOGGLE_LINE_RIGHT: {
1401 bool lineSet = !tabular.rightLine(actcell, flag);
1402 for (int i = sel_row_start; i <= sel_row_end; ++i)
1403 for (int j = sel_col_start; j <= sel_col_end; ++j)
1404 tabular.setRightLine(
1405 tabular.getCellNumber(i,j),
1411 case LyXTabular::M_ALIGN_LEFT:
1412 case LyXTabular::M_ALIGN_RIGHT:
1413 case LyXTabular::M_ALIGN_CENTER:
1415 case LyXTabular::ALIGN_LEFT:
1416 case LyXTabular::ALIGN_RIGHT:
1417 case LyXTabular::ALIGN_CENTER:
1418 case LyXTabular::ALIGN_BLOCK:
1419 for (int i = sel_row_start; i <= sel_row_end; ++i)
1420 for (int j = sel_col_start; j <= sel_col_end; ++j)
1421 tabular.setAlignment(
1422 tabular.getCellNumber(i, j),
1427 case LyXTabular::M_VALIGN_TOP:
1428 case LyXTabular::M_VALIGN_BOTTOM:
1429 case LyXTabular::M_VALIGN_MIDDLE:
1431 case LyXTabular::VALIGN_TOP:
1432 case LyXTabular::VALIGN_BOTTOM:
1433 case LyXTabular::VALIGN_MIDDLE:
1434 for (int i = sel_row_start; i <= sel_row_end; ++i)
1435 for (int j = sel_col_start; j <= sel_col_end; ++j)
1436 tabular.setVAlignment(
1437 tabular.getCellNumber(i, j),
1441 case LyXTabular::MULTICOLUMN: {
1442 if (sel_row_start != sel_row_end) {
1443 #ifdef WITH_WARNINGS
1444 #warning Need I say it ? This is horrible.
1446 Alert::error(_("Error setting multicolumn"),
1447 _("You cannot set multicolumn vertically."));
1450 // just multicol for one Single Cell
1451 if (!hasSelection()) {
1452 // check wether we are completly in a multicol
1453 if (tabular.isMultiColumn(actcell))
1454 tabular.unsetMultiColumn(actcell);
1456 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1459 // we have a selection so this means we just add all this
1460 // cells to form a multicolumn cell
1464 if (sel_cell_start > sel_cell_end) {
1465 s_start = sel_cell_end;
1466 s_end = sel_cell_start;
1468 s_start = sel_cell_start;
1469 s_end = sel_cell_end;
1471 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1477 case LyXTabular::SET_ALL_LINES:
1479 case LyXTabular::UNSET_ALL_LINES:
1480 for (int i = sel_row_start; i <= sel_row_end; ++i)
1481 for (int j = sel_col_start; j <= sel_col_end; ++j)
1482 tabular.setAllLines(
1483 tabular.getCellNumber(i,j), setLines);
1486 case LyXTabular::SET_LONGTABULAR:
1487 tabular.setLongTabular(true);
1490 case LyXTabular::UNSET_LONGTABULAR:
1491 tabular.setLongTabular(false);
1494 case LyXTabular::SET_ROTATE_TABULAR:
1495 tabular.setRotateTabular(true);
1498 case LyXTabular::UNSET_ROTATE_TABULAR:
1499 tabular.setRotateTabular(false);
1502 case LyXTabular::SET_ROTATE_CELL:
1503 for (int i = sel_row_start; i <= sel_row_end; ++i)
1504 for (int j = sel_col_start; j <= sel_col_end; ++j)
1505 tabular.setRotateCell(
1506 tabular.getCellNumber(i, j), true);
1509 case LyXTabular::UNSET_ROTATE_CELL:
1510 for (int i = sel_row_start; i <= sel_row_end; ++i)
1511 for (int j = sel_col_start; j <= sel_col_end; ++j)
1512 tabular.setRotateCell(
1513 tabular.getCellNumber(i, j), false);
1516 case LyXTabular::SET_USEBOX: {
1517 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1518 if (val == tabular.getUsebox(actcell))
1519 val = LyXTabular::BOX_NONE;
1520 for (int i = sel_row_start; i <= sel_row_end; ++i)
1521 for (int j = sel_col_start; j <= sel_col_end; ++j)
1522 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1526 case LyXTabular::UNSET_LTFIRSTHEAD:
1528 case LyXTabular::SET_LTFIRSTHEAD:
1529 tabular.getRowOfLTFirstHead(row, ltt);
1530 checkLongtableSpecial(ltt, value, flag);
1531 tabular.setLTHead(row, flag, ltt, true);
1534 case LyXTabular::UNSET_LTHEAD:
1536 case LyXTabular::SET_LTHEAD:
1537 tabular.getRowOfLTHead(row, ltt);
1538 checkLongtableSpecial(ltt, value, flag);
1539 tabular.setLTHead(row, flag, ltt, false);
1542 case LyXTabular::UNSET_LTFOOT:
1544 case LyXTabular::SET_LTFOOT:
1545 tabular.getRowOfLTFoot(row, ltt);
1546 checkLongtableSpecial(ltt, value, flag);
1547 tabular.setLTFoot(row, flag, ltt, false);
1550 case LyXTabular::UNSET_LTLASTFOOT:
1552 case LyXTabular::SET_LTLASTFOOT:
1553 tabular.getRowOfLTLastFoot(row, ltt);
1554 checkLongtableSpecial(ltt, value, flag);
1555 tabular.setLTFoot(row, flag, ltt, true);
1558 case LyXTabular::SET_LTNEWPAGE:
1559 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1562 // dummy stuff just to avoid warnings
1563 case LyXTabular::LAST_ACTION:
1568 InsetTabularMailer(*this).updateDialog(bv);
1572 void InsetTabular::activateCellInset(BufferView * bv, int x, int y, bool behind)
1574 UpdatableInset & inset = tabular.getCellInset(actcell);
1577 x = inset.x() + inset.width();
1578 y = inset.descent();
1580 inset.edit(bv, x, y);
1581 bv->cursor().data_.back().idx_ = actcell;
1582 locked_cell = actcell;
1587 bool InsetTabular::showInsetDialog(BufferView * bv) const
1589 InsetTabularMailer(*this).showDialog(bv);
1594 void InsetTabular::openLayoutDialog(BufferView * bv) const
1596 InsetTabularMailer(*this).showDialog(bv);
1601 // function returns an object as defined in func_status.h:
1602 // states OK, Unknown, Disabled, On, Off.
1604 FuncStatus InsetTabular::getStatus(string const & what) const
1606 int action = LyXTabular::LAST_ACTION;
1610 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1611 string const tmp = tabularFeature[i].feature;
1612 if (tmp == what.substr(0, tmp.length())) {
1613 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1614 // tabularFeatures[i].feature.length())) {
1615 action = tabularFeature[i].action;
1619 if (action == LyXTabular::LAST_ACTION) {
1621 status.unknown(true);
1625 string const argument
1626 = ltrim(what.substr(tabularFeature[i].feature.length()));
1631 LyXTabular::ltType dummyltt;
1635 getSelection(sel_row_start, sel_row_end, dummy, dummy);
1637 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1640 case LyXTabular::SET_PWIDTH:
1641 case LyXTabular::SET_MPWIDTH:
1642 case LyXTabular::SET_SPECIAL_COLUMN:
1643 case LyXTabular::SET_SPECIAL_MULTI:
1644 case LyXTabular::APPEND_ROW:
1645 case LyXTabular::APPEND_COLUMN:
1646 case LyXTabular::DELETE_ROW:
1647 case LyXTabular::DELETE_COLUMN:
1648 case LyXTabular::SET_ALL_LINES:
1649 case LyXTabular::UNSET_ALL_LINES:
1653 case LyXTabular::MULTICOLUMN:
1654 status.setOnOff(tabular.isMultiColumn(actcell));
1657 case LyXTabular::M_TOGGLE_LINE_TOP:
1659 case LyXTabular::TOGGLE_LINE_TOP:
1660 status.setOnOff(tabular.topLine(actcell, flag));
1663 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1665 case LyXTabular::TOGGLE_LINE_BOTTOM:
1666 status.setOnOff(tabular.bottomLine(actcell, flag));
1669 case LyXTabular::M_TOGGLE_LINE_LEFT:
1671 case LyXTabular::TOGGLE_LINE_LEFT:
1672 status.setOnOff(tabular.leftLine(actcell, flag));
1675 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1677 case LyXTabular::TOGGLE_LINE_RIGHT:
1678 status.setOnOff(tabular.rightLine(actcell, flag));
1681 case LyXTabular::M_ALIGN_LEFT:
1683 case LyXTabular::ALIGN_LEFT:
1684 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
1687 case LyXTabular::M_ALIGN_RIGHT:
1689 case LyXTabular::ALIGN_RIGHT:
1690 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
1693 case LyXTabular::M_ALIGN_CENTER:
1695 case LyXTabular::ALIGN_CENTER:
1696 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
1699 case LyXTabular::ALIGN_BLOCK:
1700 status.disabled(tabular.getPWidth(actcell).zero());
1701 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
1704 case LyXTabular::M_VALIGN_TOP:
1706 case LyXTabular::VALIGN_TOP:
1708 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
1711 case LyXTabular::M_VALIGN_BOTTOM:
1713 case LyXTabular::VALIGN_BOTTOM:
1715 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
1718 case LyXTabular::M_VALIGN_MIDDLE:
1720 case LyXTabular::VALIGN_MIDDLE:
1722 tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
1725 case LyXTabular::SET_LONGTABULAR:
1726 status.setOnOff(tabular.isLongTabular());
1729 case LyXTabular::UNSET_LONGTABULAR:
1730 status.setOnOff(!tabular.isLongTabular());
1733 case LyXTabular::SET_ROTATE_TABULAR:
1734 status.setOnOff(tabular.getRotateTabular());
1737 case LyXTabular::UNSET_ROTATE_TABULAR:
1738 status.setOnOff(!tabular.getRotateTabular());
1741 case LyXTabular::SET_ROTATE_CELL:
1742 status.setOnOff(tabular.getRotateCell(actcell));
1745 case LyXTabular::UNSET_ROTATE_CELL:
1746 status.setOnOff(!tabular.getRotateCell(actcell));
1749 case LyXTabular::SET_USEBOX:
1750 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
1753 case LyXTabular::SET_LTFIRSTHEAD:
1754 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1757 case LyXTabular::SET_LTHEAD:
1758 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
1761 case LyXTabular::SET_LTFOOT:
1762 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1765 case LyXTabular::SET_LTLASTFOOT:
1766 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
1769 case LyXTabular::SET_LTNEWPAGE:
1770 status.setOnOff(tabular.getLTNewPage(sel_row_start));
1775 status.disabled(true);
1782 void InsetTabular::getLabelList(Buffer const & buffer,
1783 std::vector<string> & list) const
1785 tabular.getLabelList(buffer, list);
1789 bool InsetTabular::copySelection(BufferView * bv)
1791 if (!hasSelection())
1794 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1795 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1796 if (sel_col_start > sel_col_end) {
1797 sel_col_start = sel_col_end;
1798 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1800 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1803 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1804 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1805 if (sel_row_start > sel_row_end)
1806 swap(sel_row_start, sel_row_end);
1808 paste_tabular.reset(new LyXTabular(tabular));
1809 paste_tabular->setOwner(this);
1811 for (int i = 0; i < sel_row_start; ++i)
1812 paste_tabular->deleteRow(0);
1814 int const rows = sel_row_end - sel_row_start + 1;
1815 while (paste_tabular->rows() > rows)
1816 paste_tabular->deleteRow(rows);
1818 paste_tabular->setTopLine(0, true, true);
1819 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1822 for (int i = 0; i < sel_col_start; ++i)
1823 paste_tabular->deleteColumn(0);
1825 int const columns = sel_col_end - sel_col_start + 1;
1826 while (paste_tabular->columns() > columns)
1827 paste_tabular->deleteColumn(columns);
1829 paste_tabular->setLeftLine(0, true, true);
1830 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1834 OutputParams const runparams;
1835 paste_tabular->plaintext(*bv->buffer(), os, runparams,
1836 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
1837 bv->stuffClipboard(os.str());
1842 bool InsetTabular::pasteSelection(BufferView *)
1847 for (int r1 = 0, r2 = actrow;
1848 r1 < paste_tabular->rows() && r2 < tabular.rows();
1850 for (int c1 = 0, c2 = actcol;
1851 c1 < paste_tabular->columns() && c2 < tabular.columns();
1853 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1854 tabular.isPartOfMultiColumn(r2, c2))
1856 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1860 if (tabular.isPartOfMultiColumn(r2, c2)) {
1864 InsetText & inset = tabular.getCellInset(r2, c2);
1865 inset = paste_tabular->getCellInset(r1, c1);
1866 inset.setOwner(this);
1874 bool InsetTabular::cutSelection(BufferParams const & bp)
1876 if (!hasSelection())
1879 int sel_col_start = tabular.column_of_cell(sel_cell_start);
1880 int sel_col_end = tabular.column_of_cell(sel_cell_end);
1881 if (sel_col_start > sel_col_end) {
1882 sel_col_start = sel_col_end;
1883 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
1885 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
1888 int sel_row_start = tabular.row_of_cell(sel_cell_start);
1889 int sel_row_end = tabular.row_of_cell(sel_cell_end);
1891 if (sel_row_start > sel_row_end)
1892 swap(sel_row_start, sel_row_end);
1894 if (sel_cell_start > sel_cell_end)
1895 swap(sel_cell_start, sel_cell_end);
1897 for (int i = sel_row_start; i <= sel_row_end; ++i)
1898 for (int j = sel_col_start; j <= sel_col_end; ++j)
1899 tabular.getCellInset(tabular.getCellNumber(i, j))
1900 .clear(bp.tracking_changes);
1905 bool InsetTabular::isRightToLeft(BufferView * bv)
1907 return bv->getParentLanguage(this)->RightToLeft();
1911 void InsetTabular::getSelection(int & srow, int & erow,
1912 int & scol, int & ecol) const
1914 int const start = hasSelection() ? sel_cell_start : actcell;
1915 int const end = hasSelection() ? sel_cell_end : actcell;
1917 srow = tabular.row_of_cell(start);
1918 erow = tabular.row_of_cell(end);
1922 scol = tabular.column_of_cell(start);
1923 ecol = tabular.column_of_cell(end);
1927 ecol = tabular.right_column_of_cell(end);
1931 int InsetTabular::numParagraphs() const
1933 return tabular.getNumberOfCells();
1937 LyXText * InsetTabular::getText(int i) const
1939 return i < tabular.getNumberOfCells()
1940 ? tabular.getCellInset(i).getText(0)
1945 void InsetTabular::markErased()
1947 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
1948 tabular.getCellInset(cell).markErased();
1952 bool InsetTabular::insetAllowed(InsetOld::Code) const
1958 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
1960 const int cell = tabular.getCellFromInset(in);
1963 return tabular.getPWidth(cell).zero();
1965 // this is a workaround for a crash (New, Insert->Tabular,
1966 // Insert->FootNote)
1970 // well we didn't obviously find it so maybe our owner knows more
1971 BOOST_ASSERT(owner());
1972 return owner()->forceDefaultParagraphs(in);
1976 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
1979 if (buf.length() <= 0)
1985 string::size_type len = buf.length();
1986 string::size_type p = 0;
1988 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1996 maxCols = max(cols, maxCols);
2002 maxCols = max(cols, maxCols);
2003 LyXTabular * loctab;
2008 paste_tabular.reset(
2009 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2012 paste_tabular->setOwner(this);
2013 loctab = paste_tabular.get();
2022 string::size_type op = 0;
2023 int cells = loctab->getNumberOfCells();
2026 rows = loctab->rows();
2027 int const columns = loctab->columns();
2029 while (cell < cells && p < len && row < rows &&
2030 (p = buf.find_first_of("\t\n", p)) != string::npos)
2036 // we can only set this if we are not too far right
2037 if (cols < columns) {
2038 InsetText & inset = loctab->getCellInset(cell);
2039 LyXFont const font = inset.text_.
2040 getFont(inset.paragraphs().begin(), 0);
2041 inset.setText(buf.substr(op, p - op), font);
2047 // we can only set this if we are not too far right
2048 if (cols < columns) {
2049 InsetText & inset = tabular.getCellInset(cell);
2050 LyXFont const font = inset.text_.
2051 getFont(inset.paragraphs().begin(), 0);
2052 inset.setText(buf.substr(op, p - op), font);
2057 cell = loctab->getCellNumber(row, cols);
2063 // check for the last cell if there is no trailing '\n'
2064 if (cell < cells && op < len) {
2065 InsetText & inset = loctab->getCellInset(cell);
2066 LyXFont const font = inset.text_.getFont(inset.paragraphs().begin(), 0);
2067 inset.setText(buf.substr(op, len - op), font);
2074 void InsetTabular::addPreview(PreviewLoader & loader) const
2076 int const rows = tabular.rows();
2077 int const columns = tabular.columns();
2078 for (int i = 0; i < rows; ++i) {
2079 for (int j = 0; j < columns; ++j)
2080 tabular.getCellInset(i, j).addPreview(loader);
2085 string const InsetTabularMailer::name_("tabular");
2087 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2088 : inset_(const_cast<InsetTabular &>(inset))
2092 string const InsetTabularMailer::inset2string(Buffer const &) const
2094 return params2string(inset_);
2098 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2100 istringstream data(in);
2102 lex.setStream(data);
2104 #warning CHECK verify that this is a sane value to return.
2110 string const token = lex.getString();
2118 string const token = lex.getString();
2119 if (token != "\\active_cell")
2122 cell = lex.getInteger();
2125 // This is part of the inset proper that is usually swallowed
2126 // by Buffer::readInset
2129 string const token = lex.getString();
2130 if (token != "Tabular")
2137 Buffer const & buffer = inset.buffer();
2138 inset.read(buffer, lex);
2140 // We can't set the active cell, but we can tell the frontend
2146 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2149 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2150 inset.write(inset.buffer(), data);
2151 data << "\\end_inset\n";