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"
28 #include "LaTeXFeatures.h"
32 #include "lyx_gui_misc.h"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
40 #include "BufferView.h"
52 const int ADD_TO_HEIGHT = 2;
53 const int ADD_TO_TABULAR_WIDTH = 2;
55 LyXTabular * paste_tabular = 0;
58 struct tabular_features {
59 LyXTabular::Feature action;
63 //tabular_features * tabularFeatures = 0;
65 tabular_features tabularFeatures[] =
67 { LyXTabular::APPEND_ROW, "append-row" },
68 { LyXTabular::APPEND_COLUMN, "append-column" },
69 { LyXTabular::DELETE_ROW, "delete-row" },
70 { LyXTabular::DELETE_COLUMN, "delete-column" },
71 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
72 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
73 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
74 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
75 { LyXTabular::ALIGN_LEFT, "align-left" },
76 { LyXTabular::ALIGN_RIGHT, "align-right" },
77 { LyXTabular::ALIGN_CENTER, "align-center" },
78 { LyXTabular::VALIGN_TOP, "valign-top" },
79 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
80 { LyXTabular::VALIGN_CENTER, "valign-center" },
81 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
82 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
83 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
84 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
85 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
86 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
87 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
88 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
89 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
90 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
91 { LyXTabular::MULTICOLUMN, "multicolumn" },
92 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
93 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
94 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
95 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
96 { LyXTabular::SET_PWIDTH, "set-pwidth" },
97 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
98 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
99 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
100 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
101 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
102 { LyXTabular::SET_USEBOX, "set-usebox" },
103 { LyXTabular::SET_LTHEAD, "set-lthead" },
104 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
105 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
106 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
107 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
108 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
109 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
110 { LyXTabular::LAST_ACTION, "" }
116 bool InsetTabular::hasPasteBuffer() const
118 return (paste_tabular != 0);
122 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
129 tabular.reset(new LyXTabular(this, rows,columns));
130 // for now make it always display as display() inset
132 the_locking_inset = 0;
133 locked = no_selection = false;
135 actrow = actcell = 0;
141 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
144 tabular.reset(new LyXTabular(this, *(tab.tabular)));
145 the_locking_inset = 0;
146 locked = no_selection = false;
148 actrow = actcell = 0;
149 sel_cell_start = sel_cell_end = 0;
154 InsetTabular::~InsetTabular()
161 Inset * InsetTabular::Clone(Buffer const & buf) const
163 InsetTabular * t = new InsetTabular(*this, buf);
165 //t->tabular = tabular->Clone(t);
166 t->tabular.reset(tabular->Clone(t));
171 void InsetTabular::Write(Buffer const * buf, ostream & os) const
173 os << " Tabular" << endl;
174 tabular->Write(buf, os);
178 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
180 bool const old_format = (lex.GetString() == "\\LyXTable");
184 //tabular = new LyXTabular(buf, this, lex);
185 tabular.reset(new LyXTabular(buf, this, lex));
193 string token = lex.GetString();
194 while (lex.IsOK() && (token != "\\end_inset")) {
196 token = lex.GetString();
198 if (token != "\\end_inset") {
199 lex.printError("Missing \\end_inset at this point. "
205 int InsetTabular::ascent(BufferView *, LyXFont const &) const
207 return tabular->GetAscentOfRow(0);
211 int InsetTabular::descent(BufferView *, LyXFont const &) const
213 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
217 int InsetTabular::width(BufferView *, LyXFont const &) const
219 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
223 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
224 float & x, bool cleared) const
232 if (need_update == INIT) {
233 if (calculate_dimensions_of_cells(bv, font, true))
234 bv->text->status = LyXText::CHANGED_IN_DRAW;
239 Painter & pain = bv->painter();
245 UpdatableInset::draw(bv, font, baseline, x, cleared);
248 x += static_cast<float>(scroll());
250 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
251 (top_x != int(x)) || (top_baseline != baseline))) {
252 int h = ascent(bv, font) + descent(bv, font);
253 int const tx = display() || !owner() ? 0 : top_x;
254 int w = tx ? width(bv, font) : pain.paperWidth();
255 int ty = baseline - ascent(bv, font);
259 if ((ty + h) > pain.paperHeight())
260 h = pain.paperHeight();
261 if ((top_x + w) > pain.paperWidth())
262 w = pain.paperWidth();
263 pain.fillRectangle(tx, ty, w, h);
268 top_baseline = baseline;
269 x += ADD_TO_TABULAR_WIDTH;
273 first_visible_cell = -1;
274 for (i = 0; i < tabular->rows(); ++i) {
276 cell = tabular->GetCellNumber(i, 0);
277 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
278 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
280 baseline += tabular->GetDescentOfRow(i) +
281 tabular->GetAscentOfRow(i + 1) +
282 tabular->GetAdditionalHeight(i + 1);
285 for (j = 0; j < tabular->columns(); ++j) {
286 if (nx > bv->workWidth())
288 if (tabular->IsPartOfMultiColumn(i, j))
290 cx = nx + tabular->GetBeginningOfTextInCell(cell);
291 if (first_visible_cell < 0)
292 first_visible_cell = cell;
294 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 // copute 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);
370 // clear behind the inset
372 int(cx + the_locking_inset->width(bv,font) + 1),
373 baseline - tabular->GetAscentOfRow(i) + 1,
374 tabular->GetWidthOfColumn(cell) -
375 tabular->GetBeginningOfTextInCell(cell) -
376 the_locking_inset->width(bv,font) -
377 tabular->GetAdditionalWidth(cell) - 1,
378 tabular->GetAscentOfRow(i) +
379 tabular->GetDescentOfRow(i) - 1);
382 x -= ADD_TO_TABULAR_WIDTH;
383 x += width(bv, font);
384 if (bv->text->status == LyXText::CHANGED_IN_DRAW) {
386 for(Inset * inset=owner();inset;++i)
387 inset = inset->owner();
388 if (calculate_dimensions_of_cells(bv, font, false))
396 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
397 int row, int cell) const
399 int x2 = x + tabular->GetWidthOfColumn(cell);
402 if (!tabular->TopAlreadyDrawed(cell)) {
403 on_off = !tabular->TopLine(cell);
404 pain.line(x, baseline - tabular->GetAscentOfRow(row),
405 x2, baseline - tabular->GetAscentOfRow(row),
406 on_off ? LColor::tabularonoffline : LColor::tabularline,
407 on_off ? Painter::line_onoffdash : Painter::line_solid);
409 on_off = !tabular->BottomLine(cell);
410 pain.line(x,baseline + tabular->GetDescentOfRow(row),
411 x2, baseline + tabular->GetDescentOfRow(row),
412 on_off ? LColor::tabularonoffline : LColor::tabularline,
413 on_off ? Painter::line_onoffdash : Painter::line_solid);
414 if (!tabular->LeftAlreadyDrawed(cell)) {
415 on_off = !tabular->LeftLine(cell);
416 pain.line(x, baseline - tabular->GetAscentOfRow(row),
417 x, baseline + tabular->GetDescentOfRow(row),
418 on_off ? LColor::tabularonoffline : LColor::tabularline,
419 on_off ? Painter::line_onoffdash : Painter::line_solid);
421 on_off = !tabular->RightLine(cell);
422 pain.line(x2 - tabular->GetAdditionalWidth(cell),
423 baseline - tabular->GetAscentOfRow(row),
424 x2 - tabular->GetAdditionalWidth(cell),
425 baseline + tabular->GetDescentOfRow(row),
426 on_off ? LColor::tabularonoffline : LColor::tabularline,
427 on_off ? Painter::line_onoffdash : Painter::line_solid);
431 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
432 int row, int column, int cell) const
434 int cs = tabular->column_of_cell(sel_cell_start);
435 int ce = tabular->column_of_cell(sel_cell_end);
438 cs = tabular->column_of_cell(sel_cell_end);
440 ce = tabular->right_column_of_cell(sel_cell_end);
443 int rs = tabular->row_of_cell(sel_cell_start);
444 int re = tabular->row_of_cell(sel_cell_end);
445 if (rs > re) swap(rs, re);
447 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
448 int w = tabular->GetWidthOfColumn(cell);
449 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
450 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
451 w, h, LColor::selection);
456 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
460 calculate_dimensions_of_cells(bv, font, true);
462 owner()->update(bv, font, true);
465 if (the_locking_inset)
466 the_locking_inset->update(bv, font, reinit);
468 switch (need_update) {
472 if (calculate_dimensions_of_cells(bv, font, false))
484 string const InsetTabular::EditMessage() const
486 return _("Opened Tabular Inset");
490 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
492 UpdatableInset::Edit(bv, x, y, button);
494 if (!bv->lockInset(this)) {
495 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
499 the_locking_inset = 0;
503 sel_cell_start = sel_cell_end = actcell;
504 bv->text->finishUndo();
505 if (InsetHit(bv, x, y) && (button != 3)) {
506 ActivateCellInsetAbs(bv, x, y, button);
508 // UpdateLocal(bv, NONE, false);
509 // bv->getOwner()->getPopups().updateFormTabular();
513 void InsetTabular::InsetUnlock(BufferView * bv)
515 if (the_locking_inset) {
516 the_locking_inset->InsetUnlock(bv);
517 the_locking_inset = 0;
520 no_selection = false;
523 if (scroll(false) || hasSelection()) {
524 sel_cell_start = sel_cell_end = 0;
528 UpdateLocal(bv, FULL, false);
533 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
534 bool mark_dirty) const
536 if (need_update < what) // only set this if it has greater update
538 if ((what == INIT) && hasSelection())
541 if (need_update != NONE) {
542 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
543 if (locked) // && (what != NONE))
549 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
551 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
556 if (inset == tabular->GetCellInset(actcell)) {
557 lyxerr[Debug::INSETS] << "OK" << endl;
558 the_locking_inset = tabular->GetCellInset(actcell);
560 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
561 inset_y = cursor.y();
563 } else if (the_locking_inset && (the_locking_inset == inset)) {
564 lyxerr[Debug::INSETS] << "OK" << endl;
566 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
567 inset_y = cursor.y();
568 } else if (the_locking_inset) {
569 lyxerr[Debug::INSETS] << "MAYBE" << endl;
570 return the_locking_inset->LockInsetInInset(bv, inset);
572 lyxerr[Debug::INSETS] << "NOT OK" << endl;
577 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
580 if (!the_locking_inset)
582 if (the_locking_inset == inset) {
583 the_locking_inset->InsetUnlock(bv);
584 the_locking_inset = 0;
585 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
590 UpdateLocal(bv, CELL, false);
591 ShowInsetCursor(bv, false);
594 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
595 if (inset->LyxCode() == TABULAR_CODE &&
596 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
597 bv->owner()->getDialogs()->updateTabular(this);
606 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
608 if (!the_locking_inset)
610 if (the_locking_inset != inset) {
611 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
614 UpdateLocal(bv, CELL, false);
619 unsigned int InsetTabular::InsetInInsetY()
621 if (!the_locking_inset)
624 return inset_y + the_locking_inset->InsetInInsetY();
628 UpdatableInset * InsetTabular::GetLockingInset()
630 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
634 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
638 if (the_locking_inset)
639 return the_locking_inset->GetFirstLockingInsetOfType(c);
644 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
646 if (the_locking_inset)
647 return the_locking_inset->InsertInset(bv, inset);
652 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
654 if (hasSelection() && (button == 3))
657 if (hasSelection()) {
659 UpdateLocal(bv, SELECTION, false);
662 no_selection = false;
664 int const ocell = actcell;
665 int const orow = actrow;
670 UpdateLocal(bv, NONE, false);
671 sel_cell_start = sel_cell_end = actcell;
673 if ((ocell != actcell) && the_locking_inset) {
674 the_locking_inset->InsetUnlock(bv);
675 the_locking_inset = 0;
681 bool const inset_hit = InsetHit(bv, x, y);
683 if ((ocell == actcell) && the_locking_inset && inset_hit) {
685 the_locking_inset->InsetButtonPress(bv,
686 x - inset_x, y - inset_y,
689 } else if (the_locking_inset) {
690 the_locking_inset->InsetUnlock(bv);
692 the_locking_inset = 0;
694 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
697 if (inset_hit && bv->theLockingInset()) {
698 if (ActivateCellInsetAbs(bv, x, y, button))
699 the_locking_inset->InsetButtonPress(bv,
709 void InsetTabular::InsetButtonRelease(BufferView * bv,
710 int x, int y, int button)
713 if (the_locking_inset) {
715 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
716 i->InsetButtonRelease(bv, x, y, button);
720 bv->owner()->getDialogs()->showTabular(this);
723 if (the_locking_inset) {
724 the_locking_inset->InsetButtonRelease(bv,
725 x - inset_x, y - inset_y,
729 no_selection = false;
733 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
735 if (the_locking_inset) {
736 the_locking_inset->InsetMotionNotify(bv,
744 int const old_cell = actcell;
747 sel_cell_end = actcell;
748 if (sel_cell_end != old_cell)
749 UpdateLocal(bv, SELECTION, false);
752 no_selection = false;
756 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
758 if (the_locking_inset) {
759 the_locking_inset->InsetKeyPress(xke);
765 UpdatableInset::RESULT
766 InsetTabular::LocalDispatch(BufferView * bv,
770 // We need to save the value of the_locking_inset as the call to
771 // the_locking_inset->LocalDispatch might unlock it.
772 old_locking_inset = the_locking_inset;
773 no_selection = false;
774 UpdatableInset::RESULT result =
775 UpdatableInset::LocalDispatch(bv, action, arg);
776 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
781 if ((action < 0) && arg.empty())
784 bool hs = hasSelection();
787 // this one have priority over the locked InsetText!
792 if (GetFirstLockingInsetOfType(Inset::TABULAR_CODE) != this)
795 if (the_locking_inset) {
796 UnlockInsetInInset(bv, the_locking_inset);
797 the_locking_inset = 0;
799 if (action == LFUN_TAB)
800 moveNextCell(bv, old_locking_inset != 0);
802 movePrevCell(bv, old_locking_inset != 0);
803 sel_cell_start = sel_cell_end = actcell;
805 UpdateLocal(bv, SELECTION, false);
806 if (!the_locking_inset) {
808 return DISPATCHED_NOUPDATE;
812 // this to avoid compiler warnings.
817 if (the_locking_inset) {
818 result=the_locking_inset->LocalDispatch(bv, action, arg);
819 if (result == DISPATCHED_NOUPDATE) {
822 if (sc != scroll()) { // inset has been scrolled
823 the_locking_inset->ToggleInsetCursor(bv);
824 UpdateLocal(bv, FULL, false);
825 the_locking_inset->ToggleInsetCursor(bv);
828 } else if (result == DISPATCHED) {
829 the_locking_inset->ToggleInsetCursor(bv);
830 UpdateLocal(bv, CELL, false);
831 the_locking_inset->ToggleInsetCursor(bv);
833 } else if (result == FINISHED) {
840 // --- Cursor Movements ----------------------------------
842 if (tabular->IsLastCellInRow(actcell))
844 moveRight(bv, false);
845 sel_cell_end = actcell;
846 UpdateLocal(bv, SELECTION, false);
849 result = moveRight(bv);
850 sel_cell_start = sel_cell_end = actcell;
852 UpdateLocal(bv, SELECTION, false);
855 if (tabular->IsFirstCellInRow(actcell))
858 sel_cell_end = actcell;
859 UpdateLocal(bv, SELECTION, false);
862 result = moveLeft(bv);
863 sel_cell_start = sel_cell_end = actcell;
865 UpdateLocal(bv, SELECTION, false);
869 int const ocell = actcell;
871 if ((ocell == sel_cell_end) ||
872 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
873 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
875 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
876 UpdateLocal(bv, SELECTION, false);
880 result = moveDown(bv, old_locking_inset != 0);
881 sel_cell_start = sel_cell_end = actcell;
883 UpdateLocal(bv, SELECTION, false);
887 int const ocell = actcell;
889 if ((ocell == sel_cell_end) ||
890 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
891 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
893 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
894 UpdateLocal(bv, SELECTION, false);
898 result = moveUp(bv, old_locking_inset != 0);
899 sel_cell_start = sel_cell_end = actcell;
901 UpdateLocal(bv, SELECTION, false);
905 if (the_locking_inset) {
906 UnlockInsetInInset(bv, the_locking_inset);
907 the_locking_inset = 0;
909 if (bv->text->first + bv->painter().paperHeight() <
910 (top_baseline + tabular->GetHeightOfTabular()))
912 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
913 UpdateLocal(bv, FULL, false);
914 actcell = tabular->GetCellBelow(first_visible_cell) + column;
916 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
919 UpdateLocal(bv, CURSOR, false);
924 if (the_locking_inset) {
925 UnlockInsetInInset(bv, the_locking_inset);
926 the_locking_inset = 0;
928 if (top_baseline < 0) {
929 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
930 UpdateLocal(bv, FULL, false);
931 if (top_baseline > 0)
934 actcell = tabular->GetCellBelow(first_visible_cell) + column;
939 UpdateLocal(bv, CURSOR, false);
950 case LFUN_LAYOUT_TABULAR:
952 bv->owner()->getDialogs()->showTabular(this);
955 case LFUN_TABULAR_FEATURE:
956 if (!TabularFeatures(bv, arg))
957 result = UNDISPATCHED;
960 if (!copySelection(bv))
962 bv->text->setUndo(bv->buffer(), Undo::DELETE,
963 bv->text->cursor.par()->previous(),
964 bv->text->cursor.par()->next());
966 UpdateLocal(bv, INIT, true);
971 bv->text->finishUndo();
974 case LFUN_PASTESELECTION:
976 string const clip(bv->getClipboard());
980 if (clip.find('\t') != string::npos) {
984 unsigned int len = clip.length();
985 string::size_type p = 0;
988 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
996 maxCols = max(cols, maxCols);
1002 maxCols = max(cols, maxCols);
1003 delete paste_tabular;
1004 paste_tabular = new LyXTabular(this, rows, maxCols);
1005 string::size_type op = 0;
1007 int cells = paste_tabular->GetNumberOfCells();
1009 while((cell < cells) && (p < len) &&
1010 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1015 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1020 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1021 while(cols++ < maxCols)
1029 // check for the last cell if there is no trailing '\n'
1030 if ((cell < cells) && (op < len))
1031 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1033 // so that the clipboard is used and it goes on
1035 // and executes LFUN_PASTESELECTION in insettext!
1036 delete paste_tabular;
1041 if (hasPasteBuffer()) {
1042 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1043 bv->text->cursor.par()->previous(),
1044 bv->text->cursor.par()->next());
1046 UpdateLocal(bv, INIT, true);
1049 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1051 // we try to activate the actual inset and put this event down to
1052 // the insets dispatch function.
1053 result = UNDISPATCHED;
1054 if (the_locking_inset)
1057 if (ActivateCellInset(bv)) {
1058 // reset need_update setted in above function!
1060 result = the_locking_inset->LocalDispatch(bv, action, arg);
1061 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1062 UnlockInsetInInset(bv, the_locking_inset);
1064 the_locking_inset = 0;
1065 // we need to update if this was requested before
1066 UpdateLocal(bv, NONE, false);
1067 return UNDISPATCHED;
1070 // the_locking_inset->ToggleInsetCursor(bv);
1071 UpdateLocal(bv, CELL, false);
1072 // the_locking_inset->ToggleInsetCursor(bv);
1077 if (result!=FINISHED) {
1078 if (!the_locking_inset) {
1079 ShowInsetCursor(bv);
1082 bv->unlockInset(this);
1087 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1088 bool fragile, bool fp) const
1090 return tabular->Latex(buf, os, fragile, fp);
1094 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1096 // This should be changed to a real ascii export
1097 return tabular->Ascii(buf, os);
1101 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1107 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1109 return tabular->DocBook(buf,os);
1113 void InsetTabular::Validate(LaTeXFeatures & features) const
1115 tabular->Validate(features);
1119 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1120 LyXFont const & font,
1127 bool changed = false;
1129 // if we have a locking_inset we should have to check only this cell for
1130 // change so I'll try this to have a boost, but who knows ;)
1131 if ((need_update != INIT) &&
1132 (the_locking_inset == tabular->GetCellInset(actcell))) {
1133 for(int i = 0; i < tabular->columns(); ++i) {
1134 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1136 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1139 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1140 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1141 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1144 for (int i = 0; i < tabular->rows(); ++i) {
1147 for (int j= 0; j < tabular->columns(); ++j) {
1148 if (tabular->IsPartOfMultiColumn(i,j))
1151 inset = tabular->GetCellInset(cell);
1153 inset->update(bv, font, false);
1154 maxAsc = max(maxAsc, inset->ascent(bv, font));
1155 maxDesc = max(maxDesc, inset->descent(bv, font));
1156 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1158 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1159 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1165 void InsetTabular::GetCursorPos(BufferView *,
1166 int & x, int & y) const
1168 x = cursor.x() - top_x;
1173 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1175 if (the_locking_inset) {
1176 the_locking_inset->ToggleInsetCursor(bv);
1180 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1182 int const asc = lyxfont::maxAscent(font);
1183 int const desc = lyxfont::maxDescent(font);
1185 if (isCursorVisible())
1186 bv->hideLockedInsetCursor();
1188 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1189 toggleCursorVisible();
1193 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1195 if (!isCursorVisible()) {
1196 LyXFont font; // = GetFont(par, cursor.pos);
1198 int const asc = lyxfont::maxAscent(font);
1199 int const desc = lyxfont::maxDescent(font);
1200 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1202 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1203 setCursorVisible(true);
1208 void InsetTabular::HideInsetCursor(BufferView * bv)
1210 if (isCursorVisible()) {
1211 bv->hideLockedInsetCursor();
1212 setCursorVisible(false);
1214 // if (cursor_visible)
1215 // ToggleInsetCursor(bv);
1219 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1223 actcell = actrow = actcol = 0;
1224 int ly = tabular->GetDescentOfRow(actrow);
1226 // first search the right row
1227 while((ly < y) && (actrow < tabular->rows())) {
1228 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1229 tabular->GetAscentOfRow(actrow + 1) +
1230 tabular->GetAdditionalHeight(actrow + 1));
1232 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1234 actcell = tabular->GetCellNumber(actrow, actcol);
1236 // now search the right column
1237 int lx = tabular->GetWidthOfColumn(actcell) -
1238 tabular->GetAdditionalWidth(actcell);
1240 #ifdef WITH_WARNINGS
1241 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1243 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1244 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1245 tabular->GetAdditionalWidth(actcell - 1));
1247 // Jürgen, you should check that this is correct. (Lgb)
1248 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1249 lx += tabular->GetWidthOfColumn(actcell + 1)
1250 + tabular->GetAdditionalWidth(actcell);
1254 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1259 int InsetTabular::getCellXPos(int cell) const
1263 for (; !tabular->IsFirstCellInRow(c); --c)
1265 int lx = tabular->GetWidthOfColumn(cell);
1266 for (; c < cell; ++c) {
1267 lx += tabular->GetWidthOfColumn(c);
1269 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1273 void InsetTabular::resetPos(BufferView * bv) const
1277 actcol = tabular->column_of_cell(actcell);
1282 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1283 if (tabular->IsLastCellInRow(cell)) {
1284 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1285 tabular->GetAscentOfRow(actrow + 1) +
1286 tabular->GetAdditionalHeight(actrow + 1));
1290 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1291 int new_x = getCellXPos(actcell);
1292 int old_x = cursor.x();
1295 // cursor.x(getCellXPos(actcell) + offset);
1296 if ((actcol < tabular->columns()-1) && scroll(false) &&
1297 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1300 UpdateLocal(bv, FULL, false);
1301 } else if (the_locking_inset &&
1302 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1304 int xx = cursor.x() - offset + bv->text->getRealCursorX(bv);
1305 if (xx > (bv->workWidth()-20)) {
1306 scroll(bv, -(xx - bv->workWidth() + 60));
1307 UpdateLocal(bv, FULL, false);
1308 } else if (xx < 20) {
1314 UpdateLocal(bv, FULL, false);
1316 } else if ((cursor.x() - offset) > 20 &&
1317 (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1318 > (bv->workWidth() - 20)) {
1319 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1320 UpdateLocal(bv, FULL, false);
1321 } else if ((cursor.x() - offset) < 20) {
1322 scroll(bv, 20 - cursor.x() + offset);
1323 UpdateLocal(bv, FULL, false);
1324 } else if (scroll(false) && top_x > 20 &&
1325 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1326 scroll(bv, old_x - cursor.x());
1327 UpdateLocal(bv, FULL, false);
1329 if ((!the_locking_inset ||
1330 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1331 actcell != oldcell) {
1332 InsetTabular * inset = const_cast<InsetTabular *>(this);
1333 bv->owner()->getDialogs()->updateTabular(inset);
1339 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1341 if (lock && !old_locking_inset) {
1342 if (ActivateCellInset(bv))
1345 bool moved = isRightToLeft(bv)
1346 ? movePrevCell(bv) : moveNextCell(bv);
1349 if (lock && ActivateCellInset(bv))
1353 return DISPATCHED_NOUPDATE;
1357 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1359 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1362 if (lock) { // behind the inset
1363 if (ActivateCellInset(bv, 0, 0, 0, true))
1367 return DISPATCHED_NOUPDATE;
1371 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1373 int const ocell = actcell;
1374 actcell = tabular->GetCellAbove(actcell);
1375 if (actcell == ocell) // we moved out of the inset
1381 if (old_locking_inset) {
1382 old_locking_inset->GetCursorPos(bv, x, y);
1383 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1385 if (ActivateCellInset(bv, x, 0))
1388 return DISPATCHED_NOUPDATE;
1392 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1394 int const ocell = actcell;
1395 actcell = tabular->GetCellBelow(actcell);
1396 if (actcell == ocell) // we moved out of the inset
1402 if (old_locking_inset) {
1403 old_locking_inset->GetCursorPos(bv, x, y);
1404 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1406 if (ActivateCellInset(bv, x, 0))
1409 return DISPATCHED_NOUPDATE;
1413 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1415 if (isRightToLeft(bv)) {
1416 if (tabular->IsFirstCellInRow(actcell)) {
1417 int row = tabular->row_of_cell(actcell);
1418 if (row == tabular->rows() - 1)
1420 actcell = tabular->GetLastCellInRow(row);
1421 actcell = tabular->GetCellBelow(actcell);
1428 if (tabular->IsLastCell(actcell))
1433 bool rtl = tabular->GetCellInset(actcell)->par->
1434 isRightToLeftPar(bv->buffer()->params);
1435 ActivateCellInset(bv, 0, 0, 0, !rtl);
1442 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1444 if (isRightToLeft(bv)) {
1445 if (tabular->IsLastCellInRow(actcell)) {
1446 int row = tabular->row_of_cell(actcell);
1449 actcell = tabular->GetFirstCellInRow(row);
1450 actcell = tabular->GetCellAbove(actcell);
1452 if (tabular->IsLastCell(actcell))
1457 if (!actcell) // first cell
1462 bool rtl = tabular->GetCellInset(actcell)->par->
1463 isRightToLeftPar(bv->buffer()->params);
1464 ActivateCellInset(bv, 0, 0, 0, !rtl);
1471 bool InsetTabular::Delete()
1477 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall,
1482 sel_cell_end = tabular->GetNumberOfCells() - 1;
1484 if (hasSelection()) {
1486 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1487 bv->text->cursor.par()->previous(),
1488 bv->text->cursor.par()->next());
1489 frozen = bv->text->undo_frozen;
1491 bv->text->freezeUndo();
1492 // apply the fontchange on the whole selection
1497 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1498 for(int i=sel_row_start; i <= sel_row_end; ++i) {
1499 for(int j=sel_col_start; j <= sel_col_end; ++j) {
1500 tabular->GetCellInset(i, j)->SetFont(bv, font, tall, true);
1504 bv->text->unFreezeUndo();
1505 UpdateLocal(bv, INIT, true);
1507 if (the_locking_inset)
1508 the_locking_inset->SetFont(bv, font, tall);
1512 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1514 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1517 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1518 string const tmp = tabularFeatures[i].feature;
1520 if (tmp == what.substr(0, tmp.length())) {
1521 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1522 //tabularFeatures[i].feature.length())) {
1523 action = tabularFeatures[i].action;
1527 if (action == LyXTabular::LAST_ACTION)
1531 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1532 TabularFeatures(bv, action, val);
1537 void InsetTabular::TabularFeatures(BufferView * bv,
1538 LyXTabular::Feature feature,
1539 string const & value)
1547 bool setLines = false;
1548 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1549 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1554 case LyXTabular::M_ALIGN_LEFT:
1555 case LyXTabular::ALIGN_LEFT:
1556 setAlign=LYX_ALIGN_LEFT;
1558 case LyXTabular::M_ALIGN_RIGHT:
1559 case LyXTabular::ALIGN_RIGHT:
1560 setAlign=LYX_ALIGN_RIGHT;
1562 case LyXTabular::M_ALIGN_CENTER:
1563 case LyXTabular::ALIGN_CENTER:
1564 setAlign=LYX_ALIGN_CENTER;
1566 case LyXTabular::M_VALIGN_TOP:
1567 case LyXTabular::VALIGN_TOP:
1568 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1570 case LyXTabular::M_VALIGN_BOTTOM:
1571 case LyXTabular::VALIGN_BOTTOM:
1572 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1574 case LyXTabular::M_VALIGN_CENTER:
1575 case LyXTabular::VALIGN_CENTER:
1576 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1581 if (hasSelection()) {
1582 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1584 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1585 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1587 bv->text->setUndo(bv->buffer(), Undo::FINISH,
1588 bv->text->cursor.par()->previous(),
1589 bv->text->cursor.par()->next());
1591 int row = tabular->row_of_cell(actcell);
1592 int column = tabular->column_of_cell(actcell);
1596 case LyXTabular::SET_PWIDTH:
1598 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1599 tabular->SetColumnPWidth(actcell,value);
1601 for (int i = 0; i < tabular->rows(); ++i) {
1602 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1605 UpdateLocal(bv, INIT, true);
1609 case LyXTabular::SET_MPWIDTH:
1611 bool const update = (tabular->GetPWidth(actcell) != value);
1612 tabular->SetMColumnPWidth(actcell,value);
1614 for (int i = 0; i < tabular->rows(); ++i) {
1615 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1618 UpdateLocal(bv, INIT, true);
1622 case LyXTabular::SET_SPECIAL_COLUMN:
1623 case LyXTabular::SET_SPECIAL_MULTI:
1624 tabular->SetAlignSpecial(actcell,value,feature);
1626 case LyXTabular::APPEND_ROW:
1627 // append the row into the tabular
1628 UnlockInsetInInset(bv, the_locking_inset);
1629 tabular->AppendRow(actcell);
1630 UpdateLocal(bv, INIT, true);
1632 case LyXTabular::APPEND_COLUMN:
1633 // append the column into the tabular
1634 UnlockInsetInInset(bv, the_locking_inset);
1635 tabular->AppendColumn(actcell);
1636 actcell = tabular->GetCellNumber(row, column);
1637 UpdateLocal(bv, INIT, true);
1639 case LyXTabular::DELETE_ROW:
1640 UnlockInsetInInset(bv, the_locking_inset);
1641 tabular->DeleteRow(tabular->row_of_cell(actcell));
1642 if ((row+1) > tabular->rows())
1644 actcell = tabular->GetCellNumber(row, column);
1646 UpdateLocal(bv, INIT, true);
1648 case LyXTabular::DELETE_COLUMN:
1649 UnlockInsetInInset(bv, the_locking_inset);
1650 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1651 if ((column+1) > tabular->columns())
1653 actcell = tabular->GetCellNumber(row, column);
1655 UpdateLocal(bv, INIT, true);
1657 case LyXTabular::M_TOGGLE_LINE_TOP:
1659 case LyXTabular::TOGGLE_LINE_TOP:
1661 bool lineSet = !tabular->TopLine(actcell, flag);
1662 for (int i = sel_row_start; i <= sel_row_end; ++i)
1663 for (int j = sel_col_start; j <= sel_col_end; ++j)
1664 tabular->SetTopLine(
1665 tabular->GetCellNumber(i, j),
1667 UpdateLocal(bv, INIT, true);
1671 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1673 case LyXTabular::TOGGLE_LINE_BOTTOM:
1675 bool lineSet = !tabular->BottomLine(actcell, flag);
1676 for (int i = sel_row_start; i <= sel_row_end; ++i)
1677 for (int j = sel_col_start; j <= sel_col_end; ++j)
1678 tabular->SetBottomLine(
1679 tabular->GetCellNumber(i, j),
1682 UpdateLocal(bv, INIT, true);
1686 case LyXTabular::M_TOGGLE_LINE_LEFT:
1688 case LyXTabular::TOGGLE_LINE_LEFT:
1690 bool lineSet = !tabular->LeftLine(actcell, flag);
1691 for (int i = sel_row_start; i <= sel_row_end; ++i)
1692 for (int j = sel_col_start; j <= sel_col_end; ++j)
1693 tabular->SetLeftLine(
1694 tabular->GetCellNumber(i,j),
1697 UpdateLocal(bv, INIT, true);
1701 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1703 case LyXTabular::TOGGLE_LINE_RIGHT:
1705 bool lineSet = !tabular->RightLine(actcell, flag);
1706 for (int i = sel_row_start; i <= sel_row_end; ++i)
1707 for (int j = sel_col_start; j <= sel_col_end; ++j)
1708 tabular->SetRightLine(
1709 tabular->GetCellNumber(i,j),
1712 UpdateLocal(bv, INIT, true);
1716 case LyXTabular::M_ALIGN_LEFT:
1717 case LyXTabular::M_ALIGN_RIGHT:
1718 case LyXTabular::M_ALIGN_CENTER:
1720 case LyXTabular::ALIGN_LEFT:
1721 case LyXTabular::ALIGN_RIGHT:
1722 case LyXTabular::ALIGN_CENTER:
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->SetAlignment(
1726 tabular->GetCellNumber(i, j),
1729 UpdateLocal(bv, INIT, true);
1731 case LyXTabular::M_VALIGN_TOP:
1732 case LyXTabular::M_VALIGN_BOTTOM:
1733 case LyXTabular::M_VALIGN_CENTER:
1735 case LyXTabular::VALIGN_TOP:
1736 case LyXTabular::VALIGN_BOTTOM:
1737 case LyXTabular::VALIGN_CENTER:
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->SetVAlignment(
1741 tabular->GetCellNumber(i, j),
1743 UpdateLocal(bv, INIT, true);
1745 case LyXTabular::MULTICOLUMN:
1747 if (sel_row_start != sel_row_end) {
1748 WriteAlert(_("Impossible Operation!"),
1749 _("Multicolumns can only be horizontally."),
1753 // just multicol for one Single Cell
1754 if (!hasSelection()) {
1755 // check wether we are completly in a multicol
1756 if (tabular->IsMultiColumn(actcell)) {
1757 tabular->UnsetMultiColumn(actcell);
1758 UpdateLocal(bv, INIT, true);
1760 tabular->SetMultiColumn(actcell, 1);
1761 UpdateLocal(bv, CELL, true);
1765 // we have a selection so this means we just add all this
1766 // cells to form a multicolumn cell
1770 if (sel_cell_start > sel_cell_end) {
1771 s_start = sel_cell_end;
1772 s_end = sel_cell_start;
1774 s_start = sel_cell_start;
1775 s_end = sel_cell_end;
1777 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1779 sel_cell_end = sel_cell_start;
1780 UpdateLocal(bv, INIT, true);
1783 case LyXTabular::SET_ALL_LINES:
1785 case LyXTabular::UNSET_ALL_LINES:
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->SetAllLines(
1789 tabular->GetCellNumber(i,j), setLines);
1790 UpdateLocal(bv, INIT, true);
1792 case LyXTabular::SET_LONGTABULAR:
1793 tabular->SetLongTabular(true);
1794 UpdateLocal(bv, INIT, true); // because this toggles displayed
1796 case LyXTabular::UNSET_LONGTABULAR:
1797 tabular->SetLongTabular(false);
1798 UpdateLocal(bv, INIT, true); // because this toggles displayed
1800 case LyXTabular::SET_ROTATE_TABULAR:
1801 tabular->SetRotateTabular(true);
1803 case LyXTabular::UNSET_ROTATE_TABULAR:
1804 tabular->SetRotateTabular(false);
1806 case LyXTabular::SET_ROTATE_CELL:
1807 for (int i = sel_row_start; i <= sel_row_end; ++i)
1808 for (int j = sel_col_start; j<=sel_col_end; ++j)
1809 tabular->SetRotateCell(
1810 tabular->GetCellNumber(i, j),
1813 case LyXTabular::UNSET_ROTATE_CELL:
1814 for (int i = sel_row_start; i <= sel_row_end; ++i)
1815 for (int j = sel_col_start; j <= sel_col_end; ++j)
1816 tabular->SetRotateCell(
1817 tabular->GetCellNumber(i, j), false);
1819 case LyXTabular::SET_USEBOX:
1821 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1822 if (val == tabular->GetUsebox(actcell))
1823 val = LyXTabular::BOX_NONE;
1824 for (int i = sel_row_start; i <= sel_row_end; ++i)
1825 for (int j = sel_col_start; j <= sel_col_end; ++j)
1827 tabular->GetCellNumber(i, j), val);
1830 case LyXTabular::SET_LTFIRSTHEAD:
1831 tabular->SetLTHead(actcell, true);
1833 case LyXTabular::SET_LTHEAD:
1834 tabular->SetLTHead(actcell, false);
1836 case LyXTabular::SET_LTFOOT:
1837 tabular->SetLTFoot(actcell, false);
1839 case LyXTabular::SET_LTLASTFOOT:
1840 tabular->SetLTFoot(actcell, true);
1842 case LyXTabular::SET_LTNEWPAGE:
1844 bool what = !tabular->GetLTNewPage(actcell);
1845 tabular->SetLTNewPage(actcell, what);
1848 // dummy stuff just to avoid warnings
1849 case LyXTabular::LAST_ACTION:
1855 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1858 UpdatableInset * inset =
1859 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1860 LyXFont font(LyXFont::ALL_SANE);
1862 x = inset->x() + inset->width(bv, font);
1863 y = inset->descent(bv, font);
1865 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1866 //inset_y = cursor.y();
1867 inset->Edit(bv, x, y, button);
1868 if (!the_locking_inset)
1870 UpdateLocal(bv, CELL, false);
1871 return (the_locking_inset != 0);
1875 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1878 inset_x = cursor.x()
1879 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1880 inset_y = cursor.y();
1881 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1885 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1888 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1892 // This returns paperWidth() if the cell-width is unlimited or the width
1893 // in pixels if we have a pwidth for this cell.
1894 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1896 string const s = tabular->GetPWidth(cell);
1900 return VSpace(s).inPixels(bv);
1904 int InsetTabular::getMaxWidth(BufferView * bv,
1905 UpdatableInset const * inset) const
1907 typedef std::map<UpdatableInset const *, int> Cache;
1911 Cache::const_iterator ci = cache.find(inset);
1912 if (ci != cache.end()) {
1913 cell = (*ci).second;
1914 if (tabular->GetCellInset(cell) != inset) {
1919 int const n = tabular->GetNumberOfCells();
1922 for (; cell < n; ++cell) {
1923 if (tabular->GetCellInset(cell) == inset)
1926 cache[inset] = cell;
1931 int w = GetMaxWidthOfCell(bv, cell);
1933 // because the inset then subtracts it's top_x and owner->x()
1934 w += (inset->x() - top_x);
1939 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1941 resizeLyXText(bv, recursive);
1945 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
1948 for(int i=0; i < tabular->rows(); ++i) {
1949 for(int j=0; j < tabular->columns(); ++j) {
1950 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
1958 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1959 bool const recursive) const
1961 if (the_locking_inset)
1962 return the_locking_inset->getLyXText(bv, recursive);
1963 return Inset::getLyXText(bv, recursive);
1967 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1969 if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1970 bv->owner()->getDialogs()
1971 ->showTabular(const_cast<InsetTabular *>(this));
1976 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1978 if (the_locking_inset) {
1979 InsetTabular * i = static_cast<InsetTabular *>
1980 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1982 i->OpenLayoutDialog(bv);
1986 bv->owner()->getDialogs()->showTabular(
1987 const_cast<InsetTabular *>(this));
1991 // functions returns:
1995 // 3 ... toggled off
1997 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1999 int action = LyXTabular::LAST_ACTION;
2000 LyXFunc::func_status status = LyXFunc::OK;
2003 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
2004 string const tmp = tabularFeatures[i].feature;
2005 if (tmp == what.substr(0, tmp.length())) {
2006 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2007 // tabularFeatures[i].feature.length())) {
2008 action = tabularFeatures[i].action;
2012 if (action == LyXTabular::LAST_ACTION)
2013 return LyXFunc::Unknown;
2015 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
2022 if (hasSelection()) {
2023 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2025 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2029 case LyXTabular::SET_PWIDTH:
2030 case LyXTabular::SET_MPWIDTH:
2031 case LyXTabular::SET_SPECIAL_COLUMN:
2032 case LyXTabular::SET_SPECIAL_MULTI:
2033 status |= LyXFunc::Disabled;
2036 case LyXTabular::APPEND_ROW:
2037 case LyXTabular::APPEND_COLUMN:
2038 case LyXTabular::DELETE_ROW:
2039 case LyXTabular::DELETE_COLUMN:
2040 case LyXTabular::SET_ALL_LINES:
2041 case LyXTabular::UNSET_ALL_LINES:
2042 status |= LyXFunc::OK;
2045 case LyXTabular::MULTICOLUMN:
2046 if (tabular->IsMultiColumn(actcell))
2047 status |= LyXFunc::ToggleOn;
2049 status |= LyXFunc::ToggleOff;
2051 case LyXTabular::M_TOGGLE_LINE_TOP:
2053 case LyXTabular::TOGGLE_LINE_TOP:
2054 if (tabular->TopLine(actcell, flag))
2055 status |= LyXFunc::ToggleOn;
2057 status |= LyXFunc::ToggleOff;
2059 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2061 case LyXTabular::TOGGLE_LINE_BOTTOM:
2062 if (tabular->BottomLine(actcell, flag))
2063 status |= LyXFunc::ToggleOn;
2065 status |= LyXFunc::ToggleOff;
2067 case LyXTabular::M_TOGGLE_LINE_LEFT:
2069 case LyXTabular::TOGGLE_LINE_LEFT:
2070 if (tabular->LeftLine(actcell, flag))
2071 status |= LyXFunc::ToggleOn;
2073 status |= LyXFunc::ToggleOff;
2075 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2077 case LyXTabular::TOGGLE_LINE_RIGHT:
2078 if (tabular->RightLine(actcell, flag))
2079 status |= LyXFunc::ToggleOn;
2081 status |= LyXFunc::ToggleOff;
2083 case LyXTabular::M_ALIGN_LEFT:
2085 case LyXTabular::ALIGN_LEFT:
2086 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2087 status |= LyXFunc::ToggleOn;
2089 status |= LyXFunc::ToggleOff;
2091 case LyXTabular::M_ALIGN_RIGHT:
2093 case LyXTabular::ALIGN_RIGHT:
2094 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2095 status |= LyXFunc::ToggleOn;
2097 status |= LyXFunc::ToggleOff;
2099 case LyXTabular::M_ALIGN_CENTER:
2101 case LyXTabular::ALIGN_CENTER:
2102 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2103 status |= LyXFunc::ToggleOn;
2105 status |= LyXFunc::ToggleOff;
2107 case LyXTabular::M_VALIGN_TOP:
2109 case LyXTabular::VALIGN_TOP:
2110 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2111 status |= LyXFunc::ToggleOn;
2113 status |= LyXFunc::ToggleOff;
2115 case LyXTabular::M_VALIGN_BOTTOM:
2117 case LyXTabular::VALIGN_BOTTOM:
2118 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2119 status |= LyXFunc::ToggleOn;
2121 status |= LyXFunc::ToggleOff;
2123 case LyXTabular::M_VALIGN_CENTER:
2125 case LyXTabular::VALIGN_CENTER:
2126 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2127 status |= LyXFunc::ToggleOn;
2129 status |= LyXFunc::ToggleOff;
2131 case LyXTabular::SET_LONGTABULAR:
2132 if (tabular->IsLongTabular())
2133 status |= LyXFunc::ToggleOn;
2135 status |= LyXFunc::ToggleOff;
2137 case LyXTabular::UNSET_LONGTABULAR:
2138 if (!tabular->IsLongTabular())
2139 status |= LyXFunc::ToggleOn;
2141 status |= LyXFunc::ToggleOff;
2143 case LyXTabular::SET_ROTATE_TABULAR:
2144 if (tabular->GetRotateTabular())
2145 status |= LyXFunc::ToggleOn;
2147 status |= LyXFunc::ToggleOff;
2149 case LyXTabular::UNSET_ROTATE_TABULAR:
2150 if (!tabular->GetRotateTabular())
2151 status |= LyXFunc::ToggleOn;
2153 status |= LyXFunc::ToggleOff;
2155 case LyXTabular::SET_ROTATE_CELL:
2156 if (tabular->GetRotateCell(actcell))
2157 status |= LyXFunc::ToggleOn;
2159 status |= LyXFunc::ToggleOff;
2161 case LyXTabular::UNSET_ROTATE_CELL:
2162 if (!tabular->GetRotateCell(actcell))
2163 status |= LyXFunc::ToggleOn;
2165 status |= LyXFunc::ToggleOff;
2167 case LyXTabular::SET_USEBOX:
2168 if (strToInt(argument) == tabular->GetUsebox(actcell))
2169 status |= LyXFunc::ToggleOn;
2171 status |= LyXFunc::ToggleOff;
2173 case LyXTabular::SET_LTFIRSTHEAD:
2174 if (tabular->GetRowOfLTHead(actcell, dummy))
2175 status |= LyXFunc::ToggleOn;
2177 status |= LyXFunc::ToggleOff;
2179 case LyXTabular::SET_LTHEAD:
2180 if (tabular->GetRowOfLTHead(actcell, dummy))
2181 status |= LyXFunc::ToggleOn;
2183 status |= LyXFunc::ToggleOff;
2185 case LyXTabular::SET_LTFOOT:
2186 if (tabular->GetRowOfLTFoot(actcell, dummy))
2187 status |= LyXFunc::ToggleOn;
2189 status |= LyXFunc::ToggleOff;
2191 case LyXTabular::SET_LTLASTFOOT:
2192 if (tabular->GetRowOfLTFoot(actcell, dummy))
2193 status |= LyXFunc::ToggleOn;
2195 status |= LyXFunc::ToggleOff;
2197 case LyXTabular::SET_LTNEWPAGE:
2198 if (tabular->GetLTNewPage(actcell))
2199 status |= LyXFunc::ToggleOn;
2201 status |= LyXFunc::ToggleOff;
2204 status = LyXFunc::Disabled;
2211 std::vector<string> const InsetTabular::getLabelList() const
2213 return tabular->getLabelList();
2217 bool InsetTabular::copySelection(BufferView * bv)
2219 if (!hasSelection())
2221 //delete paste_tabular;
2223 //int sel_col_start;
2225 //int sel_row_start;
2228 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2229 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2230 if (sel_col_start > sel_col_end) {
2231 sel_col_start = sel_col_end;
2232 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2234 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2236 int const columns = sel_col_end - sel_col_start + 1;
2238 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2239 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2240 if (sel_row_start > sel_row_end) {
2241 //int tmp tmp = sel_row_start;
2242 //sel_row_start = sel_row_end;
2243 //sel_row_end = tmp;
2244 swap(sel_row_start, sel_row_end);
2246 int const rows = sel_row_end - sel_row_start + 1;
2248 delete paste_tabular;
2249 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2251 for (int i = 0; i < sel_row_start; ++i)
2252 paste_tabular->DeleteRow(0);
2253 while(paste_tabular->rows() > rows)
2254 paste_tabular->DeleteRow(rows);
2255 paste_tabular->SetTopLine(0, true, true);
2256 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2258 for (int i = 0; i < sel_col_start; ++i)
2259 paste_tabular->DeleteColumn(0);
2260 while (paste_tabular->columns() > columns)
2261 paste_tabular->DeleteColumn(columns);
2262 paste_tabular->SetLeftLine(0, true, true);
2263 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2267 paste_tabular->Ascii(bv->buffer(), sstr);
2268 bv->stuffClipboard(sstr.str().c_str());
2273 bool InsetTabular::pasteSelection(BufferView * bv)
2278 for (int r1 = 0, r2 = actrow;
2279 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2281 for(int c1 = 0, c2 = actcol;
2282 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2284 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2285 tabular->IsPartOfMultiColumn(r2,c2))
2287 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2291 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2295 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2296 int const n2 = tabular->GetCellNumber(r2, c2);
2297 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2298 tabular->GetCellInset(n2)->setOwner(this);
2299 tabular->GetCellInset(n2)->deleteLyXText(bv);
2306 bool InsetTabular::cutSelection()
2308 if (!hasSelection())
2311 //int sel_col_start;
2313 //int sel_row_start;
2316 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2317 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2318 if (sel_col_start > sel_col_end) {
2319 sel_col_start = sel_col_end;
2320 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2322 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2324 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2325 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2326 if (sel_row_start > sel_row_end) {
2327 //int tmp = sel_row_start;
2328 //sel_row_start = sel_row_end;
2329 //sel_row_end = tmp;
2330 swap(sel_row_start, sel_row_end);
2332 if (sel_cell_start > sel_cell_end) {
2333 //int tmp = sel_cell_start;
2334 //sel_cell_start = sel_cell_end;
2335 //sel_cell_end = tmp;
2336 swap(sel_cell_start, sel_cell_end);
2338 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2339 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2340 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2347 bool InsetTabular::isRightToLeft(BufferView *bv )
2349 return bv->getParentLanguage(this)->RightToLeft();
2353 bool InsetTabular::nodraw() const
2355 if (!UpdatableInset::nodraw() && the_locking_inset)
2356 return the_locking_inset->nodraw();
2357 return UpdatableInset::nodraw();
2361 int InsetTabular::scroll(bool recursive) const
2363 int sx = UpdatableInset::scroll(false);
2365 if (recursive && the_locking_inset)
2366 sx += the_locking_inset->scroll(recursive);
2372 bool InsetTabular::doClearArea() const
2374 return !locked || (need_update & (FULL|INIT));
2378 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2380 srow = tabular->row_of_cell(sel_cell_start);
2381 erow = tabular->row_of_cell(sel_cell_end);
2385 scol = tabular->column_of_cell(sel_cell_start);
2386 ecol = tabular->column_of_cell(sel_cell_end);
2390 ecol = tabular->right_column_of_cell(sel_cell_end);