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"
44 #include "support/lstrings.h"
55 const int ADD_TO_HEIGHT = 2;
56 const int ADD_TO_TABULAR_WIDTH = 2;
58 LyXTabular * paste_tabular = 0;
61 struct TabularFeature {
62 LyXTabular::Feature action;
66 //tabular_features * tabularFeatures = 0;
68 TabularFeature tabularFeature[] =
70 { LyXTabular::APPEND_ROW, "append-row" },
71 { LyXTabular::APPEND_COLUMN, "append-column" },
72 { LyXTabular::DELETE_ROW, "delete-row" },
73 { LyXTabular::DELETE_COLUMN, "delete-column" },
74 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
75 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
76 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
77 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
78 { LyXTabular::ALIGN_LEFT, "align-left" },
79 { LyXTabular::ALIGN_RIGHT, "align-right" },
80 { LyXTabular::ALIGN_CENTER, "align-center" },
81 { LyXTabular::VALIGN_TOP, "valign-top" },
82 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
83 { LyXTabular::VALIGN_CENTER, "valign-center" },
84 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
85 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
86 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
87 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
88 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
89 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
90 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
91 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
92 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
93 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
94 { LyXTabular::MULTICOLUMN, "multicolumn" },
95 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
96 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
97 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
98 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
99 { LyXTabular::SET_PWIDTH, "set-pwidth" },
100 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
101 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
102 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
103 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
104 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
105 { LyXTabular::SET_USEBOX, "set-usebox" },
106 { LyXTabular::SET_LTHEAD, "set-lthead" },
107 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
108 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
109 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
110 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
111 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
112 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
113 { LyXTabular::LAST_ACTION, "" }
119 bool InsetTabular::hasPasteBuffer() const
121 return (paste_tabular != 0);
125 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
132 tabular.reset(new LyXTabular(this, rows, columns));
133 // for now make it always display as display() inset
135 the_locking_inset = 0;
136 locked = no_selection = false;
138 actrow = actcell = 0;
144 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
146 : UpdatableInset(tab, same_id), buffer(&buf)
148 tabular.reset(new LyXTabular(this, *(tab.tabular)));
149 the_locking_inset = 0;
150 locked = no_selection = false;
152 actrow = actcell = 0;
153 sel_cell_start = sel_cell_end = 0;
158 InsetTabular::~InsetTabular()
164 Inset * InsetTabular::clone(Buffer const & buf, bool same_id) const
166 return new InsetTabular(*this, buf, same_id);
170 void InsetTabular::write(Buffer const * buf, ostream & os) const
172 os << " Tabular" << endl;
173 tabular->Write(buf, os);
177 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
179 bool const old_format = (lex.GetString() == "\\LyXTable");
183 //tabular = new LyXTabular(buf, this, lex);
184 tabular.reset(new LyXTabular(buf, this, lex));
192 string token = lex.GetString();
193 while (lex.IsOK() && (token != "\\end_inset")) {
195 token = lex.GetString();
197 if (token != "\\end_inset") {
198 lex.printError("Missing \\end_inset at this point. "
204 int InsetTabular::ascent(BufferView *, LyXFont const &) const
206 return tabular->GetAscentOfRow(0);
210 int InsetTabular::descent(BufferView *, LyXFont const &) const
212 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
216 int InsetTabular::width(BufferView *, LyXFont const &) const
218 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
222 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
223 float & x, bool cleared) const
231 if (need_update == INIT) {
232 if (calculate_dimensions_of_cells(bv, font, true))
233 bv->text->status = LyXText::CHANGED_IN_DRAW;
238 Painter & pain = bv->painter();
244 UpdatableInset::draw(bv, font, baseline, x, cleared);
247 x += static_cast<float>(scroll());
249 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
250 (top_x != int(x)) || (top_baseline != baseline))) {
251 int h = ascent(bv, font) + descent(bv, font);
252 int const tx = display() || !owner() ? 0 : top_x;
253 int w = tx ? width(bv, font) : pain.paperWidth();
254 int ty = baseline - ascent(bv, font);
258 if ((ty + h) > pain.paperHeight())
259 h = pain.paperHeight();
260 if ((top_x + w) > pain.paperWidth())
261 w = pain.paperWidth();
262 pain.fillRectangle(tx, ty, w, h, backgroundColor());
267 top_baseline = baseline;
268 x += ADD_TO_TABULAR_WIDTH;
272 first_visible_cell = -1;
273 for (i = 0; i < tabular->rows(); ++i) {
275 cell = tabular->GetCellNumber(i, 0);
276 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
277 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
279 baseline += tabular->GetDescentOfRow(i) +
280 tabular->GetAscentOfRow(i + 1) +
281 tabular->GetAdditionalHeight(i + 1);
284 for (j = 0; j < tabular->columns(); ++j) {
285 if (nx > bv->workWidth())
287 if (tabular->IsPartOfMultiColumn(i, j))
289 cx = nx + tabular->GetBeginningOfTextInCell(cell);
290 if (first_visible_cell < 0)
291 first_visible_cell = cell;
293 drawCellSelection(pain, nx, baseline, i, j, cell);
295 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
297 drawCellLines(pain, nx, baseline, i, cell);
298 nx += tabular->GetWidthOfColumn(cell);
301 baseline += tabular->GetDescentOfRow(i) +
302 tabular->GetAscentOfRow(i + 1) +
303 tabular->GetAdditionalHeight(i + 1);
305 } else if (need_update == CELL) {
308 if (the_locking_inset &&
309 tabular->GetCellInset(actcell) != the_locking_inset)
311 Inset * inset = tabular->GetCellInset(cell);
313 inset != the_locking_inset && i < tabular->rows();
316 inset != the_locking_inset
317 && j < tabular->columns();
319 if (tabular->IsPartOfMultiColumn(i, j))
321 nx += tabular->GetWidthOfColumn(cell);
323 inset = tabular->GetCellInset(cell);
325 if (tabular->row_of_cell(cell) > i) {
327 baseline += tabular->GetDescentOfRow(i) +
328 tabular->GetAscentOfRow(i + 1) +
329 tabular->GetAdditionalHeight(i + 1);
333 // compute baseline for actual row
334 for (i = 0; i < actrow; ++i) {
335 baseline += tabular->GetDescentOfRow(i) +
336 tabular->GetAscentOfRow(i + 1) +
337 tabular->GetAdditionalHeight(i + 1);
339 // now compute the right x position
340 cell = tabular->GetCellNumber(actrow, 0);
341 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
342 if (tabular->IsPartOfMultiColumn(actrow, j))
344 nx += tabular->GetWidthOfColumn(cell);
348 i = tabular->row_of_cell(cell);
349 if (the_locking_inset != tabular->GetCellInset(cell)) {
350 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
353 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
355 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
356 tabular->GetCellInset(cell)->draw(bv, font, baseline, dx, false);
358 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
361 // clear only if we didn't have a change
362 if (need_update == CELL) {
363 // clear before the inset
366 baseline - tabular->GetAscentOfRow(i) + 1,
368 tabular->GetAscentOfRow(i) +
369 tabular->GetDescentOfRow(i) - 1,
371 // clear behind the inset
373 int(cx + the_locking_inset->width(bv,font) + 1),
374 baseline - tabular->GetAscentOfRow(i) + 1,
375 tabular->GetWidthOfColumn(cell) -
376 tabular->GetBeginningOfTextInCell(cell) -
377 the_locking_inset->width(bv,font) -
378 tabular->GetAdditionalWidth(cell) - 1,
379 tabular->GetAscentOfRow(i) +
380 tabular->GetDescentOfRow(i) - 1,
384 x -= ADD_TO_TABULAR_WIDTH;
385 x += width(bv, font);
386 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
388 for(Inset * inset=owner(); inset; ++i)
389 inset = inset->owner();
390 if (calculate_dimensions_of_cells(bv, font, false))
398 void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
399 int row, int cell) const
401 int x2 = x + tabular->GetWidthOfColumn(cell);
404 if (!tabular->TopAlreadyDrawed(cell)) {
405 on_off = !tabular->TopLine(cell);
406 pain.line(x, baseline - tabular->GetAscentOfRow(row),
407 x2, baseline - tabular->GetAscentOfRow(row),
408 on_off ? LColor::tabularonoffline : LColor::tabularline,
409 on_off ? Painter::line_onoffdash : Painter::line_solid);
411 on_off = !tabular->BottomLine(cell);
412 pain.line(x,baseline + tabular->GetDescentOfRow(row),
413 x2, baseline + tabular->GetDescentOfRow(row),
414 on_off ? LColor::tabularonoffline : LColor::tabularline,
415 on_off ? Painter::line_onoffdash : Painter::line_solid);
416 if (!tabular->LeftAlreadyDrawed(cell)) {
417 on_off = !tabular->LeftLine(cell);
418 pain.line(x, baseline - tabular->GetAscentOfRow(row),
419 x, baseline + tabular->GetDescentOfRow(row),
420 on_off ? LColor::tabularonoffline : LColor::tabularline,
421 on_off ? Painter::line_onoffdash : Painter::line_solid);
423 on_off = !tabular->RightLine(cell);
424 pain.line(x2 - tabular->GetAdditionalWidth(cell),
425 baseline - tabular->GetAscentOfRow(row),
426 x2 - tabular->GetAdditionalWidth(cell),
427 baseline + tabular->GetDescentOfRow(row),
428 on_off ? LColor::tabularonoffline : LColor::tabularline,
429 on_off ? Painter::line_onoffdash : Painter::line_solid);
433 void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
434 int row, int column, int cell) const
436 int cs = tabular->column_of_cell(sel_cell_start);
437 int ce = tabular->column_of_cell(sel_cell_end);
440 cs = tabular->column_of_cell(sel_cell_end);
442 ce = tabular->right_column_of_cell(sel_cell_end);
445 int rs = tabular->row_of_cell(sel_cell_start);
446 int re = tabular->row_of_cell(sel_cell_end);
447 if (rs > re) swap(rs, re);
449 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
450 int w = tabular->GetWidthOfColumn(cell);
451 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
452 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
453 w, h, LColor::selection);
458 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
462 calculate_dimensions_of_cells(bv, font, true);
464 owner()->update(bv, font, true);
467 if (the_locking_inset)
468 the_locking_inset->update(bv, font, reinit);
469 if (need_update < FULL &&
470 bv->text->status() == LyXText::NEED_MORE_REFRESH)
475 switch (need_update) {
479 if (calculate_dimensions_of_cells(bv, font, false))
491 string const InsetTabular::editMessage() const
493 return _("Opened Tabular Inset");
497 void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
499 UpdatableInset::edit(bv, x, y, button);
501 if (!bv->lockInset(this)) {
502 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
506 the_locking_inset = 0;
510 sel_cell_start = sel_cell_end = actcell;
512 if (insetHit(bv, x, y) && (button != 3)) {
513 activateCellInsetAbs(bv, x, y, button);
518 void InsetTabular::edit(BufferView * bv, bool front)
520 UpdatableInset::edit(bv, front);
522 if (!bv->lockInset(this)) {
523 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
527 the_locking_inset = 0;
533 actcell = tabular->GetNumberOfCells() - 1;
534 sel_cell_start = sel_cell_end = actcell;
540 void InsetTabular::insetUnlock(BufferView * bv)
542 if (the_locking_inset) {
543 the_locking_inset->insetUnlock(bv);
544 the_locking_inset = 0;
547 no_selection = false;
550 if (scroll(false) || hasSelection()) {
551 sel_cell_start = sel_cell_end = 0;
555 updateLocal(bv, FULL, false);
560 void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
561 bool mark_dirty) const
563 if (need_update < what) // only set this if it has greater update
565 if ((what == INIT) && hasSelection())
568 if (need_update != NONE) {
569 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
570 if (locked) // && (what != NONE))
576 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
578 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
583 if (inset == tabular->GetCellInset(actcell)) {
584 lyxerr[Debug::INSETS] << "OK" << endl;
585 the_locking_inset = tabular->GetCellInset(actcell);
587 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
588 inset_y = cursor_.y();
590 } else if (the_locking_inset && (the_locking_inset == inset)) {
591 lyxerr[Debug::INSETS] << "OK" << endl;
593 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
594 inset_y = cursor_.y();
595 } else if (the_locking_inset) {
596 lyxerr[Debug::INSETS] << "MAYBE" << endl;
597 return the_locking_inset->lockInsetInInset(bv, inset);
599 lyxerr[Debug::INSETS] << "NOT OK" << endl;
604 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
607 if (!the_locking_inset)
609 if (the_locking_inset == inset) {
610 the_locking_inset->insetUnlock(bv);
611 the_locking_inset = 0;
613 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
619 updateLocal(bv, CELL, false);
620 showInsetCursor(bv, false);
623 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
624 if (inset->lyxCode() == TABULAR_CODE &&
625 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
626 bv->owner()->getDialogs()->updateTabular(this);
635 bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
637 if (!the_locking_inset)
639 if (the_locking_inset != inset) {
640 if (!the_locking_inset->updateInsetInInset(bv, inset))
643 updateLocal(bv, CELL, false);
648 unsigned int InsetTabular::insetInInsetY()
650 if (!the_locking_inset)
653 return inset_y + the_locking_inset->insetInInsetY();
657 UpdatableInset * InsetTabular::getLockingInset() const
659 return the_locking_inset ? the_locking_inset->getLockingInset() :
660 const_cast<InsetTabular *>(this);
664 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(Inset::Code c)
668 if (the_locking_inset)
669 return the_locking_inset->getFirstLockingInsetOfType(c);
674 bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
676 if (the_locking_inset)
677 return the_locking_inset->insertInset(bv, inset);
682 void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
684 if (hasSelection() && (button == 3))
687 if (hasSelection()) {
689 updateLocal(bv, SELECTION, false);
692 no_selection = false;
694 int const ocell = actcell;
695 int const orow = actrow;
700 updateLocal(bv, NONE, false);
701 sel_cell_start = sel_cell_end = actcell;
703 if ((ocell != actcell) && the_locking_inset) {
704 the_locking_inset->insetUnlock(bv);
705 the_locking_inset = 0;
711 bool const inset_hit = insetHit(bv, x, y);
713 if ((ocell == actcell) && the_locking_inset && inset_hit) {
715 the_locking_inset->insetButtonPress(bv,
716 x - inset_x, y - inset_y,
719 } else if (the_locking_inset) {
720 the_locking_inset->insetUnlock(bv);
722 the_locking_inset = 0;
724 localDispatch(bv, LFUN_PASTESELECTION, "paragraph");
727 if (inset_hit && bv->theLockingInset()) {
728 if (activateCellInsetAbs(bv, x, y, button))
729 the_locking_inset->insetButtonPress(bv,
739 void InsetTabular::insetButtonRelease(BufferView * bv,
740 int x, int y, int button)
743 if (the_locking_inset) {
745 if ((i = the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE))) {
746 i->insetButtonRelease(bv, x, y, button);
750 bv->owner()->getDialogs()->showTabular(this);
753 if (the_locking_inset) {
754 the_locking_inset->insetButtonRelease(bv,
755 x - inset_x, y - inset_y,
759 no_selection = false;
763 void InsetTabular::insetMotionNotify(BufferView * bv, int x, int y, int button)
765 if (the_locking_inset) {
766 the_locking_inset->insetMotionNotify(bv,
774 int const old_cell = actcell;
777 sel_cell_end = actcell;
778 if (sel_cell_end != old_cell)
779 updateLocal(bv, SELECTION, false);
782 no_selection = false;
786 void InsetTabular::insetKeyPress(XKeyEvent * xke)
788 if (the_locking_inset) {
789 the_locking_inset->insetKeyPress(xke);
795 UpdatableInset::RESULT
796 InsetTabular::localDispatch(BufferView * bv,
800 // We need to save the value of the_locking_inset as the call to
801 // the_locking_inset->LocalDispatch might unlock it.
802 old_locking_inset = the_locking_inset;
803 no_selection = false;
804 UpdatableInset::RESULT result =
805 UpdatableInset::localDispatch(bv, action, arg);
806 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
811 if ((action < 0) && arg.empty())
814 bool hs = hasSelection();
817 // this one have priority over the locked InsetText!
822 if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) != this)
825 if (the_locking_inset) {
826 unlockInsetInInset(bv, the_locking_inset);
827 the_locking_inset = 0;
829 if (action == LFUN_TAB)
830 moveNextCell(bv, old_locking_inset != 0);
832 movePrevCell(bv, old_locking_inset != 0);
833 sel_cell_start = sel_cell_end = actcell;
835 updateLocal(bv, SELECTION, false);
836 if (!the_locking_inset) {
838 return DISPATCHED_NOUPDATE;
842 // this to avoid compiler warnings.
847 if (the_locking_inset) {
848 result = the_locking_inset->localDispatch(bv, action, arg);
849 if (result == DISPATCHED_NOUPDATE) {
852 if (sc != scroll()) { // inset has been scrolled
853 the_locking_inset->toggleInsetCursor(bv);
854 updateLocal(bv, FULL, false);
855 the_locking_inset->toggleInsetCursor(bv);
858 } else if (result == DISPATCHED) {
859 the_locking_inset->toggleInsetCursor(bv);
860 updateLocal(bv, CELL, false);
861 the_locking_inset->toggleInsetCursor(bv);
863 } else if (result == FINISHED) {
870 // --- Cursor Movements ----------------------------------
872 if (tabular->IsLastCellInRow(actcell))
874 moveRight(bv, false);
875 sel_cell_end = actcell;
876 updateLocal(bv, SELECTION, false);
879 result = moveRight(bv);
880 sel_cell_start = sel_cell_end = actcell;
882 updateLocal(bv, SELECTION, false);
885 if (tabular->IsFirstCellInRow(actcell))
888 sel_cell_end = actcell;
889 updateLocal(bv, SELECTION, false);
892 result = moveLeft(bv);
893 sel_cell_start = sel_cell_end = actcell;
895 updateLocal(bv, SELECTION, false);
899 int const ocell = actcell;
901 if ((ocell == sel_cell_end) ||
902 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
903 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
905 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
906 updateLocal(bv, SELECTION, false);
910 result = moveDown(bv, old_locking_inset != 0);
911 sel_cell_start = sel_cell_end = actcell;
913 updateLocal(bv, SELECTION, false);
917 int const ocell = actcell;
919 if ((ocell == sel_cell_end) ||
920 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
921 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
923 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
924 updateLocal(bv, SELECTION, false);
928 result = moveUp(bv, old_locking_inset != 0);
929 sel_cell_start = sel_cell_end = actcell;
931 updateLocal(bv, SELECTION, false);
935 if (the_locking_inset) {
936 unlockInsetInInset(bv, the_locking_inset);
937 the_locking_inset = 0;
939 if (bv->text->first + bv->painter().paperHeight() <
940 (top_baseline + tabular->GetHeightOfTabular()))
942 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
943 updateLocal(bv, FULL, false);
944 actcell = tabular->GetCellBelow(first_visible_cell) + column;
946 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
949 updateLocal(bv, CURSOR, false);
954 if (the_locking_inset) {
955 unlockInsetInInset(bv, the_locking_inset);
956 the_locking_inset = 0;
958 if (top_baseline < 0) {
959 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
960 updateLocal(bv, FULL, false);
961 if (top_baseline > 0)
964 actcell = tabular->GetCellBelow(first_visible_cell) + column;
969 updateLocal(bv, CURSOR, false);
980 case LFUN_LAYOUT_TABULAR:
982 bv->owner()->getDialogs()->showTabular(this);
985 case LFUN_TABULAR_FEATURE:
986 if (!tabularFeatures(bv, arg))
987 result = UNDISPATCHED;
990 if (!copySelection(bv))
992 setUndo(bv, Undo::DELETE,
993 bv->text->cursor.par(),
994 bv->text->cursor.par()->next());
996 updateLocal(bv, INIT, true);
1004 case LFUN_PASTESELECTION:
1006 string const clip(bv->getClipboard());
1010 if (clip.find('\t') != string::npos) {
1014 unsigned int len = clip.length();
1015 string::size_type p = 0;
1018 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1026 maxCols = max(cols, maxCols);
1032 maxCols = max(cols, maxCols);
1033 delete paste_tabular;
1034 paste_tabular = new LyXTabular(this, rows, maxCols);
1035 string::size_type op = 0;
1037 int cells = paste_tabular->GetNumberOfCells();
1039 while((cell < cells) && (p < len) &&
1040 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1045 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1050 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1051 while(cols++ < maxCols)
1059 // check for the last cell if there is no trailing '\n'
1060 if ((cell < cells) && (op < len))
1061 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1063 // so that the clipboard is used and it goes on
1065 // and executes LFUN_PASTESELECTION in insettext!
1066 delete paste_tabular;
1071 if (hasPasteBuffer()) {
1072 setUndo(bv, Undo::INSERT,
1073 bv->text->cursor.par(),
1074 bv->text->cursor.par()->next());
1076 updateLocal(bv, INIT, true);
1079 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1081 // we try to activate the actual inset and put this event down to
1082 // the insets dispatch function.
1083 result = UNDISPATCHED;
1084 if (the_locking_inset)
1087 if (activateCellInset(bv)) {
1088 // reset need_update setted in above function!
1090 result = the_locking_inset->localDispatch(bv, action, arg);
1091 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1092 unlockInsetInInset(bv, the_locking_inset);
1094 the_locking_inset = 0;
1095 // we need to update if this was requested before
1096 updateLocal(bv, NONE, false);
1097 return UNDISPATCHED;
1100 // the_locking_inset->ToggleInsetCursor(bv);
1101 updateLocal(bv, CELL, false);
1102 // the_locking_inset->ToggleInsetCursor(bv);
1107 if (result!=FINISHED) {
1108 if (!the_locking_inset) {
1109 showInsetCursor(bv);
1112 bv->unlockInset(this);
1117 int InsetTabular::latex(Buffer const * buf, ostream & os,
1118 bool fragile, bool fp) const
1120 return tabular->Latex(buf, os, fragile, fp);
1124 int InsetTabular::ascii(Buffer const * buf, ostream & os, int) const
1126 // This should be changed to a real ascii export
1127 return tabular->Ascii(buf, os);
1131 int InsetTabular::linuxdoc(Buffer const *, ostream &) const
1137 int InsetTabular::docBook(Buffer const * buf, ostream & os) const
1139 return tabular->DocBook(buf,os);
1143 void InsetTabular::validate(LaTeXFeatures & features) const
1145 tabular->Validate(features);
1149 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1150 LyXFont const & font,
1157 bool changed = false;
1159 // if we have a locking_inset we should have to check only this cell for
1160 // change so I'll try this to have a boost, but who knows ;)
1161 if ((need_update != INIT) &&
1162 (the_locking_inset == tabular->GetCellInset(actcell))) {
1163 for(int i = 0; i < tabular->columns(); ++i) {
1164 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1166 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1169 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1170 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1171 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1177 for (int i = 0; i < tabular->rows(); ++i) {
1180 for (int j = 0; j < tabular->columns(); ++j) {
1181 if (tabular->IsPartOfMultiColumn(i,j))
1184 inset = tabular->GetCellInset(cell);
1186 inset->update(bv, font, false);
1187 maxAsc = max(maxAsc, inset->ascent(bv, font));
1188 maxDesc = max(maxDesc, inset->descent(bv, font));
1189 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1191 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1192 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1198 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1200 if (the_locking_inset) {
1201 the_locking_inset->getCursorPos(bv, x, y);
1204 x = cursor_.x() - top_x;
1209 void InsetTabular::toggleInsetCursor(BufferView * bv)
1211 if (the_locking_inset) {
1212 the_locking_inset->toggleInsetCursor(bv);
1216 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1218 int const asc = lyxfont::maxAscent(font);
1219 int const desc = lyxfont::maxDescent(font);
1221 if (isCursorVisible())
1222 bv->hideLockedInsetCursor();
1224 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1225 toggleCursorVisible();
1229 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1231 if (!isCursorVisible()) {
1232 LyXFont font; // = GetFont(par, cursor.pos);
1234 int const asc = lyxfont::maxAscent(font);
1235 int const desc = lyxfont::maxDescent(font);
1236 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1238 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1239 setCursorVisible(true);
1244 void InsetTabular::hideInsetCursor(BufferView * bv)
1246 if (isCursorVisible()) {
1247 bv->hideLockedInsetCursor();
1248 setCursorVisible(false);
1250 // if (cursor_visible)
1251 // ToggleInsetCursor(bv);
1255 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1259 actcell = actrow = actcol = 0;
1260 int ly = tabular->GetDescentOfRow(actrow);
1262 // first search the right row
1263 while((ly < y) && (actrow < tabular->rows())) {
1264 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1265 tabular->GetAscentOfRow(actrow + 1) +
1266 tabular->GetAdditionalHeight(actrow + 1));
1268 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1270 actcell = tabular->GetCellNumber(actrow, actcol);
1272 // now search the right column
1273 int lx = tabular->GetWidthOfColumn(actcell) -
1274 tabular->GetAdditionalWidth(actcell);
1276 #ifdef WITH_WARNINGS
1277 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1279 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1280 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1281 tabular->GetAdditionalWidth(actcell - 1));
1283 // Jürgen, you should check that this is correct. (Lgb)
1284 #warning Jürgen, please check. (Lgb)
1285 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1286 lx += tabular->GetWidthOfColumn(actcell + 1)
1287 + tabular->GetAdditionalWidth(actcell);
1291 cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1296 int InsetTabular::getCellXPos(int cell) const
1300 for (; !tabular->IsFirstCellInRow(c); --c)
1302 int lx = tabular->GetWidthOfColumn(cell);
1303 for (; c < cell; ++c) {
1304 lx += tabular->GetWidthOfColumn(c);
1306 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1310 void InsetTabular::resetPos(BufferView * bv) const
1314 actcol = tabular->column_of_cell(actcell);
1319 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1320 if (tabular->IsLastCellInRow(cell)) {
1321 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1322 tabular->GetAscentOfRow(actrow + 1) +
1323 tabular->GetAdditionalHeight(actrow + 1));
1327 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1328 int new_x = getCellXPos(actcell);
1329 int old_x = cursor_.x();
1332 // cursor.x(getCellXPos(actcell) + offset);
1333 if ((actcol < tabular->columns()-1) && scroll(false) &&
1334 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1337 updateLocal(bv, FULL, false);
1338 } else if (the_locking_inset &&
1339 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1341 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1342 if (xx > (bv->workWidth()-20)) {
1343 scroll(bv, -(xx - bv->workWidth() + 60));
1344 updateLocal(bv, FULL, false);
1345 } else if (xx < 20) {
1351 updateLocal(bv, FULL, false);
1353 } else if ((cursor_.x() - offset) > 20 &&
1354 (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1355 > (bv->workWidth() - 20)) {
1356 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1357 updateLocal(bv, FULL, false);
1358 } else if ((cursor_.x() - offset) < 20) {
1359 scroll(bv, 20 - cursor_.x() + offset);
1360 updateLocal(bv, FULL, false);
1361 } else if (scroll(false) && top_x > 20 &&
1362 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1363 scroll(bv, old_x - cursor_.x());
1364 updateLocal(bv, FULL, false);
1366 if ((!the_locking_inset ||
1367 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1368 actcell != oldcell) {
1369 InsetTabular * inset = const_cast<InsetTabular *>(this);
1370 bv->owner()->getDialogs()->updateTabular(inset);
1376 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1378 if (lock && !old_locking_inset) {
1379 if (activateCellInset(bv))
1382 bool moved = isRightToLeft(bv)
1383 ? movePrevCell(bv) : moveNextCell(bv);
1386 if (lock && activateCellInset(bv))
1390 return DISPATCHED_NOUPDATE;
1394 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1396 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1399 if (lock) { // behind the inset
1400 if (activateCellInset(bv, 0, 0, 0, true))
1404 return DISPATCHED_NOUPDATE;
1408 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1410 int const ocell = actcell;
1411 actcell = tabular->GetCellAbove(actcell);
1412 if (actcell == ocell) // we moved out of the inset
1418 if (old_locking_inset) {
1419 old_locking_inset->getCursorPos(bv, x, y);
1420 x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1422 if (activateCellInset(bv, x, 0))
1425 return DISPATCHED_NOUPDATE;
1429 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1431 int const ocell = actcell;
1432 actcell = tabular->GetCellBelow(actcell);
1433 if (actcell == ocell) // we moved out of the inset
1439 if (old_locking_inset) {
1440 old_locking_inset->getCursorPos(bv, x, y);
1441 x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1443 if (activateCellInset(bv, x, 0))
1446 return DISPATCHED_NOUPDATE;
1450 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1452 if (isRightToLeft(bv)) {
1453 if (tabular->IsFirstCellInRow(actcell)) {
1454 int row = tabular->row_of_cell(actcell);
1455 if (row == tabular->rows() - 1)
1457 actcell = tabular->GetLastCellInRow(row);
1458 actcell = tabular->GetCellBelow(actcell);
1465 if (tabular->IsLastCell(actcell))
1470 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1471 isRightToLeftPar(bv->buffer()->params);
1472 activateCellInset(bv, 0, 0, 0, !rtl);
1479 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1481 if (isRightToLeft(bv)) {
1482 if (tabular->IsLastCellInRow(actcell)) {
1483 int row = tabular->row_of_cell(actcell);
1486 actcell = tabular->GetFirstCellInRow(row);
1487 actcell = tabular->GetCellAbove(actcell);
1489 if (tabular->IsLastCell(actcell))
1494 if (!actcell) // first cell
1499 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1500 isRightToLeftPar(bv->buffer()->params);
1501 activateCellInset(bv, 0, 0, 0, !rtl);
1508 bool InsetTabular::deletable()
1514 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1519 sel_cell_end = tabular->GetNumberOfCells() - 1;
1521 if (hasSelection()) {
1522 setUndo(bv, Undo::EDIT,
1523 bv->text->cursor.par(),
1524 bv->text->cursor.par()->next());
1525 bool frozen = undo_frozen;
1528 // apply the fontchange on the whole selection
1533 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1534 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1535 for(int j = sel_col_start; j <= sel_col_end; ++j) {
1536 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1541 updateLocal(bv, INIT, true);
1543 if (the_locking_inset)
1544 the_locking_inset->setFont(bv, font, tall);
1548 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1550 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1553 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1554 string const tmp = tabularFeature[i].feature;
1556 if (tmp == what.substr(0, tmp.length())) {
1557 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1558 //tabularFeatures[i].feature.length())) {
1559 action = tabularFeature[i].action;
1563 if (action == LyXTabular::LAST_ACTION)
1567 frontStrip(what.substr(tabularFeature[i].feature.length()));
1568 tabularFeatures(bv, action, val);
1573 void InsetTabular::tabularFeatures(BufferView * bv,
1574 LyXTabular::Feature feature,
1575 string const & value)
1581 bool setLines = false;
1582 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1583 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1586 case LyXTabular::M_ALIGN_LEFT:
1587 case LyXTabular::ALIGN_LEFT:
1588 setAlign=LYX_ALIGN_LEFT;
1590 case LyXTabular::M_ALIGN_RIGHT:
1591 case LyXTabular::ALIGN_RIGHT:
1592 setAlign=LYX_ALIGN_RIGHT;
1594 case LyXTabular::M_ALIGN_CENTER:
1595 case LyXTabular::ALIGN_CENTER:
1596 setAlign=LYX_ALIGN_CENTER;
1598 case LyXTabular::M_VALIGN_TOP:
1599 case LyXTabular::VALIGN_TOP:
1600 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1602 case LyXTabular::M_VALIGN_BOTTOM:
1603 case LyXTabular::VALIGN_BOTTOM:
1604 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1606 case LyXTabular::M_VALIGN_CENTER:
1607 case LyXTabular::VALIGN_CENTER:
1608 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1613 if (hasSelection()) {
1614 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1616 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1617 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1619 setUndo(bv, Undo::FINISH,
1620 bv->text->cursor.par(),
1621 bv->text->cursor.par()->next());
1623 int row = tabular->row_of_cell(actcell);
1624 int column = tabular->column_of_cell(actcell);
1628 case LyXTabular::SET_PWIDTH:
1630 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1631 tabular->SetColumnPWidth(actcell,value);
1633 for (int i = 0; i < tabular->rows(); ++i) {
1634 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1637 updateLocal(bv, INIT, true);
1641 case LyXTabular::SET_MPWIDTH:
1643 bool const update = (tabular->GetPWidth(actcell) != value);
1644 tabular->SetMColumnPWidth(actcell,value);
1646 for (int i = 0; i < tabular->rows(); ++i) {
1647 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1650 updateLocal(bv, INIT, true);
1654 case LyXTabular::SET_SPECIAL_COLUMN:
1655 case LyXTabular::SET_SPECIAL_MULTI:
1656 tabular->SetAlignSpecial(actcell,value,feature);
1658 case LyXTabular::APPEND_ROW:
1659 // append the row into the tabular
1660 unlockInsetInInset(bv, the_locking_inset);
1661 tabular->AppendRow(actcell);
1662 updateLocal(bv, INIT, true);
1664 case LyXTabular::APPEND_COLUMN:
1665 // append the column into the tabular
1666 unlockInsetInInset(bv, the_locking_inset);
1667 tabular->AppendColumn(actcell);
1668 actcell = tabular->GetCellNumber(row, column);
1669 updateLocal(bv, INIT, true);
1671 case LyXTabular::DELETE_ROW:
1672 unlockInsetInInset(bv, the_locking_inset);
1673 tabular->DeleteRow(tabular->row_of_cell(actcell));
1674 if ((row+1) > tabular->rows())
1676 actcell = tabular->GetCellNumber(row, column);
1678 updateLocal(bv, INIT, true);
1680 case LyXTabular::DELETE_COLUMN:
1681 unlockInsetInInset(bv, the_locking_inset);
1682 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1683 if ((column+1) > tabular->columns())
1685 actcell = tabular->GetCellNumber(row, column);
1687 updateLocal(bv, INIT, true);
1689 case LyXTabular::M_TOGGLE_LINE_TOP:
1691 case LyXTabular::TOGGLE_LINE_TOP:
1693 bool lineSet = !tabular->TopLine(actcell, flag);
1694 for (int i = sel_row_start; i <= sel_row_end; ++i)
1695 for (int j = sel_col_start; j <= sel_col_end; ++j)
1696 tabular->SetTopLine(
1697 tabular->GetCellNumber(i, j),
1699 updateLocal(bv, INIT, true);
1703 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1705 case LyXTabular::TOGGLE_LINE_BOTTOM:
1707 bool lineSet = !tabular->BottomLine(actcell, flag);
1708 for (int i = sel_row_start; i <= sel_row_end; ++i)
1709 for (int j = sel_col_start; j <= sel_col_end; ++j)
1710 tabular->SetBottomLine(
1711 tabular->GetCellNumber(i, j),
1714 updateLocal(bv, INIT, true);
1718 case LyXTabular::M_TOGGLE_LINE_LEFT:
1720 case LyXTabular::TOGGLE_LINE_LEFT:
1722 bool lineSet = !tabular->LeftLine(actcell, flag);
1723 for (int i = sel_row_start; i <= sel_row_end; ++i)
1724 for (int j = sel_col_start; j <= sel_col_end; ++j)
1725 tabular->SetLeftLine(
1726 tabular->GetCellNumber(i,j),
1729 updateLocal(bv, INIT, true);
1733 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1735 case LyXTabular::TOGGLE_LINE_RIGHT:
1737 bool lineSet = !tabular->RightLine(actcell, flag);
1738 for (int i = sel_row_start; i <= sel_row_end; ++i)
1739 for (int j = sel_col_start; j <= sel_col_end; ++j)
1740 tabular->SetRightLine(
1741 tabular->GetCellNumber(i,j),
1744 updateLocal(bv, INIT, true);
1748 case LyXTabular::M_ALIGN_LEFT:
1749 case LyXTabular::M_ALIGN_RIGHT:
1750 case LyXTabular::M_ALIGN_CENTER:
1752 case LyXTabular::ALIGN_LEFT:
1753 case LyXTabular::ALIGN_RIGHT:
1754 case LyXTabular::ALIGN_CENTER:
1755 for (int i = sel_row_start; i <= sel_row_end; ++i)
1756 for (int j = sel_col_start; j <= sel_col_end; ++j)
1757 tabular->SetAlignment(
1758 tabular->GetCellNumber(i, j),
1761 updateLocal(bv, INIT, true);
1763 case LyXTabular::M_VALIGN_TOP:
1764 case LyXTabular::M_VALIGN_BOTTOM:
1765 case LyXTabular::M_VALIGN_CENTER:
1767 case LyXTabular::VALIGN_TOP:
1768 case LyXTabular::VALIGN_BOTTOM:
1769 case LyXTabular::VALIGN_CENTER:
1770 for (int i = sel_row_start; i <= sel_row_end; ++i)
1771 for (int j = sel_col_start; j <= sel_col_end; ++j)
1772 tabular->SetVAlignment(
1773 tabular->GetCellNumber(i, j),
1775 updateLocal(bv, INIT, true);
1777 case LyXTabular::MULTICOLUMN:
1779 if (sel_row_start != sel_row_end) {
1780 WriteAlert(_("Impossible Operation!"),
1781 _("Multicolumns can only be horizontally."),
1785 // just multicol for one Single Cell
1786 if (!hasSelection()) {
1787 // check wether we are completly in a multicol
1788 if (tabular->IsMultiColumn(actcell)) {
1789 tabular->UnsetMultiColumn(actcell);
1790 updateLocal(bv, INIT, true);
1792 tabular->SetMultiColumn(actcell, 1);
1793 updateLocal(bv, CELL, true);
1797 // we have a selection so this means we just add all this
1798 // cells to form a multicolumn cell
1802 if (sel_cell_start > sel_cell_end) {
1803 s_start = sel_cell_end;
1804 s_end = sel_cell_start;
1806 s_start = sel_cell_start;
1807 s_end = sel_cell_end;
1809 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1811 sel_cell_end = sel_cell_start;
1812 updateLocal(bv, INIT, true);
1815 case LyXTabular::SET_ALL_LINES:
1817 case LyXTabular::UNSET_ALL_LINES:
1818 for (int i = sel_row_start; i <= sel_row_end; ++i)
1819 for (int j = sel_col_start; j <= sel_col_end; ++j)
1820 tabular->SetAllLines(
1821 tabular->GetCellNumber(i,j), setLines);
1822 updateLocal(bv, INIT, true);
1824 case LyXTabular::SET_LONGTABULAR:
1825 tabular->SetLongTabular(true);
1826 updateLocal(bv, INIT, true); // because this toggles displayed
1828 case LyXTabular::UNSET_LONGTABULAR:
1829 tabular->SetLongTabular(false);
1830 updateLocal(bv, INIT, true); // because this toggles displayed
1832 case LyXTabular::SET_ROTATE_TABULAR:
1833 tabular->SetRotateTabular(true);
1835 case LyXTabular::UNSET_ROTATE_TABULAR:
1836 tabular->SetRotateTabular(false);
1838 case LyXTabular::SET_ROTATE_CELL:
1839 for (int i = sel_row_start; i <= sel_row_end; ++i)
1840 for (int j = sel_col_start; j<=sel_col_end; ++j)
1841 tabular->SetRotateCell(
1842 tabular->GetCellNumber(i, j),
1845 case LyXTabular::UNSET_ROTATE_CELL:
1846 for (int i = sel_row_start; i <= sel_row_end; ++i)
1847 for (int j = sel_col_start; j <= sel_col_end; ++j)
1848 tabular->SetRotateCell(
1849 tabular->GetCellNumber(i, j), false);
1851 case LyXTabular::SET_USEBOX:
1853 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1854 if (val == tabular->GetUsebox(actcell))
1855 val = LyXTabular::BOX_NONE;
1856 for (int i = sel_row_start; i <= sel_row_end; ++i)
1857 for (int j = sel_col_start; j <= sel_col_end; ++j)
1859 tabular->GetCellNumber(i, j), val);
1862 case LyXTabular::SET_LTFIRSTHEAD:
1863 tabular->SetLTHead(actcell, true);
1865 case LyXTabular::SET_LTHEAD:
1866 tabular->SetLTHead(actcell, false);
1868 case LyXTabular::SET_LTFOOT:
1869 tabular->SetLTFoot(actcell, false);
1871 case LyXTabular::SET_LTLASTFOOT:
1872 tabular->SetLTFoot(actcell, true);
1874 case LyXTabular::SET_LTNEWPAGE:
1876 bool what = !tabular->GetLTNewPage(actcell);
1877 tabular->SetLTNewPage(actcell, what);
1880 // dummy stuff just to avoid warnings
1881 case LyXTabular::LAST_ACTION:
1887 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
1890 UpdatableInset * inset =
1891 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1892 LyXFont font(LyXFont::ALL_SANE);
1894 x = inset->x() + inset->width(bv, font);
1895 y = inset->descent(bv, font);
1897 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1898 //inset_y = cursor.y();
1899 inset->edit(bv, x, y, button);
1900 if (!the_locking_inset)
1902 updateLocal(bv, CELL, false);
1903 return (the_locking_inset != 0);
1907 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1910 inset_x = cursor_.x()
1911 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1912 inset_y = cursor_.y();
1913 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1917 bool InsetTabular::insetHit(BufferView *, int x, int) const
1920 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
1924 // This returns paperWidth() if the cell-width is unlimited or the width
1925 // in pixels if we have a pwidth for this cell.
1926 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
1928 string const s = tabular->GetPWidth(cell);
1932 return VSpace(s).inPixels(bv);
1936 int InsetTabular::getMaxWidth(BufferView * bv,
1937 UpdatableInset const * inset) const
1939 int cell = tabular->cur_cell;
1940 if (tabular->GetCellInset(cell) != inset) {
1942 if (tabular->GetCellInset(cell) != inset) {
1944 lyxerr << "Actcell not equal to actual cell!\n";
1949 int const n = tabular->GetNumberOfCells();
1952 for (cell = 0; cell < n; ++cell) {
1953 if (tabular->GetCellInset(cell) == inset)
1959 lyxerr << "Own inset not found, shouldn't really happen!\n";
1963 int w = getMaxWidthOfCell(bv, cell);
1965 // because the inset then subtracts it's top_x and owner->x()
1966 w += (inset->x() - top_x);
1973 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1975 resizeLyXText(bv, recursive);
1979 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
1982 for(int i = 0; i < tabular->rows(); ++i) {
1983 for(int j = 0; j < tabular->columns(); ++j) {
1984 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
1992 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1993 bool const recursive) const
1995 if (the_locking_inset)
1996 return the_locking_inset->getLyXText(bv, recursive);
1998 // if we're locked lock the actual insettext and return it's LyXText!!!
2000 UpdatableInset * inset =
2001 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2002 inset->edit(const_cast<BufferView *>(bv), 0, 0, 0);
2003 return the_locking_inset->getLyXText(bv, recursive);
2006 return Inset::getLyXText(bv, recursive);
2010 bool InsetTabular::showInsetDialog(BufferView * bv) const
2012 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2013 bv->owner()->getDialogs()
2014 ->showTabular(const_cast<InsetTabular *>(this));
2019 void InsetTabular::openLayoutDialog(BufferView * bv) const
2021 if (the_locking_inset) {
2022 InsetTabular * i = static_cast<InsetTabular *>
2023 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2025 i->openLayoutDialog(bv);
2029 bv->owner()->getDialogs()->showTabular(
2030 const_cast<InsetTabular *>(this));
2035 // functions returns:
2039 // 3 ... toggled off
2041 func_status::value_type InsetTabular::getStatus(string const & what) const
2043 int action = LyXTabular::LAST_ACTION;
2044 func_status::value_type status = func_status::OK;
2047 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2048 string const tmp = tabularFeature[i].feature;
2049 if (tmp == what.substr(0, tmp.length())) {
2050 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2051 // tabularFeatures[i].feature.length())) {
2052 action = tabularFeature[i].action;
2056 if (action == LyXTabular::LAST_ACTION)
2057 return func_status::Unknown;
2059 string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2066 if (hasSelection()) {
2067 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2069 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2073 case LyXTabular::SET_PWIDTH:
2074 case LyXTabular::SET_MPWIDTH:
2075 case LyXTabular::SET_SPECIAL_COLUMN:
2076 case LyXTabular::SET_SPECIAL_MULTI:
2077 status |= func_status::Disabled;
2080 case LyXTabular::APPEND_ROW:
2081 case LyXTabular::APPEND_COLUMN:
2082 case LyXTabular::DELETE_ROW:
2083 case LyXTabular::DELETE_COLUMN:
2084 case LyXTabular::SET_ALL_LINES:
2085 case LyXTabular::UNSET_ALL_LINES:
2086 status |= func_status::OK;
2089 case LyXTabular::MULTICOLUMN:
2090 if (tabular->IsMultiColumn(actcell))
2091 status |= func_status::ToggleOn;
2093 status |= func_status::ToggleOff;
2095 case LyXTabular::M_TOGGLE_LINE_TOP:
2097 case LyXTabular::TOGGLE_LINE_TOP:
2098 if (tabular->TopLine(actcell, flag))
2099 status |= func_status::ToggleOn;
2101 status |= func_status::ToggleOff;
2103 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2105 case LyXTabular::TOGGLE_LINE_BOTTOM:
2106 if (tabular->BottomLine(actcell, flag))
2107 status |= func_status::ToggleOn;
2109 status |= func_status::ToggleOff;
2111 case LyXTabular::M_TOGGLE_LINE_LEFT:
2113 case LyXTabular::TOGGLE_LINE_LEFT:
2114 if (tabular->LeftLine(actcell, flag))
2115 status |= func_status::ToggleOn;
2117 status |= func_status::ToggleOff;
2119 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2121 case LyXTabular::TOGGLE_LINE_RIGHT:
2122 if (tabular->RightLine(actcell, flag))
2123 status |= func_status::ToggleOn;
2125 status |= func_status::ToggleOff;
2127 case LyXTabular::M_ALIGN_LEFT:
2129 case LyXTabular::ALIGN_LEFT:
2130 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2131 status |= func_status::ToggleOn;
2133 status |= func_status::ToggleOff;
2135 case LyXTabular::M_ALIGN_RIGHT:
2137 case LyXTabular::ALIGN_RIGHT:
2138 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2139 status |= func_status::ToggleOn;
2141 status |= func_status::ToggleOff;
2143 case LyXTabular::M_ALIGN_CENTER:
2145 case LyXTabular::ALIGN_CENTER:
2146 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2147 status |= func_status::ToggleOn;
2149 status |= func_status::ToggleOff;
2151 case LyXTabular::M_VALIGN_TOP:
2153 case LyXTabular::VALIGN_TOP:
2154 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2155 status |= func_status::ToggleOn;
2157 status |= func_status::ToggleOff;
2159 case LyXTabular::M_VALIGN_BOTTOM:
2161 case LyXTabular::VALIGN_BOTTOM:
2162 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2163 status |= func_status::ToggleOn;
2165 status |= func_status::ToggleOff;
2167 case LyXTabular::M_VALIGN_CENTER:
2169 case LyXTabular::VALIGN_CENTER:
2170 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2171 status |= func_status::ToggleOn;
2173 status |= func_status::ToggleOff;
2175 case LyXTabular::SET_LONGTABULAR:
2176 if (tabular->IsLongTabular())
2177 status |= func_status::ToggleOn;
2179 status |= func_status::ToggleOff;
2181 case LyXTabular::UNSET_LONGTABULAR:
2182 if (!tabular->IsLongTabular())
2183 status |= func_status::ToggleOn;
2185 status |= func_status::ToggleOff;
2187 case LyXTabular::SET_ROTATE_TABULAR:
2188 if (tabular->GetRotateTabular())
2189 status |= func_status::ToggleOn;
2191 status |= func_status::ToggleOff;
2193 case LyXTabular::UNSET_ROTATE_TABULAR:
2194 if (!tabular->GetRotateTabular())
2195 status |= func_status::ToggleOn;
2197 status |= func_status::ToggleOff;
2199 case LyXTabular::SET_ROTATE_CELL:
2200 if (tabular->GetRotateCell(actcell))
2201 status |= func_status::ToggleOn;
2203 status |= func_status::ToggleOff;
2205 case LyXTabular::UNSET_ROTATE_CELL:
2206 if (!tabular->GetRotateCell(actcell))
2207 status |= func_status::ToggleOn;
2209 status |= func_status::ToggleOff;
2211 case LyXTabular::SET_USEBOX:
2212 if (strToInt(argument) == tabular->GetUsebox(actcell))
2213 status |= func_status::ToggleOn;
2215 status |= func_status::ToggleOff;
2217 case LyXTabular::SET_LTFIRSTHEAD:
2218 if (tabular->GetRowOfLTHead(actcell, dummy))
2219 status |= func_status::ToggleOn;
2221 status |= func_status::ToggleOff;
2223 case LyXTabular::SET_LTHEAD:
2224 if (tabular->GetRowOfLTHead(actcell, dummy))
2225 status |= func_status::ToggleOn;
2227 status |= func_status::ToggleOff;
2229 case LyXTabular::SET_LTFOOT:
2230 if (tabular->GetRowOfLTFoot(actcell, dummy))
2231 status |= func_status::ToggleOn;
2233 status |= func_status::ToggleOff;
2235 case LyXTabular::SET_LTLASTFOOT:
2236 if (tabular->GetRowOfLTFoot(actcell, dummy))
2237 status |= func_status::ToggleOn;
2239 status |= func_status::ToggleOff;
2241 case LyXTabular::SET_LTNEWPAGE:
2242 if (tabular->GetLTNewPage(actcell))
2243 status |= func_status::ToggleOn;
2245 status |= func_status::ToggleOff;
2248 status = func_status::Disabled;
2255 std::vector<string> const InsetTabular::getLabelList() const
2257 return tabular->getLabelList();
2261 bool InsetTabular::copySelection(BufferView * bv)
2263 if (!hasSelection())
2266 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2267 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2268 if (sel_col_start > sel_col_end) {
2269 sel_col_start = sel_col_end;
2270 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2272 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2274 int const columns = sel_col_end - sel_col_start + 1;
2276 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2277 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2278 if (sel_row_start > sel_row_end) {
2279 swap(sel_row_start, sel_row_end);
2281 int const rows = sel_row_end - sel_row_start + 1;
2283 delete paste_tabular;
2284 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2285 for (int i = 0; i < sel_row_start; ++i)
2286 paste_tabular->DeleteRow(0);
2287 while(paste_tabular->rows() > rows)
2288 paste_tabular->DeleteRow(rows);
2289 paste_tabular->SetTopLine(0, true, true);
2290 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2292 for (int i = 0; i < sel_col_start; ++i)
2293 paste_tabular->DeleteColumn(0);
2294 while (paste_tabular->columns() > columns)
2295 paste_tabular->DeleteColumn(columns);
2296 paste_tabular->SetLeftLine(0, true, true);
2297 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2301 paste_tabular->Ascii(bv->buffer(), sstr);
2302 bv->stuffClipboard(sstr.str().c_str());
2307 bool InsetTabular::pasteSelection(BufferView * bv)
2312 for (int r1 = 0, r2 = actrow;
2313 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2315 for(int c1 = 0, c2 = actcol;
2316 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2318 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2319 tabular->IsPartOfMultiColumn(r2,c2))
2321 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2325 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2329 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2330 int const n2 = tabular->GetCellNumber(r2, c2);
2331 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2332 tabular->GetCellInset(n2)->setOwner(this);
2333 tabular->GetCellInset(n2)->deleteLyXText(bv);
2340 bool InsetTabular::cutSelection()
2342 if (!hasSelection())
2345 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2346 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2347 if (sel_col_start > sel_col_end) {
2348 sel_col_start = sel_col_end;
2349 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2351 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2353 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2354 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2355 if (sel_row_start > sel_row_end) {
2356 swap(sel_row_start, sel_row_end);
2358 if (sel_cell_start > sel_cell_end) {
2359 swap(sel_cell_start, sel_cell_end);
2361 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2362 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2363 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2370 bool InsetTabular::isRightToLeft(BufferView *bv )
2372 return bv->getParentLanguage(this)->RightToLeft();
2376 bool InsetTabular::nodraw() const
2378 if (!UpdatableInset::nodraw() && the_locking_inset)
2379 return the_locking_inset->nodraw();
2380 return UpdatableInset::nodraw();
2384 int InsetTabular::scroll(bool recursive) const
2386 int sx = UpdatableInset::scroll(false);
2388 if (recursive && the_locking_inset)
2389 sx += the_locking_inset->scroll(recursive);
2395 bool InsetTabular::doClearArea() const
2397 return !locked || (need_update & (FULL|INIT));
2401 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2403 srow = tabular->row_of_cell(sel_cell_start);
2404 erow = tabular->row_of_cell(sel_cell_end);
2408 scol = tabular->column_of_cell(sel_cell_start);
2409 ecol = tabular->column_of_cell(sel_cell_end);
2413 ecol = tabular->right_column_of_cell(sel_cell_end);
2417 Paragraph * InsetTabular::getParFromID(int id) const
2420 for(int i = 0; i < tabular->rows(); ++i) {
2421 for(int j = 0; j < tabular->columns(); ++j) {
2422 if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2430 Paragraph * InsetTabular::firstParagraph() const
2432 if (the_locking_inset)
2433 return the_locking_inset->firstParagraph();
2438 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2440 if (the_locking_inset)
2441 return the_locking_inset->cursor(bv);
2442 return Inset::cursor(bv);
2446 Inset * InsetTabular::getInsetFromID(int id_arg) const
2449 return const_cast<InsetTabular *>(this);
2452 for(int i=0; i < tabular->rows(); ++i) {
2453 for(int j=0; j < tabular->columns(); ++j) {
2454 if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2462 string const InsetTabular::selectNextWord(BufferView * bv, float & value) const
2464 if (the_locking_inset) {
2465 string const str(the_locking_inset->selectNextWord(bv, value));
2468 if (tabular->IsLastCell(actcell)) {
2469 bv->unlockInset(const_cast<InsetTabular *>(this));
2475 // otherwise we have to lock the next inset and ask for it's selecttion
2476 UpdatableInset * inset =
2477 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2478 inset->edit(bv, 0, 0, 0);
2479 string const str(selectNextWordInt(bv, value));
2486 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2488 // when entering this function the inset should be ALWAYS locked!
2489 lyx::Assert(the_locking_inset);
2491 string const str(the_locking_inset->selectNextWord(bv, value));
2495 if (tabular->IsLastCell(actcell)) {
2496 bv->unlockInset(const_cast<InsetTabular *>(this));
2500 // otherwise we have to lock the next inset and ask for it's selecttion
2501 UpdatableInset * inset =
2502 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2503 inset->edit(bv, 0, 0, 0);
2504 return selectNextWordInt(bv, value);
2508 void InsetTabular::selectSelectedWord(BufferView * bv)
2510 if (the_locking_inset) {
2511 the_locking_inset->selectSelectedWord(bv);
2518 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2520 if (the_locking_inset) {
2521 the_locking_inset->toggleSelection(bv, kill_selection);
2526 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2527 bool const & cs, bool const & mw)
2529 if (the_locking_inset) {
2530 if (the_locking_inset->searchForward(bv, str, cs, mw))
2532 if (tabular->IsLastCell(actcell)) {
2533 bv->unlockInset(const_cast<InsetTabular *>(this));
2539 // otherwise we have to lock the next inset and search there
2540 UpdatableInset * inset =
2541 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2543 bool const res = searchForward(bv, str, cs, mw);
2544 updateLocal(bv, NONE, false);
2546 bv->updateInset(const_cast<InsetTabular *>(this), false);
2551 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2552 bool const & cs, bool const & mw)
2554 if (the_locking_inset) {
2555 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2557 if (!actcell) { // we are already in the first cell
2558 bv->unlockInset(const_cast<InsetTabular *>(this));
2564 // otherwise we have to lock the next inset and search there
2565 UpdatableInset * inset =
2566 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2567 inset->edit(bv, false);
2568 bool const res = searchBackward(bv, str, cs, mw);
2570 bv->updateInset(const_cast<InsetTabular *>(this), false);