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 LyXTabular * paste_tabular = 0;
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 != 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;
454 in->dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
455 return the_locking_inset->lockInsetInInset(bv, inset);
460 if (the_locking_inset && (the_locking_inset == inset)) {
461 lyxerr[Debug::INSETTEXT] << "OK" << endl;
466 if (the_locking_inset) {
467 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
468 return the_locking_inset->lockInsetInInset(bv, inset);
471 lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
476 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
479 if (!the_locking_inset)
481 if (the_locking_inset == inset) {
482 the_locking_inset->insetUnlock(bv);
484 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
491 // this has to be here otherwise we don't redraw the cell!
492 the_locking_inset = 0;
495 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
496 if (inset->lyxCode() == TABULAR_CODE &&
497 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
498 InsetTabularMailer(*this).updateDialog(bv);
507 int InsetTabular::insetInInsetY() const
509 if (!the_locking_inset)
511 return inset_y + the_locking_inset->insetInInsetY();
515 UpdatableInset * InsetTabular::getLockingInset() const
517 return the_locking_inset ? the_locking_inset->getLockingInset() :
518 const_cast<InsetTabular *>(this);
522 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(InsetOld::Code c)
526 if (the_locking_inset)
527 return the_locking_inset->getFirstLockingInsetOfType(c);
532 bool InsetTabular::insertInset(BufferView * bv, InsetOld * inset)
534 if (the_locking_inset)
535 return the_locking_inset->insertInset(bv, inset);
540 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
542 if (hasSelection() && cmd.button() == mouse_button::button3)
545 if (hasSelection()) {
547 updateLocal(cmd.view());
550 int const ocell = actcell;
551 BufferView * bv = cmd.view();
555 the_locking_inset = 0;
559 setPos(bv, cmd.x, cmd.y);
562 bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
564 if (ocell == actcell && the_locking_inset && inset_hit) {
566 FuncRequest cmd1 = cmd;
569 the_locking_inset->dispatch(cmd1);
573 if (the_locking_inset) {
574 the_locking_inset->insetUnlock(bv);
576 the_locking_inset = 0;
579 if (cmd.button() == mouse_button::button2) {
580 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
584 if (inset_hit && bv->theLockingInset()) {
585 if (!bv->lockInset(&tabular.getCellInset(actcell))) {
586 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
589 FuncRequest cmd1 = cmd;
592 the_locking_inset->dispatch(cmd1);
597 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
599 DispatchResult ret(false);
600 if (the_locking_inset) {
601 FuncRequest cmd1 = cmd;
604 ret = the_locking_inset->dispatch(cmd1);
606 if (cmd.button() == mouse_button::button3 && ret == DispatchResult(false)) {
607 InsetTabularMailer(*this).showDialog(cmd.view());
610 return ret.dispatched() || ret.val() > FINISHED;
614 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
616 if (the_locking_inset) {
617 FuncRequest cmd1 = cmd;
620 the_locking_inset->dispatch(cmd1);
624 BufferView * bv = cmd.view();
625 int const old_cell = actcell;
627 setPos(bv, cmd.x, cmd.y);
628 if (!hasSelection()) {
629 setSelection(actcell, actcell);
631 } else if (old_cell != actcell) {
632 setSelection(sel_cell_start, actcell);
638 void InsetTabular::edit(BufferView * bv, int index)
640 lyxerr << "InsetTabular::edit" << endl;
641 if (!bv->lockInset(this)) {
642 lyxerr << "InsetTabular::Cannot lock inset (2)" << endl;
647 the_locking_inset = 0;
655 UpdatableInset & inset = tabular.getCellInset(actcell);
656 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
657 if (the_locking_inset)
663 InsetTabular::priv_dispatch(FuncRequest const & cmd,
664 idx_type & idx, pos_type & pos)
666 // We need to save the value of the_locking_inset as the call to
667 // the_locking_inset->localDispatch might unlock it.
668 old_locking_inset = the_locking_inset;
669 DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
670 BufferView * bv = cmd.view();
672 if (cmd.action == LFUN_INSET_EDIT) {
674 lyxerr << "InsetTabular::edit: " << this << " args: '"
675 << cmd.argument << "' first cell: "
676 << &tabular.cell_info[0][0].inset << endl;
678 if (!bv->lockInset(this)) {
679 lyxerr << "InsetTabular::Cannot lock inset" << endl;
680 return DispatchResult(true);
685 the_locking_inset = 0;
689 if (cmd.argument.size()) {
690 if (cmd.argument == "left") {
691 if (isRightToLeft(bv))
692 actcell = tabular.getLastCellInRow(0);
696 if (isRightToLeft(bv))
697 actcell = tabular.getFirstCellInRow(tabular.rows()-1);
699 actcell = tabular.getNumberOfCells() - 1;
707 setPos(bv, cmd.x, cmd.y);
710 if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
711 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
713 activateCellInset(bv, cmd.x - inset_x, cmd.y - inset_y, cmd.button());
716 return DispatchResult(true);
719 if (result.dispatched()) {
724 if (cmd.action < 0 && cmd.argument.empty())
725 return DispatchResult(false, FINISHED);
727 bool hs = hasSelection();
729 result = DispatchResult(true);
730 // this one have priority over the locked InsetText, if we're not already
731 // inside another tabular then that one get's priority!
732 if (getFirstLockingInsetOfType(InsetOld::TABULAR_CODE) == this) {
733 switch (cmd.action) {
734 case LFUN_MOUSE_PRESS:
736 return DispatchResult(true);
738 case LFUN_MOUSE_MOTION:
739 lfunMouseMotion(cmd);
740 return DispatchResult(true);
742 case LFUN_MOUSE_RELEASE:
743 return DispatchResult(lfunMouseRelease(cmd));
745 case LFUN_CELL_BACKWARD:
746 case LFUN_CELL_FORWARD:
747 unlockInsetInInset(bv, the_locking_inset);
748 if (cmd.action == LFUN_CELL_FORWARD)
749 moveNextCell(bv, old_locking_inset != 0);
751 movePrevCell(bv, old_locking_inset != 0);
755 if (!the_locking_inset)
756 return DispatchResult(true, NOUPDATE);
758 // this to avoid compiler warnings.
764 kb_action action = cmd.action;
765 string arg = cmd.argument;
766 if (the_locking_inset) {
767 result = the_locking_inset->dispatch(cmd);
768 if (result.dispatched()) {
769 if (result.val() == NOUPDATE) {
770 int const sc = scroll();
772 if (sc != scroll()) {
773 // inset has been scrolled
780 } else if (result.val() == FINISHED_UP) {
782 // Make sure to reset status message after
783 // exiting, e.g. math inset
784 bv->owner()->clearMessage();
785 } else if (result.val() == FINISHED_DOWN) {
787 bv->owner()->clearMessage();
788 } else if (result.val() == FINISHED_RIGHT) {
790 bv->owner()->clearMessage();
791 } else if (result.val() == FINISHED) {
792 bv->owner()->clearMessage();
796 result = DispatchResult(true);
798 // --- Cursor Movements ----------------------------------
799 case LFUN_RIGHTSEL: {
800 int const start = hasSelection() ? sel_cell_start : actcell;
801 if (tabular.isLastCellInRow(actcell)) {
802 setSelection(start, actcell);
807 // if we are starting a selection, only select
808 // the current cell at the beginning
809 if (hasSelection()) {
810 moveRight(bv, false);
813 setSelection(start, end);
818 result = moveRight(bv);
824 int const start = hasSelection() ? sel_cell_start : actcell;
825 if (tabular.isFirstCellInRow(actcell)) {
826 setSelection(start, actcell);
831 // if we are starting a selection, only select
832 // the current cell at the beginning
833 if (hasSelection()) {
837 setSelection(start, end);
842 result = moveLeft(bv);
848 int const start = hasSelection() ? sel_cell_start : actcell;
849 int const ocell = actcell;
850 // if we are starting a selection, only select
851 // the current cell at the beginning
852 if (hasSelection()) {
854 if (ocell == sel_cell_end ||
855 tabular.column_of_cell(ocell) > tabular.column_of_cell(actcell))
856 setSelection(start, tabular.getCellBelow(sel_cell_end));
858 setSelection(start, tabular.getLastCellBelow(sel_cell_end));
860 setSelection(start, start);
866 result = moveDown(bv, old_locking_inset != 0);
872 int const start = hasSelection() ? sel_cell_start : actcell;
873 int const ocell = actcell;
874 // if we are starting a selection, only select
875 // the current cell at the beginning
876 if (hasSelection()) {
878 if ((ocell == sel_cell_end) ||
879 (tabular.column_of_cell(ocell)>tabular.column_of_cell(actcell)))
880 setSelection(start, tabular.getCellAbove(sel_cell_end));
882 setSelection(start, tabular.getLastCellAbove(sel_cell_end));
884 setSelection(start, start);
890 result = moveUp(bv, old_locking_inset != 0);
899 unlockInsetInInset(bv, the_locking_inset);
900 if (bv->top_y() + bv->painter().paperHeight() <
901 top_baseline + tabular.getHeightOfTabular())
903 bv->scrollDocView(bv->top_y() + bv->painter().paperHeight());
904 actcell = tabular.getCellBelow(first_visible_cell) + column;
906 actcell = tabular.getFirstCellInRow(tabular.rows() - 1) + column;
916 unlockInsetInInset(bv, the_locking_inset);
917 if (top_baseline < 0) {
918 bv->scrollDocView(bv->top_y() - bv->painter().paperHeight());
919 if (top_baseline > 0)
922 actcell = tabular.getCellBelow(first_visible_cell) + column;
930 // none of these make sense for insettabular,
931 // but we must catch them to prevent any
932 // selection from being confused
936 case LFUN_WORDLEFTSEL:
938 case LFUN_WORDRIGHTSEL:
940 case LFUN_DOWN_PARAGRAPH:
941 case LFUN_DOWN_PARAGRAPHSEL:
942 case LFUN_UP_PARAGRAPH:
943 case LFUN_UP_PARAGRAPHSEL:
949 case LFUN_BEGINNINGBUF:
950 case LFUN_BEGINNINGBUFSEL:
954 case LFUN_LAYOUT_TABULAR:
955 InsetTabularMailer(*this).showDialog(bv);
957 case LFUN_INSET_DIALOG_UPDATE:
958 InsetTabularMailer(*this).updateDialog(bv);
960 case LFUN_TABULAR_FEATURE:
961 if (!tabularFeatures(bv, arg))
962 result = DispatchResult(false);
964 // insert file functions
965 case LFUN_FILE_INSERT_ASCII_PARA:
966 case LFUN_FILE_INSERT_ASCII:
968 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
971 if (insertAsciiString(bv, tmpstr, false))
974 result = DispatchResult(false);
977 // cut and paste functions
979 if (!copySelection(bv))
983 recordUndo(bv, Undo::DELETE);
984 cutSelection(bv->buffer()->params());
993 case LFUN_PASTESELECTION:
995 string const clip = bv->getClipboard();
999 if (clip.find('\t') != string::npos) {
1003 string::size_type len = clip.length();
1004 string::size_type p = 0;
1007 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1015 maxCols = max(cols, maxCols);
1021 maxCols = max(cols, maxCols);
1022 delete paste_tabular;
1023 paste_tabular = new LyXTabular(bv->buffer()->params(),
1024 this, rows, maxCols);
1025 string::size_type op = 0;
1027 int cells = paste_tabular->getNumberOfCells();
1029 while ((cell < cells) && (p < len) &&
1030 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1035 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1040 paste_tabular->getCellInset(cell)->setText(clip.substr(op, p-op));
1041 while (cols++ < maxCols)
1049 // check for the last cell if there is no trailing '\n'
1050 if ((cell < cells) && (op < len))
1051 paste_tabular->getCellInset(cell)->setText(clip.substr(op, len-op));
1054 if (!insertAsciiString(bv, clip, true))
1057 // so that the clipboard is used and it goes on
1059 // and executes LFUN_PASTESELECTION in insettext!
1060 delete paste_tabular;
1065 if (hasPasteBuffer()) {
1066 recordUndo(bv, Undo::INSERT);
1071 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1073 // handle font changing stuff on selection before we lock the inset
1074 // in the default part!
1075 result = DispatchResult(false);
1086 case LFUN_UNDERLINE:
1087 case LFUN_FONT_SIZE:
1088 if (bv->dispatch(FuncRequest(bv, action, arg)))
1089 result = DispatchResult(true);
1095 // we try to activate the actual inset and put this event down to
1096 // the insets dispatch function.
1097 if (result.dispatched() || the_locking_inset)
1099 if (activateCellInset(bv)) {
1100 result = the_locking_inset->dispatch(FuncRequest(bv, action, arg));
1101 if (!result.dispatched()) {
1102 unlockInsetInInset(bv, the_locking_inset);
1103 // we need to update if this was requested before
1105 return DispatchResult(false);
1115 if (result.val() >= FINISHED)
1116 bv->unlockInset(this);
1117 else if (!the_locking_inset && bv->fitCursor())
1124 int InsetTabular::latex(Buffer const & buf, ostream & os,
1125 LatexRunParams const & runparams) const
1127 return tabular.latex(buf, os, runparams);
1131 int InsetTabular::ascii(Buffer const & buf, ostream & os,
1132 LatexRunParams const & runparams) const
1134 if (runparams.linelen > 0)
1135 return tabular.ascii(buf, os, runparams,
1136 ownerPar(buf, this).params().depth(),
1138 return tabular.ascii(buf, os, runparams, 0, false, 0);
1142 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1143 LatexRunParams const & runparams) const
1145 return tabular.linuxdoc(buf,os, runparams);
1149 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1150 LatexRunParams const & runparams) const
1155 // if the table is inside a float it doesn't need the informaltable
1156 // wrapper. Search for it.
1157 for (master = owner();
1158 master && master->lyxCode() != InsetOld::FLOAT_CODE;
1159 master = master->owner());
1162 os << "<informaltable>";
1163 if (runparams.mixed_content)
1167 ret += tabular.docbook(buf, os, runparams);
1169 os << "</informaltable>";
1170 if (runparams.mixed_content)
1178 void InsetTabular::validate(LaTeXFeatures & features) const
1180 tabular.validate(features);
1184 void InsetTabular::calculate_dimensions_of_cells(MetricsInfo & mi) const
1187 // if we have a locking_inset we should have to check only this cell for
1188 // change so I'll try this to have a boost, but who knows ;) (Jug?)
1189 // This is _really_ important (André)
1190 if (the_locking_inset == &tabular.getCellInset(actcell)) {
1193 for (int j = 0; j < tabular.columns(); ++j) {
1197 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1198 tabular.getCellInset(actrow, j).metrics(m, dim);
1199 maxAsc = max(dim.asc, maxAsc);
1200 maxDesc = max(dim.des, maxDesc);
1202 tabular.setWidthOfCell(actcell, the_locking_inset->width());
1203 tabular.setAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT);
1204 tabular.setDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT);
1210 for (int i = 0; i < tabular.rows(); ++i) {
1213 for (int j = 0; j < tabular.columns(); ++j) {
1214 if (tabular.isPartOfMultiColumn(i, j))
1220 tabular.column_info[j].p_width.inPixels(mi.base.textwidth);
1221 tabular.getCellInset(cell).metrics(m, dim);
1222 maxAsc = max(maxAsc, dim.asc);
1223 maxDesc = max(maxDesc, dim.des);
1224 tabular.setWidthOfCell(cell, dim.wid);
1226 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
1227 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
1232 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1234 if (the_locking_inset) {
1235 the_locking_inset->getCursor(bv, x, y);
1240 y = cursory_ + InsetTabular::y();
1243 int desc = tabular.getDescentOfRow(actrow);
1245 int ascdesc = tabular.getAscentOfRow(actrow) + desc;
1247 y += ADD_TO_HEIGHT * 2;
1248 y += TEXT_TO_INSET_OFFSET;
1252 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1254 if (the_locking_inset) {
1255 the_locking_inset->getCursorPos(bv, x, y);
1258 x = cursorx_ - top_x;
1263 void InsetTabular::fitInsetCursor(BufferView * bv) const
1265 if (the_locking_inset) {
1266 the_locking_inset->fitInsetCursor(bv);
1271 int const asc = font_metrics::maxAscent(font);
1272 int const desc = font_metrics::maxDescent(font);
1275 bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
1279 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1282 actcell = actrow = actcol = 0;
1283 int ly = tabular.getDescentOfRow(actrow);
1285 // first search the right row
1286 while (ly < y && actrow + 1 < tabular.rows()) {
1287 cursory_ += tabular.getDescentOfRow(actrow) +
1288 tabular.getAscentOfRow(actrow + 1) +
1289 tabular.getAdditionalHeight(actrow + 1);
1291 ly = cursory_ + tabular.getDescentOfRow(actrow);
1293 actcell = tabular.getCellNumber(actrow, actcol);
1295 // now search the right column
1296 int lx = tabular.getWidthOfColumn(actcell) -
1297 tabular.getAdditionalWidth(actcell);
1299 for (; !tabular.isLastCellInRow(actcell) && lx < x; ++actcell)
1300 lx += tabular.getWidthOfColumn(actcell + 1)
1301 + tabular.getAdditionalWidth(actcell);
1303 cursorx_ = lx - tabular.getWidthOfColumn(actcell) + top_x + 2;
1308 int InsetTabular::getCellXPos(int cell) const
1312 for (; !tabular.isFirstCellInRow(c); --c)
1314 int lx = tabular.getWidthOfColumn(cell);
1315 for (; c < cell; ++c)
1316 lx += tabular.getWidthOfColumn(c);
1318 return (lx - tabular.getWidthOfColumn(cell) + top_x);
1322 void InsetTabular::resetPos(BufferView * bv) const
1324 #ifdef WITH_WARNINGS
1325 #warning This should be fixed in the right manner (20011128 Jug)
1327 // fast hack to fix infinite repaintings!
1328 if (in_reset_pos > 0)
1332 actcol = tabular.column_of_cell(actcell);
1335 for (; cell < actcell && !tabular.isLastRow(cell); ++cell) {
1336 if (tabular.isLastCellInRow(cell)) {
1337 cursory_ += tabular.getDescentOfRow(actrow) +
1338 tabular.getAscentOfRow(actrow + 1) +
1339 tabular.getAdditionalHeight(actrow + 1);
1344 if (the_locking_inset)
1348 // we need this only from here on!!!
1350 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1351 int new_x = getCellXPos(actcell);
1352 int old_x = cursorx_;
1355 // cursor.x(getCellXPos(actcell) + offset);
1356 if (actcol < tabular.columns() - 1 && scroll(false) &&
1357 tabular.getWidthOfTabular() < bv->workWidth()-20)
1361 } else if (the_locking_inset &&
1362 tabular.getWidthOfColumn(actcell) > bv->workWidth() - 20)
1364 int xx = cursorx_ - offset + bv->text->getRealCursorX();
1365 if (xx > bv->workWidth()-20) {
1366 scroll(bv, -(xx - bv->workWidth() + 60));
1368 } else if (xx < 20) {
1376 } else if (cursorx_ - offset > 20 &&
1377 cursorx_ - offset + tabular.getWidthOfColumn(actcell)
1378 > bv->workWidth() - 20) {
1379 scroll(bv, -tabular.getWidthOfColumn(actcell) - 20);
1381 } else if (cursorx_ - offset < 20) {
1382 scroll(bv, 20 - cursorx_ + offset);
1384 } else if (scroll() && top_x > 20 &&
1385 (top_x + tabular.getWidthOfTabular()) > bv->workWidth() - 20) {
1386 scroll(bv, old_x - cursorx_);
1389 if (the_locking_inset) {
1390 inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1393 if ((!the_locking_inset ||
1394 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1395 actcell != oldcell) {
1396 InsetTabularMailer(*this).updateDialog(bv);
1403 DispatchResult InsetTabular::moveRight(BufferView * bv, bool lock)
1405 if (lock && !old_locking_inset) {
1406 if (activateCellInset(bv))
1407 return DispatchResult(true);
1409 bool moved = isRightToLeft(bv)
1410 ? movePrevCell(bv) : moveNextCell(bv);
1412 return DispatchResult(false, FINISHED_RIGHT);
1413 if (lock && activateCellInset(bv))
1414 return DispatchResult(true);
1417 return DispatchResult(true, NOUPDATE);
1421 DispatchResult InsetTabular::moveLeft(BufferView * bv, bool lock)
1423 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1425 return DispatchResult(false, FINISHED);
1427 if (lock && activateCellInset(bv, 0, 0, mouse_button::none, true))
1428 return DispatchResult(true);
1430 return DispatchResult(true, NOUPDATE);
1434 DispatchResult InsetTabular::moveUp(BufferView * bv, bool lock)
1436 int const ocell = actcell;
1437 actcell = tabular.getCellAbove(actcell);
1438 if (actcell == ocell) // we moved out of the inset
1439 return DispatchResult(false, FINISHED_UP);
1444 if (old_locking_inset) {
1445 old_locking_inset->getCursorPos(bv, x, y);
1446 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1448 if (activateCellInset(bv, x, 0))
1449 return DispatchResult(true);
1451 return DispatchResult(true, NOUPDATE);
1455 DispatchResult InsetTabular::moveDown(BufferView * bv, bool lock)
1457 int const ocell = actcell;
1458 actcell = tabular.getCellBelow(actcell);
1459 if (actcell == ocell) // we moved out of the inset
1460 return DispatchResult(false, FINISHED_DOWN);
1465 if (old_locking_inset) {
1466 old_locking_inset->getCursorPos(bv, x, y);
1467 x -= cursorx_ + tabular.getBeginningOfTextInCell(actcell);
1469 if (activateCellInset(bv, x, 0))
1470 return DispatchResult(true);
1472 return DispatchResult(true, NOUPDATE);
1476 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1478 if (isRightToLeft(bv)) {
1479 if (tabular.isFirstCellInRow(actcell)) {
1480 int row = tabular.row_of_cell(actcell);
1481 if (row == tabular.rows() - 1)
1483 actcell = tabular.getLastCellInRow(row);
1484 actcell = tabular.getCellBelow(actcell);
1491 if (tabular.isLastCell(actcell))
1496 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1497 isRightToLeftPar(bv->buffer()->params());
1498 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1505 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1507 if (isRightToLeft(bv)) {
1508 if (tabular.isLastCellInRow(actcell)) {
1509 int row = tabular.row_of_cell(actcell);
1512 actcell = tabular.getFirstCellInRow(row);
1513 actcell = tabular.getCellAbove(actcell);
1515 if (tabular.isLastCell(actcell))
1520 if (!actcell) // first cell
1525 bool rtl = tabular.getCellInset(actcell).paragraphs.begin()->
1526 isRightToLeftPar(bv->buffer()->params());
1527 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1534 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1538 setSelection(0, tabular.getNumberOfCells() - 1);
1540 if (hasSelection()) {
1541 recordUndo(bv, Undo::ATOMIC);
1542 bool const frozen = undo_frozen;
1545 // apply the fontchange on the whole selection
1550 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1551 for (int i = sel_row_start; i <= sel_row_end; ++i)
1552 for (int j = sel_col_start; j <= sel_col_end; ++j)
1553 tabular.getCellInset(i, j).setFont(bv, font, tall, true);
1561 if (the_locking_inset)
1562 the_locking_inset->setFont(bv, font, tall);
1566 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1568 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1571 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1572 string const tmp = tabularFeature[i].feature;
1574 if (tmp == what.substr(0, tmp.length())) {
1575 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1576 //tabularFeatures[i].feature.length())) {
1577 action = tabularFeature[i].action;
1581 if (action == LyXTabular::LAST_ACTION)
1585 ltrim(what.substr(tabularFeature[i].feature.length()));
1586 tabularFeatures(bv, action, val);
1592 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1593 string const & special, bool & flag)
1595 if (special == "dl_above") {
1598 } else if (special == "dl_below") {
1599 ltt.bottomDL = flag;
1601 } else if (special == "empty") {
1613 void InsetTabular::tabularFeatures(BufferView * bv,
1614 LyXTabular::Feature feature, string const & value)
1620 bool setLines = false;
1621 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1622 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1626 case LyXTabular::M_ALIGN_LEFT:
1627 case LyXTabular::ALIGN_LEFT:
1628 setAlign = LYX_ALIGN_LEFT;
1631 case LyXTabular::M_ALIGN_RIGHT:
1632 case LyXTabular::ALIGN_RIGHT:
1633 setAlign = LYX_ALIGN_RIGHT;
1636 case LyXTabular::M_ALIGN_CENTER:
1637 case LyXTabular::ALIGN_CENTER:
1638 setAlign = LYX_ALIGN_CENTER;
1641 case LyXTabular::ALIGN_BLOCK:
1642 setAlign = LYX_ALIGN_BLOCK;
1645 case LyXTabular::M_VALIGN_TOP:
1646 case LyXTabular::VALIGN_TOP:
1647 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1650 case LyXTabular::M_VALIGN_BOTTOM:
1651 case LyXTabular::VALIGN_BOTTOM:
1652 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1655 case LyXTabular::M_VALIGN_MIDDLE:
1656 case LyXTabular::VALIGN_MIDDLE:
1657 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1664 if (hasSelection()) {
1665 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1667 sel_col_start = sel_col_end = tabular.column_of_cell(actcell);
1668 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
1670 recordUndo(bv, Undo::ATOMIC);
1672 int row = tabular.row_of_cell(actcell);
1673 int column = tabular.column_of_cell(actcell);
1675 LyXTabular::ltType ltt;
1679 case LyXTabular::SET_PWIDTH: {
1680 LyXLength const len(value);
1681 LyXLength const & oldlen = tabular.getColumnPWidth(actcell);
1683 tabular.setColumnPWidth(actcell, len);
1684 if (oldlen != len) {
1685 // We need this otherwise we won't resize
1686 // the insettext of the active cell (if any)
1687 // until later (see InsetText::do_resize)
1688 unlockInsetInInset(bv, the_locking_inset);
1693 && tabular.getAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1694 tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1695 else if (!len.zero()
1696 && tabular.getAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1697 tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1701 case LyXTabular::SET_MPWIDTH:
1703 LyXLength const len(value);
1704 LyXLength const & oldlen = tabular.getPWidth(actcell);
1705 tabular.setMColumnPWidth(actcell, len);
1706 if (oldlen != len) {
1707 // We need this otherwise we won't resize
1708 // the insettext of the active cell (if any)
1709 // until later (see InsetText::do_resize)
1710 unlockInsetInInset(bv, the_locking_inset);
1716 case LyXTabular::SET_SPECIAL_COLUMN:
1717 case LyXTabular::SET_SPECIAL_MULTI:
1718 tabular.setAlignSpecial(actcell,value,feature);
1722 case LyXTabular::APPEND_ROW:
1723 // append the row into the tabular
1724 unlockInsetInInset(bv, the_locking_inset);
1725 tabular.appendRow(bv->buffer()->params(), actcell);
1726 tabular.setOwner(this);
1730 case LyXTabular::APPEND_COLUMN:
1731 // append the column into the tabular
1732 unlockInsetInInset(bv, the_locking_inset);
1733 tabular.appendColumn(bv->buffer()->params(), actcell);
1734 tabular.setOwner(this);
1735 actcell = tabular.getCellNumber(row, column);
1739 case LyXTabular::DELETE_ROW:
1740 unlockInsetInInset(bv, the_locking_inset);
1741 for (int i = sel_row_start; i <= sel_row_end; ++i)
1742 tabular.deleteRow(sel_row_start);
1743 if (sel_row_start >= tabular.rows())
1745 actcell = tabular.getCellNumber(sel_row_start, column);
1750 case LyXTabular::DELETE_COLUMN:
1751 unlockInsetInInset(bv, the_locking_inset);
1752 for (int i = sel_col_start; i <= sel_col_end; ++i)
1753 tabular.deleteColumn(sel_col_start);
1754 if (sel_col_start >= tabular.columns())
1756 actcell = tabular.getCellNumber(row, sel_col_start);
1761 case LyXTabular::M_TOGGLE_LINE_TOP:
1763 case LyXTabular::TOGGLE_LINE_TOP: {
1764 bool lineSet = !tabular.topLine(actcell, flag);
1765 for (int i = sel_row_start; i <= sel_row_end; ++i)
1766 for (int j = sel_col_start; j <= sel_col_end; ++j)
1768 tabular.getCellNumber(i, j),
1774 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1776 case LyXTabular::TOGGLE_LINE_BOTTOM: {
1777 bool lineSet = !tabular.bottomLine(actcell, flag);
1778 for (int i = sel_row_start; i <= sel_row_end; ++i)
1779 for (int j = sel_col_start; j <= sel_col_end; ++j)
1780 tabular.setBottomLine(
1781 tabular.getCellNumber(i, j),
1788 case LyXTabular::M_TOGGLE_LINE_LEFT:
1790 case LyXTabular::TOGGLE_LINE_LEFT: {
1791 bool lineSet = !tabular.leftLine(actcell, flag);
1792 for (int i = sel_row_start; i <= sel_row_end; ++i)
1793 for (int j = sel_col_start; j <= sel_col_end; ++j)
1794 tabular.setLeftLine(
1795 tabular.getCellNumber(i,j),
1802 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1804 case LyXTabular::TOGGLE_LINE_RIGHT: {
1805 bool lineSet = !tabular.rightLine(actcell, flag);
1806 for (int i = sel_row_start; i <= sel_row_end; ++i)
1807 for (int j = sel_col_start; j <= sel_col_end; ++j)
1808 tabular.setRightLine(
1809 tabular.getCellNumber(i,j),
1816 case LyXTabular::M_ALIGN_LEFT:
1817 case LyXTabular::M_ALIGN_RIGHT:
1818 case LyXTabular::M_ALIGN_CENTER:
1820 case LyXTabular::ALIGN_LEFT:
1821 case LyXTabular::ALIGN_RIGHT:
1822 case LyXTabular::ALIGN_CENTER:
1823 case LyXTabular::ALIGN_BLOCK:
1824 for (int i = sel_row_start; i <= sel_row_end; ++i)
1825 for (int j = sel_col_start; j <= sel_col_end; ++j)
1826 tabular.setAlignment(
1827 tabular.getCellNumber(i, j),
1833 case LyXTabular::M_VALIGN_TOP:
1834 case LyXTabular::M_VALIGN_BOTTOM:
1835 case LyXTabular::M_VALIGN_MIDDLE:
1837 case LyXTabular::VALIGN_TOP:
1838 case LyXTabular::VALIGN_BOTTOM:
1839 case LyXTabular::VALIGN_MIDDLE:
1840 for (int i = sel_row_start; i <= sel_row_end; ++i)
1841 for (int j = sel_col_start; j <= sel_col_end; ++j)
1842 tabular.setVAlignment(
1843 tabular.getCellNumber(i, j),
1848 case LyXTabular::MULTICOLUMN: {
1849 if (sel_row_start != sel_row_end) {
1850 #ifdef WITH_WARNINGS
1851 #warning Need I say it ? This is horrible.
1853 Alert::error(_("Error setting multicolumn"),
1854 _("You cannot set multicolumn vertically."));
1857 // just multicol for one Single Cell
1858 if (!hasSelection()) {
1859 // check wether we are completly in a multicol
1860 if (tabular.isMultiColumn(actcell))
1861 tabular.unsetMultiColumn(actcell);
1863 tabular.setMultiColumn(bv->buffer(), actcell, 1);
1867 // we have a selection so this means we just add all this
1868 // cells to form a multicolumn cell
1872 if (sel_cell_start > sel_cell_end) {
1873 s_start = sel_cell_end;
1874 s_end = sel_cell_start;
1876 s_start = sel_cell_start;
1877 s_end = sel_cell_end;
1879 tabular.setMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1886 case LyXTabular::SET_ALL_LINES:
1888 case LyXTabular::UNSET_ALL_LINES:
1889 for (int i = sel_row_start; i <= sel_row_end; ++i)
1890 for (int j = sel_col_start; j <= sel_col_end; ++j)
1891 tabular.setAllLines(
1892 tabular.getCellNumber(i,j), setLines);
1896 case LyXTabular::SET_LONGTABULAR:
1897 tabular.setLongTabular(true);
1898 updateLocal(bv); // because this toggles displayed
1901 case LyXTabular::UNSET_LONGTABULAR:
1902 tabular.setLongTabular(false);
1903 updateLocal(bv); // because this toggles displayed
1906 case LyXTabular::SET_ROTATE_TABULAR:
1907 tabular.setRotateTabular(true);
1910 case LyXTabular::UNSET_ROTATE_TABULAR:
1911 tabular.setRotateTabular(false);
1914 case LyXTabular::SET_ROTATE_CELL:
1915 for (int i = sel_row_start; i <= sel_row_end; ++i)
1916 for (int j = sel_col_start; j <= sel_col_end; ++j)
1917 tabular.setRotateCell(
1918 tabular.getCellNumber(i, j), true);
1921 case LyXTabular::UNSET_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), false);
1928 case LyXTabular::SET_USEBOX: {
1929 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1930 if (val == tabular.getUsebox(actcell))
1931 val = LyXTabular::BOX_NONE;
1932 for (int i = sel_row_start; i <= sel_row_end; ++i)
1933 for (int j = sel_col_start; j <= sel_col_end; ++j)
1934 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1938 case LyXTabular::UNSET_LTFIRSTHEAD:
1940 case LyXTabular::SET_LTFIRSTHEAD:
1941 tabular.getRowOfLTFirstHead(row, ltt);
1942 checkLongtableSpecial(ltt, value, flag);
1943 tabular.setLTHead(row, flag, ltt, true);
1946 case LyXTabular::UNSET_LTHEAD:
1948 case LyXTabular::SET_LTHEAD:
1949 tabular.getRowOfLTHead(row, ltt);
1950 checkLongtableSpecial(ltt, value, flag);
1951 tabular.setLTHead(row, flag, ltt, false);
1954 case LyXTabular::UNSET_LTFOOT:
1956 case LyXTabular::SET_LTFOOT:
1957 tabular.getRowOfLTFoot(row, ltt);
1958 checkLongtableSpecial(ltt, value, flag);
1959 tabular.setLTFoot(row, flag, ltt, false);
1962 case LyXTabular::UNSET_LTLASTFOOT:
1964 case LyXTabular::SET_LTLASTFOOT:
1965 tabular.getRowOfLTLastFoot(row, ltt);
1966 checkLongtableSpecial(ltt, value, flag);
1967 tabular.setLTFoot(row, flag, ltt, true);
1970 case LyXTabular::SET_LTNEWPAGE:
1971 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1974 // dummy stuff just to avoid warnings
1975 case LyXTabular::LAST_ACTION:
1979 InsetTabularMailer(*this).updateDialog(bv);
1983 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y,
1984 mouse_button::state button, bool behind)
1986 UpdatableInset & inset = tabular.getCellInset(actcell);
1989 x = inset.x() + inset.width();
1990 y = inset.descent();
1992 //inset_x = cursorx_ - top_x + tabular.getBeginningOfTextInCell(actcell);
1993 //inset_y = cursory_;
1994 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, x, y, button));
1995 if (!the_locking_inset)
1998 return the_locking_inset;
2002 bool InsetTabular::insetHit(BufferView *, int x, int) const
2004 return x + top_x > cursorx_ + tabular.getBeginningOfTextInCell(actcell);
2008 void InsetTabular::deleteLyXText(BufferView * /*bv*/, bool /*recursive*/) const
2010 //resizeLyXText(bv, recursive);
2014 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2015 bool const recursive) const
2017 if (the_locking_inset)
2018 return the_locking_inset->getLyXText(bv, recursive);
2019 return InsetOld::getLyXText(bv, recursive);
2023 bool InsetTabular::showInsetDialog(BufferView * bv) const
2025 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2026 InsetTabularMailer(*this).showDialog(bv);
2031 void InsetTabular::openLayoutDialog(BufferView * bv) const
2033 if (the_locking_inset) {
2034 InsetTabular * inset = static_cast<InsetTabular *>
2035 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2037 inset->openLayoutDialog(bv);
2041 InsetTabularMailer(*this).showDialog(bv);
2046 // function returns an object as defined in func_status.h:
2047 // states OK, Unknown, Disabled, On, Off.
2049 FuncStatus InsetTabular::getStatus(string const & what) const
2051 int action = LyXTabular::LAST_ACTION;
2055 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2056 string const tmp = tabularFeature[i].feature;
2057 if (tmp == what.substr(0, tmp.length())) {
2058 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2059 // tabularFeatures[i].feature.length())) {
2060 action = tabularFeature[i].action;
2064 if (action == LyXTabular::LAST_ACTION) {
2066 status.unknown(true);
2070 string const argument
2071 = ltrim(what.substr(tabularFeature[i].feature.length()));
2076 LyXTabular::ltType dummyltt;
2080 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2082 sel_row_start = sel_row_end = tabular.row_of_cell(actcell);
2085 case LyXTabular::SET_PWIDTH:
2086 case LyXTabular::SET_MPWIDTH:
2087 case LyXTabular::SET_SPECIAL_COLUMN:
2088 case LyXTabular::SET_SPECIAL_MULTI:
2089 case LyXTabular::APPEND_ROW:
2090 case LyXTabular::APPEND_COLUMN:
2091 case LyXTabular::DELETE_ROW:
2092 case LyXTabular::DELETE_COLUMN:
2093 case LyXTabular::SET_ALL_LINES:
2094 case LyXTabular::UNSET_ALL_LINES:
2098 case LyXTabular::MULTICOLUMN:
2099 status.setOnOff(tabular.isMultiColumn(actcell));
2102 case LyXTabular::M_TOGGLE_LINE_TOP:
2104 case LyXTabular::TOGGLE_LINE_TOP:
2105 status.setOnOff(tabular.topLine(actcell, flag));
2108 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2110 case LyXTabular::TOGGLE_LINE_BOTTOM:
2111 status.setOnOff(tabular.bottomLine(actcell, flag));
2114 case LyXTabular::M_TOGGLE_LINE_LEFT:
2116 case LyXTabular::TOGGLE_LINE_LEFT:
2117 status.setOnOff(tabular.leftLine(actcell, flag));
2120 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2122 case LyXTabular::TOGGLE_LINE_RIGHT:
2123 status.setOnOff(tabular.rightLine(actcell, flag));
2126 case LyXTabular::M_ALIGN_LEFT:
2128 case LyXTabular::ALIGN_LEFT:
2129 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2132 case LyXTabular::M_ALIGN_RIGHT:
2134 case LyXTabular::ALIGN_RIGHT:
2135 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2138 case LyXTabular::M_ALIGN_CENTER:
2140 case LyXTabular::ALIGN_CENTER:
2141 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2144 case LyXTabular::ALIGN_BLOCK:
2145 status.disabled(tabular.getPWidth(actcell).zero());
2146 status.setOnOff(tabular.getAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2149 case LyXTabular::M_VALIGN_TOP:
2151 case LyXTabular::VALIGN_TOP:
2152 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2155 case LyXTabular::M_VALIGN_BOTTOM:
2157 case LyXTabular::VALIGN_BOTTOM:
2158 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2161 case LyXTabular::M_VALIGN_MIDDLE:
2163 case LyXTabular::VALIGN_MIDDLE:
2164 status.setOnOff(tabular.getVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_MIDDLE);
2167 case LyXTabular::SET_LONGTABULAR:
2168 status.setOnOff(tabular.isLongTabular());
2171 case LyXTabular::UNSET_LONGTABULAR:
2172 status.setOnOff(!tabular.isLongTabular());
2175 case LyXTabular::SET_ROTATE_TABULAR:
2176 status.setOnOff(tabular.getRotateTabular());
2179 case LyXTabular::UNSET_ROTATE_TABULAR:
2180 status.setOnOff(!tabular.getRotateTabular());
2183 case LyXTabular::SET_ROTATE_CELL:
2184 status.setOnOff(tabular.getRotateCell(actcell));
2187 case LyXTabular::UNSET_ROTATE_CELL:
2188 status.setOnOff(!tabular.getRotateCell(actcell));
2191 case LyXTabular::SET_USEBOX:
2192 status.setOnOff(strToInt(argument) == tabular.getUsebox(actcell));
2195 case LyXTabular::SET_LTFIRSTHEAD:
2196 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2199 case LyXTabular::SET_LTHEAD:
2200 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
2203 case LyXTabular::SET_LTFOOT:
2204 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2207 case LyXTabular::SET_LTLASTFOOT:
2208 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
2211 case LyXTabular::SET_LTNEWPAGE:
2212 status.setOnOff(tabular.getLTNewPage(sel_row_start));
2217 status.disabled(true);
2224 void InsetTabular::getLabelList(Buffer const & buffer,
2225 std::vector<string> & list) const
2227 tabular.getLabelList(buffer, list);
2231 bool InsetTabular::copySelection(BufferView * bv)
2233 if (!hasSelection())
2236 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2237 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2238 if (sel_col_start > sel_col_end) {
2239 sel_col_start = sel_col_end;
2240 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2242 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2245 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2246 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2247 if (sel_row_start > sel_row_end)
2248 swap(sel_row_start, sel_row_end);
2250 delete paste_tabular;
2251 paste_tabular = new LyXTabular(tabular);
2252 paste_tabular->setOwner(this);
2254 for (int i = 0; i < sel_row_start; ++i)
2255 paste_tabular->deleteRow(0);
2257 int const rows = sel_row_end - sel_row_start + 1;
2258 while (paste_tabular->rows() > rows)
2259 paste_tabular->deleteRow(rows);
2261 paste_tabular->setTopLine(0, true, true);
2262 paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
2265 for (int i = 0; i < sel_col_start; ++i)
2266 paste_tabular->deleteColumn(0);
2268 int const columns = sel_col_end - sel_col_start + 1;
2269 while (paste_tabular->columns() > columns)
2270 paste_tabular->deleteColumn(columns);
2272 paste_tabular->setLeftLine(0, true, true);
2273 paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
2277 LatexRunParams const runparams;
2278 paste_tabular->ascii(*bv->buffer(), os, runparams,
2279 ownerPar(*bv->buffer(), this).params().depth(), true, '\t');
2280 bv->stuffClipboard(os.str());
2285 bool InsetTabular::pasteSelection(BufferView * bv)
2290 for (int r1 = 0, r2 = actrow;
2291 r1 < paste_tabular->rows() && r2 < tabular.rows();
2293 for (int c1 = 0, c2 = actcol;
2294 c1 < paste_tabular->columns() && c2 < tabular.columns();
2296 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
2297 tabular.isPartOfMultiColumn(r2, c2))
2299 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
2303 if (tabular.isPartOfMultiColumn(r2, c2)) {
2307 InsetText & inset = tabular.getCellInset(r2, c2);
2308 inset = paste_tabular->getCellInset(r1, c1);
2309 inset.setOwner(this);
2310 inset.deleteLyXText(bv);
2318 bool InsetTabular::cutSelection(BufferParams const & bp)
2320 if (!hasSelection())
2323 int sel_col_start = tabular.column_of_cell(sel_cell_start);
2324 int sel_col_end = tabular.column_of_cell(sel_cell_end);
2325 if (sel_col_start > sel_col_end) {
2326 sel_col_start = sel_col_end;
2327 sel_col_end = tabular.right_column_of_cell(sel_cell_start);
2329 sel_col_end = tabular.right_column_of_cell(sel_cell_end);
2332 int sel_row_start = tabular.row_of_cell(sel_cell_start);
2333 int sel_row_end = tabular.row_of_cell(sel_cell_end);
2335 if (sel_row_start > sel_row_end)
2336 swap(sel_row_start, sel_row_end);
2338 if (sel_cell_start > sel_cell_end)
2339 swap(sel_cell_start, sel_cell_end);
2341 for (int i = sel_row_start; i <= sel_row_end; ++i)
2342 for (int j = sel_col_start; j <= sel_col_end; ++j)
2343 tabular.getCellInset(tabular.getCellNumber(i, j))
2344 .clear(bp.tracking_changes);
2349 bool InsetTabular::isRightToLeft(BufferView * bv)
2351 return bv->getParentLanguage(this)->RightToLeft();
2355 int InsetTabular::scroll(bool recursive) const
2357 int sx = UpdatableInset::scroll(false);
2359 if (recursive && the_locking_inset)
2360 sx += the_locking_inset->scroll(recursive);
2366 void InsetTabular::getSelection(int & srow, int & erow,
2367 int & scol, int & ecol) const
2369 int const start = hasSelection() ? sel_cell_start : actcell;
2370 int const end = hasSelection() ? sel_cell_end : actcell;
2372 srow = tabular.row_of_cell(start);
2373 erow = tabular.row_of_cell(end);
2377 scol = tabular.column_of_cell(start);
2378 ecol = tabular.column_of_cell(end);
2382 ecol = tabular.right_column_of_cell(end);
2386 ParagraphList * InsetTabular::getParagraphs(int i) const
2388 return i < tabular.getNumberOfCells()
2389 ? tabular.getCellInset(i).getParagraphs(0)
2394 int InsetTabular::numParagraphs() const
2396 return tabular.getNumberOfCells();
2400 LyXText * InsetTabular::getText(int i) const
2402 return i < tabular.getNumberOfCells()
2403 ? tabular.getCellInset(i).getText(0)
2408 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2410 if (the_locking_inset)
2411 return the_locking_inset->cursor(bv);
2412 return InsetOld::cursor(bv);
2417 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2419 if (the_locking_inset) {
2420 WordLangTuple word =
2421 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2422 if (!word.word().empty())
2424 if (tabular.isLastCell(actcell)) {
2425 bv->unlockInset(const_cast<InsetTabular *>(this));
2426 return WordLangTuple();
2430 // otherwise we have to lock the next inset and ask for it's selecttion
2431 tabular.getCellInset(actcell)
2432 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2433 WordLangTuple word = selectNextWordInt(bv, value);
2434 if (!word.word().empty())
2440 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2442 // when entering this function the inset should be ALWAYS locked!
2443 BOOST_ASSERT(the_locking_inset);
2445 WordLangTuple word =
2446 the_locking_inset->selectNextWordToSpellcheck(bv, value);
2447 if (!word.word().empty())
2450 if (tabular.isLastCell(actcell)) {
2451 bv->unlockInset(const_cast<InsetTabular *>(this));
2452 return WordLangTuple();
2455 // otherwise we have to lock the next inset and ask for it's selecttion
2457 tabular.getCellInset(actcell)
2458 .dispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2459 return selectNextWordInt(bv, value);
2463 void InsetTabular::selectSelectedWord(BufferView * bv)
2465 if (the_locking_inset)
2466 the_locking_inset->selectSelectedWord(bv);
2470 void InsetTabular::markErased()
2472 for (int cell = 0; cell < tabular.getNumberOfCells(); ++cell)
2473 tabular.getCellInset(cell).markErased();
2477 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2479 if (the_locking_inset) {
2480 if (the_locking_inset->nextChange(bv, length)) {
2484 if (tabular.isLastCell(actcell))
2488 InsetText & inset = tabular.getCellInset(actcell);
2489 if (inset.nextChange(bv, length)) {
2493 while (!tabular.isLastCell(actcell)) {
2495 InsetText & inset = tabular.getCellInset(actcell);
2496 if (inset.nextChange(bv, length)) {
2505 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2509 if (the_locking_inset) {
2510 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2514 if (tabular.isLastCell(actcell))
2518 InsetText & inset = tabular.getCellInset(cell);
2519 if (inset.searchForward(bv, str, cs, mw)) {
2523 while (!tabular.isLastCell(cell)) {
2525 InsetText & inset = tabular.getCellInset(cell);
2526 if (inset.searchForward(bv, str, cs, mw)) {
2535 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2538 int cell = tabular.getNumberOfCells();
2539 if (the_locking_inset) {
2540 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2549 InsetText & inset = tabular.getCellInset(cell);
2550 if (inset.searchBackward(bv, str, cs, mw)) {
2559 bool InsetTabular::insetAllowed(InsetOld::Code code) const
2561 if (the_locking_inset)
2562 return the_locking_inset->insetAllowed(code);
2563 // we return true here because if the inset is not locked someone
2564 // wants to insert something in one of our insettexts and we generally
2570 bool InsetTabular::forceDefaultParagraphs(InsetOld const * in) const
2572 const int cell = tabular.getCellFromInset(in);
2575 return tabular.getPWidth(cell).zero();
2577 // this is a workaround for a crash (New, Insert->Tabular,
2578 // Insert->FootNote)
2582 // well we didn't obviously find it so maybe our owner knows more
2583 BOOST_ASSERT(owner());
2584 return owner()->forceDefaultParagraphs(in);
2588 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2591 if (buf.length() <= 0)
2597 string::size_type len = buf.length();
2598 string::size_type p = 0;
2600 while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
2608 maxCols = max(cols, maxCols);
2614 maxCols = max(cols, maxCols);
2615 LyXTabular * loctab;
2620 delete paste_tabular;
2621 paste_tabular = new LyXTabular(bv->buffer()->params(),
2623 paste_tabular->setOwner(this);
2624 loctab = paste_tabular;
2633 string::size_type op = 0;
2634 int cells = loctab->getNumberOfCells();
2637 rows = loctab->rows();
2638 int const columns = loctab->columns();
2640 while (cell < cells && p < len && row < rows &&
2641 (p = buf.find_first_of("\t\n", p)) != string::npos)
2647 // we can only set this if we are not too far right
2648 if (cols < columns) {
2649 InsetText & inset = loctab->getCellInset(cell);
2650 LyXFont const font = inset.getLyXText(bv)->
2651 getFont(inset.paragraphs.begin(), 0);
2652 inset.setText(buf.substr(op, p - op), font);
2658 // we can only set this if we are not too far right
2659 if (cols < columns) {
2660 InsetText & inset = tabular.getCellInset(cell);
2661 LyXFont const font = inset.getLyXText(bv)->
2662 getFont(inset.paragraphs.begin(), 0);
2663 inset.setText(buf.substr(op, p - op), font);
2668 cell = loctab->getCellNumber(row, cols);
2674 // check for the last cell if there is no trailing '\n'
2675 if (cell < cells && op < len) {
2676 InsetText & inset = loctab->getCellInset(cell);
2677 LyXFont const font = inset.getLyXText(bv)->
2678 getFont(inset.paragraphs.begin(), 0);
2679 inset.setText(buf.substr(op, len - op), font);
2686 void InsetTabular::addPreview(PreviewLoader & loader) const
2688 int const rows = tabular.rows();
2689 int const columns = tabular.columns();
2690 for (int i = 0; i < rows; ++i)
2691 for (int j = 0; j < columns; ++j)
2692 tabular.getCellInset(i, j).addPreview(loader);
2696 string const InsetTabularMailer::name_("tabular");
2698 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
2699 : inset_(const_cast<InsetTabular &>(inset))
2703 string const InsetTabularMailer::inset2string(Buffer const &) const
2705 return params2string(inset_);
2709 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2711 istringstream data(in);
2713 lex.setStream(data);
2715 #warning CHECK verify that this is a sane value to return.
2721 string const token = lex.getString();
2729 string const token = lex.getString();
2730 if (token != "\\active_cell")
2733 cell = lex.getInteger();
2736 // This is part of the inset proper that is usually swallowed
2737 // by Buffer::readInset
2740 string const token = lex.getString();
2741 if (token != "Tabular")
2748 Buffer const & buffer = inset.buffer();
2749 inset.read(buffer, lex);
2751 // We can't set the active cell, but we can tell the frontend
2757 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2759 Buffer const & buffer = inset.buffer();
2762 data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2763 inset.write(buffer, data);
2764 data << "\\end_inset\n";