3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insettabular.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
24 #include "latexrunparams.h"
28 #include "metricsinfo.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "ParagraphParameters.h"
33 #include "WordLangTuple.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;
53 using std::istringstream;
55 using std::ostringstream;
60 int const ADD_TO_HEIGHT = 2;
61 int const ADD_TO_TABULAR_WIDTH = 2;
64 boost::scoped_ptr<LyXTabular> paste_tabular;
67 struct TabularFeature {
68 LyXTabular::Feature action;
73 TabularFeature tabularFeature[] =
75 { LyXTabular::APPEND_ROW, "append-row" },
76 { LyXTabular::APPEND_COLUMN, "append-column" },
77 { LyXTabular::DELETE_ROW, "delete-row" },
78 { LyXTabular::DELETE_COLUMN, "delete-column" },
79 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
80 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
81 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
82 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
83 { LyXTabular::ALIGN_LEFT, "align-left" },
84 { LyXTabular::ALIGN_RIGHT, "align-right" },
85 { LyXTabular::ALIGN_CENTER, "align-center" },
86 { LyXTabular::ALIGN_BLOCK, "align-block" },
87 { LyXTabular::VALIGN_TOP, "valign-top" },
88 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
89 { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
90 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
91 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
92 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
93 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
94 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
95 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
96 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
97 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
98 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
99 { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
100 { LyXTabular::MULTICOLUMN, "multicolumn" },
101 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
102 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
103 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
104 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
105 { LyXTabular::SET_PWIDTH, "set-pwidth" },
106 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
107 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
108 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
109 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
110 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
111 { LyXTabular::SET_USEBOX, "set-usebox" },
112 { LyXTabular::SET_LTHEAD, "set-lthead" },
113 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
114 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
115 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
116 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
117 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
118 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
119 { LyXTabular::LAST_ACTION, "" }
123 FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
124 bool operator()(TabularFeature & tf)
126 return tf.action == feature_;
129 LyXTabular::Feature feature_;
135 string const featureAsString(LyXTabular::Feature feature)
137 TabularFeature * it = tabularFeature;
138 TabularFeature * end = it +
139 sizeof(tabularFeature) / sizeof(TabularFeature);
140 it = std::find_if(it, end, FindFeature(feature));
141 return (it == end) ? string() : it->feature;
145 bool InsetTabular::hasPasteBuffer() const
147 return (paste_tabular.get() != 0);
151 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
152 : tabular(buf.params(), max(rows, 1), max(columns, 1)),
153 buffer_(&buf), cursorx_(0), cursory_(0)
155 tabular.setOwner(this);
156 // for now make it always display as display() inset
158 the_locking_inset = 0;
159 old_locking_inset = 0;
162 actrow = actcell = 0;
170 InsetTabular::InsetTabular(InsetTabular const & tab)
171 : UpdatableInset(tab), tabular(tab.tabular),
172 buffer_(tab.buffer_), cursorx_(0), cursory_(0)
174 tabular.setOwner(this);
175 the_locking_inset = 0;
176 old_locking_inset = 0;
179 actrow = actcell = 0;
187 InsetTabular::~InsetTabular()
189 InsetTabularMailer(*this).hideDialog();
193 auto_ptr<InsetBase> InsetTabular::clone() const
195 return auto_ptr<InsetBase>(new InsetTabular(*this));
199 Buffer const & InsetTabular::buffer() const
205 BufferView * InsetTabular::view() const
212 void InsetTabular::buffer(Buffer * b)
218 void InsetTabular::write(Buffer const & buf, ostream & os) const
220 os << "Tabular" << endl;
221 tabular.write(buf, os);
225 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
227 bool const old_format = (lex.getString() == "\\LyXTable");
229 tabular.read(buf, lex);
235 string token = lex.getString();
236 while (lex.isOK() && (token != "\\end_inset")) {
238 token = lex.getString();
240 if (token != "\\end_inset") {
241 lex.printError("Missing \\end_inset at this point. "
247 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
249 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
250 // mi.base.textwidth << "\n";
252 lyxerr << "InsetTabular::metrics: need bv" << endl;
256 calculate_dimensions_of_cells(mi);
258 dim.asc = tabular.getAscentOfRow(0);
259 dim.des = tabular.getHeightOfTabular() - tabular.getAscentOfRow(0) + 1;
260 dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
265 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
267 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
269 BufferView * bv = pi.base.bv;
272 UpdatableInset::draw(pi, x, y);
280 x += ADD_TO_TABULAR_WIDTH;
283 first_visible_cell = -1;
284 for (int i = 0; i < tabular.rows(); ++i) {
286 cell = tabular.getCellNumber(i, 0);
287 if (y + tabular.getDescentOfRow(i) <= 0 &&
288 y - tabular.getAscentOfRow(i) < pi.pain.paperHeight())
290 y += tabular.getDescentOfRow(i) +
291 tabular.getAscentOfRow(i + 1) +
292 tabular.getAdditionalHeight(i + 1);
295 for (int j = 0; j < tabular.columns(); ++j) {
296 if (nx > bv->workWidth())
298 if (tabular.isPartOfMultiColumn(i, j))
300 int cx = nx + tabular.getBeginningOfTextInCell(cell);
301 if (first_visible_cell < 0)
302 first_visible_cell = cell;
303 if (hasSelection()) {
304 drawCellSelection(pi.pain, nx, y, i, j, cell);
307 tabular.getCellInset(cell).draw(pi, cx, y);
308 drawCellLines(pi.pain, nx, y, i, cell);
309 nx += tabular.getWidthOfColumn(cell);
313 // Would be nice, but for some completely unfathomable reason,
314 // on a col resize to a new fixed width, even though the insettexts
315 // are resized, the cell isn't, but drawing all cells in a tall table
316 // has the desired effect somehow. Complete dark magic.
318 // avoiding drawing the rest of a long table is
319 // a pretty big speedup
320 if (y > bv->workHeight())
324 y += tabular.getDescentOfRow(i) +
325 tabular.getAscentOfRow(i + 1) +
326 tabular.getAdditionalHeight(i + 1);
331 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
332 int row, int cell) const
334 int x2 = x + tabular.getWidthOfColumn(cell);
337 if (!tabular.topAlreadyDrawn(cell)) {
338 on_off = !tabular.topLine(cell);
339 pain.line(x, y - tabular.getAscentOfRow(row),
340 x2, y - tabular.getAscentOfRow(row),
341 on_off ? LColor::tabularonoffline : LColor::tabularline,
342 on_off ? Painter::line_onoffdash : Painter::line_solid);
344 on_off = !tabular.bottomLine(cell);
345 pain.line(x, y + tabular.getDescentOfRow(row),
346 x2, y + tabular.getDescentOfRow(row),
347 on_off ? LColor::tabularonoffline : LColor::tabularline,
348 on_off ? Painter::line_onoffdash : Painter::line_solid);
349 if (!tabular.leftAlreadyDrawn(cell)) {
350 on_off = !tabular.leftLine(cell);
351 pain.line(x, y - tabular.getAscentOfRow(row),
352 x, y + tabular.getDescentOfRow(row),
353 on_off ? LColor::tabularonoffline : LColor::tabularline,
354 on_off ? Painter::line_onoffdash : Painter::line_solid);
356 on_off = !tabular.rightLine(cell);
357 pain.line(x2 - tabular.getAdditionalWidth(cell),
358 y - tabular.getAscentOfRow(row),
359 x2 - tabular.getAdditionalWidth(cell),
360 y + tabular.getDescentOfRow(row),
361 on_off ? LColor::tabularonoffline : LColor::tabularline,
362 on_off ? Painter::line_onoffdash : Painter::line_solid);
366 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
367 int row, int column, int cell) const
369 BOOST_ASSERT(hasSelection());
370 int cs = tabular.column_of_cell(sel_cell_start);
371 int ce = tabular.column_of_cell(sel_cell_end);
374 cs = tabular.column_of_cell(sel_cell_end);
376 ce = tabular.right_column_of_cell(sel_cell_end);
379 int rs = tabular.row_of_cell(sel_cell_start);
380 int re = tabular.row_of_cell(sel_cell_end);
384 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
385 int w = tabular.getWidthOfColumn(cell);
386 int h = tabular.getAscentOfRow(row) + tabular.getDescentOfRow(row)-1;
387 pain.fillRectangle(x, y - tabular.getAscentOfRow(row) + 1,
388 w, h, LColor::selection);
393 string const InsetTabular::editMessage() const
395 return _("Opened table");
399 void InsetTabular::insetUnlock(BufferView * bv)
401 if (the_locking_inset) {
402 the_locking_inset->insetUnlock(bv);
404 the_locking_inset = 0;
409 if (scroll(false) || hasSelection()) {
418 void InsetTabular::updateLocal(BufferView * bv) const
420 bv->updateInset(this);
426 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
428 lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
435 if (inset == &tabular.getCellInset(actcell)) {
436 lyxerr[Debug::INSETTEXT] << "OK" << endl;
437 the_locking_inset = &tabular.getCellInset(actcell);
442 if (!the_locking_inset) {
443 int const n = tabular.getNumberOfCells();
444 for (int i = 0; i < n; ++i) {
445 InsetText * in = &tabular.getCellInset(i);
448 the_locking_inset = in;
453 if (in->lockInsetInInset(bv, inset)) {
455 the_locking_inset = in;
458 in->dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
465 if (the_locking_inset && (the_locking_inset == inset)) {
466 lyxerr[Debug::INSETTEXT] << "OK" << endl;
471 if (the_locking_inset) {
472 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
473 return the_locking_inset->lockInsetInInset(bv, inset);
476 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
481 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
484 if (!the_locking_inset)
486 if (the_locking_inset == inset) {
487 the_locking_inset->insetUnlock(bv);
489 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
496 // this has to be here otherwise we don't redraw the cell!
497 the_locking_inset = 0;
500 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
501 if (inset->lyxCode() == TABULAR_CODE &&
502 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
503 InsetTabularMailer(*this).updateDialog(bv);
512 int InsetTabular::insetInInsetY() const
514 if (!the_locking_inset)
516 return inset_y + the_locking_inset->insetInInsetY();
520 UpdatableInset * InsetTabular::getLockingInset() const
522 return the_locking_inset ? the_locking_inset->getLockingInset() :
523 const_cast<InsetTabular *>(this);
527 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
531 if (the_locking_inset)
532 return the_locking_inset->getFirstLockingInsetOfType(c);
537 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
539 if (the_locking_inset)
540 return the_locking_inset->insertInset(bv, inset);
545 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
547 if (hasSelection() && cmd.button() == mouse_button::button3)
550 if (hasSelection()) {
552 updateLocal(cmd.view());
555 int const ocell = actcell;
556 BufferView * bv = cmd.view();
560 the_locking_inset = 0;
564 setPos(bv, cmd.x, cmd.y);
567 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
569 if (ocell == actcell && the_locking_inset && inset_hit) {
571 FuncRequest cmd1 = cmd;
574 the_locking_inset->dispatch(cmd1);
578 if (the_locking_inset) {
579 the_locking_inset->insetUnlock(bv);
581 the_locking_inset = 0;
584 if (cmd.button() == mouse_button::button2) {
585 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
589 if (inset_hit && bv->theLockingInset()) {
590 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
591 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
594 FuncRequest cmd1 = cmd;
597 the_locking_inset->dispatch(cmd1);
602 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
604 DispatchResult ret(false);
605 if (the_locking_inset) {
606 FuncRequest cmd1 = cmd;
609 ret = the_locking_inset->dispatch(cmd1);
611 if (cmd.button() == mouse_button::button3 && ret == DispatchResult(false)) {
612 InsetTabularMailer(*this).showDialog(cmd.view());
615 return ret.dispatched() || ret.val() > FINISHED;
619 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
621 if (the_locking_inset) {
622 FuncRequest cmd1 = cmd;
625 the_locking_inset->dispatch(cmd1);
629 BufferView * bv = cmd.view();
630 int const old_cell = actcell;
632 setPos(bv, cmd.x, cmd.y);
633 if (!hasSelection()) {
634 setSelection(actcell, actcell);
636 } else if (old_cell != actcell) {
637 setSelection(sel_cell_start, actcell);
643 void InsetTabular::edit(BufferView * bv, int index)
645 lyxerr << "InsetTabular::edit" << endl;
646 if (!bv->lockInset(this)) {
647 lyxerr << "InsetTabular::Cannot lock inset (2)" << endl;
652 the_locking_inset = 0;
660 UpdatableInset & inset = tabular.getCellInset(actcell);
661 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
662 if (the_locking_inset)
668 InsetTabular::priv_dispatch(FuncRequest const & cmd,
669 idx_type & idx, pos_type & pos)
671 // We need to save the value of the_locking_inset as the call to
672 // the_locking_inset->localDispatch might unlock it.
673 old_locking_inset = the_locking_inset;
674 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
675 BufferView * bv = cmd.view();
677 if (cmd.action == LFUN_INSET_EDIT) {
679 lyxerr << "InsetTabular::edit: " << this << " args: '"
680 << cmd.argument << "' first cell: "
681 << &tabular.cell_info[0][0].inset << endl;
683 if (!bv->lockInset(this)) {
684 lyxerr << "InsetTabular::Cannot lock inset" << endl;
685 return DispatchResult(true, true);
690 the_locking_inset = 0;
694 if (cmd.argument.size()) {
695 if (cmd.argument == "left") {
696 if (isRightToLeft(bv))
697 actcell = tabular.getLastCellInRow(0);
701 if (isRightToLeft(bv))
702 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
704 actcell = tabular.getNumberOfCells() - 1;
712 setPos(bv, cmd.x, cmd.y);
715 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
716 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
718 activateCellInset(bv, cmd.x - inset_x, cmd.y - inset_y, cmd.button());
721 return DispatchResult(true, true);
724 if (result.dispatched()) {
729 if (cmd.action < 0 && cmd.argument.empty())
730 return DispatchResult(false, FINISHED);
732 bool hs = hasSelection();
734 result = DispatchResult(true, true);
735 // this one have priority over the locked InsetText, if we're not already
736 // inside another tabular then that one get's priority!
737 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
738 switch (cmd.action) {
739 case LFUN_MOUSE_PRESS:
741 return DispatchResult(true, true);
743 case LFUN_MOUSE_MOTION:
744 lfunMouseMotion(cmd);
745 return DispatchResult(true, true);
747 case LFUN_MOUSE_RELEASE:
748 return DispatchResult(lfunMouseRelease(cmd));
750 case LFUN_CELL_BACKWARD:
751 case LFUN_CELL_FORWARD:
752 unlockInsetInInset(bv, the_locking_inset);
753 if (cmd.action == LFUN_CELL_FORWARD)
754 moveNextCell(bv, old_locking_inset != 0);
756 movePrevCell(bv, old_locking_inset != 0);
760 if (!the_locking_inset)
761 return DispatchResult(true);
763 // this to avoid compiler warnings.
769 kb_action action = cmd.action;
770 string arg = cmd.argument;
771 if (the_locking_inset) {
772 result = the_locking_inset->dispatch(cmd);
773 if (result.dispatched()) {
774 if (result.update()) {
777 int const sc = scroll();
779 if (sc != scroll()) {
780 // inset has been scrolled
785 } else if (result.val() == FINISHED_UP) {
787 // Make sure to reset status message after
788 // exiting, e.g. math inset
789 bv->owner()->clearMessage();
790 } else if (result.val() == FINISHED_DOWN) {
792 bv->owner()->clearMessage();
793 } else if (result.val() == FINISHED_RIGHT) {
795 bv->owner()->clearMessage();
796 } else if (result.val() == FINISHED) {
797 bv->owner()->clearMessage();
801 result = DispatchResult(true, true);
803 // --- Cursor Movements ----------------------------------
804 case LFUN_RIGHTSEL: {
805 int const start = hasSelection() ? sel_cell_start : actcell;
806 if (tabular.isLastCellInRow(actcell)) {
807 setSelection(start, actcell);
812 // if we are starting a selection, only select
813 // the current cell at the beginning
814 if (hasSelection()) {
815 moveRight(bv, false);
818 setSelection(start, end);
823 result = moveRight(bv);
829 int const start = hasSelection() ? sel_cell_start : actcell;
830 if (tabular.isFirstCellInRow(actcell)) {
831 setSelection(start, actcell);
836 // if we are starting a selection, only select
837 // the current cell at the beginning
838 if (hasSelection()) {
842 setSelection(start, end);
847 result = moveLeft(bv);
853 int const start = hasSelection() ? sel_cell_start : actcell;
854 int const ocell = actcell;
855 // if we are starting a selection, only select
856 // the current cell at the beginning
857 if (hasSelection()) {
859 if (ocell == sel_cell_end ||
860 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
861 setSelection(start, tabular.getCellBelow(sel_cell_end));
863 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
865 setSelection(start, start);
871 result = moveDown(bv, old_locking_inset != 0);
877 int const start = hasSelection() ? sel_cell_start : actcell;
878 int const ocell = actcell;
879 // if we are starting a selection, only select
880 // the current cell at the beginning
881 if (hasSelection()) {
883 if ((ocell == sel_cell_end) ||
884 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
885 setSelection(start, tabular.getCellAbove(sel_cell_end));
887 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
889 setSelection(start, start);
895 result = moveUp(bv, old_locking_inset != 0);
904 unlockInsetInInset(bv, the_locking_inset);
905 if (bv->top_y() + bv->painter().paperHeight() <
906 top_baseline + tabular.getHeightOfTabular())
908 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
909 actcell = tabular.getCellBelow(first_visible_cell) + column;
911 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
921 unlockInsetInInset(bv, the_locking_inset);
922 if (top_baseline < 0) {
923 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
924 if (top_baseline > 0)
927 actcell = tabular.getCellBelow(first_visible_cell) + column;
935 // none of these make sense for insettabular,
936 // but we must catch them to prevent any
937 // selection from being confused
941 case LFUN_WORDLEFTSEL:
943 case LFUN_WORDRIGHTSEL:
945 case LFUN_DOWN_PARAGRAPH:
946 case LFUN_DOWN_PARAGRAPHSEL:
947 case LFUN_UP_PARAGRAPH:
948 case LFUN_UP_PARAGRAPHSEL:
954 case LFUN_BEGINNINGBUF:
955 case LFUN_BEGINNINGBUFSEL:
959 case LFUN_LAYOUT_TABULAR:
960 InsetTabularMailer(*this).showDialog(bv);
962 case LFUN_INSET_DIALOG_UPDATE:
963 InsetTabularMailer(*this).updateDialog(bv);
965 case LFUN_TABULAR_FEATURE:
966 if (!tabularFeatures(bv, arg))
967 result = DispatchResult(false);
969 // insert file functions
970 case LFUN_FILE_INSERT_ASCII_PARA:
971 case LFUN_FILE_INSERT_ASCII:
973 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
976 if (insertAsciiString(bv, tmpstr, false))
979 result = DispatchResult(false);
982 // cut and paste functions
984 if (!copySelection(bv))
988 recordUndo(bv, Undo::DELETE);
989 cutSelection(bv->buffer()->params());
998 case LFUN_PASTESELECTION:
1000 string const clip = bv->getClipboard();
1004 if (clip.find('\t') != string::npos) {
1008 string::size_type len = clip.length();
1009 string::size_type p = 0;
1012 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1020 maxCols = max(cols, maxCols);
1026 maxCols = max(cols, maxCols);
1028 paste_tabular.reset(
1029 new LyXTabular(bv->buffer()->params(),
1030 this, rows, maxCols)
1033 string::size_type op = 0;
1035 int cells = paste_tabular->getNumberOfCells();
1037 while ((cell < cells) && (p < len) &&
1038 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1043 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1048 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1049 while (cols++ < maxCols)
1057 // check for the last cell if there is no trailing '\n'
1058 if ((cell < cells) && (op < len))
1059 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1062 if (!insertAsciiString(bv, clip, true))
1065 // so that the clipboard is used and it goes on
1067 // and executes LFUN_PASTESELECTION in insettext!
1068 paste_tabular.reset();
1072 if (hasPasteBuffer()) {
1073 recordUndo(bv, Undo::INSERT);
1078 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1080 // handle font changing stuff on selection before we lock the inset
1081 // in the default part!
1082 result = DispatchResult(false);
1093 case LFUN_UNDERLINE:
1094 case LFUN_FONT_SIZE:
1095 if (bv->dispatch(FuncRequest(bv, action, arg)))
1096 result = DispatchResult(true, true);
1102 // we try to activate the actual inset and put this event down to
1103 // the insets dispatch function.
1104 if (result.dispatched() || the_locking_inset)
1106 if (activateCellInset(bv)) {
1107 result = the_locking_inset->dispatch(FuncRequest(bv, action, arg));
1108 if (!result.dispatched()) {
1109 unlockInsetInInset(bv, the_locking_inset);
1110 // we need to update if this was requested before
1112 return DispatchResult(false);
1122 if (result.val() >= FINISHED)
1123 bv->unlockInset(this);
1124 else if (!the_locking_inset && bv->fitCursor())
1131 int InsetTabular::latex(Buffer const & buf, ostream & os,
1132 LatexRunParams const & runparams) const
1134 return tabular.latex(buf, os, runparams);
1138 int InsetTabular::ascii(Buffer const & buf, ostream & os,
1139 LatexRunParams const & runparams) const
1141 if (runparams.linelen > 0)
1142 return tabular.ascii(buf, os, runparams,
1143 ownerPar(buf, this).params().depth(),
1145 return tabular.ascii(buf, os, runparams, 0, false, 0);
1149 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1150 LatexRunParams const & runparams) const
1152 return tabular.linuxdoc(buf,os, runparams);
1156 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1157 LatexRunParams const & runparams) const
1162 // if the table is inside a float it doesn't need the informaltable
1163 // wrapper. Search for it.
1164 for (master = owner();
1165 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1166 master = master->owner());
1169 os << "<informaltable>";
1170 if (runparams.mixed_content)
1174 ret += tabular.docbook(buf, os, runparams);
1176 os << "</informaltable>";
1177 if (runparams.mixed_content)
1185 void InsetTabular::validate(LaTeXFeatures & features) const
1187 tabular.validate(features);
1191 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1194 // if we have a locking_inset we should have to check only this cell for
1195 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1196 // This is _really_ important (André)
1197 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1200 for (int j = 0; j < tabular.columns(); ++j) {
1204 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1205 tabular.getCellInset(actrow, j).metrics(m, dim);
1206 maxAsc = max(dim.asc, maxAsc);
1207 maxDesc = max(dim.des, maxDesc);
1209 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1210 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1211 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1217 for (int i = 0; i < tabular.rows(); ++i) {
1220 for (int j = 0; j < tabular.columns(); ++j) {
1221 if (tabular.isPartOfMultiColumn(i, j))
1227 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1228 tabular.getCellInset(cell).metrics(m, dim);
1229 maxAsc = max(maxAsc, dim.asc);
1230 maxDesc = max(maxDesc, dim.des);
1231 tabular.setWidthOfCell(cell, dim.wid);
1233 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1234 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1239 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1241 if (the_locking_inset) {
1242 the_locking_inset->getCursor(bv, x, y);
1247 y = cursory_ + InsetTabular::y();
1250 int desc = tabular.getDescentOfRow(actrow);
1252 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1254 y += ADD_TO_HEIGHT * 2;
1255 y += TEXT_TO_INSET_OFFSET;
1259 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1261 if (the_locking_inset) {
1262 the_locking_inset->getCursorPos(bv, x, y);
1265 x = cursorx_ - top_x;
1270 void InsetTabular::fitInsetCursor(BufferView * bv) const
1272 if (the_locking_inset) {
1273 the_locking_inset->fitInsetCursor(bv);
1278 int const asc = font_metrics::maxAscent(font);
1279 int const desc = font_metrics::maxDescent(font);
1282 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1286 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1289 actcell = actrow = actcol = 0;
1290 int ly = tabular.getDescentOfRow(actrow);
1292 // first search the right row
1293 while (ly < y && actrow + 1 < tabular.rows()) {
1294 cursory_ += tabular.getDescentOfRow(actrow) +
1295 tabular.getAscentOfRow(actrow + 1) +
1296 tabular.getAdditionalHeight(actrow + 1);
1298 ly = cursory_ + tabular.getDescentOfRow(actrow);
1300 actcell = tabular.getCellNumber(actrow, actcol);
1302 // now search the right column
1303 int lx = tabular.getWidthOfColumn(actcell) -
1304 tabular.getAdditionalWidth(actcell);
1306 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1307 lx += tabular.getWidthOfColumn(actcell + 1)
1308 + tabular.getAdditionalWidth(actcell);
1310 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1315 int InsetTabular::getCellXPos(int cell) const
1319 for (; !tabular.isFirstCellInRow(c); --c)
1321 int lx = tabular.getWidthOfColumn(cell);
1322 for (; c < cell; ++c)
1323 lx += tabular.getWidthOfColumn(c);
1325 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1329 void InsetTabular::resetPos(BufferView * bv) const
1331 #ifdef WITH_WARNINGS
1332 #warning This should be fixed in the right manner (20011128 Jug)
1334 // fast hack to fix infinite repaintings!
1335 if (in_reset_pos > 0)
1339 actcol = tabular.column_of_cell(actcell);
1342 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1343 if (tabular.isLastCellInRow(cell)) {
1344 cursory_ += tabular.getDescentOfRow(actrow) +
1345 tabular.getAscentOfRow(actrow + 1) +
1346 tabular.getAdditionalHeight(actrow + 1);
1351 if (the_locking_inset)
1355 // we need this only from here on!!!
1357 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1358 int new_x = getCellXPos(actcell);
1359 int old_x = cursorx_;
1362 // cursor.x(getCellXPos(actcell) + offset);
1363 if (actcol < tabular.columns() - 1 && scroll(false) &&
1364 tabular.getWidthOfTabular() < bv->workWidth()-20)
1368 } else if (the_locking_inset &&
1369 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1371 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1372 if (xx > bv->workWidth()-20) {
1373 scroll(bv, -(xx - bv->workWidth() + 60));
1375 } else if (xx < 20) {
1383 } else if (cursorx_ - offset > 20 &&
1384 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1385 > bv->workWidth() - 20) {
1386 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1388 } else if (cursorx_ - offset < 20) {
1389 scroll(bv, 20 - cursorx_ + offset);
1391 } else if (scroll() && top_x > 20 &&
1392 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1393 scroll(bv, old_x - cursorx_);
1396 if (the_locking_inset) {
1397 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1400 if ((!the_locking_inset ||
1401 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1402 actcell != oldcell) {
1403 InsetTabularMailer(*this).updateDialog(bv);
1410 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1412 if (lock && !old_locking_inset) {
1413 if (activateCellInset(bv))
1414 return DispatchResult(true, true);
1416 bool moved = isRightToLeft(bv)
1417 ? movePrevCell(bv) : moveNextCell(bv);
1419 return DispatchResult(false, FINISHED_RIGHT);
1420 if (lock && activateCellInset(bv))
1421 return DispatchResult(true, true);
1424 return DispatchResult(true);
1428 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1430 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1432 return DispatchResult(false, FINISHED);
1434 if (lock && activateCellInset(bv, 0, 0, mouse_button::none, true))
1435 return DispatchResult(true, true);
1437 return DispatchResult(true);
1441 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1443 int const ocell = actcell;
1444 actcell = tabular.getCellAbove(actcell);
1445 if (actcell == ocell) // we moved out of the inset
1446 return DispatchResult(false, FINISHED_UP);
1451 if (old_locking_inset) {
1452 old_locking_inset->getCursorPos(bv, x, y);
1453 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1455 if (activateCellInset(bv, x, 0))
1456 return DispatchResult(true, true);
1458 return DispatchResult(true);
1462 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1464 int const ocell = actcell;
1465 actcell = tabular.getCellBelow(actcell);
1466 if (actcell == ocell) // we moved out of the inset
1467 return DispatchResult(false, FINISHED_DOWN);
1472 if (old_locking_inset) {
1473 old_locking_inset->getCursorPos(bv, x, y);
1474 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1476 if (activateCellInset(bv, x, 0))
1477 return DispatchResult(true, true);
1479 return DispatchResult(true);
1483 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1485 if (isRightToLeft(bv)) {
1486 if (tabular.isFirstCellInRow(actcell)) {
1487 int row = tabular.row_of_cell(actcell);
1488 if (row == tabular.rows() - 1)
1490 actcell = tabular.getLastCellInRow(row);
1491 actcell = tabular.getCellBelow(actcell);
1498 if (tabular.isLastCell(actcell))
1503 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1504 isRightToLeftPar(bv->buffer()->params());
1505 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1512 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1514 if (isRightToLeft(bv)) {
1515 if (tabular.isLastCellInRow(actcell)) {
1516 int row = tabular.row_of_cell(actcell);
1519 actcell = tabular.getFirstCellInRow(row);
1520 actcell = tabular.getCellAbove(actcell);
1522 if (tabular.isLastCell(actcell))
1527 if (!actcell) // first cell
1532 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1533 isRightToLeftPar(bv->buffer()->params());
1534 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1541 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1545 setSelection(0, tabular.getNumberOfCells() - 1);
1547 if (hasSelection()) {
1548 recordUndo(bv, Undo::ATOMIC);
1549 bool const frozen = undo_frozen;
1552 // apply the fontchange on the whole selection
1557 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1558 for (int i = sel_row_start; i <= sel_row_end; ++i)
1559 for (int j = sel_col_start; j <= sel_col_end; ++j)
1560 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1568 if (the_locking_inset)
1569 the_locking_inset->setFont(bv, font, tall);
1573 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1575 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1578 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1579 string const tmp = tabularFeature[i].feature;
1581 if (tmp == what.substr(0, tmp.length())) {
1582 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1583 //tabularFeatures[i].feature.length())) {
1584 action = tabularFeature[i].action;
1588 if (action == LyXTabular::LAST_ACTION)
1592 ltrim(what.substr(tabularFeature[i].feature.length()));
1593 tabularFeatures(bv, action, val);
1599 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1600 string const & special, bool & flag)
1602 if (special == "dl_above") {
1605 } else if (special == "dl_below") {
1606 ltt.bottomDL = flag;
1608 } else if (special == "empty") {
1620 void InsetTabular::tabularFeatures(BufferView * bv,
1621 LyXTabular::Feature feature, string const & value)
1627 bool setLines = false;
1628 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1629 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1633 case LyXTabular::M_ALIGN_LEFT:
1634 case LyXTabular::ALIGN_LEFT:
1635 setAlign = LYX_ALIGN_LEFT;
1638 case LyXTabular::M_ALIGN_RIGHT:
1639 case LyXTabular::ALIGN_RIGHT:
1640 setAlign = LYX_ALIGN_RIGHT;
1643 case LyXTabular::M_ALIGN_CENTER:
1644 case LyXTabular::ALIGN_CENTER:
1645 setAlign = LYX_ALIGN_CENTER;
1648 case LyXTabular::ALIGN_BLOCK:
1649 setAlign = LYX_ALIGN_BLOCK;
1652 case LyXTabular::M_VALIGN_TOP:
1653 case LyXTabular::VALIGN_TOP:
1654 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1657 case LyXTabular::M_VALIGN_BOTTOM:
1658 case LyXTabular::VALIGN_BOTTOM:
1659 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1662 case LyXTabular::M_VALIGN_MIDDLE:
1663 case LyXTabular::VALIGN_MIDDLE:
1664 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1671 if (hasSelection()) {
1672 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1674 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1675 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1677 recordUndo(bv, Undo::ATOMIC);
1679 int row = tabular.row_of_cell(actcell);
1680 int column = tabular.column_of_cell(actcell);
1682 LyXTabular::ltType ltt;
1686 case LyXTabular::SET_PWIDTH: {
1687 LyXLength const len(value);
1688 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1690 tabular.setColumnPWidth(actcell, len);
1691 if (oldlen != len) {
1692 // We need this otherwise we won't resize
1693 // the insettext of the active cell (if any)
1694 // until later (see InsetText::do_resize)
1695 unlockInsetInInset(bv, the_locking_inset);
1700 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1701 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1702 else if (!len.zero()
1703 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1704 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1708 case LyXTabular::SET_MPWIDTH:
1710 LyXLength const len(value);
1711 LyXLength const & oldlen = tabular.getPWidth(actcell);
1712 tabular.setMColumnPWidth(actcell, len);
1713 if (oldlen != len) {
1714 // We need this otherwise we won't resize
1715 // the insettext of the active cell (if any)
1716 // until later (see InsetText::do_resize)
1717 unlockInsetInInset(bv, the_locking_inset);
1723 case LyXTabular::SET_SPECIAL_COLUMN:
1724 case LyXTabular::SET_SPECIAL_MULTI:
1725 tabular.setAlignSpecial(actcell,value,feature);
1729 case LyXTabular::APPEND_ROW:
1730 // append the row into the tabular
1731 unlockInsetInInset(bv, the_locking_inset);
1732 tabular.appendRow(bv->buffer()->params(), actcell);
1733 tabular.setOwner(this);
1737 case LyXTabular::APPEND_COLUMN:
1738 // append the column into the tabular
1739 unlockInsetInInset(bv, the_locking_inset);
1740 tabular.appendColumn(bv->buffer()->params(), actcell);
1741 tabular.setOwner(this);
1742 actcell = tabular.getCellNumber(row, column);
1746 case LyXTabular::DELETE_ROW:
1747 unlockInsetInInset(bv, the_locking_inset);
1748 for (int i = sel_row_start; i <= sel_row_end; ++i)
1749 tabular.deleteRow(sel_row_start);
1750 if (sel_row_start >= tabular.rows())
1752 actcell = tabular.getCellNumber(sel_row_start, column);
1757 case LyXTabular::DELETE_COLUMN:
1758 unlockInsetInInset(bv, the_locking_inset);
1759 for (int i = sel_col_start; i <= sel_col_end; ++i)
1760 tabular.deleteColumn(sel_col_start);
1761 if (sel_col_start >= tabular.columns())
1763 actcell = tabular.getCellNumber(row, sel_col_start);
1768 case LyXTabular::M_TOGGLE_LINE_TOP:
1770 case LyXTabular::TOGGLE_LINE_TOP: {
1771 bool lineSet = !tabular.topLine(actcell, flag);
1772 for (int i = sel_row_start; i <= sel_row_end; ++i)
1773 for (int j = sel_col_start; j <= sel_col_end; ++j)
1775 tabular.getCellNumber(i, j),
1781 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1783 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1784 bool lineSet = !tabular.bottomLine(actcell, flag);
1785 for (int i = sel_row_start; i <= sel_row_end; ++i)
1786 for (int j = sel_col_start; j <= sel_col_end; ++j)
1787 tabular.setBottomLine(
1788 tabular.getCellNumber(i, j),
1795 case LyXTabular::M_TOGGLE_LINE_LEFT:
1797 case LyXTabular::TOGGLE_LINE_LEFT: {
1798 bool lineSet = !tabular.leftLine(actcell, flag);
1799 for (int i = sel_row_start; i <= sel_row_end; ++i)
1800 for (int j = sel_col_start; j <= sel_col_end; ++j)
1801 tabular.setLeftLine(
1802 tabular.getCellNumber(i,j),
1809 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1811 case LyXTabular::TOGGLE_LINE_RIGHT: {
1812 bool lineSet = !tabular.rightLine(actcell, flag);
1813 for (int i = sel_row_start; i <= sel_row_end; ++i)
1814 for (int j = sel_col_start; j <= sel_col_end; ++j)
1815 tabular.setRightLine(
1816 tabular.getCellNumber(i,j),
1823 case LyXTabular::M_ALIGN_LEFT:
1824 case LyXTabular::M_ALIGN_RIGHT:
1825 case LyXTabular::M_ALIGN_CENTER:
1827 case LyXTabular::ALIGN_LEFT:
1828 case LyXTabular::ALIGN_RIGHT:
1829 case LyXTabular::ALIGN_CENTER:
1830 case LyXTabular::ALIGN_BLOCK:
1831 for (int i = sel_row_start; i <= sel_row_end; ++i)
1832 for (int j = sel_col_start; j <= sel_col_end; ++j)
1833 tabular.setAlignment(
1834 tabular.getCellNumber(i, j),
1840 case LyXTabular::M_VALIGN_TOP:
1841 case LyXTabular::M_VALIGN_BOTTOM:
1842 case LyXTabular::M_VALIGN_MIDDLE:
1844 case LyXTabular::VALIGN_TOP:
1845 case LyXTabular::VALIGN_BOTTOM:
1846 case LyXTabular::VALIGN_MIDDLE:
1847 for (int i = sel_row_start; i <= sel_row_end; ++i)
1848 for (int j = sel_col_start; j <= sel_col_end; ++j)
1849 tabular.setVAlignment(
1850 tabular.getCellNumber(i, j),
1855 case LyXTabular::MULTICOLUMN: {
1856 if (sel_row_start != sel_row_end) {
1857 #ifdef WITH_WARNINGS
1858 #warning Need I say it ? This is horrible.
1860 Alert::error(_("Error setting multicolumn"),
1861 _("You cannot set multicolumn vertically."));
1864 // just multicol for one Single Cell
1865 if (!hasSelection()) {
1866 // check wether we are completly in a multicol
1867 if (tabular.isMultiColumn(actcell))
1868 tabular.unsetMultiColumn(actcell);
1870 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1874 // we have a selection so this means we just add all this
1875 // cells to form a multicolumn cell
1879 if (sel_cell_start > sel_cell_end) {
1880 s_start = sel_cell_end;
1881 s_end = sel_cell_start;
1883 s_start = sel_cell_start;
1884 s_end = sel_cell_end;
1886 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1893 case LyXTabular::SET_ALL_LINES:
1895 case LyXTabular::UNSET_ALL_LINES:
1896 for (int i = sel_row_start; i <= sel_row_end; ++i)
1897 for (int j = sel_col_start; j <= sel_col_end; ++j)
1898 tabular.setAllLines(
1899 tabular.getCellNumber(i,j), setLines);
1903 case LyXTabular::SET_LONGTABULAR:
1904 tabular.setLongTabular(true);
1905 updateLocal(bv); // because this toggles displayed
1908 case LyXTabular::UNSET_LONGTABULAR:
1909 tabular.setLongTabular(false);
1910 updateLocal(bv); // because this toggles displayed
1913 case LyXTabular::SET_ROTATE_TABULAR:
1914 tabular.setRotateTabular(true);
1917 case LyXTabular::UNSET_ROTATE_TABULAR:
1918 tabular.setRotateTabular(false);
1921 case LyXTabular::SET_ROTATE_CELL:
1922 for (int i = sel_row_start; i <= sel_row_end; ++i)
1923 for (int j = sel_col_start; j <= sel_col_end; ++j)
1924 tabular.setRotateCell(
1925 tabular.getCellNumber(i, j), true);
1928 case LyXTabular::UNSET_ROTATE_CELL:
1929 for (int i = sel_row_start; i <= sel_row_end; ++i)
1930 for (int j = sel_col_start; j <= sel_col_end; ++j)
1931 tabular.setRotateCell(
1932 tabular.getCellNumber(i, j), false);
1935 case LyXTabular::SET_USEBOX: {
1936 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1937 if (val == tabular.getUsebox(actcell))
1938 val = LyXTabular::BOX_NONE;
1939 for (int i = sel_row_start; i <= sel_row_end; ++i)
1940 for (int j = sel_col_start; j <= sel_col_end; ++j)
1941 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1945 case LyXTabular::UNSET_LTFIRSTHEAD:
1947 case LyXTabular::SET_LTFIRSTHEAD:
1948 tabular.getRowOfLTFirstHead(row, ltt);
1949 checkLongtableSpecial(ltt, value, flag);
1950 tabular.setLTHead(row, flag, ltt, true);
1953 case LyXTabular::UNSET_LTHEAD:
1955 case LyXTabular::SET_LTHEAD:
1956 tabular.getRowOfLTHead(row, ltt);
1957 checkLongtableSpecial(ltt, value, flag);
1958 tabular.setLTHead(row, flag, ltt, false);
1961 case LyXTabular::UNSET_LTFOOT:
1963 case LyXTabular::SET_LTFOOT:
1964 tabular.getRowOfLTFoot(row, ltt);
1965 checkLongtableSpecial(ltt, value, flag);
1966 tabular.setLTFoot(row, flag, ltt, false);
1969 case LyXTabular::UNSET_LTLASTFOOT:
1971 case LyXTabular::SET_LTLASTFOOT:
1972 tabular.getRowOfLTLastFoot(row, ltt);
1973 checkLongtableSpecial(ltt, value, flag);
1974 tabular.setLTFoot(row, flag, ltt, true);
1977 case LyXTabular::SET_LTNEWPAGE:
1978 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1981 // dummy stuff just to avoid warnings
1982 case LyXTabular::LAST_ACTION:
1986 InsetTabularMailer(*this).updateDialog(bv);
1990 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1991 mouse_button::state button, bool behind)
1993 UpdatableInset & inset = tabular.getCellInset(actcell);
1996 x = inset.x() + inset.width();
1997 y = inset.descent();
1999 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
2000 //inset_y = cursory_;
2001 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
2002 if (!the_locking_inset)
2005 return the_locking_inset;
2009 bool InsetTabular::insetHit(BufferView *, int x, int) const
2011 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
2015 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2017 //resizeLyXText(bv, recursive);
2021 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2022 bool const recursive) const
2024 if (the_locking_inset)
2025 return the_locking_inset->getLyXText(bv, recursive);
2026 return InsetOld::getLyXText(bv, recursive);
2030 bool InsetTabular::showInsetDialog(BufferView * bv) const
2032 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2033 InsetTabularMailer(*this).showDialog(bv);
2038 void InsetTabular::openLayoutDialog(BufferView * bv) const
2040 if (the_locking_inset) {
2041 InsetTabular * inset = static_cast<InsetTabular *>
2042 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2044 inset->openLayoutDialog(bv);
2048 InsetTabularMailer(*this).showDialog(bv);
2053 // function returns an object as defined in func_status.h:
2054 // states OK, Unknown, Disabled, On, Off.
2056 FuncStatus InsetTabular::getStatus(string const & what) const
2058 int action = LyXTabular::LAST_ACTION;
2062 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2063 string const tmp = tabularFeature[i].feature;
2064 if (tmp == what.substr(0, tmp.length())) {
2065 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2066 // tabularFeatures[i].feature.length())) {
2067 action = tabularFeature[i].action;
2071 if (action == LyXTabular::LAST_ACTION) {
2073 status.unknown(true);
2077 string const argument
2078 = ltrim(what.substr(tabularFeature[i].feature.length()));
2083 LyXTabular::ltType dummyltt;
2087 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2089 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2092 case LyXTabular::SET_PWIDTH:
2093 case LyXTabular::SET_MPWIDTH:
2094 case LyXTabular::SET_SPECIAL_COLUMN:
2095 case LyXTabular::SET_SPECIAL_MULTI:
2096 case LyXTabular::APPEND_ROW:
2097 case LyXTabular::APPEND_COLUMN:
2098 case LyXTabular::DELETE_ROW:
2099 case LyXTabular::DELETE_COLUMN:
2100 case LyXTabular::SET_ALL_LINES:
2101 case LyXTabular::UNSET_ALL_LINES:
2105 case LyXTabular::MULTICOLUMN:
2106 status.setOnOff(tabular.isMultiColumn(actcell));
2109 case LyXTabular::M_TOGGLE_LINE_TOP:
2111 case LyXTabular::TOGGLE_LINE_TOP:
2112 status.setOnOff(tabular.topLine(actcell, flag));
2115 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2117 case LyXTabular::TOGGLE_LINE_BOTTOM:
2118 status.setOnOff(tabular.bottomLine(actcell, flag));
2121 case LyXTabular::M_TOGGLE_LINE_LEFT:
2123 case LyXTabular::TOGGLE_LINE_LEFT:
2124 status.setOnOff(tabular.leftLine(actcell, flag));
2127 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2129 case LyXTabular::TOGGLE_LINE_RIGHT:
2130 status.setOnOff(tabular.rightLine(actcell, flag));
2133 case LyXTabular::M_ALIGN_LEFT:
2135 case LyXTabular::ALIGN_LEFT:
2136 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2139 case LyXTabular::M_ALIGN_RIGHT:
2141 case LyXTabular::ALIGN_RIGHT:
2142 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2145 case LyXTabular::M_ALIGN_CENTER:
2147 case LyXTabular::ALIGN_CENTER:
2148 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2151 case LyXTabular::ALIGN_BLOCK:
2152 status.disabled(tabular.getPWidth(actcell).zero());
2153 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2156 case LyXTabular::M_VALIGN_TOP:
2158 case LyXTabular::VALIGN_TOP:
2159 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2162 case LyXTabular::M_VALIGN_BOTTOM:
2164 case LyXTabular::VALIGN_BOTTOM:
2165 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2168 case LyXTabular::M_VALIGN_MIDDLE:
2170 case LyXTabular::VALIGN_MIDDLE:
2171 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2174 case LyXTabular::SET_LONGTABULAR:
2175 status.setOnOff(tabular.isLongTabular());
2178 case LyXTabular::UNSET_LONGTABULAR:
2179 status.setOnOff(!tabular.isLongTabular());
2182 case LyXTabular::SET_ROTATE_TABULAR:
2183 status.setOnOff(tabular.getRotateTabular());
2186 case LyXTabular::UNSET_ROTATE_TABULAR:
2187 status.setOnOff(!tabular.getRotateTabular());
2190 case LyXTabular::SET_ROTATE_CELL:
2191 status.setOnOff(tabular.getRotateCell(actcell));
2194 case LyXTabular::UNSET_ROTATE_CELL:
2195 status.setOnOff(!tabular.getRotateCell(actcell));
2198 case LyXTabular::SET_USEBOX:
2199 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2202 case LyXTabular::SET_LTFIRSTHEAD:
2203 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2206 case LyXTabular::SET_LTHEAD:
2207 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2210 case LyXTabular::SET_LTFOOT:
2211 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2214 case LyXTabular::SET_LTLASTFOOT:
2215 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2218 case LyXTabular::SET_LTNEWPAGE:
2219 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2224 status.disabled(true);
2231 void InsetTabular::getLabelList(Buffer const & buffer,
2232 std::vector<string> & list) const
2234 tabular.getLabelList(buffer, list);
2238 bool InsetTabular::copySelection(BufferView * bv)
2240 if (!hasSelection())
2243 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2244 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2245 if (sel_col_start > sel_col_end) {
2246 sel_col_start = sel_col_end;
2247 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2249 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2252 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2253 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2254 if (sel_row_start > sel_row_end)
2255 swap(sel_row_start, sel_row_end);
2257 paste_tabular.reset(new LyXTabular(tabular));
2258 paste_tabular->setOwner(this);
2260 for (int i = 0; i < sel_row_start; ++i)
2261 paste_tabular->deleteRow(0);
2263 int const rows = sel_row_end - sel_row_start + 1;
2264 while (paste_tabular->rows() > rows)
2265 paste_tabular->deleteRow(rows);
2267 paste_tabular->setTopLine(0, true, true);
2268 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2271 for (int i = 0; i < sel_col_start; ++i)
2272 paste_tabular->deleteColumn(0);
2274 int const columns = sel_col_end - sel_col_start + 1;
2275 while (paste_tabular->columns() > columns)
2276 paste_tabular->deleteColumn(columns);
2278 paste_tabular->setLeftLine(0, true, true);
2279 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2283 LatexRunParams const runparams;
2284 paste_tabular->ascii(*bv->buffer(), os, runparams,
2285 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2286 bv->stuffClipboard(os.str());
2291 bool InsetTabular::pasteSelection(BufferView * bv)
2296 for (int r1 = 0, r2 = actrow;
2297 r1 < paste_tabular->rows() && r2 < tabular.rows();
2299 for (int c1 = 0, c2 = actcol;
2300 c1 < paste_tabular->columns() && c2 < tabular.columns();
2302 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2303 tabular.isPartOfMultiColumn(r2, c2))
2305 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2309 if (tabular.isPartOfMultiColumn(r2, c2)) {
2313 InsetText & inset = tabular.getCellInset(r2, c2);
2314 inset = paste_tabular->getCellInset(r1, c1);
2315 inset.setOwner(this);
2316 inset.deleteLyXText(bv);
2324 bool InsetTabular::cutSelection(BufferParams const & bp)
2326 if (!hasSelection())
2329 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2330 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2331 if (sel_col_start > sel_col_end) {
2332 sel_col_start = sel_col_end;
2333 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2335 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2338 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2339 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2341 if (sel_row_start > sel_row_end)
2342 swap(sel_row_start, sel_row_end);
2344 if (sel_cell_start > sel_cell_end)
2345 swap(sel_cell_start, sel_cell_end);
2347 for (int i = sel_row_start; i <= sel_row_end; ++i)
2348 for (int j = sel_col_start; j <= sel_col_end; ++j)
2349 tabular.getCellInset(tabular.getCellNumber(i, j))
2350 .clear(bp.tracking_changes);
2355 bool InsetTabular::isRightToLeft(BufferView * bv)
2357 return bv->getParentLanguage(this)->RightToLeft();
2361 int InsetTabular::scroll(bool recursive) const
2363 int sx = UpdatableInset::scroll(false);
2365 if (recursive && the_locking_inset)
2366 sx += the_locking_inset->scroll(recursive);
2372 void InsetTabular::getSelection(int & srow, int & erow,
2373 int & scol, int & ecol) const
2375 int const start = hasSelection() ? sel_cell_start : actcell;
2376 int const end = hasSelection() ? sel_cell_end : actcell;
2378 srow = tabular.row_of_cell(start);
2379 erow = tabular.row_of_cell(end);
2383 scol = tabular.column_of_cell(start);
2384 ecol = tabular.column_of_cell(end);
2388 ecol = tabular.right_column_of_cell(end);
2392 ParagraphList * InsetTabular::getParagraphs(int i) const
2394 return i < tabular.getNumberOfCells()
2395 ? tabular.getCellInset(i).getParagraphs(0)
2400 int InsetTabular::numParagraphs() const
2402 return tabular.getNumberOfCells();
2406 LyXText * InsetTabular::getText(int i) const
2408 return i < tabular.getNumberOfCells()
2409 ? tabular.getCellInset(i).getText(0)
2414 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2416 if (the_locking_inset)
2417 return the_locking_inset->cursor(bv);
2418 return InsetOld::cursor(bv);
2423 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2425 if (the_locking_inset) {
2426 WordLangTuple word =
2427 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2428 if (!word.word().empty())
2430 if (tabular.isLastCell(actcell)) {
2431 bv->unlockInset(const_cast<InsetTabular *>(this));
2432 return WordLangTuple();
2436 // otherwise we have to lock the next inset and ask for it's selecttion
2437 tabular.getCellInset(actcell)
2438 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2439 WordLangTuple word = selectNextWordInt(bv, value);
2440 if (!word.word().empty())
2446 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2448 // when entering this function the inset should be ALWAYS locked!
2449 BOOST_ASSERT(the_locking_inset);
2451 WordLangTuple word =
2452 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2453 if (!word.word().empty())
2456 if (tabular.isLastCell(actcell)) {
2457 bv->unlockInset(const_cast<InsetTabular *>(this));
2458 return WordLangTuple();
2461 // otherwise we have to lock the next inset and ask for it's selecttion
2463 tabular.getCellInset(actcell)
2464 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2465 return selectNextWordInt(bv, value);
2469 void InsetTabular::selectSelectedWord(BufferView * bv)
2471 if (the_locking_inset)
2472 the_locking_inset->selectSelectedWord(bv);
2476 void InsetTabular::markErased()
2478 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2479 tabular.getCellInset(cell).markErased();
2483 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2485 if (the_locking_inset) {
2486 if (the_locking_inset->nextChange(bv, length)) {
2490 if (tabular.isLastCell(actcell))
2494 InsetText & inset = tabular.getCellInset(actcell);
2495 if (inset.nextChange(bv, length)) {
2499 while (!tabular.isLastCell(actcell)) {
2501 InsetText & inset = tabular.getCellInset(actcell);
2502 if (inset.nextChange(bv, length)) {
2511 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2515 if (the_locking_inset) {
2516 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2520 if (tabular.isLastCell(actcell))
2524 InsetText & inset = tabular.getCellInset(cell);
2525 if (inset.searchForward(bv, str, cs, mw)) {
2529 while (!tabular.isLastCell(cell)) {
2531 InsetText & inset = tabular.getCellInset(cell);
2532 if (inset.searchForward(bv, str, cs, mw)) {
2541 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2544 int cell = tabular.getNumberOfCells();
2545 if (the_locking_inset) {
2546 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2555 InsetText & inset = tabular.getCellInset(cell);
2556 if (inset.searchBackward(bv, str, cs, mw)) {
2565 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2567 if (the_locking_inset)
2568 return the_locking_inset->insetAllowed(code);
2569 // we return true here because if the inset is not locked someone
2570 // wants to insert something in one of our insettexts and we generally
2576 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2578 const int cell = tabular.getCellFromInset(in);
2581 return tabular.getPWidth(cell).zero();
2583 // this is a workaround for a crash (New, Insert->Tabular,
2584 // Insert->FootNote)
2588 // well we didn't obviously find it so maybe our owner knows more
2589 BOOST_ASSERT(owner());
2590 return owner()->forceDefaultParagraphs(in);
2594 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2597 if (buf.length() <= 0)
2603 string::size_type len = buf.length();
2604 string::size_type p = 0;
2606 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2614 maxCols = max(cols, maxCols);
2620 maxCols = max(cols, maxCols);
2621 LyXTabular * loctab;
2626 paste_tabular.reset(
2627 new LyXTabular(bv->buffer()->params(), rows, maxCols)
2630 paste_tabular->setOwner(this);
2631 loctab = paste_tabular.get();
2640 string::size_type op = 0;
2641 int cells = loctab->getNumberOfCells();
2644 rows = loctab->rows();
2645 int const columns = loctab->columns();
2647 while (cell < cells && p < len && row < rows &&
2648 (p = buf.find_first_of("\t\n", p)) != string::npos)
2654 // we can only set this if we are not too far right
2655 if (cols < columns) {
2656 InsetText & inset = loctab->getCellInset(cell);
2657 LyXFont const font = inset.getLyXText(bv)->
2658 getFont(inset.paragraphs.begin(), 0);
2659 inset.setText(buf.substr(op, p - op), font);
2665 // we can only set this if we are not too far right
2666 if (cols < columns) {
2667 InsetText & inset = tabular.getCellInset(cell);
2668 LyXFont const font = inset.getLyXText(bv)->
2669 getFont(inset.paragraphs.begin(), 0);
2670 inset.setText(buf.substr(op, p - op), font);
2675 cell = loctab->getCellNumber(row, cols);
2681 // check for the last cell if there is no trailing '\n'
2682 if (cell < cells && op < len) {
2683 InsetText & inset = loctab->getCellInset(cell);
2684 LyXFont const font = inset.getLyXText(bv)->
2685 getFont(inset.paragraphs.begin(), 0);
2686 inset.setText(buf.substr(op, len - op), font);
2693 void InsetTabular::addPreview(PreviewLoader & loader) const
2695 int const rows = tabular.rows();
2696 int const columns = tabular.columns();
2697 for (int i = 0; i < rows; ++i)
2698 for (int j = 0; j < columns; ++j)
2699 tabular.getCellInset(i, j).addPreview(loader);
2703 string const InsetTabularMailer::name_("tabular");
2705 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2706 : inset_(const_cast<InsetTabular &>(inset))
2710 string const InsetTabularMailer::inset2string(Buffer const &) const
2712 return params2string(inset_);
2716 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2718 istringstream data(in);
2720 lex.setStream(data);
2722 #warning CHECK verify that this is a sane value to return.
2728 string const token = lex.getString();
2736 string const token = lex.getString();
2737 if (token != "\\active_cell")
2740 cell = lex.getInteger();
2743 // This is part of the inset proper that is usually swallowed
2744 // by Buffer::readInset
2747 string const token = lex.getString();
2748 if (token != "Tabular")
2755 Buffer const & buffer = inset.buffer();
2756 inset.read(buffer, lex);
2758 // We can't set the active cell, but we can tell the frontend
2764 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2766 Buffer const & buffer = inset.buffer();
2769 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2770 inset.write(buffer, data);
2771 data << "\\end_inset\n";