1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
20 #pragma implementation
23 #include "insettabular.h"
26 #include "commandtags.h"
29 #include "LaTeXFeatures.h"
33 #include "lyx_gui_misc.h"
35 #include "insets/insettext.h"
36 #include "frontends/Dialogs.h"
41 #include "BufferView.h"
42 #include "undo_funcs.h"
43 #include "support/LAssert.h"
54 const int ADD_TO_HEIGHT = 2;
55 const int ADD_TO_TABULAR_WIDTH = 2;
57 LyXTabular * paste_tabular = 0;
60 struct TabularFeature {
61 LyXTabular::Feature action;
65 //tabular_features * tabularFeatures = 0;
67 TabularFeature tabularFeature[] =
69 { LyXTabular::APPEND_ROW, "append-row" },
70 { LyXTabular::APPEND_COLUMN, "append-column" },
71 { LyXTabular::DELETE_ROW, "delete-row" },
72 { LyXTabular::DELETE_COLUMN, "delete-column" },
73 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
74 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
75 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
76 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
77 { LyXTabular::ALIGN_LEFT, "align-left" },
78 { LyXTabular::ALIGN_RIGHT, "align-right" },
79 { LyXTabular::ALIGN_CENTER, "align-center" },
80 { LyXTabular::VALIGN_TOP, "valign-top" },
81 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
82 { LyXTabular::VALIGN_CENTER, "valign-center" },
83 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
84 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
85 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
86 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
87 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
88 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
89 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
90 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
91 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
92 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
93 { LyXTabular::MULTICOLUMN, "multicolumn" },
94 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
95 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
96 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
97 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
98 { LyXTabular::SET_PWIDTH, "set-pwidth" },
99 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
100 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
101 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
102 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
103 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
104 { LyXTabular::SET_USEBOX, "set-usebox" },
105 { LyXTabular::SET_LTHEAD, "set-lthead" },
106 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
107 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
108 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
109 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
110 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
111 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
112 { LyXTabular::LAST_ACTION, "" }
118 bool InsetTabular::hasPasteBuffer() const
120 return (paste_tabular != 0);
124 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
131 tabular.reset(new LyXTabular(this, rows, columns));
132 // for now make it always display as display() inset
134 the_locking_inset = 0;
135 locked = no_selection = false;
137 actrow = actcell = 0;
143 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
145 : UpdatableInset(tab, same_id), buffer(&buf)
147 tabular.reset(new LyXTabular(this, *(tab.tabular)));
148 the_locking_inset = 0;
149 locked = no_selection = false;
151 actrow = actcell = 0;
152 sel_cell_start = sel_cell_end = 0;
157 InsetTabular::~InsetTabular()
163 Inset * InsetTabular::clone(Buffer const & buf, bool same_id) const
165 return new InsetTabular(*this, buf, same_id);
169 void InsetTabular::write(Buffer const * buf, ostream & os) const
171 os << " Tabular" << endl;
172 tabular->Write(buf, os);
176 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
178 bool const old_format = (lex.GetString() == "\\LyXTable");
182 //tabular = new LyXTabular(buf, this, lex);
183 tabular.reset(new LyXTabular(buf, this, lex));
191 string token = lex.GetString();
192 while (lex.IsOK() && (token != "\\end_inset")) {
194 token = lex.GetString();
196 if (token != "\\end_inset") {
197 lex.printError("Missing \\end_inset at this point. "
203 int InsetTabular::ascent(BufferView *, LyXFont const &) const
205 return tabular->GetAscentOfRow(0);
209 int InsetTabular::descent(BufferView *, LyXFont const &) const
211 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
215 int InsetTabular::width(BufferView *, LyXFont const &) const
217 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
221 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
222 float & x, bool cleared) const
230 if (need_update == INIT) {
231 if (calculate_dimensions_of_cells(bv, font, true))
232 bv->text->status = LyXText::CHANGED_IN_DRAW;
237 Painter & pain = bv->painter();
243 UpdatableInset::draw(bv, font, baseline, x, cleared);
246 x += static_cast<float>(scroll());
248 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
249 (top_x != int(x)) || (top_baseline != baseline))) {
250 int h = ascent(bv, font) + descent(bv, font);
251 int const tx = display() || !owner() ? 0 : top_x;
252 int w = tx ? width(bv, font) : pain.paperWidth();
253 int ty = baseline - ascent(bv, font);
257 if ((ty + h) > pain.paperHeight())
258 h = pain.paperHeight();
259 if ((top_x + w) > pain.paperWidth())
260 w = pain.paperWidth();
261 pain.fillRectangle(tx, ty, w, h, backgroundColor());
266 top_baseline = baseline;
267 x += ADD_TO_TABULAR_WIDTH;
271 first_visible_cell = -1;
272 for (i = 0; i < tabular->rows(); ++i) {
274 cell = tabular->GetCellNumber(i, 0);
275 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
276 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
278 baseline += tabular->GetDescentOfRow(i) +
279 tabular->GetAscentOfRow(i + 1) +
280 tabular->GetAdditionalHeight(i + 1);
283 for (j = 0; j < tabular->columns(); ++j) {
284 if (nx > bv->workWidth())
286 if (tabular->IsPartOfMultiColumn(i, j))
288 cx = nx + tabular->GetBeginningOfTextInCell(cell);
289 if (first_visible_cell < 0)
290 first_visible_cell = cell;
292 drawCellSelection(pain, nx, baseline, i, j, cell);
294 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
296 drawCellLines(pain, nx, baseline, i, cell);
297 nx += tabular->GetWidthOfColumn(cell);
300 baseline += tabular->GetDescentOfRow(i) +
301 tabular->GetAscentOfRow(i + 1) +
302 tabular->GetAdditionalHeight(i + 1);
304 } else if (need_update == CELL) {
307 if (the_locking_inset &&
308 tabular->GetCellInset(actcell) != the_locking_inset)
310 Inset * inset = tabular->GetCellInset(cell);
312 inset != the_locking_inset && i < tabular->rows();
315 inset != the_locking_inset
316 && j < tabular->columns();
318 if (tabular->IsPartOfMultiColumn(i, j))
320 nx += tabular->GetWidthOfColumn(cell);
322 inset = tabular->GetCellInset(cell);
324 if (tabular->row_of_cell(cell) > i) {
326 baseline += tabular->GetDescentOfRow(i) +
327 tabular->GetAscentOfRow(i + 1) +
328 tabular->GetAdditionalHeight(i + 1);
332 // compute baseline for actual row
333 for (i = 0; i < actrow; ++i) {
334 baseline += tabular->GetDescentOfRow(i) +
335 tabular->GetAscentOfRow(i + 1) +
336 tabular->GetAdditionalHeight(i + 1);
338 // now compute the right x position
339 cell = tabular->GetCellNumber(actrow, 0);
340 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
341 if (tabular->IsPartOfMultiColumn(actrow, j))
343 nx += tabular->GetWidthOfColumn(cell);
347 i = tabular->row_of_cell(cell);
348 if (the_locking_inset != tabular->GetCellInset(cell)) {
349 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
352 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
354 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
355 tabular->GetCellInset(cell)->draw(bv, font, baseline, dx, false);
357 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
360 // clear only if we didn't have a change
361 if (need_update == CELL) {
362 // clear before the inset
365 baseline - tabular->GetAscentOfRow(i) + 1,
367 tabular->GetAscentOfRow(i) +
368 tabular->GetDescentOfRow(i) - 1,
370 // clear behind the inset
372 int(cx + the_locking_inset->width(bv,font) + 1),
373 baseline - tabular->GetAscentOfRow(i) + 1,
374 tabular->GetWidthOfColumn(cell) -
375 tabular->GetBeginningOfTextInCell(cell) -
376 the_locking_inset->width(bv,font) -
377 tabular->GetAdditionalWidth(cell) - 1,
378 tabular->GetAscentOfRow(i) +
379 tabular->GetDescentOfRow(i) - 1,
383 x -= ADD_TO_TABULAR_WIDTH;
384 x += width(bv, font);
385 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
387 for(Inset * inset=owner(); inset; ++i)
388 inset = inset->owner();
389 if (calculate_dimensions_of_cells(bv, font, false))
397 void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
398 int row, int cell) const
400 int x2 = x + tabular->GetWidthOfColumn(cell);
403 if (!tabular->TopAlreadyDrawed(cell)) {
404 on_off = !tabular->TopLine(cell);
405 pain.line(x, baseline - tabular->GetAscentOfRow(row),
406 x2, baseline - tabular->GetAscentOfRow(row),
407 on_off ? LColor::tabularonoffline : LColor::tabularline,
408 on_off ? Painter::line_onoffdash : Painter::line_solid);
410 on_off = !tabular->BottomLine(cell);
411 pain.line(x,baseline + tabular->GetDescentOfRow(row),
412 x2, baseline + tabular->GetDescentOfRow(row),
413 on_off ? LColor::tabularonoffline : LColor::tabularline,
414 on_off ? Painter::line_onoffdash : Painter::line_solid);
415 if (!tabular->LeftAlreadyDrawed(cell)) {
416 on_off = !tabular->LeftLine(cell);
417 pain.line(x, baseline - tabular->GetAscentOfRow(row),
418 x, baseline + tabular->GetDescentOfRow(row),
419 on_off ? LColor::tabularonoffline : LColor::tabularline,
420 on_off ? Painter::line_onoffdash : Painter::line_solid);
422 on_off = !tabular->RightLine(cell);
423 pain.line(x2 - tabular->GetAdditionalWidth(cell),
424 baseline - tabular->GetAscentOfRow(row),
425 x2 - tabular->GetAdditionalWidth(cell),
426 baseline + tabular->GetDescentOfRow(row),
427 on_off ? LColor::tabularonoffline : LColor::tabularline,
428 on_off ? Painter::line_onoffdash : Painter::line_solid);
432 void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
433 int row, int column, int cell) const
435 int cs = tabular->column_of_cell(sel_cell_start);
436 int ce = tabular->column_of_cell(sel_cell_end);
439 cs = tabular->column_of_cell(sel_cell_end);
441 ce = tabular->right_column_of_cell(sel_cell_end);
444 int rs = tabular->row_of_cell(sel_cell_start);
445 int re = tabular->row_of_cell(sel_cell_end);
446 if (rs > re) swap(rs, re);
448 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
449 int w = tabular->GetWidthOfColumn(cell);
450 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
451 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
452 w, h, LColor::selection);
457 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
461 calculate_dimensions_of_cells(bv, font, true);
463 owner()->update(bv, font, true);
466 if (the_locking_inset)
467 the_locking_inset->update(bv, font, reinit);
468 if (need_update < FULL &&
469 bv->text->status() == LyXText::NEED_MORE_REFRESH)
474 switch (need_update) {
478 if (calculate_dimensions_of_cells(bv, font, false))
490 string const InsetTabular::editMessage() const
492 return _("Opened Tabular Inset");
496 void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
498 UpdatableInset::edit(bv, x, y, button);
500 if (!bv->lockInset(this)) {
501 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
505 the_locking_inset = 0;
509 sel_cell_start = sel_cell_end = actcell;
511 if (insetHit(bv, x, y) && (button != 3)) {
512 activateCellInsetAbs(bv, x, y, button);
517 void InsetTabular::edit(BufferView * bv, bool front)
519 UpdatableInset::edit(bv, front);
521 if (!bv->lockInset(this)) {
522 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
526 the_locking_inset = 0;
532 actcell = tabular->GetNumberOfCells() - 1;
533 sel_cell_start = sel_cell_end = actcell;
539 void InsetTabular::insetUnlock(BufferView * bv)
541 if (the_locking_inset) {
542 the_locking_inset->insetUnlock(bv);
543 the_locking_inset = 0;
546 no_selection = false;
549 if (scroll(false) || hasSelection()) {
550 sel_cell_start = sel_cell_end = 0;
554 updateLocal(bv, FULL, false);
559 void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
560 bool mark_dirty) const
562 if (need_update < what) // only set this if it has greater update
564 if ((what == INIT) && hasSelection())
567 if (need_update != NONE) {
568 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
569 if (locked) // && (what != NONE))
575 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
577 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
582 if (inset == tabular->GetCellInset(actcell)) {
583 lyxerr[Debug::INSETS] << "OK" << endl;
584 the_locking_inset = tabular->GetCellInset(actcell);
586 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
587 inset_y = cursor_.y();
589 } else if (the_locking_inset && (the_locking_inset == inset)) {
590 lyxerr[Debug::INSETS] << "OK" << endl;
592 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
593 inset_y = cursor_.y();
594 } else if (the_locking_inset) {
595 lyxerr[Debug::INSETS] << "MAYBE" << endl;
596 return the_locking_inset->lockInsetInInset(bv, inset);
598 lyxerr[Debug::INSETS] << "NOT OK" << endl;
603 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
606 if (!the_locking_inset)
608 if (the_locking_inset == inset) {
609 the_locking_inset->insetUnlock(bv);
610 the_locking_inset = 0;
612 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
618 updateLocal(bv, CELL, false);
619 showInsetCursor(bv, false);
622 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
623 if (inset->lyxCode() == TABULAR_CODE &&
624 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
625 bv->owner()->getDialogs()->updateTabular(this);
634 bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
636 if (!the_locking_inset)
638 if (the_locking_inset != inset) {
639 if (!the_locking_inset->updateInsetInInset(bv, inset))
642 updateLocal(bv, CELL, false);
647 unsigned int InsetTabular::insetInInsetY()
649 if (!the_locking_inset)
652 return inset_y + the_locking_inset->insetInInsetY();
656 UpdatableInset * InsetTabular::getLockingInset() const
658 return the_locking_inset ? the_locking_inset->getLockingInset() :
659 const_cast<InsetTabular *>(this);
663 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(Inset::Code c)
667 if (the_locking_inset)
668 return the_locking_inset->getFirstLockingInsetOfType(c);
673 bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
675 if (the_locking_inset)
676 return the_locking_inset->insertInset(bv, inset);
681 void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
683 if (hasSelection() && (button == 3))
686 if (hasSelection()) {
688 updateLocal(bv, SELECTION, false);
691 no_selection = false;
693 int const ocell = actcell;
694 int const orow = actrow;
699 updateLocal(bv, NONE, false);
700 sel_cell_start = sel_cell_end = actcell;
702 if ((ocell != actcell) && the_locking_inset) {
703 the_locking_inset->insetUnlock(bv);
704 the_locking_inset = 0;
710 bool const inset_hit = insetHit(bv, x, y);
712 if ((ocell == actcell) && the_locking_inset && inset_hit) {
714 the_locking_inset->insetButtonPress(bv,
715 x - inset_x, y - inset_y,
718 } else if (the_locking_inset) {
719 the_locking_inset->insetUnlock(bv);
721 the_locking_inset = 0;
723 localDispatch(bv, LFUN_PASTESELECTION, "paragraph");
726 if (inset_hit && bv->theLockingInset()) {
727 if (activateCellInsetAbs(bv, x, y, button))
728 the_locking_inset->insetButtonPress(bv,
738 void InsetTabular::insetButtonRelease(BufferView * bv,
739 int x, int y, int button)
742 if (the_locking_inset) {
744 if ((i = the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE))) {
745 i->insetButtonRelease(bv, x, y, button);
749 bv->owner()->getDialogs()->showTabular(this);
752 if (the_locking_inset) {
753 the_locking_inset->insetButtonRelease(bv,
754 x - inset_x, y - inset_y,
758 no_selection = false;
762 void InsetTabular::insetMotionNotify(BufferView * bv, int x, int y, int button)
764 if (the_locking_inset) {
765 the_locking_inset->insetMotionNotify(bv,
773 int const old_cell = actcell;
776 sel_cell_end = actcell;
777 if (sel_cell_end != old_cell)
778 updateLocal(bv, SELECTION, false);
781 no_selection = false;
785 void InsetTabular::insetKeyPress(XKeyEvent * xke)
787 if (the_locking_inset) {
788 the_locking_inset->insetKeyPress(xke);
794 UpdatableInset::RESULT
795 InsetTabular::localDispatch(BufferView * bv,
799 // We need to save the value of the_locking_inset as the call to
800 // the_locking_inset->LocalDispatch might unlock it.
801 old_locking_inset = the_locking_inset;
802 no_selection = false;
803 UpdatableInset::RESULT result =
804 UpdatableInset::localDispatch(bv, action, arg);
805 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
810 if ((action < 0) && arg.empty())
813 bool hs = hasSelection();
816 // this one have priority over the locked InsetText!
821 if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) != this)
824 if (the_locking_inset) {
825 unlockInsetInInset(bv, the_locking_inset);
826 the_locking_inset = 0;
828 if (action == LFUN_TAB)
829 moveNextCell(bv, old_locking_inset != 0);
831 movePrevCell(bv, old_locking_inset != 0);
832 sel_cell_start = sel_cell_end = actcell;
834 updateLocal(bv, SELECTION, false);
835 if (!the_locking_inset) {
837 return DISPATCHED_NOUPDATE;
841 // this to avoid compiler warnings.
846 if (the_locking_inset) {
847 result = the_locking_inset->localDispatch(bv, action, arg);
848 if (result == DISPATCHED_NOUPDATE) {
851 if (sc != scroll()) { // inset has been scrolled
852 the_locking_inset->toggleInsetCursor(bv);
853 updateLocal(bv, FULL, false);
854 the_locking_inset->toggleInsetCursor(bv);
857 } else if (result == DISPATCHED) {
858 the_locking_inset->toggleInsetCursor(bv);
859 updateLocal(bv, CELL, false);
860 the_locking_inset->toggleInsetCursor(bv);
862 } else if (result == FINISHED) {
869 // --- Cursor Movements ----------------------------------
871 if (tabular->IsLastCellInRow(actcell))
873 moveRight(bv, false);
874 sel_cell_end = actcell;
875 updateLocal(bv, SELECTION, false);
878 result = moveRight(bv);
879 sel_cell_start = sel_cell_end = actcell;
881 updateLocal(bv, SELECTION, false);
884 if (tabular->IsFirstCellInRow(actcell))
887 sel_cell_end = actcell;
888 updateLocal(bv, SELECTION, false);
891 result = moveLeft(bv);
892 sel_cell_start = sel_cell_end = actcell;
894 updateLocal(bv, SELECTION, false);
898 int const ocell = actcell;
900 if ((ocell == sel_cell_end) ||
901 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
902 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
904 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
905 updateLocal(bv, SELECTION, false);
909 result = moveDown(bv, old_locking_inset != 0);
910 sel_cell_start = sel_cell_end = actcell;
912 updateLocal(bv, SELECTION, false);
916 int const ocell = actcell;
918 if ((ocell == sel_cell_end) ||
919 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
920 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
922 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
923 updateLocal(bv, SELECTION, false);
927 result = moveUp(bv, old_locking_inset != 0);
928 sel_cell_start = sel_cell_end = actcell;
930 updateLocal(bv, SELECTION, false);
934 if (the_locking_inset) {
935 unlockInsetInInset(bv, the_locking_inset);
936 the_locking_inset = 0;
938 if (bv->text->first + bv->painter().paperHeight() <
939 (top_baseline + tabular->GetHeightOfTabular()))
941 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
942 updateLocal(bv, FULL, false);
943 actcell = tabular->GetCellBelow(first_visible_cell) + column;
945 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
948 updateLocal(bv, CURSOR, false);
953 if (the_locking_inset) {
954 unlockInsetInInset(bv, the_locking_inset);
955 the_locking_inset = 0;
957 if (top_baseline < 0) {
958 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
959 updateLocal(bv, FULL, false);
960 if (top_baseline > 0)
963 actcell = tabular->GetCellBelow(first_visible_cell) + column;
968 updateLocal(bv, CURSOR, false);
979 case LFUN_LAYOUT_TABULAR:
981 bv->owner()->getDialogs()->showTabular(this);
984 case LFUN_TABULAR_FEATURE:
985 if (!tabularFeatures(bv, arg))
986 result = UNDISPATCHED;
989 if (!copySelection(bv))
991 setUndo(bv, Undo::DELETE,
992 bv->text->cursor.par(),
993 bv->text->cursor.par()->next());
995 updateLocal(bv, INIT, true);
1003 case LFUN_PASTESELECTION:
1005 string const clip(bv->getClipboard());
1009 if (clip.find('\t') != string::npos) {
1013 unsigned int len = clip.length();
1014 string::size_type p = 0;
1017 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1025 maxCols = max(cols, maxCols);
1031 maxCols = max(cols, maxCols);
1032 delete paste_tabular;
1033 paste_tabular = new LyXTabular(this, rows, maxCols);
1034 string::size_type op = 0;
1036 int cells = paste_tabular->GetNumberOfCells();
1038 while((cell < cells) && (p < len) &&
1039 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1044 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1049 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1050 while(cols++ < maxCols)
1058 // check for the last cell if there is no trailing '\n'
1059 if ((cell < cells) && (op < len))
1060 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1062 // so that the clipboard is used and it goes on
1064 // and executes LFUN_PASTESELECTION in insettext!
1065 delete paste_tabular;
1070 if (hasPasteBuffer()) {
1071 setUndo(bv, Undo::INSERT,
1072 bv->text->cursor.par(),
1073 bv->text->cursor.par()->next());
1075 updateLocal(bv, INIT, true);
1078 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1080 // we try to activate the actual inset and put this event down to
1081 // the insets dispatch function.
1082 result = UNDISPATCHED;
1083 if (the_locking_inset)
1086 if (activateCellInset(bv)) {
1087 // reset need_update setted in above function!
1089 result = the_locking_inset->localDispatch(bv, action, arg);
1090 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1091 unlockInsetInInset(bv, the_locking_inset);
1093 the_locking_inset = 0;
1094 // we need to update if this was requested before
1095 updateLocal(bv, NONE, false);
1096 return UNDISPATCHED;
1099 // the_locking_inset->ToggleInsetCursor(bv);
1100 updateLocal(bv, CELL, false);
1101 // the_locking_inset->ToggleInsetCursor(bv);
1106 if (result!=FINISHED) {
1107 if (!the_locking_inset) {
1108 showInsetCursor(bv);
1111 bv->unlockInset(this);
1116 int InsetTabular::latex(Buffer const * buf, ostream & os,
1117 bool fragile, bool fp) const
1119 return tabular->Latex(buf, os, fragile, fp);
1123 int InsetTabular::ascii(Buffer const * buf, ostream & os, int) const
1125 // This should be changed to a real ascii export
1126 return tabular->Ascii(buf, os);
1130 int InsetTabular::linuxdoc(Buffer const *, ostream &) const
1136 int InsetTabular::docBook(Buffer const * buf, ostream & os) const
1138 return tabular->DocBook(buf,os);
1142 void InsetTabular::validate(LaTeXFeatures & features) const
1144 tabular->Validate(features);
1148 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1149 LyXFont const & font,
1156 bool changed = false;
1158 // if we have a locking_inset we should have to check only this cell for
1159 // change so I'll try this to have a boost, but who knows ;)
1160 if ((need_update != INIT) &&
1161 (the_locking_inset == tabular->GetCellInset(actcell))) {
1162 for(int i = 0; i < tabular->columns(); ++i) {
1163 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1165 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1168 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1169 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1170 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1176 for (int i = 0; i < tabular->rows(); ++i) {
1179 for (int j = 0; j < tabular->columns(); ++j) {
1180 if (tabular->IsPartOfMultiColumn(i,j))
1183 inset = tabular->GetCellInset(cell);
1185 inset->update(bv, font, false);
1186 maxAsc = max(maxAsc, inset->ascent(bv, font));
1187 maxDesc = max(maxDesc, inset->descent(bv, font));
1188 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1190 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1191 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1197 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1199 if (the_locking_inset) {
1200 the_locking_inset->getCursorPos(bv, x, y);
1203 x = cursor_.x() - top_x;
1208 void InsetTabular::toggleInsetCursor(BufferView * bv)
1210 if (the_locking_inset) {
1211 the_locking_inset->toggleInsetCursor(bv);
1215 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1217 int const asc = lyxfont::maxAscent(font);
1218 int const desc = lyxfont::maxDescent(font);
1220 if (isCursorVisible())
1221 bv->hideLockedInsetCursor();
1223 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1224 toggleCursorVisible();
1228 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1230 if (!isCursorVisible()) {
1231 LyXFont font; // = GetFont(par, cursor.pos);
1233 int const asc = lyxfont::maxAscent(font);
1234 int const desc = lyxfont::maxDescent(font);
1235 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1237 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1238 setCursorVisible(true);
1243 void InsetTabular::hideInsetCursor(BufferView * bv)
1245 if (isCursorVisible()) {
1246 bv->hideLockedInsetCursor();
1247 setCursorVisible(false);
1249 // if (cursor_visible)
1250 // ToggleInsetCursor(bv);
1254 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1258 actcell = actrow = actcol = 0;
1259 int ly = tabular->GetDescentOfRow(actrow);
1261 // first search the right row
1262 while((ly < y) && (actrow < tabular->rows())) {
1263 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1264 tabular->GetAscentOfRow(actrow + 1) +
1265 tabular->GetAdditionalHeight(actrow + 1));
1267 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1269 actcell = tabular->GetCellNumber(actrow, actcol);
1271 // now search the right column
1272 int lx = tabular->GetWidthOfColumn(actcell) -
1273 tabular->GetAdditionalWidth(actcell);
1275 #ifdef WITH_WARNINGS
1276 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1278 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1279 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1280 tabular->GetAdditionalWidth(actcell - 1));
1282 // Jürgen, you should check that this is correct. (Lgb)
1283 #warning Jürgen, please check. (Lgb)
1284 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1285 lx += tabular->GetWidthOfColumn(actcell + 1)
1286 + tabular->GetAdditionalWidth(actcell);
1290 cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1295 int InsetTabular::getCellXPos(int cell) const
1299 for (; !tabular->IsFirstCellInRow(c); --c)
1301 int lx = tabular->GetWidthOfColumn(cell);
1302 for (; c < cell; ++c) {
1303 lx += tabular->GetWidthOfColumn(c);
1305 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1309 void InsetTabular::resetPos(BufferView * bv) const
1313 actcol = tabular->column_of_cell(actcell);
1318 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1319 if (tabular->IsLastCellInRow(cell)) {
1320 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1321 tabular->GetAscentOfRow(actrow + 1) +
1322 tabular->GetAdditionalHeight(actrow + 1));
1326 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1327 int new_x = getCellXPos(actcell);
1328 int old_x = cursor_.x();
1331 // cursor.x(getCellXPos(actcell) + offset);
1332 if ((actcol < tabular->columns()-1) && scroll(false) &&
1333 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1336 updateLocal(bv, FULL, false);
1337 } else if (the_locking_inset &&
1338 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1340 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1341 if (xx > (bv->workWidth()-20)) {
1342 scroll(bv, -(xx - bv->workWidth() + 60));
1343 updateLocal(bv, FULL, false);
1344 } else if (xx < 20) {
1350 updateLocal(bv, FULL, false);
1352 } else if ((cursor_.x() - offset) > 20 &&
1353 (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1354 > (bv->workWidth() - 20)) {
1355 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1356 updateLocal(bv, FULL, false);
1357 } else if ((cursor_.x() - offset) < 20) {
1358 scroll(bv, 20 - cursor_.x() + offset);
1359 updateLocal(bv, FULL, false);
1360 } else if (scroll(false) && top_x > 20 &&
1361 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1362 scroll(bv, old_x - cursor_.x());
1363 updateLocal(bv, FULL, false);
1365 if ((!the_locking_inset ||
1366 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1367 actcell != oldcell) {
1368 InsetTabular * inset = const_cast<InsetTabular *>(this);
1369 bv->owner()->getDialogs()->updateTabular(inset);
1375 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1377 if (lock && !old_locking_inset) {
1378 if (activateCellInset(bv))
1381 bool moved = isRightToLeft(bv)
1382 ? movePrevCell(bv) : moveNextCell(bv);
1385 if (lock && activateCellInset(bv))
1389 return DISPATCHED_NOUPDATE;
1393 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1395 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1398 if (lock) { // behind the inset
1399 if (activateCellInset(bv, 0, 0, 0, true))
1403 return DISPATCHED_NOUPDATE;
1407 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1409 int const ocell = actcell;
1410 actcell = tabular->GetCellAbove(actcell);
1411 if (actcell == ocell) // we moved out of the inset
1417 if (old_locking_inset) {
1418 old_locking_inset->getCursorPos(bv, x, y);
1419 x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1421 if (activateCellInset(bv, x, 0))
1424 return DISPATCHED_NOUPDATE;
1428 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1430 int const ocell = actcell;
1431 actcell = tabular->GetCellBelow(actcell);
1432 if (actcell == ocell) // we moved out of the inset
1438 if (old_locking_inset) {
1439 old_locking_inset->getCursorPos(bv, x, y);
1440 x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1442 if (activateCellInset(bv, x, 0))
1445 return DISPATCHED_NOUPDATE;
1449 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1451 if (isRightToLeft(bv)) {
1452 if (tabular->IsFirstCellInRow(actcell)) {
1453 int row = tabular->row_of_cell(actcell);
1454 if (row == tabular->rows() - 1)
1456 actcell = tabular->GetLastCellInRow(row);
1457 actcell = tabular->GetCellBelow(actcell);
1464 if (tabular->IsLastCell(actcell))
1469 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1470 isRightToLeftPar(bv->buffer()->params);
1471 activateCellInset(bv, 0, 0, 0, !rtl);
1478 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1480 if (isRightToLeft(bv)) {
1481 if (tabular->IsLastCellInRow(actcell)) {
1482 int row = tabular->row_of_cell(actcell);
1485 actcell = tabular->GetFirstCellInRow(row);
1486 actcell = tabular->GetCellAbove(actcell);
1488 if (tabular->IsLastCell(actcell))
1493 if (!actcell) // first cell
1498 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1499 isRightToLeftPar(bv->buffer()->params);
1500 activateCellInset(bv, 0, 0, 0, !rtl);
1507 bool InsetTabular::deletable()
1513 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1518 sel_cell_end = tabular->GetNumberOfCells() - 1;
1520 if (hasSelection()) {
1521 setUndo(bv, Undo::EDIT,
1522 bv->text->cursor.par(),
1523 bv->text->cursor.par()->next());
1524 bool frozen = undo_frozen;
1527 // apply the fontchange on the whole selection
1532 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1533 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1534 for(int j = sel_col_start; j <= sel_col_end; ++j) {
1535 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1540 updateLocal(bv, INIT, true);
1542 if (the_locking_inset)
1543 the_locking_inset->setFont(bv, font, tall);
1547 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1549 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1552 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1553 string const tmp = tabularFeature[i].feature;
1555 if (tmp == what.substr(0, tmp.length())) {
1556 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1557 //tabularFeatures[i].feature.length())) {
1558 action = tabularFeature[i].action;
1562 if (action == LyXTabular::LAST_ACTION)
1566 frontStrip(what.substr(tabularFeature[i].feature.length()));
1567 tabularFeatures(bv, action, val);
1572 void InsetTabular::tabularFeatures(BufferView * bv,
1573 LyXTabular::Feature feature,
1574 string const & value)
1580 bool setLines = false;
1581 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1582 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1585 case LyXTabular::M_ALIGN_LEFT:
1586 case LyXTabular::ALIGN_LEFT:
1587 setAlign=LYX_ALIGN_LEFT;
1589 case LyXTabular::M_ALIGN_RIGHT:
1590 case LyXTabular::ALIGN_RIGHT:
1591 setAlign=LYX_ALIGN_RIGHT;
1593 case LyXTabular::M_ALIGN_CENTER:
1594 case LyXTabular::ALIGN_CENTER:
1595 setAlign=LYX_ALIGN_CENTER;
1597 case LyXTabular::M_VALIGN_TOP:
1598 case LyXTabular::VALIGN_TOP:
1599 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1601 case LyXTabular::M_VALIGN_BOTTOM:
1602 case LyXTabular::VALIGN_BOTTOM:
1603 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1605 case LyXTabular::M_VALIGN_CENTER:
1606 case LyXTabular::VALIGN_CENTER:
1607 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1612 if (hasSelection()) {
1613 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1615 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1616 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1618 setUndo(bv, Undo::FINISH,
1619 bv->text->cursor.par(),
1620 bv->text->cursor.par()->next());
1622 int row = tabular->row_of_cell(actcell);
1623 int column = tabular->column_of_cell(actcell);
1627 case LyXTabular::SET_PWIDTH:
1629 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1630 tabular->SetColumnPWidth(actcell,value);
1632 for (int i = 0; i < tabular->rows(); ++i) {
1633 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1636 updateLocal(bv, INIT, true);
1640 case LyXTabular::SET_MPWIDTH:
1642 bool const update = (tabular->GetPWidth(actcell) != value);
1643 tabular->SetMColumnPWidth(actcell,value);
1645 for (int i = 0; i < tabular->rows(); ++i) {
1646 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1649 updateLocal(bv, INIT, true);
1653 case LyXTabular::SET_SPECIAL_COLUMN:
1654 case LyXTabular::SET_SPECIAL_MULTI:
1655 tabular->SetAlignSpecial(actcell,value,feature);
1657 case LyXTabular::APPEND_ROW:
1658 // append the row into the tabular
1659 unlockInsetInInset(bv, the_locking_inset);
1660 tabular->AppendRow(actcell);
1661 updateLocal(bv, INIT, true);
1663 case LyXTabular::APPEND_COLUMN:
1664 // append the column into the tabular
1665 unlockInsetInInset(bv, the_locking_inset);
1666 tabular->AppendColumn(actcell);
1667 actcell = tabular->GetCellNumber(row, column);
1668 updateLocal(bv, INIT, true);
1670 case LyXTabular::DELETE_ROW:
1671 unlockInsetInInset(bv, the_locking_inset);
1672 tabular->DeleteRow(tabular->row_of_cell(actcell));
1673 if ((row+1) > tabular->rows())
1675 actcell = tabular->GetCellNumber(row, column);
1677 updateLocal(bv, INIT, true);
1679 case LyXTabular::DELETE_COLUMN:
1680 unlockInsetInInset(bv, the_locking_inset);
1681 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1682 if ((column+1) > tabular->columns())
1684 actcell = tabular->GetCellNumber(row, column);
1686 updateLocal(bv, INIT, true);
1688 case LyXTabular::M_TOGGLE_LINE_TOP:
1690 case LyXTabular::TOGGLE_LINE_TOP:
1692 bool lineSet = !tabular->TopLine(actcell, flag);
1693 for (int i = sel_row_start; i <= sel_row_end; ++i)
1694 for (int j = sel_col_start; j <= sel_col_end; ++j)
1695 tabular->SetTopLine(
1696 tabular->GetCellNumber(i, j),
1698 updateLocal(bv, INIT, true);
1702 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1704 case LyXTabular::TOGGLE_LINE_BOTTOM:
1706 bool lineSet = !tabular->BottomLine(actcell, flag);
1707 for (int i = sel_row_start; i <= sel_row_end; ++i)
1708 for (int j = sel_col_start; j <= sel_col_end; ++j)
1709 tabular->SetBottomLine(
1710 tabular->GetCellNumber(i, j),
1713 updateLocal(bv, INIT, true);
1717 case LyXTabular::M_TOGGLE_LINE_LEFT:
1719 case LyXTabular::TOGGLE_LINE_LEFT:
1721 bool lineSet = !tabular->LeftLine(actcell, flag);
1722 for (int i = sel_row_start; i <= sel_row_end; ++i)
1723 for (int j = sel_col_start; j <= sel_col_end; ++j)
1724 tabular->SetLeftLine(
1725 tabular->GetCellNumber(i,j),
1728 updateLocal(bv, INIT, true);
1732 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1734 case LyXTabular::TOGGLE_LINE_RIGHT:
1736 bool lineSet = !tabular->RightLine(actcell, flag);
1737 for (int i = sel_row_start; i <= sel_row_end; ++i)
1738 for (int j = sel_col_start; j <= sel_col_end; ++j)
1739 tabular->SetRightLine(
1740 tabular->GetCellNumber(i,j),
1743 updateLocal(bv, INIT, true);
1747 case LyXTabular::M_ALIGN_LEFT:
1748 case LyXTabular::M_ALIGN_RIGHT:
1749 case LyXTabular::M_ALIGN_CENTER:
1751 case LyXTabular::ALIGN_LEFT:
1752 case LyXTabular::ALIGN_RIGHT:
1753 case LyXTabular::ALIGN_CENTER:
1754 for (int i = sel_row_start; i <= sel_row_end; ++i)
1755 for (int j = sel_col_start; j <= sel_col_end; ++j)
1756 tabular->SetAlignment(
1757 tabular->GetCellNumber(i, j),
1760 updateLocal(bv, INIT, true);
1762 case LyXTabular::M_VALIGN_TOP:
1763 case LyXTabular::M_VALIGN_BOTTOM:
1764 case LyXTabular::M_VALIGN_CENTER:
1766 case LyXTabular::VALIGN_TOP:
1767 case LyXTabular::VALIGN_BOTTOM:
1768 case LyXTabular::VALIGN_CENTER:
1769 for (int i = sel_row_start; i <= sel_row_end; ++i)
1770 for (int j = sel_col_start; j <= sel_col_end; ++j)
1771 tabular->SetVAlignment(
1772 tabular->GetCellNumber(i, j),
1774 updateLocal(bv, INIT, true);
1776 case LyXTabular::MULTICOLUMN:
1778 if (sel_row_start != sel_row_end) {
1779 WriteAlert(_("Impossible Operation!"),
1780 _("Multicolumns can only be horizontally."),
1784 // just multicol for one Single Cell
1785 if (!hasSelection()) {
1786 // check wether we are completly in a multicol
1787 if (tabular->IsMultiColumn(actcell)) {
1788 tabular->UnsetMultiColumn(actcell);
1789 updateLocal(bv, INIT, true);
1791 tabular->SetMultiColumn(actcell, 1);
1792 updateLocal(bv, CELL, true);
1796 // we have a selection so this means we just add all this
1797 // cells to form a multicolumn cell
1801 if (sel_cell_start > sel_cell_end) {
1802 s_start = sel_cell_end;
1803 s_end = sel_cell_start;
1805 s_start = sel_cell_start;
1806 s_end = sel_cell_end;
1808 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1810 sel_cell_end = sel_cell_start;
1811 updateLocal(bv, INIT, true);
1814 case LyXTabular::SET_ALL_LINES:
1816 case LyXTabular::UNSET_ALL_LINES:
1817 for (int i = sel_row_start; i <= sel_row_end; ++i)
1818 for (int j = sel_col_start; j <= sel_col_end; ++j)
1819 tabular->SetAllLines(
1820 tabular->GetCellNumber(i,j), setLines);
1821 updateLocal(bv, INIT, true);
1823 case LyXTabular::SET_LONGTABULAR:
1824 tabular->SetLongTabular(true);
1825 updateLocal(bv, INIT, true); // because this toggles displayed
1827 case LyXTabular::UNSET_LONGTABULAR:
1828 tabular->SetLongTabular(false);
1829 updateLocal(bv, INIT, true); // because this toggles displayed
1831 case LyXTabular::SET_ROTATE_TABULAR:
1832 tabular->SetRotateTabular(true);
1834 case LyXTabular::UNSET_ROTATE_TABULAR:
1835 tabular->SetRotateTabular(false);
1837 case LyXTabular::SET_ROTATE_CELL:
1838 for (int i = sel_row_start; i <= sel_row_end; ++i)
1839 for (int j = sel_col_start; j<=sel_col_end; ++j)
1840 tabular->SetRotateCell(
1841 tabular->GetCellNumber(i, j),
1844 case LyXTabular::UNSET_ROTATE_CELL:
1845 for (int i = sel_row_start; i <= sel_row_end; ++i)
1846 for (int j = sel_col_start; j <= sel_col_end; ++j)
1847 tabular->SetRotateCell(
1848 tabular->GetCellNumber(i, j), false);
1850 case LyXTabular::SET_USEBOX:
1852 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1853 if (val == tabular->GetUsebox(actcell))
1854 val = LyXTabular::BOX_NONE;
1855 for (int i = sel_row_start; i <= sel_row_end; ++i)
1856 for (int j = sel_col_start; j <= sel_col_end; ++j)
1858 tabular->GetCellNumber(i, j), val);
1861 case LyXTabular::SET_LTFIRSTHEAD:
1862 tabular->SetLTHead(actcell, true);
1864 case LyXTabular::SET_LTHEAD:
1865 tabular->SetLTHead(actcell, false);
1867 case LyXTabular::SET_LTFOOT:
1868 tabular->SetLTFoot(actcell, false);
1870 case LyXTabular::SET_LTLASTFOOT:
1871 tabular->SetLTFoot(actcell, true);
1873 case LyXTabular::SET_LTNEWPAGE:
1875 bool what = !tabular->GetLTNewPage(actcell);
1876 tabular->SetLTNewPage(actcell, what);
1879 // dummy stuff just to avoid warnings
1880 case LyXTabular::LAST_ACTION:
1886 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
1889 UpdatableInset * inset =
1890 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1891 LyXFont font(LyXFont::ALL_SANE);
1893 x = inset->x() + inset->width(bv, font);
1894 y = inset->descent(bv, font);
1896 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1897 //inset_y = cursor.y();
1898 inset->edit(bv, x, y, button);
1899 if (!the_locking_inset)
1901 updateLocal(bv, CELL, false);
1902 return (the_locking_inset != 0);
1906 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1909 inset_x = cursor_.x()
1910 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1911 inset_y = cursor_.y();
1912 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1916 bool InsetTabular::insetHit(BufferView *, int x, int) const
1919 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
1923 // This returns paperWidth() if the cell-width is unlimited or the width
1924 // in pixels if we have a pwidth for this cell.
1925 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
1927 string const s = tabular->GetPWidth(cell);
1931 return VSpace(s).inPixels(bv);
1935 int InsetTabular::getMaxWidth(BufferView * bv,
1936 UpdatableInset const * inset) const
1938 int cell = tabular->cur_cell;
1939 if (tabular->GetCellInset(cell) != inset) {
1941 if (tabular->GetCellInset(cell) != inset) {
1943 lyxerr << "Actcell not equal to actual cell!\n";
1948 int const n = tabular->GetNumberOfCells();
1951 for (cell = 0; cell < n; ++cell) {
1952 if (tabular->GetCellInset(cell) == inset)
1958 lyxerr << "Own inset not found, shouldn't really happen!\n";
1962 int w = getMaxWidthOfCell(bv, cell);
1964 // because the inset then subtracts it's top_x and owner->x()
1965 w += (inset->x() - top_x);
1972 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1974 resizeLyXText(bv, recursive);
1978 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
1981 for(int i = 0; i < tabular->rows(); ++i) {
1982 for(int j = 0; j < tabular->columns(); ++j) {
1983 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
1991 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1992 bool const recursive) const
1994 if (the_locking_inset)
1995 return the_locking_inset->getLyXText(bv, recursive);
1997 // if we're locked lock the actual insettext and return it's LyXText!!!
1999 UpdatableInset * inset =
2000 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2001 inset->edit(const_cast<BufferView *>(bv), 0, 0, 0);
2002 return the_locking_inset->getLyXText(bv, recursive);
2005 return Inset::getLyXText(bv, recursive);
2009 bool InsetTabular::showInsetDialog(BufferView * bv) const
2011 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2012 bv->owner()->getDialogs()
2013 ->showTabular(const_cast<InsetTabular *>(this));
2018 void InsetTabular::openLayoutDialog(BufferView * bv) const
2020 if (the_locking_inset) {
2021 InsetTabular * i = static_cast<InsetTabular *>
2022 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2024 i->openLayoutDialog(bv);
2028 bv->owner()->getDialogs()->showTabular(
2029 const_cast<InsetTabular *>(this));
2034 // functions returns:
2038 // 3 ... toggled off
2040 func_status::value_type InsetTabular::getStatus(string const & what) const
2042 int action = LyXTabular::LAST_ACTION;
2043 func_status::value_type status = func_status::OK;
2046 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2047 string const tmp = tabularFeature[i].feature;
2048 if (tmp == what.substr(0, tmp.length())) {
2049 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2050 // tabularFeatures[i].feature.length())) {
2051 action = tabularFeature[i].action;
2055 if (action == LyXTabular::LAST_ACTION)
2056 return func_status::Unknown;
2058 string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2065 if (hasSelection()) {
2066 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2068 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2072 case LyXTabular::SET_PWIDTH:
2073 case LyXTabular::SET_MPWIDTH:
2074 case LyXTabular::SET_SPECIAL_COLUMN:
2075 case LyXTabular::SET_SPECIAL_MULTI:
2076 status |= func_status::Disabled;
2079 case LyXTabular::APPEND_ROW:
2080 case LyXTabular::APPEND_COLUMN:
2081 case LyXTabular::DELETE_ROW:
2082 case LyXTabular::DELETE_COLUMN:
2083 case LyXTabular::SET_ALL_LINES:
2084 case LyXTabular::UNSET_ALL_LINES:
2085 status |= func_status::OK;
2088 case LyXTabular::MULTICOLUMN:
2089 if (tabular->IsMultiColumn(actcell))
2090 status |= func_status::ToggleOn;
2092 status |= func_status::ToggleOff;
2094 case LyXTabular::M_TOGGLE_LINE_TOP:
2096 case LyXTabular::TOGGLE_LINE_TOP:
2097 if (tabular->TopLine(actcell, flag))
2098 status |= func_status::ToggleOn;
2100 status |= func_status::ToggleOff;
2102 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2104 case LyXTabular::TOGGLE_LINE_BOTTOM:
2105 if (tabular->BottomLine(actcell, flag))
2106 status |= func_status::ToggleOn;
2108 status |= func_status::ToggleOff;
2110 case LyXTabular::M_TOGGLE_LINE_LEFT:
2112 case LyXTabular::TOGGLE_LINE_LEFT:
2113 if (tabular->LeftLine(actcell, flag))
2114 status |= func_status::ToggleOn;
2116 status |= func_status::ToggleOff;
2118 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2120 case LyXTabular::TOGGLE_LINE_RIGHT:
2121 if (tabular->RightLine(actcell, flag))
2122 status |= func_status::ToggleOn;
2124 status |= func_status::ToggleOff;
2126 case LyXTabular::M_ALIGN_LEFT:
2128 case LyXTabular::ALIGN_LEFT:
2129 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2130 status |= func_status::ToggleOn;
2132 status |= func_status::ToggleOff;
2134 case LyXTabular::M_ALIGN_RIGHT:
2136 case LyXTabular::ALIGN_RIGHT:
2137 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2138 status |= func_status::ToggleOn;
2140 status |= func_status::ToggleOff;
2142 case LyXTabular::M_ALIGN_CENTER:
2144 case LyXTabular::ALIGN_CENTER:
2145 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2146 status |= func_status::ToggleOn;
2148 status |= func_status::ToggleOff;
2150 case LyXTabular::M_VALIGN_TOP:
2152 case LyXTabular::VALIGN_TOP:
2153 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2154 status |= func_status::ToggleOn;
2156 status |= func_status::ToggleOff;
2158 case LyXTabular::M_VALIGN_BOTTOM:
2160 case LyXTabular::VALIGN_BOTTOM:
2161 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2162 status |= func_status::ToggleOn;
2164 status |= func_status::ToggleOff;
2166 case LyXTabular::M_VALIGN_CENTER:
2168 case LyXTabular::VALIGN_CENTER:
2169 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2170 status |= func_status::ToggleOn;
2172 status |= func_status::ToggleOff;
2174 case LyXTabular::SET_LONGTABULAR:
2175 if (tabular->IsLongTabular())
2176 status |= func_status::ToggleOn;
2178 status |= func_status::ToggleOff;
2180 case LyXTabular::UNSET_LONGTABULAR:
2181 if (!tabular->IsLongTabular())
2182 status |= func_status::ToggleOn;
2184 status |= func_status::ToggleOff;
2186 case LyXTabular::SET_ROTATE_TABULAR:
2187 if (tabular->GetRotateTabular())
2188 status |= func_status::ToggleOn;
2190 status |= func_status::ToggleOff;
2192 case LyXTabular::UNSET_ROTATE_TABULAR:
2193 if (!tabular->GetRotateTabular())
2194 status |= func_status::ToggleOn;
2196 status |= func_status::ToggleOff;
2198 case LyXTabular::SET_ROTATE_CELL:
2199 if (tabular->GetRotateCell(actcell))
2200 status |= func_status::ToggleOn;
2202 status |= func_status::ToggleOff;
2204 case LyXTabular::UNSET_ROTATE_CELL:
2205 if (!tabular->GetRotateCell(actcell))
2206 status |= func_status::ToggleOn;
2208 status |= func_status::ToggleOff;
2210 case LyXTabular::SET_USEBOX:
2211 if (strToInt(argument) == tabular->GetUsebox(actcell))
2212 status |= func_status::ToggleOn;
2214 status |= func_status::ToggleOff;
2216 case LyXTabular::SET_LTFIRSTHEAD:
2217 if (tabular->GetRowOfLTHead(actcell, dummy))
2218 status |= func_status::ToggleOn;
2220 status |= func_status::ToggleOff;
2222 case LyXTabular::SET_LTHEAD:
2223 if (tabular->GetRowOfLTHead(actcell, dummy))
2224 status |= func_status::ToggleOn;
2226 status |= func_status::ToggleOff;
2228 case LyXTabular::SET_LTFOOT:
2229 if (tabular->GetRowOfLTFoot(actcell, dummy))
2230 status |= func_status::ToggleOn;
2232 status |= func_status::ToggleOff;
2234 case LyXTabular::SET_LTLASTFOOT:
2235 if (tabular->GetRowOfLTFoot(actcell, dummy))
2236 status |= func_status::ToggleOn;
2238 status |= func_status::ToggleOff;
2240 case LyXTabular::SET_LTNEWPAGE:
2241 if (tabular->GetLTNewPage(actcell))
2242 status |= func_status::ToggleOn;
2244 status |= func_status::ToggleOff;
2247 status = func_status::Disabled;
2254 std::vector<string> const InsetTabular::getLabelList() const
2256 return tabular->getLabelList();
2260 bool InsetTabular::copySelection(BufferView * bv)
2262 if (!hasSelection())
2265 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2266 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2267 if (sel_col_start > sel_col_end) {
2268 sel_col_start = sel_col_end;
2269 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2271 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2273 int const columns = sel_col_end - sel_col_start + 1;
2275 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2276 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2277 if (sel_row_start > sel_row_end) {
2278 swap(sel_row_start, sel_row_end);
2280 int const rows = sel_row_end - sel_row_start + 1;
2282 delete paste_tabular;
2283 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2284 for (int i = 0; i < sel_row_start; ++i)
2285 paste_tabular->DeleteRow(0);
2286 while(paste_tabular->rows() > rows)
2287 paste_tabular->DeleteRow(rows);
2288 paste_tabular->SetTopLine(0, true, true);
2289 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2291 for (int i = 0; i < sel_col_start; ++i)
2292 paste_tabular->DeleteColumn(0);
2293 while (paste_tabular->columns() > columns)
2294 paste_tabular->DeleteColumn(columns);
2295 paste_tabular->SetLeftLine(0, true, true);
2296 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2300 paste_tabular->Ascii(bv->buffer(), sstr);
2301 bv->stuffClipboard(sstr.str().c_str());
2306 bool InsetTabular::pasteSelection(BufferView * bv)
2311 for (int r1 = 0, r2 = actrow;
2312 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2314 for(int c1 = 0, c2 = actcol;
2315 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2317 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2318 tabular->IsPartOfMultiColumn(r2,c2))
2320 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2324 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2328 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2329 int const n2 = tabular->GetCellNumber(r2, c2);
2330 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2331 tabular->GetCellInset(n2)->setOwner(this);
2332 tabular->GetCellInset(n2)->deleteLyXText(bv);
2339 bool InsetTabular::cutSelection()
2341 if (!hasSelection())
2344 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2345 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2346 if (sel_col_start > sel_col_end) {
2347 sel_col_start = sel_col_end;
2348 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2350 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2352 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2353 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2354 if (sel_row_start > sel_row_end) {
2355 swap(sel_row_start, sel_row_end);
2357 if (sel_cell_start > sel_cell_end) {
2358 swap(sel_cell_start, sel_cell_end);
2360 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2361 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2362 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2369 bool InsetTabular::isRightToLeft(BufferView *bv )
2371 return bv->getParentLanguage(this)->RightToLeft();
2375 bool InsetTabular::nodraw() const
2377 if (!UpdatableInset::nodraw() && the_locking_inset)
2378 return the_locking_inset->nodraw();
2379 return UpdatableInset::nodraw();
2383 int InsetTabular::scroll(bool recursive) const
2385 int sx = UpdatableInset::scroll(false);
2387 if (recursive && the_locking_inset)
2388 sx += the_locking_inset->scroll(recursive);
2394 bool InsetTabular::doClearArea() const
2396 return !locked || (need_update & (FULL|INIT));
2400 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2402 srow = tabular->row_of_cell(sel_cell_start);
2403 erow = tabular->row_of_cell(sel_cell_end);
2407 scol = tabular->column_of_cell(sel_cell_start);
2408 ecol = tabular->column_of_cell(sel_cell_end);
2412 ecol = tabular->right_column_of_cell(sel_cell_end);
2416 Paragraph * InsetTabular::getParFromID(int id) const
2419 for(int i = 0; i < tabular->rows(); ++i) {
2420 for(int j = 0; j < tabular->columns(); ++j) {
2421 if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2429 Paragraph * InsetTabular::firstParagraph() const
2431 if (the_locking_inset)
2432 return the_locking_inset->firstParagraph();
2437 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2439 if (the_locking_inset)
2440 return the_locking_inset->cursor(bv);
2441 return Inset::cursor(bv);
2445 Inset * InsetTabular::getInsetFromID(int id_arg) const
2448 return const_cast<InsetTabular *>(this);
2451 for(int i=0; i < tabular->rows(); ++i) {
2452 for(int j=0; j < tabular->columns(); ++j) {
2453 if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2461 string const InsetTabular::selectNextWord(BufferView * bv, float & value) const
2463 if (the_locking_inset) {
2464 string const str(the_locking_inset->selectNextWord(bv, value));
2467 if (tabular->IsLastCell(actcell)) {
2468 bv->unlockInset(const_cast<InsetTabular *>(this));
2474 // otherwise we have to lock the next inset and ask for it's selecttion
2475 UpdatableInset * inset =
2476 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2477 inset->edit(bv, 0, 0, 0);
2478 string const str(selectNextWordInt(bv, value));
2485 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2487 // when entering this function the inset should be ALWAYS locked!
2488 lyx::Assert(the_locking_inset);
2490 string const str(the_locking_inset->selectNextWord(bv, value));
2494 if (tabular->IsLastCell(actcell)) {
2495 bv->unlockInset(const_cast<InsetTabular *>(this));
2499 // otherwise we have to lock the next inset and ask for it's selecttion
2500 UpdatableInset * inset =
2501 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2502 inset->edit(bv, 0, 0, 0);
2503 return selectNextWordInt(bv, value);
2507 void InsetTabular::selectSelectedWord(BufferView * bv)
2509 if (the_locking_inset) {
2510 the_locking_inset->selectSelectedWord(bv);
2517 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2519 if (the_locking_inset) {
2520 the_locking_inset->toggleSelection(bv, kill_selection);
2525 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2526 bool const & cs, bool const & mw)
2528 if (the_locking_inset) {
2529 if (the_locking_inset->searchForward(bv, str, cs, mw))
2531 if (tabular->IsLastCell(actcell)) {
2532 bv->unlockInset(const_cast<InsetTabular *>(this));
2538 // otherwise we have to lock the next inset and search there
2539 UpdatableInset * inset =
2540 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2542 bool const res = searchForward(bv, str, cs, mw);
2543 updateLocal(bv, NONE, false);
2545 bv->updateInset(const_cast<InsetTabular *>(this), false);
2550 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2551 bool const & cs, bool const & mw)
2553 if (the_locking_inset) {
2554 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2556 if (!actcell) { // we are already in the first cell
2557 bv->unlockInset(const_cast<InsetTabular *>(this));
2563 // otherwise we have to lock the next inset and search there
2564 UpdatableInset * inset =
2565 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2566 inset->edit(bv, false);
2567 bool const res = searchBackward(bv, str, cs, mw);
2569 bv->updateInset(const_cast<InsetTabular *>(this), false);