1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 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"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
38 const int ADD_TO_HEIGHT = 2;
39 const int ADD_TO_TABULAR_WIDTH = 2;
47 //#define cellstart(p) ((p % 2) == 0)
49 bool cellstart(LyXParagraph::size_type p)
51 return ((p & 2) == 0);
55 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
62 tabular = new LyXTabular(this, rows,columns);
63 // for now make it always display as display() inset
65 the_locking_inset = 0;
66 locked = no_selection = cursor_visible = false;
71 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
77 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
80 tabular = new LyXTabular(this, *(tab.tabular));
81 the_locking_inset = 0;
82 locked = no_selection = cursor_visible = false;
87 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
93 InsetTabular::~InsetTabular()
97 dialogs_->hideTabular(this);
101 Inset * InsetTabular::Clone() const
103 InsetTabular * t = new InsetTabular(*this, buffer);
105 t->tabular = tabular->Clone(t);
110 void InsetTabular::Write(Buffer const * buf, ostream & os) const
112 os << " Tabular" << endl;
113 tabular->Write(buf, os);
117 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
119 bool old_format = (lex.GetString() == "\\LyXTable");
124 tabular = new LyXTabular(buf, this, lex);
132 token = lex.GetString();
133 while (lex.IsOK() && (token != "\\end_inset")) {
135 token = lex.GetString();
137 if (token != "\\end_inset") {
138 lex.printError("Missing \\end_inset at this point. "
144 int InsetTabular::ascent(BufferView *, LyXFont const &) const
146 return tabular->GetAscentOfRow(0);
150 int InsetTabular::descent(BufferView *, LyXFont const &) const
152 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
156 int InsetTabular::width(BufferView *, LyXFont const &) const
158 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
162 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
163 float & x, bool cleared) const
165 Painter & pain = bv->painter();
170 UpdatableInset::draw(bv,font,baseline,x,cleared);
171 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
172 (top_x != int(x)) || (top_baseline != baseline))) {
173 int h = ascent(bv, font) + descent(bv, font);
174 int tx = display()||!owner()? 0:top_x;
175 int w = tx? width(bv, font):pain.paperWidth();
176 int ty = baseline - ascent(bv, font);
180 if ((ty + h) > pain.paperHeight())
181 h = pain.paperHeight();
182 if ((top_x + w) > pain.paperWidth())
183 w = pain.paperWidth();
184 pain.fillRectangle(tx, ty, w, h);
189 top_baseline = baseline;
191 x += ADD_TO_TABULAR_WIDTH;
192 if (cleared || (need_update == FULL) || (need_update == CELL)) {
193 for(i=0;i<tabular->rows();++i) {
195 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
196 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
197 for(j=0;j<tabular->columns();++j) {
198 if (tabular->IsPartOfMultiColumn(i,j))
200 cx = nx + tabular->GetBeginningOfTextInCell(cell);
202 DrawCellSelection(pain, nx, baseline, i, j, cell);
203 if (dodraw && !cleared && locked && the_locking_inset) {
204 if (the_locking_inset == tabular->GetCellInset(cell))
205 tabular->GetCellInset(cell)->draw(bv, font,
209 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
211 DrawCellLines(pain, nx, baseline, i, cell);
213 nx += tabular->GetWidthOfColumn(cell);
216 baseline += tabular->GetDescentOfRow(i) +
217 tabular->GetAscentOfRow(i+1)+
218 tabular->GetAdditionalHeight(cell+1);
221 x += width(bv, font);
226 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
227 int row, int cell) const
229 int x2 = x + tabular->GetWidthOfColumn(cell);
232 if (!tabular->TopAlreadyDrawed(cell)) {
233 on_off = !tabular->TopLine(cell);
234 pain.line(x, baseline - tabular->GetAscentOfRow(row),
235 x2, baseline - tabular->GetAscentOfRow(row),
236 on_off ? LColor::tabularonoffline:LColor::tabularline,
237 on_off ? Painter::line_onoffdash:Painter::line_solid);
239 on_off = !tabular->BottomLine(cell);
240 pain.line(x,baseline + tabular->GetDescentOfRow(row),
241 x2, baseline + tabular->GetDescentOfRow(row),
242 on_off ? LColor::tabularonoffline:LColor::tabularline,
243 on_off ? Painter::line_onoffdash:Painter::line_solid);
244 if (!tabular->LeftAlreadyDrawed(cell)) {
245 on_off = !tabular->LeftLine(cell);
246 pain.line(x, baseline - tabular->GetAscentOfRow(row),
247 x, baseline + tabular->GetDescentOfRow(row),
248 on_off ? LColor::tabularonoffline:LColor::tabularline,
249 on_off ? Painter::line_onoffdash:Painter::line_solid);
251 on_off = !tabular->RightLine(cell);
252 pain.line(x2 - tabular->GetAdditionalWidth(cell),
253 baseline - tabular->GetAscentOfRow(row),
254 x2 - tabular->GetAdditionalWidth(cell),
255 baseline + tabular->GetDescentOfRow(row),
256 on_off ? LColor::tabularonoffline:LColor::tabularline,
257 on_off ? Painter::line_onoffdash:Painter::line_solid);
261 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
262 int row, int column, int cell) const
264 int cs = tabular->column_of_cell(sel_cell_start);
265 int ce = tabular->column_of_cell(sel_cell_end);
268 cs = tabular->column_of_cell(sel_cell_end);
270 ce = tabular->right_column_of_cell(sel_cell_end);
273 int rs = tabular->row_of_cell(sel_cell_start);
274 int re = tabular->row_of_cell(sel_cell_end);
275 if (rs > re) swap(rs, re);
277 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
278 int w = tabular->GetWidthOfColumn(cell);
279 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
280 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
281 w, h, LColor::selection);
286 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
290 calculate_dimensions_of_cells(bv, font, true);
292 owner()->update(bv, font, true);
295 if (the_locking_inset)
296 the_locking_inset->update(bv, font, reinit);
297 switch(need_update) {
301 if (calculate_dimensions_of_cells(bv, font, false))
313 char const * InsetTabular::EditMessage() const
315 return _("Opened Tabular Inset");
319 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
321 UpdatableInset::Edit(bv, x, y, button);
323 if (!bv->lockInset(this)) {
324 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
328 the_locking_inset = 0;
329 inset_pos = inset_x = inset_y = 0;
331 sel_pos_start = sel_pos_end = cursor.pos();
332 sel_cell_start = sel_cell_end = actcell;
333 bv->text->FinishUndo();
334 if (InsetHit(bv, x, y)) {
335 ActivateCellInset(bv, x, y, button);
337 UpdateLocal(bv, NONE, false);
338 // bv->getOwner()->getPopups().updateFormTabular();
342 void InsetTabular::InsetUnlock(BufferView * bv)
344 if (the_locking_inset) {
345 the_locking_inset->InsetUnlock(bv);
346 the_locking_inset = 0;
349 if (hasSelection()) {
350 sel_pos_start = sel_pos_end = 0;
351 sel_cell_start = sel_cell_end = 0;
352 UpdateLocal(bv, FULL, false);
354 no_selection = false;
360 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
364 bv->updateInset(this, mark_dirty);
370 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
372 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
376 if (inset == tabular->GetCellInset(actcell)) {
377 lyxerr[Debug::INSETS] << "OK" << endl;
378 the_locking_inset = tabular->GetCellInset(actcell);
380 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
381 inset_y = cursor.y();
382 inset_pos = cursor.pos();
384 } else if (the_locking_inset && (the_locking_inset == inset)) {
385 if (cursor.pos() == inset_pos) {
386 lyxerr[Debug::INSETS] << "OK" << endl;
388 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
389 inset_y = cursor.y();
391 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
393 } else if (the_locking_inset) {
394 lyxerr[Debug::INSETS] << "MAYBE" << endl;
395 return the_locking_inset->LockInsetInInset(bv, inset);
397 lyxerr[Debug::INSETS] << "NOT OK" << endl;
402 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
405 if (!the_locking_inset)
407 if (the_locking_inset == inset) {
408 the_locking_inset->InsetUnlock(bv);
409 the_locking_inset = 0;
411 moveRight(bv, false);
412 UpdateLocal(bv, CELL, false);
415 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
416 if ((inset->LyxCode() == TABULAR_CODE) &&
417 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
419 dialogs_ = bv->owner()->getDialogs();
420 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
429 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
431 if (!the_locking_inset)
433 if (the_locking_inset != inset)
434 return the_locking_inset->UpdateInsetInInset(bv, inset);
435 UpdateLocal(bv, CELL, false);
440 int InsetTabular::InsetInInsetY()
442 if (!the_locking_inset)
445 return (inset_y + the_locking_inset->InsetInInsetY());
449 UpdatableInset * InsetTabular::GetLockingInset()
451 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
455 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
459 if (the_locking_inset)
460 return the_locking_inset->GetFirstLockingInsetOfType(c);
465 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
467 if (the_locking_inset)
468 return the_locking_inset->InsertInset(bv, inset);
473 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
475 if (hasSelection()) {
476 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
477 UpdateLocal(bv, SELECTION, false);
479 no_selection = false;
484 sel_pos_start = sel_pos_end = cursor.pos();
485 sel_cell_start = sel_cell_end = actcell;
487 bool inset_hit = InsetHit(bv, x, y);
489 if ((ocell == actcell) && the_locking_inset && inset_hit) {
490 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
492 } else if (the_locking_inset) {
493 the_locking_inset->InsetUnlock(bv);
495 the_locking_inset = 0;
496 if (inset_hit && bv->the_locking_inset) {
497 ActivateCellInset(bv, x, y, button);
498 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
503 void InsetTabular::InsetButtonRelease(BufferView * bv,
504 int x, int y, int button)
507 if (the_locking_inset) {
509 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
510 i->InsetButtonRelease(bv, x, y, button);
514 dialogs_ = bv->owner()->getDialogs();
515 dialogs_->showTabular(this);
517 else if (ocell != actcell)
518 bview->getOwner()->getPopups().updateTabular();
522 if (the_locking_inset) {
523 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
526 no_selection = false;
530 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
532 if (the_locking_inset) {
533 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
534 y - inset_y, button);
538 // int ocell = actcell,
539 int old = sel_pos_end;
542 sel_pos_end = cursor.pos();
543 sel_cell_end = actcell;
544 if (old != sel_pos_end)
545 UpdateLocal(bv, SELECTION, false);
547 if (ocell != actcell)
548 bview->getOwner()->getPopups().updateFormTabular();
551 no_selection = false;
555 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
557 if (the_locking_inset) {
558 the_locking_inset->InsetKeyPress(xke);
564 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
567 UpdatableInset::RESULT
570 no_selection = false;
571 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
572 || (result == DISPATCHED_NOUPDATE)) {
578 if ((action < 0) && arg.empty())
581 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
582 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
584 if (the_locking_inset) {
585 result=the_locking_inset->LocalDispatch(bv, action, arg);
586 if (result == DISPATCHED_NOUPDATE)
588 else if (result == DISPATCHED) {
589 the_locking_inset->ToggleInsetCursor(bv);
590 UpdateLocal(bv, CELL, false);
591 the_locking_inset->ToggleInsetCursor(bv);
593 } else if (result == FINISHED) {
594 if ((action == LFUN_RIGHT) || (action == -1)) {
595 cursor.pos(inset_pos + 1);
598 sel_pos_start = sel_pos_end = cursor.pos();
599 sel_cell_start = sel_cell_end = actcell;
606 bool hs = hasSelection();
610 // Normal chars not handled here
613 // --- Cursor Movements ---------------------------------------------
615 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
617 moveRight(bv, false);
618 sel_pos_end = cursor.pos();
619 if (!cellstart(cursor.pos())) {
620 if (tabular->right_column_of_cell(sel_cell_start) >
621 tabular->right_column_of_cell(actcell))
622 sel_cell_end = actcell+1;
624 sel_cell_end = actcell;
626 UpdateLocal(bv, SELECTION, false);
629 result = moveRight(bv);
630 sel_pos_start = sel_pos_end = cursor.pos();
631 sel_cell_start = sel_cell_end = actcell;
633 UpdateLocal(bv, SELECTION, false);
636 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
639 sel_pos_end = cursor.pos();
640 if (cellstart(cursor.pos())) {
641 if (tabular->column_of_cell(sel_cell_start) >=
642 tabular->column_of_cell(actcell))
643 sel_cell_end = actcell;
645 sel_cell_end = actcell-1;
647 UpdateLocal(bv, SELECTION, false);
650 result = moveLeft(bv);
651 sel_pos_start = sel_pos_end = cursor.pos();
652 sel_cell_start = sel_cell_end = actcell;
654 UpdateLocal(bv, SELECTION, false);
660 sel_pos_end = cursor.pos();
661 if ((ocell == sel_cell_end) ||
662 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
663 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
665 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
666 UpdateLocal(bv, SELECTION, false);
670 result= moveDown(bv);
671 sel_pos_start = sel_pos_end = cursor.pos();
672 sel_cell_start = sel_cell_end = actcell;
674 UpdateLocal(bv, SELECTION, false);
680 sel_pos_end = cursor.pos();
681 if ((ocell == sel_cell_end) ||
682 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
683 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
685 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
686 UpdateLocal(bv, SELECTION, false);
691 sel_pos_start = sel_pos_end = cursor.pos();
692 sel_cell_start = sel_cell_end = actcell;
694 UpdateLocal(bv, SELECTION, false);
706 if (the_locking_inset) {
707 UnlockInsetInInset(bv, the_locking_inset);
708 the_locking_inset = 0;
710 if (action == LFUN_TAB)
714 sel_pos_start = sel_pos_end = cursor.pos();
715 sel_cell_start = sel_cell_end = actcell;
717 UpdateLocal(bv, SELECTION, false);
719 case LFUN_LAYOUT_TABLE:
721 dialogs_ = bv->owner()->getDialogs();
722 dialogs_->showTabular(this);
726 result = UNDISPATCHED;
729 if (result!=FINISHED) {
730 if (!the_locking_inset) {
732 if (ocell != actcell)
733 bview->getOwner()->getPopups().updateFormTabular();
738 bv->unlockInset(this);
743 int InsetTabular::Latex(Buffer const * buf, ostream & os,
744 bool fragile, bool fp) const
746 return tabular->Latex(buf, os, fragile, fp);
750 int InsetTabular::Ascii(Buffer const *, ostream &) const
756 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
762 int InsetTabular::DocBook(Buffer const *, ostream &) const
768 void InsetTabular::Validate(LaTeXFeatures & features) const
770 tabular->Validate(features);
774 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
775 LyXFont const & font,
781 bool changed = false;
783 for(int i = 0; i < tabular->rows(); ++i) {
784 maxAsc = maxDesc = 0;
785 for(int j= 0; j < tabular->columns(); ++j) {
786 if (tabular->IsPartOfMultiColumn(i,j))
789 inset = tabular->GetCellInset(cell);
791 inset->update(bv, font, false);
792 maxAsc = max(maxAsc, inset->ascent(bv, font));
793 maxDesc = max(maxDesc, inset->descent(bv, font));
794 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
796 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
797 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
803 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
805 x = cursor.x() - top_x;
810 void InsetTabular::ToggleInsetCursor(BufferView * bv)
812 if (the_locking_inset) {
813 the_locking_inset->ToggleInsetCursor(bv);
817 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
819 int asc = lyxfont::maxAscent(font);
820 int desc = lyxfont::maxDescent(font);
823 bv->hideLockedInsetCursor();
825 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
826 cursor_visible = !cursor_visible;
830 void InsetTabular::ShowInsetCursor(BufferView * bv)
832 if (!cursor_visible) {
833 LyXFont font; // = GetFont(par, cursor.pos);
835 int asc = lyxfont::maxAscent(font);
836 int desc = lyxfont::maxDescent(font);
837 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
838 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
839 cursor_visible = true;
844 void InsetTabular::HideInsetCursor(BufferView * bv)
847 ToggleInsetCursor(bv);
851 void InsetTabular::setPos(BufferView * bv, int x, int y) const
856 actcell = actrow = actcol = 0;
857 int ly = tabular->GetDescentOfRow(actrow);
859 // first search the right row
860 while((ly < y) && (actrow < tabular->rows())) {
861 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
862 tabular->GetAscentOfRow(actrow+1) +
863 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
866 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
868 actcell = tabular->GetCellNumber(actrow, actcol);
870 // now search the right column
871 int lx = tabular->GetWidthOfColumn(actcell) -
872 tabular->GetAdditionalWidth(actcell);
873 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
874 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
875 tabular->GetAdditionalWidth(actcell - 1));
876 cursor.pos(((actcell+1) * 2) - 1);
878 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
879 cursor.x(lx + top_x - 2);
881 cursor.pos(cursor.pos() - 1);
882 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
888 int InsetTabular::getCellXPos(int cell) const
892 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
894 int lx = tabular->GetWidthOfColumn(cell);
895 for(; (c < cell); ++c) {
896 lx += tabular->GetWidthOfColumn(c);
898 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
899 ADD_TO_TABULAR_WIDTH);
903 void InsetTabular::resetPos(BufferView * bv) const
907 actcol = tabular->column_of_cell(actcell);
912 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
913 if (tabular->IsLastCellInRow(cell)) {
914 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
915 tabular->GetAscentOfRow(actrow + 1) +
916 tabular->GetAdditionalHeight(cell + 1));
920 cursor.x(getCellXPos(actcell) + 2);
921 if (cursor.pos() % 2) {
922 LyXFont font(LyXFont::ALL_SANE);
923 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
924 tabular->GetBeginningOfTextInCell(actcell));
926 if ((!the_locking_inset ||
927 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
928 (actcell != oldcell)) {
929 dialogs_ = bv->owner()->getDialogs();
930 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
936 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
938 if (!cellstart(cursor.pos())) {
939 if (tabular->IsLastCell(actcell))
942 cursor.pos(cursor.pos() + 1);
944 if (ActivateCellInset(bv))
946 } else { // before the inset
947 cursor.pos(cursor.pos() + 1);
950 return DISPATCHED_NOUPDATE;
954 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
961 cursor.pos(cursor.pos() - 1);
962 if (!cellstart(cursor.pos())) {
964 } else if (lock) { // behind the inset
965 if (ActivateCellInset(bv, 0, 0, 0, true))
969 return DISPATCHED_NOUPDATE;
973 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
976 actcell = tabular->GetCellAbove(actcell);
977 if (actcell == ocell) // we moved out of the inset
980 return DISPATCHED_NOUPDATE;
984 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
987 actcell = tabular->GetCellBelow(actcell);
988 if (actcell == ocell) // we moved out of the inset
991 return DISPATCHED_NOUPDATE;
995 bool InsetTabular::moveNextCell(BufferView * bv)
997 if (tabular->IsLastCell(actcell))
1000 cursor.pos(cursor.pos() + 1);
1001 if (!cellstart(cursor.pos()))
1002 cursor.pos(cursor.pos() + 1);
1008 bool InsetTabular::movePrevCell(BufferView * bv)
1010 if (!actcell) // first cell
1013 cursor.pos(cursor.pos() - 1);
1014 if (cellstart(cursor.pos()))
1015 cursor.pos(cursor.pos() - 1);
1021 bool InsetTabular::Delete()
1027 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1029 if (the_locking_inset)
1030 the_locking_inset->SetFont(bv, font, tall);
1034 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1043 setAlign = LYX_ALIGN_LEFT,
1049 case LyXTabular::M_ALIGN_LEFT:
1050 case LyXTabular::ALIGN_LEFT:
1051 setAlign=LYX_ALIGN_LEFT;
1053 case LyXTabular::M_ALIGN_RIGHT:
1054 case LyXTabular::ALIGN_RIGHT:
1055 setAlign=LYX_ALIGN_RIGHT;
1057 case LyXTabular::M_ALIGN_CENTER:
1058 case LyXTabular::ALIGN_CENTER:
1059 setAlign=LYX_ALIGN_CENTER;
1061 case LyXTabular::M_VALIGN_TOP:
1062 case LyXTabular::VALIGN_TOP:
1063 setAlign=LyXTabular::LYX_VALIGN_TOP;
1065 case LyXTabular::M_VALIGN_BOTTOM:
1066 case LyXTabular::VALIGN_BOTTOM:
1067 setAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1069 case LyXTabular::M_VALIGN_CENTER:
1070 case LyXTabular::VALIGN_CENTER:
1071 setAlign=LyXTabular::LYX_VALIGN_CENTER;
1076 if (hasSelection()) {
1078 sel_col_start = tabular->column_of_cell(sel_cell_start);
1079 sel_col_end = tabular->column_of_cell(sel_cell_end);
1080 if (sel_col_start > sel_col_end) {
1081 sel_col_end = sel_col_start;
1082 sel_col_start = tabular->column_of_cell(sel_cell_end);
1084 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1087 sel_row_start = tabular->row_of_cell(sel_cell_start);
1088 sel_row_end = tabular->row_of_cell(sel_cell_end);
1089 if (sel_row_start > sel_row_end) {
1090 tmp = sel_row_start;
1091 sel_row_start = sel_row_end;
1095 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1096 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1098 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1100 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1101 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1103 bv->text->cursor.par()->previous,
1104 bv->text->cursor.par()->next
1108 int row = tabular->row_of_cell(actcell);
1109 int column = tabular->column_of_cell(actcell);
1113 case LyXTabular::SET_PWIDTH:
1115 bool update = (tabular->GetColumnPWidth(actcell) != val);
1116 tabular->SetColumnPWidth(actcell,val);
1118 for (int i=0; i < tabular->rows(); ++i) {
1119 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1122 UpdateLocal(bv, INIT, true);
1126 case LyXTabular::SET_MPWIDTH:
1128 bool update = (tabular->GetPWidth(actcell) != val);
1129 tabular->SetMColumnPWidth(actcell,val);
1131 for (int i=0; i < tabular->rows(); ++i) {
1132 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1135 UpdateLocal(bv, INIT, true);
1139 case LyXTabular::SET_SPECIAL_COLUMN:
1140 case LyXTabular::SET_SPECIAL_MULTI:
1141 tabular->SetAlignSpecial(actcell,val,feature);
1143 case LyXTabular::APPEND_ROW:
1144 // append the row into the tabular
1145 UnlockInsetInInset(bv, the_locking_inset);
1146 tabular->AppendRow(actcell);
1147 UpdateLocal(bv, INIT, true);
1149 case LyXTabular::APPEND_COLUMN:
1150 // append the column into the tabular
1151 tabular->AppendColumn(actcell);
1152 actcell = tabular->GetCellNumber(row, column);
1153 UpdateLocal(bv, INIT, true);
1155 case LyXTabular::DELETE_ROW:
1156 tabular->DeleteRow(tabular->row_of_cell(actcell));
1157 if ((row+1) > tabular->rows())
1159 actcell = tabular->GetCellNumber(row, column);
1160 UpdateLocal(bv, INIT, true);
1162 case LyXTabular::DELETE_COLUMN:
1163 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1164 if ((column+1) > tabular->columns())
1166 actcell = tabular->GetCellNumber(row, column);
1167 UpdateLocal(bv, INIT, true);
1169 case LyXTabular::M_TOGGLE_LINE_TOP:
1171 case LyXTabular::TOGGLE_LINE_TOP:
1172 lineSet = !tabular->TopLine(actcell, flag);
1173 for(i=sel_row_start; i<=sel_row_end; ++i)
1174 for(j=sel_col_start; j<=sel_col_end; ++j)
1175 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1176 UpdateLocal(bv, INIT, true);
1179 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1181 case LyXTabular::TOGGLE_LINE_BOTTOM:
1182 lineSet = !tabular->BottomLine(actcell, flag);
1183 for(i=sel_row_start; i<=sel_row_end; ++i)
1184 for(j=sel_col_start; j<=sel_col_end; ++j)
1185 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1187 UpdateLocal(bv, INIT, true);
1190 case LyXTabular::M_TOGGLE_LINE_LEFT:
1192 case LyXTabular::TOGGLE_LINE_LEFT:
1193 lineSet = !tabular->LeftLine(actcell, flag);
1194 for(i=sel_row_start; i<=sel_row_end; ++i)
1195 for(j=sel_col_start; j<=sel_col_end; ++j)
1196 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1198 UpdateLocal(bv, INIT, true);
1201 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1203 case LyXTabular::TOGGLE_LINE_RIGHT:
1204 lineSet = !tabular->RightLine(actcell, flag);
1205 for(i=sel_row_start; i<=sel_row_end; ++i)
1206 for(j=sel_col_start; j<=sel_col_end; ++j)
1207 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1209 UpdateLocal(bv, INIT, true);
1211 case LyXTabular::M_ALIGN_LEFT:
1212 case LyXTabular::M_ALIGN_RIGHT:
1213 case LyXTabular::M_ALIGN_CENTER:
1215 case LyXTabular::ALIGN_LEFT:
1216 case LyXTabular::ALIGN_RIGHT:
1217 case LyXTabular::ALIGN_CENTER:
1218 for(i=sel_row_start; i<=sel_row_end; ++i)
1219 for(j=sel_col_start; j<=sel_col_end; ++j)
1220 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign,
1223 UpdateLocal(bv, INIT, true);
1225 UpdateLocal(bv, CELL, true);
1227 case LyXTabular::M_VALIGN_TOP:
1228 case LyXTabular::M_VALIGN_BOTTOM:
1229 case LyXTabular::M_VALIGN_CENTER:
1231 case LyXTabular::VALIGN_TOP:
1232 case LyXTabular::VALIGN_BOTTOM:
1233 case LyXTabular::VALIGN_CENTER:
1234 for(i=sel_row_start; i<=sel_row_end; ++i)
1235 for(j=sel_col_start; j<=sel_col_end; ++j)
1236 tabular->SetVAlignment(tabular->GetCellNumber(i,j), setAlign,
1239 UpdateLocal(bv, INIT, true);
1241 UpdateLocal(bv, CELL, true);
1243 case LyXTabular::MULTICOLUMN:
1245 if (sel_row_start != sel_row_end) {
1246 WriteAlert(_("Impossible Operation!"),
1247 _("Multicolumns can only be horizontally."),
1251 // just multicol for one Single Cell
1252 if (!hasSelection()) {
1253 // check wether we are completly in a multicol
1254 if (tabular->IsMultiColumn(actcell)) {
1255 tabular->UnsetMultiColumn(actcell);
1256 UpdateLocal(bv, INIT, true);
1258 tabular->SetMultiColumn(actcell, 1);
1259 UpdateLocal(bv, CELL, true);
1263 // we have a selection so this means we just add all this
1264 // cells to form a multicolumn cell
1268 if (sel_cell_start > sel_cell_end) {
1269 s_start = sel_cell_end;
1270 s_end = sel_cell_start;
1272 s_start = sel_cell_start;
1273 s_end = sel_cell_end;
1275 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1278 sel_cell_end = sel_cell_start;
1279 sel_pos_end = sel_pos_start;
1280 UpdateLocal(bv, INIT, true);
1283 case LyXTabular::SET_ALL_LINES:
1285 case LyXTabular::UNSET_ALL_LINES:
1286 for(i=sel_row_start; i<=sel_row_end; ++i)
1287 for(j=sel_col_start; j<=sel_col_end; ++j)
1288 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1289 UpdateLocal(bv, INIT, true);
1291 case LyXTabular::SET_LONGTABULAR:
1292 tabular->SetLongTabular(true);
1293 UpdateLocal(bv, INIT, true); // because this toggles displayed
1295 case LyXTabular::UNSET_LONGTABULAR:
1296 tabular->SetLongTabular(false);
1297 UpdateLocal(bv, INIT, true); // because this toggles displayed
1299 case LyXTabular::SET_ROTATE_TABULAR:
1300 tabular->SetRotateTabular(true);
1302 case LyXTabular::UNSET_ROTATE_TABULAR:
1303 tabular->SetRotateTabular(false);
1305 case LyXTabular::SET_ROTATE_CELL:
1306 for(i=sel_row_start; i<=sel_row_end; ++i)
1307 for(j=sel_col_start; j<=sel_col_end; ++j)
1308 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1310 case LyXTabular::UNSET_ROTATE_CELL:
1311 for(i=sel_row_start; i<=sel_row_end; ++i)
1312 for(j=sel_col_start; j<=sel_col_end; ++j)
1313 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1315 case LyXTabular::SET_LINEBREAKS:
1316 what = !tabular->GetLinebreaks(actcell);
1317 for(i=sel_row_start; i<=sel_row_end; ++i)
1318 for(j=sel_col_start; j<=sel_col_end; ++j)
1319 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1321 case LyXTabular::SET_LTFIRSTHEAD:
1322 tabular->SetLTHead(actcell,true);
1324 case LyXTabular::SET_LTHEAD:
1325 tabular->SetLTHead(actcell,false);
1327 case LyXTabular::SET_LTFOOT:
1328 tabular->SetLTFoot(actcell,false);
1330 case LyXTabular::SET_LTLASTFOOT:
1331 tabular->SetLTFoot(actcell,true);
1333 case LyXTabular::SET_LTNEWPAGE:
1334 what = !tabular->GetLTNewPage(actcell);
1335 tabular->SetLTNewPage(actcell,what);
1341 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1344 // the cursor.pos has to be before the inset so if it isn't now just
1345 // reset the curor pos first!
1346 if (cursor.pos() % 2) { // behind the inset
1347 cursor.pos(cursor.pos() - 1);
1350 UpdatableInset * inset =
1351 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1352 LyXFont font(LyXFont::ALL_SANE);
1354 x = inset->x() + inset->width(bv, font);
1355 y = inset->descent(bv, font);
1357 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1358 inset_y = cursor.y();
1359 inset->Edit(bv, x - inset_x, y - inset_y, button);
1360 if (!the_locking_inset)
1362 UpdateLocal(bv, CELL, false);
1367 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1369 InsetText * inset = tabular->GetCellInset(actcell);
1372 if (cursor.pos() % 2) { // behind the inset
1373 return (((x + top_x) < cursor.x()) &&
1374 ((x + top_x) > (cursor.x() - inset->width(bv,
1375 LyXFont(LyXFont::ALL_SANE)))));
1377 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1378 return ((x1 > x2) &&
1379 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1384 // This returns paperWidth() if the cell-width is unlimited or the width
1385 // in pixels if we have a pwidth for this cell.
1386 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1388 string s = tabular->GetPWidth(cell);
1392 return VSpace(s).inPixels( 0, 0);
1396 int InsetTabular::getMaxWidth(Painter & pain,
1397 UpdatableInset const * inset) const
1400 int n = tabular->GetNumberOfCells();
1401 for(cell=0; cell < n; ++cell) {
1402 if (tabular->GetCellInset(cell) == inset)
1407 int w = GetMaxWidthOfCell(pain, cell);
1408 // this because text insets remove the xpos from the maxwidth because
1409 // otherwise the would not break good!!!
1410 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1416 void InsetTabular::recomputeTextInsets(BufferView * bv,
1417 LyXFont const & font) const
1423 for(int j= 0; j < tabular->columns(); ++j) {
1424 for(int i = 0; i < tabular->rows(); ++i) {
1425 if (tabular->IsPartOfMultiColumn(i,j))
1427 cell = tabular->GetCellNumber(i,j);
1428 inset = tabular->GetCellInset(cell);
1429 inset->update(bv, font);
1430 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1432 // cell = tabular->GetCellNumber(0, j);
1433 // cx += tabular->GetWidthOfColumn(cell);
1438 void InsetTabular::resizeLyXText(BufferView *) const
1443 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1445 if (the_locking_inset)
1446 return the_locking_inset->getLyXText(bv);
1447 return Inset::getLyXText(bv);