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;
145 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf,
147 : UpdatableInset(tab, same_id), buffer(&buf)
149 tabular.reset(new LyXTabular(this, *(tab.tabular)));
150 the_locking_inset = 0;
151 locked = no_selection = false;
153 actrow = actcell = 0;
154 sel_cell_start = sel_cell_end = 0;
160 InsetTabular::~InsetTabular()
166 Inset * InsetTabular::clone(Buffer const & buf, bool same_id) const
168 return new InsetTabular(*this, buf, same_id);
172 void InsetTabular::write(Buffer const * buf, ostream & os) const
174 os << " Tabular" << endl;
175 tabular->Write(buf, os);
179 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
181 bool const old_format = (lex.GetString() == "\\LyXTable");
185 //tabular = new LyXTabular(buf, this, lex);
186 tabular.reset(new LyXTabular(buf, this, lex));
194 string token = lex.GetString();
195 while (lex.IsOK() && (token != "\\end_inset")) {
197 token = lex.GetString();
199 if (token != "\\end_inset") {
200 lex.printError("Missing \\end_inset at this point. "
206 int InsetTabular::ascent(BufferView *, LyXFont const &) const
208 return tabular->GetAscentOfRow(0);
212 int InsetTabular::descent(BufferView *, LyXFont const &) const
214 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
218 int InsetTabular::width(BufferView *, LyXFont const &) const
220 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
224 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
225 float & x, bool cleared) const
233 if (need_update == INIT) {
234 if (calculate_dimensions_of_cells(bv, font, true))
235 bv->text->status = LyXText::CHANGED_IN_DRAW;
240 Painter & pain = bv->painter();
246 UpdatableInset::draw(bv, font, baseline, x, cleared);
249 x += static_cast<float>(scroll());
251 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
252 (top_x != int(x)) || (top_baseline != baseline))) {
253 int h = ascent(bv, font) + descent(bv, font);
254 int const tx = display() || !owner() ? 0 : top_x;
255 int w = tx ? width(bv, font) : pain.paperWidth();
256 int ty = baseline - ascent(bv, font);
260 if ((ty + h) > pain.paperHeight())
261 h = pain.paperHeight();
262 if ((top_x + w) > pain.paperWidth())
263 w = pain.paperWidth();
264 pain.fillRectangle(tx, ty, w, h, backgroundColor());
269 top_baseline = baseline;
270 x += ADD_TO_TABULAR_WIDTH;
274 first_visible_cell = -1;
275 for (i = 0; i < tabular->rows(); ++i) {
277 cell = tabular->GetCellNumber(i, 0);
278 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
279 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
281 baseline += tabular->GetDescentOfRow(i) +
282 tabular->GetAscentOfRow(i + 1) +
283 tabular->GetAdditionalHeight(i + 1);
286 for (j = 0; j < tabular->columns(); ++j) {
287 if (nx > bv->workWidth())
289 if (tabular->IsPartOfMultiColumn(i, j))
291 cx = nx + tabular->GetBeginningOfTextInCell(cell);
292 if (first_visible_cell < 0)
293 first_visible_cell = cell;
295 drawCellSelection(pain, nx, baseline, i, j, cell);
297 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
299 drawCellLines(pain, nx, baseline, i, cell);
300 nx += tabular->GetWidthOfColumn(cell);
303 baseline += tabular->GetDescentOfRow(i) +
304 tabular->GetAscentOfRow(i + 1) +
305 tabular->GetAdditionalHeight(i + 1);
307 } else if (need_update == CELL) {
310 if (the_locking_inset &&
311 tabular->GetCellInset(actcell) != the_locking_inset)
313 Inset * inset = tabular->GetCellInset(cell);
315 inset != the_locking_inset && i < tabular->rows();
318 inset != the_locking_inset
319 && j < tabular->columns();
321 if (tabular->IsPartOfMultiColumn(i, j))
323 nx += tabular->GetWidthOfColumn(cell);
325 inset = tabular->GetCellInset(cell);
327 if (tabular->row_of_cell(cell) > i) {
329 baseline += tabular->GetDescentOfRow(i) +
330 tabular->GetAscentOfRow(i + 1) +
331 tabular->GetAdditionalHeight(i + 1);
335 // compute baseline for actual row
336 for (i = 0; i < actrow; ++i) {
337 baseline += tabular->GetDescentOfRow(i) +
338 tabular->GetAscentOfRow(i + 1) +
339 tabular->GetAdditionalHeight(i + 1);
341 // now compute the right x position
342 cell = tabular->GetCellNumber(actrow, 0);
343 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
344 if (tabular->IsPartOfMultiColumn(actrow, j))
346 nx += tabular->GetWidthOfColumn(cell);
350 i = tabular->row_of_cell(cell);
351 if (the_locking_inset != tabular->GetCellInset(cell)) {
352 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
355 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
357 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
358 tabular->GetCellInset(cell)->draw(bv, font, baseline, dx, false);
360 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
363 // clear only if we didn't have a change
364 if (need_update == CELL) {
365 // clear before the inset
368 baseline - tabular->GetAscentOfRow(i) + 1,
370 tabular->GetAscentOfRow(i) +
371 tabular->GetDescentOfRow(i) - 1,
373 // clear behind the inset
375 int(cx + the_locking_inset->width(bv,font) + 1),
376 baseline - tabular->GetAscentOfRow(i) + 1,
377 tabular->GetWidthOfColumn(cell) -
378 tabular->GetBeginningOfTextInCell(cell) -
379 the_locking_inset->width(bv,font) -
380 tabular->GetAdditionalWidth(cell) - 1,
381 tabular->GetAscentOfRow(i) +
382 tabular->GetDescentOfRow(i) - 1,
386 x -= ADD_TO_TABULAR_WIDTH;
387 x += width(bv, font);
388 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
390 for(Inset * inset=owner(); inset; ++i)
391 inset = inset->owner();
392 if (calculate_dimensions_of_cells(bv, font, false))
400 void InsetTabular::drawCellLines(Painter & pain, int x, int baseline,
401 int row, int cell) const
403 int x2 = x + tabular->GetWidthOfColumn(cell);
406 if (!tabular->TopAlreadyDrawed(cell)) {
407 on_off = !tabular->TopLine(cell);
408 pain.line(x, baseline - tabular->GetAscentOfRow(row),
409 x2, baseline - tabular->GetAscentOfRow(row),
410 on_off ? LColor::tabularonoffline : LColor::tabularline,
411 on_off ? Painter::line_onoffdash : Painter::line_solid);
413 on_off = !tabular->BottomLine(cell);
414 pain.line(x,baseline + tabular->GetDescentOfRow(row),
415 x2, baseline + tabular->GetDescentOfRow(row),
416 on_off ? LColor::tabularonoffline : LColor::tabularline,
417 on_off ? Painter::line_onoffdash : Painter::line_solid);
418 if (!tabular->LeftAlreadyDrawed(cell)) {
419 on_off = !tabular->LeftLine(cell);
420 pain.line(x, baseline - tabular->GetAscentOfRow(row),
421 x, baseline + tabular->GetDescentOfRow(row),
422 on_off ? LColor::tabularonoffline : LColor::tabularline,
423 on_off ? Painter::line_onoffdash : Painter::line_solid);
425 on_off = !tabular->RightLine(cell);
426 pain.line(x2 - tabular->GetAdditionalWidth(cell),
427 baseline - tabular->GetAscentOfRow(row),
428 x2 - tabular->GetAdditionalWidth(cell),
429 baseline + tabular->GetDescentOfRow(row),
430 on_off ? LColor::tabularonoffline : LColor::tabularline,
431 on_off ? Painter::line_onoffdash : Painter::line_solid);
435 void InsetTabular::drawCellSelection(Painter & pain, int x, int baseline,
436 int row, int column, int cell) const
438 int cs = tabular->column_of_cell(sel_cell_start);
439 int ce = tabular->column_of_cell(sel_cell_end);
442 cs = tabular->column_of_cell(sel_cell_end);
444 ce = tabular->right_column_of_cell(sel_cell_end);
447 int rs = tabular->row_of_cell(sel_cell_start);
448 int re = tabular->row_of_cell(sel_cell_end);
449 if (rs > re) swap(rs, re);
451 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
452 int w = tabular->GetWidthOfColumn(cell);
453 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
454 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
455 w, h, LColor::selection);
460 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
467 calculate_dimensions_of_cells(bv, font, true);
469 owner()->update(bv, font, true);
473 if (the_locking_inset)
474 the_locking_inset->update(bv, font, reinit);
475 if (need_update < FULL &&
476 bv->text->status() == LyXText::NEED_MORE_REFRESH)
481 switch (need_update) {
485 if (calculate_dimensions_of_cells(bv, font, false))
498 string const InsetTabular::editMessage() const
500 return _("Opened Tabular Inset");
504 void InsetTabular::edit(BufferView * bv, int x, int y, unsigned int button)
506 UpdatableInset::edit(bv, x, y, button);
508 if (!bv->lockInset(this)) {
509 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
513 the_locking_inset = 0;
517 sel_cell_start = sel_cell_end = actcell;
519 if (insetHit(bv, x, y) && (button != 3)) {
520 activateCellInsetAbs(bv, x, y, button);
525 void InsetTabular::edit(BufferView * bv, bool front)
527 UpdatableInset::edit(bv, front);
529 if (!bv->lockInset(this)) {
530 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
534 the_locking_inset = 0;
540 actcell = tabular->GetNumberOfCells() - 1;
541 sel_cell_start = sel_cell_end = actcell;
547 void InsetTabular::insetUnlock(BufferView * bv)
549 if (the_locking_inset) {
550 the_locking_inset->insetUnlock(bv);
551 the_locking_inset = 0;
554 no_selection = false;
557 if (scroll(false) || hasSelection()) {
558 sel_cell_start = sel_cell_end = 0;
562 updateLocal(bv, FULL, false);
567 void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what,
568 bool mark_dirty) const
570 if (need_update < what) // only set this if it has greater update
572 if ((what == INIT) && hasSelection())
575 if (need_update != NONE) {
576 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
577 if (locked) // && (what != NONE))
583 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
585 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
590 if (inset == tabular->GetCellInset(actcell)) {
591 lyxerr[Debug::INSETS] << "OK" << endl;
592 the_locking_inset = tabular->GetCellInset(actcell);
594 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
595 inset_y = cursor_.y();
597 } else if (the_locking_inset && (the_locking_inset == inset)) {
598 lyxerr[Debug::INSETS] << "OK" << endl;
600 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
601 inset_y = cursor_.y();
602 } else if (the_locking_inset) {
603 lyxerr[Debug::INSETS] << "MAYBE" << endl;
604 return the_locking_inset->lockInsetInInset(bv, inset);
606 lyxerr[Debug::INSETS] << "NOT OK" << endl;
611 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
614 if (!the_locking_inset)
616 if (the_locking_inset == inset) {
617 the_locking_inset->insetUnlock(bv);
618 the_locking_inset = 0;
620 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
626 updateLocal(bv, CELL, false);
627 showInsetCursor(bv, false);
630 if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
631 if (inset->lyxCode() == TABULAR_CODE &&
632 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
633 bv->owner()->getDialogs()->updateTabular(this);
642 bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
644 if (!the_locking_inset)
646 if (the_locking_inset != inset) {
647 if (!the_locking_inset->updateInsetInInset(bv, inset))
650 updateLocal(bv, CELL, false);
655 unsigned int InsetTabular::insetInInsetY()
657 if (!the_locking_inset)
660 return inset_y + the_locking_inset->insetInInsetY();
664 UpdatableInset * InsetTabular::getLockingInset() const
666 return the_locking_inset ? the_locking_inset->getLockingInset() :
667 const_cast<InsetTabular *>(this);
671 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(Inset::Code c)
675 if (the_locking_inset)
676 return the_locking_inset->getFirstLockingInsetOfType(c);
681 bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
683 if (the_locking_inset)
684 return the_locking_inset->insertInset(bv, inset);
689 void InsetTabular::insetButtonPress(BufferView * bv, int x, int y, int button)
691 if (hasSelection() && (button == 3))
694 if (hasSelection()) {
696 updateLocal(bv, SELECTION, false);
699 no_selection = false;
701 int const ocell = actcell;
702 int const orow = actrow;
707 updateLocal(bv, NONE, false);
708 sel_cell_start = sel_cell_end = actcell;
710 if ((ocell != actcell) && the_locking_inset) {
711 the_locking_inset->insetUnlock(bv);
712 the_locking_inset = 0;
718 bool const inset_hit = insetHit(bv, x, y);
720 if ((ocell == actcell) && the_locking_inset && inset_hit) {
722 the_locking_inset->insetButtonPress(bv,
723 x - inset_x, y - inset_y,
726 } else if (the_locking_inset) {
727 the_locking_inset->insetUnlock(bv);
729 the_locking_inset = 0;
731 localDispatch(bv, LFUN_PASTESELECTION, "paragraph");
734 if (inset_hit && bv->theLockingInset()) {
735 if (activateCellInsetAbs(bv, x, y, button))
736 the_locking_inset->insetButtonPress(bv,
746 void InsetTabular::insetButtonRelease(BufferView * bv,
747 int x, int y, int button)
750 if (the_locking_inset) {
752 if ((i = the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE))) {
753 i->insetButtonRelease(bv, x, y, button);
757 bv->owner()->getDialogs()->showTabular(this);
760 if (the_locking_inset) {
761 the_locking_inset->insetButtonRelease(bv,
762 x - inset_x, y - inset_y,
766 no_selection = false;
770 void InsetTabular::insetMotionNotify(BufferView * bv, int x, int y, int button)
772 if (the_locking_inset) {
773 the_locking_inset->insetMotionNotify(bv,
781 int const old_cell = actcell;
784 sel_cell_end = actcell;
785 if (sel_cell_end != old_cell)
786 updateLocal(bv, SELECTION, false);
789 no_selection = false;
793 void InsetTabular::insetKeyPress(XKeyEvent * xke)
795 if (the_locking_inset) {
796 the_locking_inset->insetKeyPress(xke);
802 UpdatableInset::RESULT
803 InsetTabular::localDispatch(BufferView * bv, kb_action action,
806 // We need to save the value of the_locking_inset as the call to
807 // the_locking_inset->LocalDispatch might unlock it.
808 old_locking_inset = the_locking_inset;
809 no_selection = false;
810 UpdatableInset::RESULT result =
811 UpdatableInset::localDispatch(bv, action, arg);
812 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
817 if ((action < 0) && arg.empty())
820 bool hs = hasSelection();
823 // this one have priority over the locked InsetText, if we're not already
824 // inside another tabular then that one get's priority!
825 if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) == this) {
830 if (the_locking_inset) {
831 unlockInsetInInset(bv, the_locking_inset);
832 the_locking_inset = 0;
834 if (action == LFUN_TAB)
835 moveNextCell(bv, old_locking_inset != 0);
837 movePrevCell(bv, old_locking_inset != 0);
838 sel_cell_start = sel_cell_end = actcell;
840 updateLocal(bv, SELECTION, false);
841 if (!the_locking_inset) {
843 return DISPATCHED_NOUPDATE;
846 // this to avoid compiler warnings.
852 if (the_locking_inset) {
853 result = the_locking_inset->localDispatch(bv, action, arg);
854 if (result == DISPATCHED_NOUPDATE) {
857 if (sc != scroll()) { // inset has been scrolled
858 the_locking_inset->toggleInsetCursor(bv);
859 updateLocal(bv, FULL, false);
860 the_locking_inset->toggleInsetCursor(bv);
863 } else if (result == DISPATCHED) {
864 the_locking_inset->toggleInsetCursor(bv);
865 updateLocal(bv, CELL, false);
866 the_locking_inset->toggleInsetCursor(bv);
874 // --- Cursor Movements ----------------------------------
876 if (tabular->IsLastCellInRow(actcell))
878 moveRight(bv, false);
879 sel_cell_end = actcell;
880 updateLocal(bv, SELECTION, false);
883 result = moveRight(bv);
884 sel_cell_start = sel_cell_end = actcell;
886 updateLocal(bv, SELECTION, false);
889 if (tabular->IsFirstCellInRow(actcell))
892 sel_cell_end = actcell;
893 updateLocal(bv, SELECTION, false);
896 result = moveLeft(bv);
897 sel_cell_start = sel_cell_end = actcell;
899 updateLocal(bv, SELECTION, false);
903 int const ocell = actcell;
905 if ((ocell == sel_cell_end) ||
906 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
907 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
909 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
910 updateLocal(bv, SELECTION, false);
914 result = moveDown(bv, old_locking_inset != 0);
915 sel_cell_start = sel_cell_end = actcell;
917 updateLocal(bv, SELECTION, false);
921 int const ocell = actcell;
923 if ((ocell == sel_cell_end) ||
924 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
925 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
927 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
928 updateLocal(bv, SELECTION, false);
932 result = moveUp(bv, old_locking_inset != 0);
933 sel_cell_start = sel_cell_end = actcell;
935 updateLocal(bv, SELECTION, false);
939 if (the_locking_inset) {
940 unlockInsetInInset(bv, the_locking_inset);
941 the_locking_inset = 0;
943 if (bv->text->first + bv->painter().paperHeight() <
944 (top_baseline + tabular->GetHeightOfTabular()))
946 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
947 updateLocal(bv, FULL, false);
948 actcell = tabular->GetCellBelow(first_visible_cell) + column;
950 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
953 updateLocal(bv, CURSOR, false);
958 if (the_locking_inset) {
959 unlockInsetInInset(bv, the_locking_inset);
960 the_locking_inset = 0;
962 if (top_baseline < 0) {
963 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
964 updateLocal(bv, FULL, false);
965 if (top_baseline > 0)
968 actcell = tabular->GetCellBelow(first_visible_cell) + column;
973 updateLocal(bv, CURSOR, false);
984 case LFUN_LAYOUT_TABULAR:
986 bv->owner()->getDialogs()->showTabular(this);
989 case LFUN_TABULAR_FEATURE:
990 if (!tabularFeatures(bv, arg))
991 result = UNDISPATCHED;
994 if (!copySelection(bv))
996 setUndo(bv, Undo::DELETE,
997 bv->text->cursor.par(),
998 bv->text->cursor.par()->next());
1000 updateLocal(bv, INIT, true);
1003 if (!hasSelection())
1008 case LFUN_PASTESELECTION:
1010 string const clip(bv->getClipboard());
1014 if (clip.find('\t') != string::npos) {
1018 unsigned int len = clip.length();
1019 string::size_type p = 0;
1022 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1030 maxCols = max(cols, maxCols);
1036 maxCols = max(cols, maxCols);
1037 delete paste_tabular;
1038 paste_tabular = new LyXTabular(this, rows, maxCols);
1039 string::size_type op = 0;
1041 int cells = paste_tabular->GetNumberOfCells();
1043 while((cell < cells) && (p < len) &&
1044 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1049 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1054 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1055 while(cols++ < maxCols)
1063 // check for the last cell if there is no trailing '\n'
1064 if ((cell < cells) && (op < len))
1065 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1067 // so that the clipboard is used and it goes on
1069 // and executes LFUN_PASTESELECTION in insettext!
1070 delete paste_tabular;
1075 if (hasPasteBuffer()) {
1076 setUndo(bv, Undo::INSERT,
1077 bv->text->cursor.par(),
1078 bv->text->cursor.par()->next());
1080 updateLocal(bv, INIT, true);
1083 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1085 // we try to activate the actual inset and put this event down to
1086 // the insets dispatch function.
1087 result = UNDISPATCHED;
1088 if (the_locking_inset)
1091 if (activateCellInset(bv)) {
1092 // reset need_update setted in above function!
1094 result = the_locking_inset->localDispatch(bv, action, arg);
1095 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1096 unlockInsetInInset(bv, the_locking_inset);
1098 the_locking_inset = 0;
1099 // we need to update if this was requested before
1100 updateLocal(bv, NONE, false);
1101 return UNDISPATCHED;
1106 updateLocal(bv, CELL, false);
1111 if (result!=FINISHED) {
1112 if (!the_locking_inset) {
1113 showInsetCursor(bv);
1116 bv->unlockInset(this);
1121 int InsetTabular::latex(Buffer const * buf, ostream & os,
1122 bool fragile, bool fp) const
1124 return tabular->Latex(buf, os, fragile, fp);
1128 int InsetTabular::ascii(Buffer const * buf, ostream & os, int) const
1130 // This should be changed to a real ascii export
1131 return tabular->Ascii(buf, os);
1135 int InsetTabular::linuxdoc(Buffer const *, ostream &) const
1141 int InsetTabular::docBook(Buffer const * buf, ostream & os) const
1143 return tabular->DocBook(buf,os);
1147 void InsetTabular::validate(LaTeXFeatures & features) const
1149 tabular->Validate(features);
1153 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1154 LyXFont const & font,
1161 bool changed = false;
1163 // if we have a locking_inset we should have to check only this cell for
1164 // change so I'll try this to have a boost, but who knows ;)
1165 if ((need_update != INIT) &&
1166 (the_locking_inset == tabular->GetCellInset(actcell))) {
1167 for(int i = 0; i < tabular->columns(); ++i) {
1168 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1170 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1173 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1174 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1175 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1181 for (int i = 0; i < tabular->rows(); ++i) {
1184 for (int j = 0; j < tabular->columns(); ++j) {
1185 if (tabular->IsPartOfMultiColumn(i,j))
1188 inset = tabular->GetCellInset(cell);
1190 inset->update(bv, font, false);
1191 maxAsc = max(maxAsc, inset->ascent(bv, font));
1192 maxDesc = max(maxDesc, inset->descent(bv, font));
1193 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1195 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1196 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1202 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1204 if (the_locking_inset) {
1205 the_locking_inset->getCursorPos(bv, x, y);
1208 x = cursor_.x() - top_x;
1213 void InsetTabular::toggleInsetCursor(BufferView * bv)
1215 if (the_locking_inset) {
1216 the_locking_inset->toggleInsetCursor(bv);
1220 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1222 int const asc = lyxfont::maxAscent(font);
1223 int const desc = lyxfont::maxDescent(font);
1225 if (isCursorVisible())
1226 bv->hideLockedInsetCursor();
1228 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1229 toggleCursorVisible();
1233 void InsetTabular::showInsetCursor(BufferView * bv, bool show)
1235 if (!isCursorVisible()) {
1236 LyXFont font; // = GetFont(par, cursor.pos);
1238 int const asc = lyxfont::maxAscent(font);
1239 int const desc = lyxfont::maxDescent(font);
1240 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1242 bv->showLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1243 setCursorVisible(true);
1248 void InsetTabular::hideInsetCursor(BufferView * bv)
1250 if (isCursorVisible()) {
1251 bv->hideLockedInsetCursor();
1252 setCursorVisible(false);
1257 void InsetTabular::fitInsetCursor(BufferView * bv) const
1259 if (the_locking_inset) {
1260 the_locking_inset->fitInsetCursor(bv);
1265 int const asc = lyxfont::maxAscent(font);
1266 int const desc = lyxfont::maxDescent(font);
1267 bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc);
1271 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1275 actcell = actrow = actcol = 0;
1276 int ly = tabular->GetDescentOfRow(actrow);
1278 // first search the right row
1279 while((ly < y) && (actrow < tabular->rows())) {
1280 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1281 tabular->GetAscentOfRow(actrow + 1) +
1282 tabular->GetAdditionalHeight(actrow + 1));
1284 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1286 actcell = tabular->GetCellNumber(actrow, actcol);
1288 // now search the right column
1289 int lx = tabular->GetWidthOfColumn(actcell) -
1290 tabular->GetAdditionalWidth(actcell);
1292 #ifdef WITH_WARNINGS
1293 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1295 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1296 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1297 tabular->GetAdditionalWidth(actcell - 1));
1299 // Jürgen, you should check that this is correct. (Lgb)
1300 #warning Jürgen, please check. (Lgb)
1301 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1302 lx += tabular->GetWidthOfColumn(actcell + 1)
1303 + tabular->GetAdditionalWidth(actcell);
1307 cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1312 int InsetTabular::getCellXPos(int cell) const
1316 for (; !tabular->IsFirstCellInRow(c); --c)
1318 int lx = tabular->GetWidthOfColumn(cell);
1319 for (; c < cell; ++c) {
1320 lx += tabular->GetWidthOfColumn(c);
1322 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1326 void InsetTabular::resetPos(BufferView * bv) const
1330 actcol = tabular->column_of_cell(actcell);
1335 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1336 if (tabular->IsLastCellInRow(cell)) {
1337 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1338 tabular->GetAscentOfRow(actrow + 1) +
1339 tabular->GetAdditionalHeight(actrow + 1));
1343 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1344 int new_x = getCellXPos(actcell);
1345 int old_x = cursor_.x();
1348 // cursor.x(getCellXPos(actcell) + offset);
1349 if ((actcol < tabular->columns()-1) && scroll(false) &&
1350 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1353 updateLocal(bv, FULL, false);
1354 } else if (the_locking_inset &&
1355 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1357 int xx = cursor_.x() - offset + bv->text->getRealCursorX(bv);
1358 if (xx > (bv->workWidth()-20)) {
1359 scroll(bv, -(xx - bv->workWidth() + 60));
1360 updateLocal(bv, FULL, false);
1361 } else if (xx < 20) {
1367 updateLocal(bv, FULL, false);
1369 } else if ((cursor_.x() - offset) > 20 &&
1370 (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1371 > (bv->workWidth() - 20)) {
1372 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1373 updateLocal(bv, FULL, false);
1374 } else if ((cursor_.x() - offset) < 20) {
1375 scroll(bv, 20 - cursor_.x() + offset);
1376 updateLocal(bv, FULL, false);
1377 } else if (scroll(false) && top_x > 20 &&
1378 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1379 scroll(bv, old_x - cursor_.x());
1380 updateLocal(bv, FULL, false);
1382 if ((!the_locking_inset ||
1383 !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1384 actcell != oldcell) {
1385 InsetTabular * inset = const_cast<InsetTabular *>(this);
1386 bv->owner()->getDialogs()->updateTabular(inset);
1392 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1394 if (lock && !old_locking_inset) {
1395 if (activateCellInset(bv))
1398 bool moved = isRightToLeft(bv)
1399 ? movePrevCell(bv) : moveNextCell(bv);
1402 if (lock && activateCellInset(bv))
1406 return DISPATCHED_NOUPDATE;
1410 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1412 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1415 if (lock) { // behind the inset
1416 if (activateCellInset(bv, 0, 0, 0, true))
1420 return DISPATCHED_NOUPDATE;
1424 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1426 int const ocell = actcell;
1427 actcell = tabular->GetCellAbove(actcell);
1428 if (actcell == ocell) // we moved out of the inset
1434 if (old_locking_inset) {
1435 old_locking_inset->getCursorPos(bv, x, y);
1436 x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1438 if (activateCellInset(bv, x, 0))
1441 return DISPATCHED_NOUPDATE;
1445 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1447 int const ocell = actcell;
1448 actcell = tabular->GetCellBelow(actcell);
1449 if (actcell == ocell) // we moved out of the inset
1455 if (old_locking_inset) {
1456 old_locking_inset->getCursorPos(bv, x, y);
1457 x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1459 if (activateCellInset(bv, x, 0))
1462 return DISPATCHED_NOUPDATE;
1466 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1468 if (isRightToLeft(bv)) {
1469 if (tabular->IsFirstCellInRow(actcell)) {
1470 int row = tabular->row_of_cell(actcell);
1471 if (row == tabular->rows() - 1)
1473 actcell = tabular->GetLastCellInRow(row);
1474 actcell = tabular->GetCellBelow(actcell);
1481 if (tabular->IsLastCell(actcell))
1486 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1487 isRightToLeftPar(bv->buffer()->params);
1488 activateCellInset(bv, 0, 0, 0, !rtl);
1495 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1497 if (isRightToLeft(bv)) {
1498 if (tabular->IsLastCellInRow(actcell)) {
1499 int row = tabular->row_of_cell(actcell);
1502 actcell = tabular->GetFirstCellInRow(row);
1503 actcell = tabular->GetCellAbove(actcell);
1505 if (tabular->IsLastCell(actcell))
1510 if (!actcell) // first cell
1515 bool rtl = tabular->GetCellInset(actcell)->paragraph()->
1516 isRightToLeftPar(bv->buffer()->params);
1517 activateCellInset(bv, 0, 0, 0, !rtl);
1524 bool InsetTabular::deletable()
1530 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1535 sel_cell_end = tabular->GetNumberOfCells() - 1;
1537 if (hasSelection()) {
1538 setUndo(bv, Undo::EDIT,
1539 bv->text->cursor.par(),
1540 bv->text->cursor.par()->next());
1541 bool frozen = undo_frozen;
1544 // apply the fontchange on the whole selection
1549 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1550 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1551 for(int j = sel_col_start; j <= sel_col_end; ++j) {
1552 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1557 updateLocal(bv, INIT, true);
1559 if (the_locking_inset)
1560 the_locking_inset->setFont(bv, font, tall);
1564 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1566 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1569 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1570 string const tmp = tabularFeature[i].feature;
1572 if (tmp == what.substr(0, tmp.length())) {
1573 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1574 //tabularFeatures[i].feature.length())) {
1575 action = tabularFeature[i].action;
1579 if (action == LyXTabular::LAST_ACTION)
1583 frontStrip(what.substr(tabularFeature[i].feature.length()));
1584 tabularFeatures(bv, action, val);
1589 void InsetTabular::tabularFeatures(BufferView * bv,
1590 LyXTabular::Feature feature,
1591 string const & value)
1597 bool setLines = false;
1598 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1599 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1602 case LyXTabular::M_ALIGN_LEFT:
1603 case LyXTabular::ALIGN_LEFT:
1604 setAlign=LYX_ALIGN_LEFT;
1606 case LyXTabular::M_ALIGN_RIGHT:
1607 case LyXTabular::ALIGN_RIGHT:
1608 setAlign=LYX_ALIGN_RIGHT;
1610 case LyXTabular::M_ALIGN_CENTER:
1611 case LyXTabular::ALIGN_CENTER:
1612 setAlign=LYX_ALIGN_CENTER;
1614 case LyXTabular::M_VALIGN_TOP:
1615 case LyXTabular::VALIGN_TOP:
1616 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1618 case LyXTabular::M_VALIGN_BOTTOM:
1619 case LyXTabular::VALIGN_BOTTOM:
1620 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1622 case LyXTabular::M_VALIGN_CENTER:
1623 case LyXTabular::VALIGN_CENTER:
1624 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1629 if (hasSelection()) {
1630 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1632 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1633 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1635 setUndo(bv, Undo::FINISH,
1636 bv->text->cursor.par(),
1637 bv->text->cursor.par()->next());
1639 int row = tabular->row_of_cell(actcell);
1640 int column = tabular->column_of_cell(actcell);
1644 case LyXTabular::SET_PWIDTH:
1646 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1647 tabular->SetColumnPWidth(actcell,value);
1649 for (int i = 0; i < tabular->rows(); ++i) {
1650 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1653 updateLocal(bv, INIT, true);
1657 case LyXTabular::SET_MPWIDTH:
1659 bool const update = (tabular->GetPWidth(actcell) != value);
1660 tabular->SetMColumnPWidth(actcell,value);
1662 for (int i = 0; i < tabular->rows(); ++i) {
1663 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1666 updateLocal(bv, INIT, true);
1670 case LyXTabular::SET_SPECIAL_COLUMN:
1671 case LyXTabular::SET_SPECIAL_MULTI:
1672 tabular->SetAlignSpecial(actcell,value,feature);
1674 case LyXTabular::APPEND_ROW:
1675 // append the row into the tabular
1676 unlockInsetInInset(bv, the_locking_inset);
1677 tabular->AppendRow(actcell);
1678 updateLocal(bv, INIT, true);
1680 case LyXTabular::APPEND_COLUMN:
1681 // append the column into the tabular
1682 unlockInsetInInset(bv, the_locking_inset);
1683 tabular->AppendColumn(actcell);
1684 actcell = tabular->GetCellNumber(row, column);
1685 updateLocal(bv, INIT, true);
1687 case LyXTabular::DELETE_ROW:
1688 unlockInsetInInset(bv, the_locking_inset);
1689 tabular->DeleteRow(tabular->row_of_cell(actcell));
1690 if ((row+1) > tabular->rows())
1692 actcell = tabular->GetCellNumber(row, column);
1694 updateLocal(bv, INIT, true);
1696 case LyXTabular::DELETE_COLUMN:
1697 unlockInsetInInset(bv, the_locking_inset);
1698 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1699 if ((column+1) > tabular->columns())
1701 actcell = tabular->GetCellNumber(row, column);
1703 updateLocal(bv, INIT, true);
1705 case LyXTabular::M_TOGGLE_LINE_TOP:
1707 case LyXTabular::TOGGLE_LINE_TOP:
1709 bool lineSet = !tabular->TopLine(actcell, flag);
1710 for (int i = sel_row_start; i <= sel_row_end; ++i)
1711 for (int j = sel_col_start; j <= sel_col_end; ++j)
1712 tabular->SetTopLine(
1713 tabular->GetCellNumber(i, j),
1715 updateLocal(bv, INIT, true);
1719 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1721 case LyXTabular::TOGGLE_LINE_BOTTOM:
1723 bool lineSet = !tabular->BottomLine(actcell, flag);
1724 for (int i = sel_row_start; i <= sel_row_end; ++i)
1725 for (int j = sel_col_start; j <= sel_col_end; ++j)
1726 tabular->SetBottomLine(
1727 tabular->GetCellNumber(i, j),
1730 updateLocal(bv, INIT, true);
1734 case LyXTabular::M_TOGGLE_LINE_LEFT:
1736 case LyXTabular::TOGGLE_LINE_LEFT:
1738 bool lineSet = !tabular->LeftLine(actcell, flag);
1739 for (int i = sel_row_start; i <= sel_row_end; ++i)
1740 for (int j = sel_col_start; j <= sel_col_end; ++j)
1741 tabular->SetLeftLine(
1742 tabular->GetCellNumber(i,j),
1745 updateLocal(bv, INIT, true);
1749 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1751 case LyXTabular::TOGGLE_LINE_RIGHT:
1753 bool lineSet = !tabular->RightLine(actcell, flag);
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->SetRightLine(
1757 tabular->GetCellNumber(i,j),
1760 updateLocal(bv, INIT, true);
1764 case LyXTabular::M_ALIGN_LEFT:
1765 case LyXTabular::M_ALIGN_RIGHT:
1766 case LyXTabular::M_ALIGN_CENTER:
1768 case LyXTabular::ALIGN_LEFT:
1769 case LyXTabular::ALIGN_RIGHT:
1770 case LyXTabular::ALIGN_CENTER:
1771 for (int i = sel_row_start; i <= sel_row_end; ++i)
1772 for (int j = sel_col_start; j <= sel_col_end; ++j)
1773 tabular->SetAlignment(
1774 tabular->GetCellNumber(i, j),
1777 updateLocal(bv, INIT, true);
1779 case LyXTabular::M_VALIGN_TOP:
1780 case LyXTabular::M_VALIGN_BOTTOM:
1781 case LyXTabular::M_VALIGN_CENTER:
1783 case LyXTabular::VALIGN_TOP:
1784 case LyXTabular::VALIGN_BOTTOM:
1785 case LyXTabular::VALIGN_CENTER:
1786 for (int i = sel_row_start; i <= sel_row_end; ++i)
1787 for (int j = sel_col_start; j <= sel_col_end; ++j)
1788 tabular->SetVAlignment(
1789 tabular->GetCellNumber(i, j),
1791 updateLocal(bv, INIT, true);
1793 case LyXTabular::MULTICOLUMN:
1795 if (sel_row_start != sel_row_end) {
1796 WriteAlert(_("Impossible Operation!"),
1797 _("Multicolumns can only be horizontally."),
1801 // just multicol for one Single Cell
1802 if (!hasSelection()) {
1803 // check wether we are completly in a multicol
1804 if (tabular->IsMultiColumn(actcell)) {
1805 tabular->UnsetMultiColumn(actcell);
1806 updateLocal(bv, INIT, true);
1808 tabular->SetMultiColumn(actcell, 1);
1809 updateLocal(bv, CELL, true);
1813 // we have a selection so this means we just add all this
1814 // cells to form a multicolumn cell
1818 if (sel_cell_start > sel_cell_end) {
1819 s_start = sel_cell_end;
1820 s_end = sel_cell_start;
1822 s_start = sel_cell_start;
1823 s_end = sel_cell_end;
1825 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1827 sel_cell_end = sel_cell_start;
1828 updateLocal(bv, INIT, true);
1831 case LyXTabular::SET_ALL_LINES:
1833 case LyXTabular::UNSET_ALL_LINES:
1834 for (int i = sel_row_start; i <= sel_row_end; ++i)
1835 for (int j = sel_col_start; j <= sel_col_end; ++j)
1836 tabular->SetAllLines(
1837 tabular->GetCellNumber(i,j), setLines);
1838 updateLocal(bv, INIT, true);
1840 case LyXTabular::SET_LONGTABULAR:
1841 tabular->SetLongTabular(true);
1842 updateLocal(bv, INIT, true); // because this toggles displayed
1844 case LyXTabular::UNSET_LONGTABULAR:
1845 tabular->SetLongTabular(false);
1846 updateLocal(bv, INIT, true); // because this toggles displayed
1848 case LyXTabular::SET_ROTATE_TABULAR:
1849 tabular->SetRotateTabular(true);
1851 case LyXTabular::UNSET_ROTATE_TABULAR:
1852 tabular->SetRotateTabular(false);
1854 case LyXTabular::SET_ROTATE_CELL:
1855 for (int i = sel_row_start; i <= sel_row_end; ++i)
1856 for (int j = sel_col_start; j<=sel_col_end; ++j)
1857 tabular->SetRotateCell(
1858 tabular->GetCellNumber(i, j),
1861 case LyXTabular::UNSET_ROTATE_CELL:
1862 for (int i = sel_row_start; i <= sel_row_end; ++i)
1863 for (int j = sel_col_start; j <= sel_col_end; ++j)
1864 tabular->SetRotateCell(
1865 tabular->GetCellNumber(i, j), false);
1867 case LyXTabular::SET_USEBOX:
1869 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1870 if (val == tabular->GetUsebox(actcell))
1871 val = LyXTabular::BOX_NONE;
1872 for (int i = sel_row_start; i <= sel_row_end; ++i)
1873 for (int j = sel_col_start; j <= sel_col_end; ++j)
1875 tabular->GetCellNumber(i, j), val);
1878 case LyXTabular::SET_LTFIRSTHEAD:
1879 tabular->SetLTHead(actcell, true);
1881 case LyXTabular::SET_LTHEAD:
1882 tabular->SetLTHead(actcell, false);
1884 case LyXTabular::SET_LTFOOT:
1885 tabular->SetLTFoot(actcell, false);
1887 case LyXTabular::SET_LTLASTFOOT:
1888 tabular->SetLTFoot(actcell, true);
1890 case LyXTabular::SET_LTNEWPAGE:
1892 bool what = !tabular->GetLTNewPage(actcell);
1893 tabular->SetLTNewPage(actcell, what);
1896 // dummy stuff just to avoid warnings
1897 case LyXTabular::LAST_ACTION:
1903 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, int button,
1906 UpdatableInset * inset =
1907 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1908 LyXFont font(LyXFont::ALL_SANE);
1910 x = inset->x() + inset->width(bv, font);
1911 y = inset->descent(bv, font);
1913 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1914 //inset_y = cursor.y();
1915 inset->edit(bv, x, y, button);
1916 if (!the_locking_inset)
1918 updateLocal(bv, CELL, false);
1919 return (the_locking_inset != 0);
1923 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
1926 inset_x = cursor_.x()
1927 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1928 inset_y = cursor_.y();
1929 return activateCellInset(bv, x - inset_x, y - inset_y, button);
1933 bool InsetTabular::insetHit(BufferView *, int x, int) const
1936 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
1940 // This returns paperWidth() if the cell-width is unlimited or the width
1941 // in pixels if we have a pwidth for this cell.
1942 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
1944 string const s = tabular->GetPWidth(cell);
1948 return VSpace(s).inPixels(bv);
1952 int InsetTabular::getMaxWidth(BufferView * bv,
1953 UpdatableInset const * inset) const
1955 int cell = tabular->cur_cell;
1956 if (tabular->GetCellInset(cell) != inset) {
1958 if (tabular->GetCellInset(cell) != inset) {
1960 lyxerr << "Actcell not equal to actual cell!\n";
1965 int const n = tabular->GetNumberOfCells();
1968 for (cell = 0; cell < n; ++cell) {
1969 if (tabular->GetCellInset(cell) == inset)
1975 lyxerr << "Own inset not found, shouldn't really happen!\n";
1979 int w = getMaxWidthOfCell(bv, cell);
1981 // because the inset then subtracts it's top_x and owner->x()
1982 w += (inset->x() - top_x);
1989 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1991 resizeLyXText(bv, recursive);
1995 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
1998 for(int i = 0; i < tabular->rows(); ++i) {
1999 for(int j = 0; j < tabular->columns(); ++j) {
2000 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2008 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2009 bool const recursive) const
2011 if (the_locking_inset)
2012 return the_locking_inset->getLyXText(bv, recursive);
2014 // if we're locked lock the actual insettext and return it's LyXText!!!
2016 UpdatableInset * inset =
2017 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2018 inset->edit(const_cast<BufferView *>(bv), 0, 0, 0);
2019 return the_locking_inset->getLyXText(bv, recursive);
2022 return Inset::getLyXText(bv, recursive);
2026 bool InsetTabular::showInsetDialog(BufferView * bv) const
2028 if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv))
2029 bv->owner()->getDialogs()
2030 ->showTabular(const_cast<InsetTabular *>(this));
2035 void InsetTabular::openLayoutDialog(BufferView * bv) const
2037 if (the_locking_inset) {
2038 InsetTabular * i = static_cast<InsetTabular *>
2039 (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2041 i->openLayoutDialog(bv);
2045 bv->owner()->getDialogs()->showTabular(
2046 const_cast<InsetTabular *>(this));
2051 // functions returns:
2055 // 3 ... toggled off
2057 func_status::value_type InsetTabular::getStatus(string const & what) const
2059 int action = LyXTabular::LAST_ACTION;
2060 func_status::value_type status = func_status::OK;
2063 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2064 string const tmp = tabularFeature[i].feature;
2065 if (tmp == what.substr(0, tmp.length())) {
2066 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2067 // tabularFeatures[i].feature.length())) {
2068 action = tabularFeature[i].action;
2072 if (action == LyXTabular::LAST_ACTION)
2073 return func_status::Unknown;
2075 string const argument = frontStrip(what.substr(tabularFeature[i].feature.length()));
2082 if (hasSelection()) {
2083 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2085 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2089 case LyXTabular::SET_PWIDTH:
2090 case LyXTabular::SET_MPWIDTH:
2091 case LyXTabular::SET_SPECIAL_COLUMN:
2092 case LyXTabular::SET_SPECIAL_MULTI:
2093 status |= func_status::Disabled;
2096 case LyXTabular::APPEND_ROW:
2097 case LyXTabular::APPEND_COLUMN:
2098 case LyXTabular::DELETE_ROW:
2099 case LyXTabular::DELETE_COLUMN:
2100 case LyXTabular::SET_ALL_LINES:
2101 case LyXTabular::UNSET_ALL_LINES:
2102 status |= func_status::OK;
2105 case LyXTabular::MULTICOLUMN:
2106 if (tabular->IsMultiColumn(actcell))
2107 status |= func_status::ToggleOn;
2109 status |= func_status::ToggleOff;
2111 case LyXTabular::M_TOGGLE_LINE_TOP:
2113 case LyXTabular::TOGGLE_LINE_TOP:
2114 if (tabular->TopLine(actcell, flag))
2115 status |= func_status::ToggleOn;
2117 status |= func_status::ToggleOff;
2119 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2121 case LyXTabular::TOGGLE_LINE_BOTTOM:
2122 if (tabular->BottomLine(actcell, flag))
2123 status |= func_status::ToggleOn;
2125 status |= func_status::ToggleOff;
2127 case LyXTabular::M_TOGGLE_LINE_LEFT:
2129 case LyXTabular::TOGGLE_LINE_LEFT:
2130 if (tabular->LeftLine(actcell, flag))
2131 status |= func_status::ToggleOn;
2133 status |= func_status::ToggleOff;
2135 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2137 case LyXTabular::TOGGLE_LINE_RIGHT:
2138 if (tabular->RightLine(actcell, flag))
2139 status |= func_status::ToggleOn;
2141 status |= func_status::ToggleOff;
2143 case LyXTabular::M_ALIGN_LEFT:
2145 case LyXTabular::ALIGN_LEFT:
2146 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2147 status |= func_status::ToggleOn;
2149 status |= func_status::ToggleOff;
2151 case LyXTabular::M_ALIGN_RIGHT:
2153 case LyXTabular::ALIGN_RIGHT:
2154 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2155 status |= func_status::ToggleOn;
2157 status |= func_status::ToggleOff;
2159 case LyXTabular::M_ALIGN_CENTER:
2161 case LyXTabular::ALIGN_CENTER:
2162 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2163 status |= func_status::ToggleOn;
2165 status |= func_status::ToggleOff;
2167 case LyXTabular::M_VALIGN_TOP:
2169 case LyXTabular::VALIGN_TOP:
2170 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2171 status |= func_status::ToggleOn;
2173 status |= func_status::ToggleOff;
2175 case LyXTabular::M_VALIGN_BOTTOM:
2177 case LyXTabular::VALIGN_BOTTOM:
2178 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2179 status |= func_status::ToggleOn;
2181 status |= func_status::ToggleOff;
2183 case LyXTabular::M_VALIGN_CENTER:
2185 case LyXTabular::VALIGN_CENTER:
2186 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2187 status |= func_status::ToggleOn;
2189 status |= func_status::ToggleOff;
2191 case LyXTabular::SET_LONGTABULAR:
2192 if (tabular->IsLongTabular())
2193 status |= func_status::ToggleOn;
2195 status |= func_status::ToggleOff;
2197 case LyXTabular::UNSET_LONGTABULAR:
2198 if (!tabular->IsLongTabular())
2199 status |= func_status::ToggleOn;
2201 status |= func_status::ToggleOff;
2203 case LyXTabular::SET_ROTATE_TABULAR:
2204 if (tabular->GetRotateTabular())
2205 status |= func_status::ToggleOn;
2207 status |= func_status::ToggleOff;
2209 case LyXTabular::UNSET_ROTATE_TABULAR:
2210 if (!tabular->GetRotateTabular())
2211 status |= func_status::ToggleOn;
2213 status |= func_status::ToggleOff;
2215 case LyXTabular::SET_ROTATE_CELL:
2216 if (tabular->GetRotateCell(actcell))
2217 status |= func_status::ToggleOn;
2219 status |= func_status::ToggleOff;
2221 case LyXTabular::UNSET_ROTATE_CELL:
2222 if (!tabular->GetRotateCell(actcell))
2223 status |= func_status::ToggleOn;
2225 status |= func_status::ToggleOff;
2227 case LyXTabular::SET_USEBOX:
2228 if (strToInt(argument) == tabular->GetUsebox(actcell))
2229 status |= func_status::ToggleOn;
2231 status |= func_status::ToggleOff;
2233 case LyXTabular::SET_LTFIRSTHEAD:
2234 if (tabular->GetRowOfLTHead(actcell, dummy))
2235 status |= func_status::ToggleOn;
2237 status |= func_status::ToggleOff;
2239 case LyXTabular::SET_LTHEAD:
2240 if (tabular->GetRowOfLTHead(actcell, dummy))
2241 status |= func_status::ToggleOn;
2243 status |= func_status::ToggleOff;
2245 case LyXTabular::SET_LTFOOT:
2246 if (tabular->GetRowOfLTFoot(actcell, dummy))
2247 status |= func_status::ToggleOn;
2249 status |= func_status::ToggleOff;
2251 case LyXTabular::SET_LTLASTFOOT:
2252 if (tabular->GetRowOfLTFoot(actcell, dummy))
2253 status |= func_status::ToggleOn;
2255 status |= func_status::ToggleOff;
2257 case LyXTabular::SET_LTNEWPAGE:
2258 if (tabular->GetLTNewPage(actcell))
2259 status |= func_status::ToggleOn;
2261 status |= func_status::ToggleOff;
2264 status = func_status::Disabled;
2271 std::vector<string> const InsetTabular::getLabelList() const
2273 return tabular->getLabelList();
2277 bool InsetTabular::copySelection(BufferView * bv)
2279 if (!hasSelection())
2282 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2283 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2284 if (sel_col_start > sel_col_end) {
2285 sel_col_start = sel_col_end;
2286 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2288 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2290 int const columns = sel_col_end - sel_col_start + 1;
2292 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2293 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2294 if (sel_row_start > sel_row_end) {
2295 swap(sel_row_start, sel_row_end);
2297 int const rows = sel_row_end - sel_row_start + 1;
2299 delete paste_tabular;
2300 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2301 for (int i = 0; i < sel_row_start; ++i)
2302 paste_tabular->DeleteRow(0);
2303 while(paste_tabular->rows() > rows)
2304 paste_tabular->DeleteRow(rows);
2305 paste_tabular->SetTopLine(0, true, true);
2306 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2308 for (int i = 0; i < sel_col_start; ++i)
2309 paste_tabular->DeleteColumn(0);
2310 while (paste_tabular->columns() > columns)
2311 paste_tabular->DeleteColumn(columns);
2312 paste_tabular->SetLeftLine(0, true, true);
2313 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2317 paste_tabular->Ascii(bv->buffer(), sstr);
2318 bv->stuffClipboard(sstr.str().c_str());
2323 bool InsetTabular::pasteSelection(BufferView * bv)
2328 for (int r1 = 0, r2 = actrow;
2329 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2331 for(int c1 = 0, c2 = actcol;
2332 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2334 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2335 tabular->IsPartOfMultiColumn(r2,c2))
2337 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2341 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2345 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2346 int const n2 = tabular->GetCellNumber(r2, c2);
2347 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2348 tabular->GetCellInset(n2)->setOwner(this);
2349 tabular->GetCellInset(n2)->deleteLyXText(bv);
2356 bool InsetTabular::cutSelection()
2358 if (!hasSelection())
2361 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2362 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2363 if (sel_col_start > sel_col_end) {
2364 sel_col_start = sel_col_end;
2365 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2367 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2369 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2370 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2371 if (sel_row_start > sel_row_end) {
2372 swap(sel_row_start, sel_row_end);
2374 if (sel_cell_start > sel_cell_end) {
2375 swap(sel_cell_start, sel_cell_end);
2377 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2378 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2379 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2386 bool InsetTabular::isRightToLeft(BufferView *bv )
2388 return bv->getParentLanguage(this)->RightToLeft();
2392 bool InsetTabular::nodraw() const
2394 if (!UpdatableInset::nodraw() && the_locking_inset)
2395 return the_locking_inset->nodraw();
2396 return UpdatableInset::nodraw();
2400 int InsetTabular::scroll(bool recursive) const
2402 int sx = UpdatableInset::scroll(false);
2404 if (recursive && the_locking_inset)
2405 sx += the_locking_inset->scroll(recursive);
2411 bool InsetTabular::doClearArea() const
2413 return !locked || (need_update & (FULL|INIT));
2417 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2419 srow = tabular->row_of_cell(sel_cell_start);
2420 erow = tabular->row_of_cell(sel_cell_end);
2424 scol = tabular->column_of_cell(sel_cell_start);
2425 ecol = tabular->column_of_cell(sel_cell_end);
2429 ecol = tabular->right_column_of_cell(sel_cell_end);
2433 Paragraph * InsetTabular::getParFromID(int id) const
2436 for(int i = 0; i < tabular->rows(); ++i) {
2437 for(int j = 0; j < tabular->columns(); ++j) {
2438 if ((result = tabular->GetCellInset(i, j)->getParFromID(id)))
2446 Paragraph * InsetTabular::firstParagraph() const
2448 if (the_locking_inset)
2449 return the_locking_inset->firstParagraph();
2454 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2456 if (the_locking_inset)
2457 return the_locking_inset->cursor(bv);
2458 return Inset::cursor(bv);
2462 Inset * InsetTabular::getInsetFromID(int id_arg) const
2465 return const_cast<InsetTabular *>(this);
2468 for(int i=0; i < tabular->rows(); ++i) {
2469 for(int j=0; j < tabular->columns(); ++j) {
2470 if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2478 string const InsetTabular::selectNextWord(BufferView * bv, float & value) const
2480 if (the_locking_inset) {
2481 string const str(the_locking_inset->selectNextWord(bv, value));
2484 if (tabular->IsLastCell(actcell)) {
2485 bv->unlockInset(const_cast<InsetTabular *>(this));
2491 // otherwise we have to lock the next inset and ask for it's selecttion
2492 UpdatableInset * inset =
2493 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2494 inset->edit(bv, 0, 0, 0);
2495 string const str(selectNextWordInt(bv, value));
2503 string InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2505 // when entering this function the inset should be ALWAYS locked!
2506 lyx::Assert(the_locking_inset);
2508 string const str(the_locking_inset->selectNextWord(bv, value));
2512 if (tabular->IsLastCell(actcell)) {
2513 bv->unlockInset(const_cast<InsetTabular *>(this));
2517 // otherwise we have to lock the next inset and ask for it's selecttion
2518 UpdatableInset * inset =
2519 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2520 inset->edit(bv, 0, 0, 0);
2521 return selectNextWordInt(bv, value);
2525 void InsetTabular::selectSelectedWord(BufferView * bv)
2527 if (the_locking_inset) {
2528 the_locking_inset->selectSelectedWord(bv);
2535 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2537 if (the_locking_inset) {
2538 the_locking_inset->toggleSelection(bv, kill_selection);
2543 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2544 bool const & cs, bool const & mw)
2546 if (the_locking_inset) {
2547 if (the_locking_inset->searchForward(bv, str, cs, mw))
2549 if (tabular->IsLastCell(actcell)) {
2550 bv->unlockInset(const_cast<InsetTabular *>(this));
2555 // otherwise we have to lock the next inset and search there
2556 UpdatableInset * inset =
2557 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2560 bool const res = searchForward(bv, str, cs, mw);
2561 updateLocal(bv, NONE, false);
2562 bv->updateInset(const_cast<InsetTabular *>(this), false);
2565 return searchForward(bv, str, cs, mw);
2570 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2571 bool const & cs, bool const & mw)
2573 if (the_locking_inset) {
2574 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2576 if (!actcell) { // we are already in the first cell
2577 bv->unlockInset(const_cast<InsetTabular *>(this));
2582 // otherwise we have to lock the next inset and search there
2583 UpdatableInset * inset =
2584 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2585 inset->edit(bv, false);
2587 bool const res = searchBackward(bv, str, cs, mw);
2588 bv->updateInset(const_cast<InsetTabular *>(this), false);
2591 return searchBackward(bv, str, cs, mw);