1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettabular.h"
25 #include "commandtags.h"
27 #include "LaTeXFeatures.h"
31 #include "lyx_gui_misc.h"
33 #include "insets/insettext.h"
34 #include "frontends/Dialogs.h"
50 const int ADD_TO_HEIGHT = 2;
51 const int ADD_TO_TABULAR_WIDTH = 2;
53 LyXTabular * paste_tabular = 0;
56 struct tabular_features {
57 LyXTabular::Feature action;
61 //tabular_features * tabularFeatures = 0;
63 tabular_features tabularFeatures[] =
65 { LyXTabular::APPEND_ROW, "append-row" },
66 { LyXTabular::APPEND_COLUMN, "append-column" },
67 { LyXTabular::DELETE_ROW, "delete-row" },
68 { LyXTabular::DELETE_COLUMN, "delete-column" },
69 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
70 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
71 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
72 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
73 { LyXTabular::ALIGN_LEFT, "align-left" },
74 { LyXTabular::ALIGN_RIGHT, "align-right" },
75 { LyXTabular::ALIGN_CENTER, "align-center" },
76 { LyXTabular::VALIGN_TOP, "valign-top" },
77 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
78 { LyXTabular::VALIGN_CENTER, "valign-center" },
79 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
80 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
81 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
82 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
83 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
84 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
85 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
86 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
87 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
88 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
89 { LyXTabular::MULTICOLUMN, "multicolumn" },
90 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
91 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
92 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
93 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
94 { LyXTabular::SET_PWIDTH, "set-pwidth" },
95 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
96 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
97 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
98 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
99 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
100 { LyXTabular::SET_USEBOX, "set-usebox" },
101 { LyXTabular::SET_LTHEAD, "set-lthead" },
102 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
103 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
104 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
105 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
106 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
107 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
108 { LyXTabular::LAST_ACTION, "" }
114 bool InsetTabular::hasPasteBuffer() const
116 return (paste_tabular != 0);
120 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
127 tabular.reset(new LyXTabular(this, rows,columns));
128 // for now make it always display as display() inset
130 the_locking_inset = 0;
131 locked = no_selection = false;
133 actrow = actcell = 0;
139 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
142 tabular.reset(new LyXTabular(this, *(tab.tabular)));
143 the_locking_inset = 0;
144 locked = no_selection = false;
146 actrow = actcell = 0;
147 sel_cell_start = sel_cell_end = 0;
152 InsetTabular::~InsetTabular()
159 Inset * InsetTabular::Clone(Buffer const & buf) const
161 InsetTabular * t = new InsetTabular(*this, buf);
163 //t->tabular = tabular->Clone(t);
164 t->tabular.reset(tabular->Clone(t));
169 void InsetTabular::Write(Buffer const * buf, ostream & os) const
171 os << " Tabular" << endl;
172 tabular->Write(buf, os);
176 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
178 bool const old_format = (lex.GetString() == "\\LyXTable");
182 //tabular = new LyXTabular(buf, this, lex);
183 tabular.reset(new LyXTabular(buf, this, lex));
191 string token = lex.GetString();
192 while (lex.IsOK() && (token != "\\end_inset")) {
194 token = lex.GetString();
196 if (token != "\\end_inset") {
197 lex.printError("Missing \\end_inset at this point. "
203 int InsetTabular::ascent(BufferView *, LyXFont const &) const
205 return tabular->GetAscentOfRow(0);
209 int InsetTabular::descent(BufferView *, LyXFont const &) const
211 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
215 int InsetTabular::width(BufferView *, LyXFont const &) const
217 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
221 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
222 float & x, bool cleared) const
230 if (need_update == INIT) {
231 if (calculate_dimensions_of_cells(bv, font, true))
232 bv->text->status = LyXText::CHANGED_IN_DRAW;
237 Painter & pain = bv->painter();
243 UpdatableInset::draw(bv, font, baseline, x, cleared);
246 x += static_cast<float>(scroll());
248 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
249 (top_x != int(x)) || (top_baseline != baseline))) {
250 int h = ascent(bv, font) + descent(bv, font);
251 int const tx = display() || !owner() ? 0 : top_x;
252 int w = tx ? width(bv, font) : pain.paperWidth();
253 int ty = baseline - ascent(bv, font);
257 if ((ty + h) > pain.paperHeight())
258 h = pain.paperHeight();
259 if ((top_x + w) > pain.paperWidth())
260 w = pain.paperWidth();
261 pain.fillRectangle(tx, ty, w, h);
266 top_baseline = baseline;
267 x += ADD_TO_TABULAR_WIDTH;
271 first_visible_cell = -1;
272 for (i = 0; i < tabular->rows(); ++i) {
274 cell = tabular->GetCellNumber(i, 0);
275 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
276 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
278 baseline += tabular->GetDescentOfRow(i) +
279 tabular->GetAscentOfRow(i + 1) +
280 tabular->GetAdditionalHeight(i + 1);
283 for (j = 0; j < tabular->columns(); ++j) {
284 if (nx > bv->workWidth())
286 if (tabular->IsPartOfMultiColumn(i, j))
288 cx = nx + tabular->GetBeginningOfTextInCell(cell);
289 if (first_visible_cell < 0)
290 first_visible_cell = cell;
292 DrawCellSelection(pain, nx, baseline, i, j, cell);
293 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
295 DrawCellLines(pain, nx, baseline, i, cell);
296 nx += tabular->GetWidthOfColumn(cell);
299 baseline += tabular->GetDescentOfRow(i) +
300 tabular->GetAscentOfRow(i + 1) +
301 tabular->GetAdditionalHeight(i + 1);
303 } else if (need_update == CELL) {
306 if (the_locking_inset &&
307 tabular->GetCellInset(actcell) != the_locking_inset)
309 Inset * inset = tabular->GetCellInset(cell);
311 inset != the_locking_inset && i < tabular->rows();
314 inset != the_locking_inset
315 && j < tabular->columns();
317 if (tabular->IsPartOfMultiColumn(i, j))
319 nx += tabular->GetWidthOfColumn(cell);
321 inset = tabular->GetCellInset(cell);
323 if (tabular->row_of_cell(cell) > i) {
325 baseline += tabular->GetDescentOfRow(i) +
326 tabular->GetAscentOfRow(i + 1) +
327 tabular->GetAdditionalHeight(i + 1);
331 // copute baseline for actual row
332 for (i = 0; i < actrow; ++i) {
333 baseline += tabular->GetDescentOfRow(i) +
334 tabular->GetAscentOfRow(i + 1) +
335 tabular->GetAdditionalHeight(i + 1);
337 // now compute the right x position
338 cell = tabular->GetCellNumber(actrow, 0);
339 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
340 if (tabular->IsPartOfMultiColumn(actrow, j))
342 nx += tabular->GetWidthOfColumn(cell);
346 i = tabular->row_of_cell(cell);
347 if (the_locking_inset != tabular->GetCellInset(cell)) {
348 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
351 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
353 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
354 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
356 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
359 // clear only if we didn't have a change
360 if (need_update == CELL) {
361 // clear before the inset
364 baseline - tabular->GetAscentOfRow(i) + 1,
366 tabular->GetAscentOfRow(i) +
367 tabular->GetDescentOfRow(i) - 1);
368 // clear behind the inset
370 int(cx + the_locking_inset->width(bv,font) + 1),
371 baseline - tabular->GetAscentOfRow(i) + 1,
372 tabular->GetWidthOfColumn(cell) -
373 tabular->GetBeginningOfTextInCell(cell) -
374 the_locking_inset->width(bv,font) -
375 tabular->GetAdditionalWidth(cell) - 1,
376 tabular->GetAscentOfRow(i) +
377 tabular->GetDescentOfRow(i) - 1);
380 x -= ADD_TO_TABULAR_WIDTH;
381 x += width(bv, font);
382 if (bv->text->status == LyXText::CHANGED_IN_DRAW) {
384 for(Inset * inset=owner();inset;++i)
385 inset = inset->owner();
386 if (calculate_dimensions_of_cells(bv, font, false))
394 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
395 int row, int cell) const
397 int x2 = x + tabular->GetWidthOfColumn(cell);
400 if (!tabular->TopAlreadyDrawed(cell)) {
401 on_off = !tabular->TopLine(cell);
402 pain.line(x, baseline - tabular->GetAscentOfRow(row),
403 x2, baseline - tabular->GetAscentOfRow(row),
404 on_off ? LColor::tabularonoffline : LColor::tabularline,
405 on_off ? Painter::line_onoffdash : Painter::line_solid);
407 on_off = !tabular->BottomLine(cell);
408 pain.line(x,baseline + tabular->GetDescentOfRow(row),
409 x2, baseline + tabular->GetDescentOfRow(row),
410 on_off ? LColor::tabularonoffline : LColor::tabularline,
411 on_off ? Painter::line_onoffdash : Painter::line_solid);
412 if (!tabular->LeftAlreadyDrawed(cell)) {
413 on_off = !tabular->LeftLine(cell);
414 pain.line(x, baseline - tabular->GetAscentOfRow(row),
415 x, baseline + tabular->GetDescentOfRow(row),
416 on_off ? LColor::tabularonoffline : LColor::tabularline,
417 on_off ? Painter::line_onoffdash : Painter::line_solid);
419 on_off = !tabular->RightLine(cell);
420 pain.line(x2 - tabular->GetAdditionalWidth(cell),
421 baseline - tabular->GetAscentOfRow(row),
422 x2 - tabular->GetAdditionalWidth(cell),
423 baseline + tabular->GetDescentOfRow(row),
424 on_off ? LColor::tabularonoffline : LColor::tabularline,
425 on_off ? Painter::line_onoffdash : Painter::line_solid);
429 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
430 int row, int column, int cell) const
432 int cs = tabular->column_of_cell(sel_cell_start);
433 int ce = tabular->column_of_cell(sel_cell_end);
436 cs = tabular->column_of_cell(sel_cell_end);
438 ce = tabular->right_column_of_cell(sel_cell_end);
441 int rs = tabular->row_of_cell(sel_cell_start);
442 int re = tabular->row_of_cell(sel_cell_end);
443 if (rs > re) swap(rs, re);
445 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
446 int w = tabular->GetWidthOfColumn(cell);
447 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
448 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
449 w, h, LColor::selection);
454 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
458 calculate_dimensions_of_cells(bv, font, true);
460 owner()->update(bv, font, true);
463 if (the_locking_inset)
464 the_locking_inset->update(bv, font, reinit);
466 switch (need_update) {
470 if (calculate_dimensions_of_cells(bv, font, false))
482 string const InsetTabular::EditMessage() const
484 return _("Opened Tabular Inset");
488 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
490 UpdatableInset::Edit(bv, x, y, button);
492 if (!bv->lockInset(this)) {
493 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
497 the_locking_inset = 0;
501 sel_cell_start = sel_cell_end = actcell;
502 bv->text->FinishUndo();
503 if (InsetHit(bv, x, y) && (button != 3)) {
504 ActivateCellInsetAbs(bv, x, y, button);
506 // UpdateLocal(bv, NONE, false);
507 // bv->getOwner()->getPopups().updateFormTabular();
511 void InsetTabular::InsetUnlock(BufferView * bv)
513 if (the_locking_inset) {
514 the_locking_inset->InsetUnlock(bv);
515 the_locking_inset = 0;
518 no_selection = false;
521 if (scroll(false) || hasSelection()) {
522 sel_cell_start = sel_cell_end = 0;
526 UpdateLocal(bv, FULL, false);
531 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
532 bool mark_dirty) const
534 if (need_update < what) // only set this if it has greater update
536 if ((what == INIT) && hasSelection())
539 if (need_update != NONE) {
540 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
541 if (locked) // && (what != NONE))
547 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
549 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
554 if (inset == tabular->GetCellInset(actcell)) {
555 lyxerr[Debug::INSETS] << "OK" << endl;
556 the_locking_inset = tabular->GetCellInset(actcell);
558 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
559 inset_y = cursor.y();
561 } else if (the_locking_inset && (the_locking_inset == inset)) {
562 lyxerr[Debug::INSETS] << "OK" << endl;
564 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
565 inset_y = cursor.y();
566 } else if (the_locking_inset) {
567 lyxerr[Debug::INSETS] << "MAYBE" << endl;
568 return the_locking_inset->LockInsetInInset(bv, inset);
570 lyxerr[Debug::INSETS] << "NOT OK" << endl;
575 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
578 if (!the_locking_inset)
580 if (the_locking_inset == inset) {
581 the_locking_inset->InsetUnlock(bv);
582 the_locking_inset = 0;
583 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
588 UpdateLocal(bv, CELL, false);
589 ShowInsetCursor(bv, false);
592 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
593 if (inset->LyxCode() == TABULAR_CODE &&
594 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
595 bv->owner()->getDialogs()->updateTabular(this);
604 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
606 if (!the_locking_inset)
608 if (the_locking_inset != inset) {
609 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
612 UpdateLocal(bv, CELL, false);
617 unsigned int InsetTabular::InsetInInsetY()
619 if (!the_locking_inset)
622 return inset_y + the_locking_inset->InsetInInsetY();
626 UpdatableInset * InsetTabular::GetLockingInset()
628 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
632 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
636 if (the_locking_inset)
637 return the_locking_inset->GetFirstLockingInsetOfType(c);
642 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
644 if (the_locking_inset)
645 return the_locking_inset->InsertInset(bv, inset);
650 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
652 if (hasSelection() && (button == 3))
655 if (hasSelection()) {
657 UpdateLocal(bv, SELECTION, false);
660 no_selection = false;
662 int const ocell = actcell;
663 int const orow = actrow;
668 UpdateLocal(bv, NONE, false);
669 sel_cell_start = sel_cell_end = actcell;
671 if ((ocell != actcell) && the_locking_inset) {
672 the_locking_inset->InsetUnlock(bv);
673 the_locking_inset = 0;
679 bool const inset_hit = InsetHit(bv, x, y);
681 if ((ocell == actcell) && the_locking_inset && inset_hit) {
683 the_locking_inset->InsetButtonPress(bv,
684 x - inset_x, y - inset_y,
687 } else if (the_locking_inset) {
688 the_locking_inset->InsetUnlock(bv);
690 the_locking_inset = 0;
692 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
695 if (inset_hit && bv->theLockingInset()) {
696 if (ActivateCellInsetAbs(bv, x, y, button))
697 the_locking_inset->InsetButtonPress(bv,
707 void InsetTabular::InsetButtonRelease(BufferView * bv,
708 int x, int y, int button)
711 if (the_locking_inset) {
713 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
714 i->InsetButtonRelease(bv, x, y, button);
718 bv->owner()->getDialogs()->showTabular(this);
721 if (the_locking_inset) {
722 the_locking_inset->InsetButtonRelease(bv,
723 x - inset_x, y - inset_y,
727 no_selection = false;
731 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
733 if (the_locking_inset) {
734 the_locking_inset->InsetMotionNotify(bv,
742 int const old_cell = actcell;
745 sel_cell_end = actcell;
746 if (sel_cell_end != old_cell)
747 UpdateLocal(bv, SELECTION, false);
750 no_selection = false;
754 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
756 if (the_locking_inset) {
757 the_locking_inset->InsetKeyPress(xke);
763 UpdatableInset::RESULT
764 InsetTabular::LocalDispatch(BufferView * bv,
768 // We need to save the value of the_locking_inset as the call to
769 // the_locking_inset->LocalDispatch might unlock it.
770 old_locking_inset = the_locking_inset;
771 no_selection = false;
772 UpdatableInset::RESULT result =
773 UpdatableInset::LocalDispatch(bv, action, arg);
774 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
779 if ((action < 0) && arg.empty())
782 bool hs = hasSelection();
785 // this one have priority over the locked InsetText!
790 if (GetFirstLockingInsetOfType(Inset::TABULAR_CODE) != this)
793 if (the_locking_inset) {
794 UnlockInsetInInset(bv, the_locking_inset);
795 the_locking_inset = 0;
797 if (action == LFUN_TAB)
798 moveNextCell(bv, old_locking_inset != 0);
800 movePrevCell(bv, old_locking_inset != 0);
801 sel_cell_start = sel_cell_end = actcell;
803 UpdateLocal(bv, SELECTION, false);
804 if (!the_locking_inset) {
806 return DISPATCHED_NOUPDATE;
810 // this to avoid compiler warnings.
815 if (the_locking_inset) {
816 result=the_locking_inset->LocalDispatch(bv, action, arg);
817 if (result == DISPATCHED_NOUPDATE) {
820 if (sc != scroll()) { // inset has been scrolled
821 the_locking_inset->ToggleInsetCursor(bv);
822 UpdateLocal(bv, FULL, false);
823 the_locking_inset->ToggleInsetCursor(bv);
826 } else if (result == DISPATCHED) {
827 the_locking_inset->ToggleInsetCursor(bv);
828 UpdateLocal(bv, CELL, false);
829 the_locking_inset->ToggleInsetCursor(bv);
831 } else if (result == FINISHED) {
838 // --- Cursor Movements ----------------------------------
840 if (tabular->IsLastCellInRow(actcell))
842 moveRight(bv, false);
843 sel_cell_end = actcell;
844 UpdateLocal(bv, SELECTION, false);
847 result = moveRight(bv);
848 sel_cell_start = sel_cell_end = actcell;
850 UpdateLocal(bv, SELECTION, false);
853 if (tabular->IsFirstCellInRow(actcell))
856 sel_cell_end = actcell;
857 UpdateLocal(bv, SELECTION, false);
860 result = moveLeft(bv);
861 sel_cell_start = sel_cell_end = actcell;
863 UpdateLocal(bv, SELECTION, false);
867 int const ocell = actcell;
869 if ((ocell == sel_cell_end) ||
870 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
871 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
873 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
874 UpdateLocal(bv, SELECTION, false);
878 result = moveDown(bv, old_locking_inset != 0);
879 sel_cell_start = sel_cell_end = actcell;
881 UpdateLocal(bv, SELECTION, false);
885 int const ocell = actcell;
887 if ((ocell == sel_cell_end) ||
888 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
889 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
891 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
892 UpdateLocal(bv, SELECTION, false);
896 result = moveUp(bv, old_locking_inset != 0);
897 sel_cell_start = sel_cell_end = actcell;
899 UpdateLocal(bv, SELECTION, false);
903 if (the_locking_inset) {
904 UnlockInsetInInset(bv, the_locking_inset);
905 the_locking_inset = 0;
907 if (bv->text->first + bv->painter().paperHeight() <
908 (top_baseline + tabular->GetHeightOfTabular()))
910 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
911 UpdateLocal(bv, FULL, false);
912 actcell = tabular->GetCellBelow(first_visible_cell) + column;
914 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
917 UpdateLocal(bv, CURSOR, false);
922 if (the_locking_inset) {
923 UnlockInsetInInset(bv, the_locking_inset);
924 the_locking_inset = 0;
926 if (top_baseline < 0) {
927 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
928 UpdateLocal(bv, FULL, false);
929 if (top_baseline > 0)
932 actcell = tabular->GetCellBelow(first_visible_cell) + column;
937 UpdateLocal(bv, CURSOR, false);
948 case LFUN_LAYOUT_TABULAR:
950 bv->owner()->getDialogs()->showTabular(this);
953 case LFUN_TABULAR_FEATURE:
954 if (!TabularFeatures(bv, arg))
955 result = UNDISPATCHED;
958 if (!copySelection(bv))
960 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
961 bv->text->cursor.par()->previous(),
962 bv->text->cursor.par()->next());
964 UpdateLocal(bv, INIT, true);
969 bv->text->FinishUndo();
972 case LFUN_PASTESELECTION:
974 string clip(bv->getClipboard());
978 if (clip.find('\t') != string::npos) {
982 unsigned int len = clip.length();
983 string::size_type p = 0;
986 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
994 maxCols = max(cols, maxCols);
1000 maxCols = max(cols, maxCols);
1001 delete paste_tabular;
1002 paste_tabular = new LyXTabular(this, rows, maxCols);
1003 string::size_type op = 0;
1005 int cells = paste_tabular->GetNumberOfCells();
1007 while((cell < cells) && (p < len) &&
1008 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1013 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1018 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1019 while(cols++ < maxCols)
1027 // check for the last cell if there is no trailing '\n'
1028 if ((cell < cells) && (op < len))
1029 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1031 // so that the clipboard is used and it goes on
1033 // and executes LFUN_PASTESELECTION in insettext!
1034 delete paste_tabular;
1039 if (hasPasteBuffer()) {
1040 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1041 bv->text->cursor.par()->previous(),
1042 bv->text->cursor.par()->next());
1044 UpdateLocal(bv, INIT, true);
1047 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1049 // we try to activate the actual inset and put this event down to
1050 // the insets dispatch function.
1051 result = UNDISPATCHED;
1052 if (the_locking_inset)
1055 if (ActivateCellInset(bv)) {
1056 // reset need_update setted in above function!
1058 result = the_locking_inset->LocalDispatch(bv, action, arg);
1059 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1060 UnlockInsetInInset(bv, the_locking_inset);
1062 the_locking_inset = 0;
1063 // we need to update if this was requested before
1064 UpdateLocal(bv, NONE, false);
1065 return UNDISPATCHED;
1068 // the_locking_inset->ToggleInsetCursor(bv);
1069 UpdateLocal(bv, CELL, false);
1070 // the_locking_inset->ToggleInsetCursor(bv);
1075 if (result!=FINISHED) {
1076 if (!the_locking_inset) {
1077 ShowInsetCursor(bv);
1080 bv->unlockInset(this);
1085 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1086 bool fragile, bool fp) const
1088 return tabular->Latex(buf, os, fragile, fp);
1092 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1094 // This should be changed to a real ascii export
1095 return tabular->Ascii(buf, os);
1099 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1105 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1107 return tabular->DocBook(buf,os);
1111 void InsetTabular::Validate(LaTeXFeatures & features) const
1113 tabular->Validate(features);
1117 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1118 LyXFont const & font,
1125 bool changed = false;
1127 // if we have a locking_inset we should have to check only this cell for
1128 // change so I'll try this to have a boost, but who knows ;)
1129 if ((need_update != INIT) &&
1130 (the_locking_inset == tabular->GetCellInset(actcell))) {
1131 for(int i = 0; i < tabular->columns(); ++i) {
1132 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1134 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1137 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1138 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1139 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1142 for (int i = 0; i < tabular->rows(); ++i) {
1145 for (int j= 0; j < tabular->columns(); ++j) {
1146 if (tabular->IsPartOfMultiColumn(i,j))
1149 inset = tabular->GetCellInset(cell);
1151 inset->update(bv, font, false);
1152 maxAsc = max(maxAsc, inset->ascent(bv, font));
1153 maxDesc = max(maxDesc, inset->descent(bv, font));
1154 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1156 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1157 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1163 void InsetTabular::GetCursorPos(BufferView *,
1164 int & x, int & y) const
1166 x = cursor.x() - top_x;
1171 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1173 if (the_locking_inset) {
1174 the_locking_inset->ToggleInsetCursor(bv);
1178 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1180 int const asc = lyxfont::maxAscent(font);
1181 int const desc = lyxfont::maxDescent(font);
1183 if (isCursorVisible())
1184 bv->hideLockedInsetCursor();
1186 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1187 toggleCursorVisible();
1191 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1193 if (!isCursorVisible()) {
1194 LyXFont font; // = GetFont(par, cursor.pos);
1196 int const asc = lyxfont::maxAscent(font);
1197 int const desc = lyxfont::maxDescent(font);
1198 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1200 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1201 setCursorVisible(true);
1206 void InsetTabular::HideInsetCursor(BufferView * bv)
1208 if (isCursorVisible()) {
1209 bv->hideLockedInsetCursor();
1210 setCursorVisible(false);
1212 // if (cursor_visible)
1213 // ToggleInsetCursor(bv);
1217 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1221 actcell = actrow = actcol = 0;
1222 int ly = tabular->GetDescentOfRow(actrow);
1224 // first search the right row
1225 while((ly < y) && (actrow < tabular->rows())) {
1226 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1227 tabular->GetAscentOfRow(actrow + 1) +
1228 tabular->GetAdditionalHeight(actrow + 1));
1230 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1232 actcell = tabular->GetCellNumber(actrow, actcol);
1234 // now search the right column
1235 int lx = tabular->GetWidthOfColumn(actcell) -
1236 tabular->GetAdditionalWidth(actcell);
1238 #ifdef WITH_WARNINGS
1239 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1241 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1242 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1243 tabular->GetAdditionalWidth(actcell - 1));
1245 // Jürgen, you should check that this is correct. (Lgb)
1246 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1247 lx += tabular->GetWidthOfColumn(actcell + 1)
1248 + tabular->GetAdditionalWidth(actcell);
1252 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1257 int InsetTabular::getCellXPos(int cell) const
1261 for (; !tabular->IsFirstCellInRow(c); --c)
1263 int lx = tabular->GetWidthOfColumn(cell);
1264 for (; c < cell; ++c) {
1265 lx += tabular->GetWidthOfColumn(c);
1267 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1271 void InsetTabular::resetPos(BufferView * bv) const
1275 actcol = tabular->column_of_cell(actcell);
1280 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1281 if (tabular->IsLastCellInRow(cell)) {
1282 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1283 tabular->GetAscentOfRow(actrow + 1) +
1284 tabular->GetAdditionalHeight(actrow + 1));
1288 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1289 int new_x = getCellXPos(actcell);
1290 int old_x = cursor.x();
1293 // cursor.x(getCellXPos(actcell) + offset);
1294 if ((actcol < tabular->columns()-1) && scroll(false) &&
1295 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1298 UpdateLocal(bv, FULL, false);
1299 } else if (the_locking_inset &&
1300 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1302 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1303 if (xx > (bv->workWidth()-20)) {
1304 scroll(bv, -(xx - bv->workWidth() + 60));
1305 UpdateLocal(bv, FULL, false);
1306 } else if (xx < 20) {
1312 UpdateLocal(bv, FULL, false);
1314 } else if ((cursor.x() - offset) > 20 &&
1315 (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1316 > (bv->workWidth() - 20)) {
1317 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1318 UpdateLocal(bv, FULL, false);
1319 } else if ((cursor.x() - offset) < 20) {
1320 scroll(bv, 20 - cursor.x() + offset);
1321 UpdateLocal(bv, FULL, false);
1322 } else if (scroll(false) && top_x > 20 &&
1323 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1324 scroll(bv, old_x - cursor.x());
1325 UpdateLocal(bv, FULL, false);
1327 if ((!the_locking_inset ||
1328 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1329 actcell != oldcell) {
1330 InsetTabular * inset = const_cast<InsetTabular *>(this);
1331 bv->owner()->getDialogs()->updateTabular(inset);
1337 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1339 if (lock && !old_locking_inset) {
1340 if (ActivateCellInset(bv))
1343 bool moved = isRightToLeft(bv)
1344 ? movePrevCell(bv) : moveNextCell(bv);
1347 if (lock && ActivateCellInset(bv))
1351 return DISPATCHED_NOUPDATE;
1355 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1357 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1360 if (lock) { // behind the inset
1361 if (ActivateCellInset(bv, 0, 0, 0, true))
1365 return DISPATCHED_NOUPDATE;
1369 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1371 int const ocell = actcell;
1372 actcell = tabular->GetCellAbove(actcell);
1373 if (actcell == ocell) // we moved out of the inset
1379 if (old_locking_inset) {
1380 old_locking_inset->GetCursorPos(bv, x, y);
1381 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1383 if (ActivateCellInset(bv, x, 0))
1386 return DISPATCHED_NOUPDATE;
1390 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1392 int const ocell = actcell;
1393 actcell = tabular->GetCellBelow(actcell);
1394 if (actcell == ocell) // we moved out of the inset
1400 if (old_locking_inset) {
1401 old_locking_inset->GetCursorPos(bv, x, y);
1402 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1404 if (ActivateCellInset(bv, x, 0))
1407 return DISPATCHED_NOUPDATE;
1411 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1413 if (isRightToLeft(bv)) {
1414 if (tabular->IsFirstCellInRow(actcell)) {
1415 int row = tabular->row_of_cell(actcell);
1416 if (row == tabular->rows() - 1)
1418 actcell = tabular->GetLastCellInRow(row);
1419 actcell = tabular->GetCellBelow(actcell);
1426 if (tabular->IsLastCell(actcell))
1431 bool rtl = tabular->GetCellInset(actcell)->par->
1432 isRightToLeftPar(bv->buffer()->params);
1433 ActivateCellInset(bv, 0, 0, 0, !rtl);
1440 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1442 if (isRightToLeft(bv)) {
1443 if (tabular->IsLastCellInRow(actcell)) {
1444 int row = tabular->row_of_cell(actcell);
1447 actcell = tabular->GetFirstCellInRow(row);
1448 actcell = tabular->GetCellAbove(actcell);
1450 if (tabular->IsLastCell(actcell))
1455 if (!actcell) // first cell
1460 bool rtl = tabular->GetCellInset(actcell)->par->
1461 isRightToLeftPar(bv->buffer()->params);
1462 ActivateCellInset(bv, 0, 0, 0, !rtl);
1469 bool InsetTabular::Delete()
1475 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall,
1480 sel_cell_end = tabular->GetNumberOfCells() - 1;
1482 if (hasSelection()) {
1484 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1485 bv->text->cursor.par()->previous(),
1486 bv->text->cursor.par()->next());
1487 frozen = bv->text->undo_frozen;
1489 bv->text->FreezeUndo();
1490 // apply the fontchange on the whole selection
1495 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1496 for(int i=sel_row_start; i <= sel_row_end; ++i) {
1497 for(int j=sel_col_start; j <= sel_col_end; ++j) {
1498 tabular->GetCellInset(i, j)->SetFont(bv, font, tall, true);
1502 bv->text->UnFreezeUndo();
1503 UpdateLocal(bv, INIT, true);
1505 if (the_locking_inset)
1506 the_locking_inset->SetFont(bv, font, tall);
1510 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1512 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1515 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1516 string const tmp = tabularFeatures[i].feature;
1518 if (tmp == what.substr(0, tmp.length())) {
1519 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1520 //tabularFeatures[i].feature.length())) {
1521 action = tabularFeatures[i].action;
1525 if (action == LyXTabular::LAST_ACTION)
1529 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1530 TabularFeatures(bv, action, val);
1535 void InsetTabular::TabularFeatures(BufferView * bv,
1536 LyXTabular::Feature feature,
1537 string const & value)
1545 bool setLines = false;
1546 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1547 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1552 case LyXTabular::M_ALIGN_LEFT:
1553 case LyXTabular::ALIGN_LEFT:
1554 setAlign=LYX_ALIGN_LEFT;
1556 case LyXTabular::M_ALIGN_RIGHT:
1557 case LyXTabular::ALIGN_RIGHT:
1558 setAlign=LYX_ALIGN_RIGHT;
1560 case LyXTabular::M_ALIGN_CENTER:
1561 case LyXTabular::ALIGN_CENTER:
1562 setAlign=LYX_ALIGN_CENTER;
1564 case LyXTabular::M_VALIGN_TOP:
1565 case LyXTabular::VALIGN_TOP:
1566 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1568 case LyXTabular::M_VALIGN_BOTTOM:
1569 case LyXTabular::VALIGN_BOTTOM:
1570 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1572 case LyXTabular::M_VALIGN_CENTER:
1573 case LyXTabular::VALIGN_CENTER:
1574 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1579 if (hasSelection()) {
1580 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1582 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1583 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1585 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1586 bv->text->cursor.par()->previous(),
1587 bv->text->cursor.par()->next());
1589 int row = tabular->row_of_cell(actcell);
1590 int column = tabular->column_of_cell(actcell);
1594 case LyXTabular::SET_PWIDTH:
1596 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1597 tabular->SetColumnPWidth(actcell,value);
1599 for (int i = 0; i < tabular->rows(); ++i) {
1600 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1603 UpdateLocal(bv, INIT, true);
1607 case LyXTabular::SET_MPWIDTH:
1609 bool const update = (tabular->GetPWidth(actcell) != value);
1610 tabular->SetMColumnPWidth(actcell,value);
1612 for (int i = 0; i < tabular->rows(); ++i) {
1613 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1616 UpdateLocal(bv, INIT, true);
1620 case LyXTabular::SET_SPECIAL_COLUMN:
1621 case LyXTabular::SET_SPECIAL_MULTI:
1622 tabular->SetAlignSpecial(actcell,value,feature);
1624 case LyXTabular::APPEND_ROW:
1625 // append the row into the tabular
1626 UnlockInsetInInset(bv, the_locking_inset);
1627 tabular->AppendRow(actcell);
1628 UpdateLocal(bv, INIT, true);
1630 case LyXTabular::APPEND_COLUMN:
1631 // append the column into the tabular
1632 UnlockInsetInInset(bv, the_locking_inset);
1633 tabular->AppendColumn(actcell);
1634 actcell = tabular->GetCellNumber(row, column);
1635 UpdateLocal(bv, INIT, true);
1637 case LyXTabular::DELETE_ROW:
1638 UnlockInsetInInset(bv, the_locking_inset);
1639 tabular->DeleteRow(tabular->row_of_cell(actcell));
1640 if ((row+1) > tabular->rows())
1642 actcell = tabular->GetCellNumber(row, column);
1644 UpdateLocal(bv, INIT, true);
1646 case LyXTabular::DELETE_COLUMN:
1647 UnlockInsetInInset(bv, the_locking_inset);
1648 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1649 if ((column+1) > tabular->columns())
1651 actcell = tabular->GetCellNumber(row, column);
1653 UpdateLocal(bv, INIT, true);
1655 case LyXTabular::M_TOGGLE_LINE_TOP:
1657 case LyXTabular::TOGGLE_LINE_TOP:
1659 bool lineSet = !tabular->TopLine(actcell, flag);
1660 for (int i = sel_row_start; i <= sel_row_end; ++i)
1661 for (int j = sel_col_start; j <= sel_col_end; ++j)
1662 tabular->SetTopLine(
1663 tabular->GetCellNumber(i, j),
1665 UpdateLocal(bv, INIT, true);
1669 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1671 case LyXTabular::TOGGLE_LINE_BOTTOM:
1673 bool lineSet = !tabular->BottomLine(actcell, flag);
1674 for (int i = sel_row_start; i <= sel_row_end; ++i)
1675 for (int j = sel_col_start; j <= sel_col_end; ++j)
1676 tabular->SetBottomLine(
1677 tabular->GetCellNumber(i, j),
1680 UpdateLocal(bv, INIT, true);
1684 case LyXTabular::M_TOGGLE_LINE_LEFT:
1686 case LyXTabular::TOGGLE_LINE_LEFT:
1688 bool lineSet = !tabular->LeftLine(actcell, flag);
1689 for (int i = sel_row_start; i <= sel_row_end; ++i)
1690 for (int j = sel_col_start; j <= sel_col_end; ++j)
1691 tabular->SetLeftLine(
1692 tabular->GetCellNumber(i,j),
1695 UpdateLocal(bv, INIT, true);
1699 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1701 case LyXTabular::TOGGLE_LINE_RIGHT:
1703 bool lineSet = !tabular->RightLine(actcell, flag);
1704 for (int i = sel_row_start; i <= sel_row_end; ++i)
1705 for (int j = sel_col_start; j <= sel_col_end; ++j)
1706 tabular->SetRightLine(
1707 tabular->GetCellNumber(i,j),
1710 UpdateLocal(bv, INIT, true);
1714 case LyXTabular::M_ALIGN_LEFT:
1715 case LyXTabular::M_ALIGN_RIGHT:
1716 case LyXTabular::M_ALIGN_CENTER:
1718 case LyXTabular::ALIGN_LEFT:
1719 case LyXTabular::ALIGN_RIGHT:
1720 case LyXTabular::ALIGN_CENTER:
1721 for (int i = sel_row_start; i <= sel_row_end; ++i)
1722 for (int j = sel_col_start; j <= sel_col_end; ++j)
1723 tabular->SetAlignment(
1724 tabular->GetCellNumber(i, j),
1727 UpdateLocal(bv, INIT, true);
1729 case LyXTabular::M_VALIGN_TOP:
1730 case LyXTabular::M_VALIGN_BOTTOM:
1731 case LyXTabular::M_VALIGN_CENTER:
1733 case LyXTabular::VALIGN_TOP:
1734 case LyXTabular::VALIGN_BOTTOM:
1735 case LyXTabular::VALIGN_CENTER:
1736 for (int i = sel_row_start; i <= sel_row_end; ++i)
1737 for (int j = sel_col_start; j <= sel_col_end; ++j)
1738 tabular->SetVAlignment(
1739 tabular->GetCellNumber(i, j),
1741 UpdateLocal(bv, INIT, true);
1743 case LyXTabular::MULTICOLUMN:
1745 if (sel_row_start != sel_row_end) {
1746 WriteAlert(_("Impossible Operation!"),
1747 _("Multicolumns can only be horizontally."),
1751 // just multicol for one Single Cell
1752 if (!hasSelection()) {
1753 // check wether we are completly in a multicol
1754 if (tabular->IsMultiColumn(actcell)) {
1755 tabular->UnsetMultiColumn(actcell);
1756 UpdateLocal(bv, INIT, true);
1758 tabular->SetMultiColumn(actcell, 1);
1759 UpdateLocal(bv, CELL, true);
1763 // we have a selection so this means we just add all this
1764 // cells to form a multicolumn cell
1768 if (sel_cell_start > sel_cell_end) {
1769 s_start = sel_cell_end;
1770 s_end = sel_cell_start;
1772 s_start = sel_cell_start;
1773 s_end = sel_cell_end;
1775 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1777 sel_cell_end = sel_cell_start;
1778 UpdateLocal(bv, INIT, true);
1781 case LyXTabular::SET_ALL_LINES:
1783 case LyXTabular::UNSET_ALL_LINES:
1784 for (int i = sel_row_start; i <= sel_row_end; ++i)
1785 for (int j = sel_col_start; j <= sel_col_end; ++j)
1786 tabular->SetAllLines(
1787 tabular->GetCellNumber(i,j), setLines);
1788 UpdateLocal(bv, INIT, true);
1790 case LyXTabular::SET_LONGTABULAR:
1791 tabular->SetLongTabular(true);
1792 UpdateLocal(bv, INIT, true); // because this toggles displayed
1794 case LyXTabular::UNSET_LONGTABULAR:
1795 tabular->SetLongTabular(false);
1796 UpdateLocal(bv, INIT, true); // because this toggles displayed
1798 case LyXTabular::SET_ROTATE_TABULAR:
1799 tabular->SetRotateTabular(true);
1801 case LyXTabular::UNSET_ROTATE_TABULAR:
1802 tabular->SetRotateTabular(false);
1804 case LyXTabular::SET_ROTATE_CELL:
1805 for (int i = sel_row_start; i <= sel_row_end; ++i)
1806 for (int j = sel_col_start; j<=sel_col_end; ++j)
1807 tabular->SetRotateCell(
1808 tabular->GetCellNumber(i, j),
1811 case LyXTabular::UNSET_ROTATE_CELL:
1812 for (int i = sel_row_start; i <= sel_row_end; ++i)
1813 for (int j = sel_col_start; j <= sel_col_end; ++j)
1814 tabular->SetRotateCell(
1815 tabular->GetCellNumber(i, j), false);
1817 case LyXTabular::SET_USEBOX:
1819 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1820 if (val == tabular->GetUsebox(actcell))
1821 val = LyXTabular::BOX_NONE;
1822 for (int i = sel_row_start; i <= sel_row_end; ++i)
1823 for (int j = sel_col_start; j <= sel_col_end; ++j)
1825 tabular->GetCellNumber(i, j), val);
1828 case LyXTabular::SET_LTFIRSTHEAD:
1829 tabular->SetLTHead(actcell, true);
1831 case LyXTabular::SET_LTHEAD:
1832 tabular->SetLTHead(actcell, false);
1834 case LyXTabular::SET_LTFOOT:
1835 tabular->SetLTFoot(actcell, false);
1837 case LyXTabular::SET_LTLASTFOOT:
1838 tabular->SetLTFoot(actcell, true);
1840 case LyXTabular::SET_LTNEWPAGE:
1842 bool what = !tabular->GetLTNewPage(actcell);
1843 tabular->SetLTNewPage(actcell, what);
1846 // dummy stuff just to avoid warnings
1847 case LyXTabular::LAST_ACTION:
1853 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1856 UpdatableInset * inset =
1857 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1858 LyXFont font(LyXFont::ALL_SANE);
1860 x = inset->x() + inset->width(bv, font);
1861 y = inset->descent(bv, font);
1863 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1864 //inset_y = cursor.y();
1865 inset->Edit(bv, x, y, button);
1866 if (!the_locking_inset)
1868 UpdateLocal(bv, CELL, false);
1869 return (the_locking_inset != 0);
1873 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1876 inset_x = cursor.x()
1877 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1878 inset_y = cursor.y();
1879 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1883 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1886 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1890 // This returns paperWidth() if the cell-width is unlimited or the width
1891 // in pixels if we have a pwidth for this cell.
1892 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1894 string const s = tabular->GetPWidth(cell);
1898 return VSpace(s).inPixels(bv);
1902 int InsetTabular::getMaxWidth(BufferView * bv,
1903 UpdatableInset const * inset) const
1905 int const n = tabular->GetNumberOfCells();
1907 for (; cell < n; ++cell) {
1908 if (tabular->GetCellInset(cell) == inset)
1913 int w = GetMaxWidthOfCell(bv, cell);
1915 // because the inset then subtracts it's top_x and owner->x()
1916 w += (inset->x() - top_x);
1920 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
1922 resizeLyXText(bv, recursive);
1925 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
1928 for(int i=0; i < tabular->rows(); ++i) {
1929 for(int j=0; j < tabular->columns(); ++j) {
1930 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
1938 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1939 bool const recursive) const
1941 if (the_locking_inset)
1942 return the_locking_inset->getLyXText(bv, recursive);
1943 return Inset::getLyXText(bv, recursive);
1947 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1949 if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1950 bv->owner()->getDialogs()
1951 ->showTabular(const_cast<InsetTabular *>(this));
1956 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1958 if (the_locking_inset) {
1959 InsetTabular * i = static_cast<InsetTabular *>
1960 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1962 i->OpenLayoutDialog(bv);
1966 bv->owner()->getDialogs()->showTabular(
1967 const_cast<InsetTabular *>(this));
1971 // functions returns:
1975 // 3 ... toggled off
1977 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1979 int action = LyXTabular::LAST_ACTION;
1980 LyXFunc::func_status status = LyXFunc::OK;
1983 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1984 string const tmp = tabularFeatures[i].feature;
1985 if (tmp == what.substr(0, tmp.length())) {
1986 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1987 // tabularFeatures[i].feature.length())) {
1988 action = tabularFeatures[i].action;
1992 if (action == LyXTabular::LAST_ACTION)
1993 return LyXFunc::Unknown;
1995 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
2002 if (hasSelection()) {
2003 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2005 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2009 case LyXTabular::SET_PWIDTH:
2010 case LyXTabular::SET_MPWIDTH:
2011 case LyXTabular::SET_SPECIAL_COLUMN:
2012 case LyXTabular::SET_SPECIAL_MULTI:
2013 status |= LyXFunc::Disabled;
2016 case LyXTabular::APPEND_ROW:
2017 case LyXTabular::APPEND_COLUMN:
2018 case LyXTabular::DELETE_ROW:
2019 case LyXTabular::DELETE_COLUMN:
2020 case LyXTabular::SET_ALL_LINES:
2021 case LyXTabular::UNSET_ALL_LINES:
2022 status |= LyXFunc::OK;
2025 case LyXTabular::MULTICOLUMN:
2026 if (tabular->IsMultiColumn(actcell))
2027 status |= LyXFunc::ToggleOn;
2029 status |= LyXFunc::ToggleOff;
2031 case LyXTabular::M_TOGGLE_LINE_TOP:
2033 case LyXTabular::TOGGLE_LINE_TOP:
2034 if (tabular->TopLine(actcell, flag))
2035 status |= LyXFunc::ToggleOn;
2037 status |= LyXFunc::ToggleOff;
2039 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2041 case LyXTabular::TOGGLE_LINE_BOTTOM:
2042 if (tabular->BottomLine(actcell, flag))
2043 status |= LyXFunc::ToggleOn;
2045 status |= LyXFunc::ToggleOff;
2047 case LyXTabular::M_TOGGLE_LINE_LEFT:
2049 case LyXTabular::TOGGLE_LINE_LEFT:
2050 if (tabular->LeftLine(actcell, flag))
2051 status |= LyXFunc::ToggleOn;
2053 status |= LyXFunc::ToggleOff;
2055 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2057 case LyXTabular::TOGGLE_LINE_RIGHT:
2058 if (tabular->RightLine(actcell, flag))
2059 status |= LyXFunc::ToggleOn;
2061 status |= LyXFunc::ToggleOff;
2063 case LyXTabular::M_ALIGN_LEFT:
2065 case LyXTabular::ALIGN_LEFT:
2066 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2067 status |= LyXFunc::ToggleOn;
2069 status |= LyXFunc::ToggleOff;
2071 case LyXTabular::M_ALIGN_RIGHT:
2073 case LyXTabular::ALIGN_RIGHT:
2074 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2075 status |= LyXFunc::ToggleOn;
2077 status |= LyXFunc::ToggleOff;
2079 case LyXTabular::M_ALIGN_CENTER:
2081 case LyXTabular::ALIGN_CENTER:
2082 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2083 status |= LyXFunc::ToggleOn;
2085 status |= LyXFunc::ToggleOff;
2087 case LyXTabular::M_VALIGN_TOP:
2089 case LyXTabular::VALIGN_TOP:
2090 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2091 status |= LyXFunc::ToggleOn;
2093 status |= LyXFunc::ToggleOff;
2095 case LyXTabular::M_VALIGN_BOTTOM:
2097 case LyXTabular::VALIGN_BOTTOM:
2098 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2099 status |= LyXFunc::ToggleOn;
2101 status |= LyXFunc::ToggleOff;
2103 case LyXTabular::M_VALIGN_CENTER:
2105 case LyXTabular::VALIGN_CENTER:
2106 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2107 status |= LyXFunc::ToggleOn;
2109 status |= LyXFunc::ToggleOff;
2111 case LyXTabular::SET_LONGTABULAR:
2112 if (tabular->IsLongTabular())
2113 status |= LyXFunc::ToggleOn;
2115 status |= LyXFunc::ToggleOff;
2117 case LyXTabular::UNSET_LONGTABULAR:
2118 if (!tabular->IsLongTabular())
2119 status |= LyXFunc::ToggleOn;
2121 status |= LyXFunc::ToggleOff;
2123 case LyXTabular::SET_ROTATE_TABULAR:
2124 if (tabular->GetRotateTabular())
2125 status |= LyXFunc::ToggleOn;
2127 status |= LyXFunc::ToggleOff;
2129 case LyXTabular::UNSET_ROTATE_TABULAR:
2130 if (!tabular->GetRotateTabular())
2131 status |= LyXFunc::ToggleOn;
2133 status |= LyXFunc::ToggleOff;
2135 case LyXTabular::SET_ROTATE_CELL:
2136 if (tabular->GetRotateCell(actcell))
2137 status |= LyXFunc::ToggleOn;
2139 status |= LyXFunc::ToggleOff;
2141 case LyXTabular::UNSET_ROTATE_CELL:
2142 if (!tabular->GetRotateCell(actcell))
2143 status |= LyXFunc::ToggleOn;
2145 status |= LyXFunc::ToggleOff;
2147 case LyXTabular::SET_USEBOX:
2148 if (strToInt(argument) == tabular->GetUsebox(actcell))
2149 status |= LyXFunc::ToggleOn;
2151 status |= LyXFunc::ToggleOff;
2153 case LyXTabular::SET_LTFIRSTHEAD:
2154 if (tabular->GetRowOfLTHead(actcell, dummy))
2155 status |= LyXFunc::ToggleOn;
2157 status |= LyXFunc::ToggleOff;
2159 case LyXTabular::SET_LTHEAD:
2160 if (tabular->GetRowOfLTHead(actcell, dummy))
2161 status |= LyXFunc::ToggleOn;
2163 status |= LyXFunc::ToggleOff;
2165 case LyXTabular::SET_LTFOOT:
2166 if (tabular->GetRowOfLTFoot(actcell, dummy))
2167 status |= LyXFunc::ToggleOn;
2169 status |= LyXFunc::ToggleOff;
2171 case LyXTabular::SET_LTLASTFOOT:
2172 if (tabular->GetRowOfLTFoot(actcell, dummy))
2173 status |= LyXFunc::ToggleOn;
2175 status |= LyXFunc::ToggleOff;
2177 case LyXTabular::SET_LTNEWPAGE:
2178 if (tabular->GetLTNewPage(actcell))
2179 status |= LyXFunc::ToggleOn;
2181 status |= LyXFunc::ToggleOff;
2184 status = LyXFunc::Disabled;
2191 std::vector<string> const InsetTabular::getLabelList() const
2193 return tabular->getLabelList();
2197 bool InsetTabular::copySelection(BufferView * bv)
2199 if (!hasSelection())
2201 //delete paste_tabular;
2203 //int sel_col_start;
2205 //int sel_row_start;
2208 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2209 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2210 if (sel_col_start > sel_col_end) {
2211 sel_col_start = sel_col_end;
2212 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2214 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2216 int const columns = sel_col_end - sel_col_start + 1;
2218 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2219 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2220 if (sel_row_start > sel_row_end) {
2221 //int tmp tmp = sel_row_start;
2222 //sel_row_start = sel_row_end;
2223 //sel_row_end = tmp;
2224 swap(sel_row_start, sel_row_end);
2226 int const rows = sel_row_end - sel_row_start + 1;
2228 delete paste_tabular;
2229 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2231 for (int i = 0; i < sel_row_start; ++i)
2232 paste_tabular->DeleteRow(0);
2233 while(paste_tabular->rows() > rows)
2234 paste_tabular->DeleteRow(rows);
2235 paste_tabular->SetTopLine(0, true, true);
2236 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2238 for (int i = 0; i < sel_col_start; ++i)
2239 paste_tabular->DeleteColumn(0);
2240 while (paste_tabular->columns() > columns)
2241 paste_tabular->DeleteColumn(columns);
2242 paste_tabular->SetLeftLine(0, true, true);
2243 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2247 paste_tabular->Ascii(bv->buffer(), sstr);
2248 bv->stuffClipboard(sstr.str().c_str());
2253 bool InsetTabular::pasteSelection(BufferView * bv)
2258 for (int r1 = 0, r2 = actrow;
2259 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2261 for(int c1 = 0, c2 = actcol;
2262 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2264 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2265 tabular->IsPartOfMultiColumn(r2,c2))
2267 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2271 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2275 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2276 int const n2 = tabular->GetCellNumber(r2, c2);
2277 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2278 tabular->GetCellInset(n2)->setOwner(this);
2279 tabular->GetCellInset(n2)->deleteLyXText(bv);
2286 bool InsetTabular::cutSelection()
2288 if (!hasSelection())
2291 //int sel_col_start;
2293 //int sel_row_start;
2296 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2297 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2298 if (sel_col_start > sel_col_end) {
2299 sel_col_start = sel_col_end;
2300 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2302 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2304 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2305 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2306 if (sel_row_start > sel_row_end) {
2307 //int tmp = sel_row_start;
2308 //sel_row_start = sel_row_end;
2309 //sel_row_end = tmp;
2310 swap(sel_row_start, sel_row_end);
2312 if (sel_cell_start > sel_cell_end) {
2313 //int tmp = sel_cell_start;
2314 //sel_cell_start = sel_cell_end;
2315 //sel_cell_end = tmp;
2316 swap(sel_cell_start, sel_cell_end);
2318 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2319 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2320 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2327 bool InsetTabular::isRightToLeft(BufferView *bv )
2329 return bv->getParentLanguage(this)->RightToLeft();
2332 bool InsetTabular::nodraw() const
2334 if (!UpdatableInset::nodraw() && the_locking_inset)
2335 return the_locking_inset->nodraw();
2336 return UpdatableInset::nodraw();
2339 int InsetTabular::scroll(bool recursive) const
2341 int sx = UpdatableInset::scroll(false);
2343 if (recursive && the_locking_inset)
2344 sx += the_locking_inset->scroll(recursive);
2349 bool InsetTabular::doClearArea() const
2351 return !locked || (need_update & (FULL|INIT));
2354 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2356 srow = tabular->row_of_cell(sel_cell_start);
2357 erow = tabular->row_of_cell(sel_cell_end);
2361 scol = tabular->column_of_cell(sel_cell_start);
2362 ecol = tabular->column_of_cell(sel_cell_end);
2366 ecol = tabular->right_column_of_cell(sel_cell_end);