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"
26 #include "LaTeXFeatures.h"
30 #include "lyx_gui_misc.h"
33 #include "insets/insettext.h"
35 extern void MenuLayoutTabular(bool, InsetTabular *);
36 extern bool UpdateLayoutTabular(bool, InsetTabular *);
37 extern void TabularOptClose();
39 const int ADD_TO_HEIGHT = 2;
40 const int ADD_TO_TABULAR_WIDTH = 2;
47 #define cellstart(p) ((p % 2) == 0)
49 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
55 buffer = buf; // set this first!!!
56 tabular = new LyXTabular(this, rows,columns);
57 // for now make it always display as display() inset
59 the_locking_inset = 0;
60 locked = no_selection = cursor_visible = false;
65 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
70 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
72 buffer = buf; // set this first
73 tabular = new LyXTabular(this, *(tab.tabular));
74 the_locking_inset = 0;
75 locked = no_selection = cursor_visible = false;
80 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
85 InsetTabular::~InsetTabular()
91 Inset * InsetTabular::Clone() const
93 InsetTabular * t = new InsetTabular(*this, buffer);
95 t->tabular = tabular->Clone(t);
100 void InsetTabular::Write(Buffer const * buf, ostream & os) const
102 os << " Tabular" << endl;
103 tabular->Write(buf, os);
107 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
109 bool old_format = (lex.GetString() == "\\LyXTable");
114 tabular = new LyXTabular(buf, this, lex);
122 token = lex.GetString();
123 while (lex.IsOK() && (token != "\\end_inset")) {
125 token = lex.GetString();
127 if (token != "\\end_inset") {
128 lex.printError("Missing \\end_inset at this point. "
134 int InsetTabular::ascent(Painter &, LyXFont const &) const
136 return tabular->GetAscentOfRow(0);
140 int InsetTabular::descent(Painter &, LyXFont const &) const
142 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
146 int InsetTabular::width(Painter &, LyXFont const &) const
148 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
152 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
155 Painter & pain = bv->painter();
160 UpdatableInset::draw(bv,font,baseline,x);
161 if (init_inset || (top_x != int(x)) || (top_baseline != baseline)) {
164 top_baseline = baseline;
166 if (locked) { // repaint this way as the background was not cleared
167 if (the_locking_inset)
168 the_locking_inset->update(bv, font, true);
170 bv->updateInset(const_cast<InsetTabular*>(this), false);
175 x += ADD_TO_TABULAR_WIDTH;
176 for(i=0;i<tabular->rows();++i) {
178 for(j=0;j<tabular->columns();++j) {
179 if (tabular->IsPartOfMultiColumn(i,j))
181 cx = nx + tabular->GetBeginningOfTextInCell(cell);
183 DrawCellSelection(pain, nx, baseline, i, j, cell);
184 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx);
185 DrawCellLines(pain, nx, baseline, i, cell);
186 nx += tabular->GetWidthOfColumn(cell);
189 baseline += tabular->GetDescentOfRow(i) + tabular->GetAscentOfRow(i+1)
190 + tabular->GetAdditionalHeight(cell+1);
192 x += width(pain, font);
196 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool dodraw)
198 if (the_locking_inset)
199 the_locking_inset->update(bv, font, dodraw);
201 // calculate_dimensions_of_cells(bv, font, dodraw);
202 init_inset = calculate_dimensions_of_cells(bv, font, dodraw) || init_inset;
207 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
208 int row, int cell) const
210 int x2 = x + tabular->GetWidthOfColumn(cell);
213 if (!tabular->TopAlreadyDrawed(cell)) {
214 on_off = !tabular->TopLine(cell);
215 pain.line(x, baseline - tabular->GetAscentOfRow(row),
216 x2, baseline - tabular->GetAscentOfRow(row),
217 on_off ? LColor::tabularonoffline:LColor::tabularline,
218 on_off ? Painter::line_onoffdash:Painter::line_solid);
220 on_off = !tabular->BottomLine(cell);
221 pain.line(x,baseline + tabular->GetDescentOfRow(row),
222 x2, baseline + tabular->GetDescentOfRow(row),
223 on_off ? LColor::tabularonoffline:LColor::tabularline,
224 on_off ? Painter::line_onoffdash:Painter::line_solid);
225 if (!tabular->LeftAlreadyDrawed(cell)) {
226 on_off = !tabular->LeftLine(cell);
227 pain.line(x, baseline - tabular->GetAscentOfRow(row),
228 x, baseline + tabular->GetDescentOfRow(row),
229 on_off ? LColor::tabularonoffline:LColor::tabularline,
230 on_off ? Painter::line_onoffdash:Painter::line_solid);
232 on_off = !tabular->RightLine(cell);
233 pain.line(x2 - tabular->GetAdditionalWidth(cell),
234 baseline - tabular->GetAscentOfRow(row),
235 x2 - tabular->GetAdditionalWidth(cell),
236 baseline + tabular->GetDescentOfRow(row),
237 on_off ? LColor::tabularonoffline:LColor::tabularline,
238 on_off ? Painter::line_onoffdash:Painter::line_solid);
242 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
243 int row, int column, int cell) const
247 int cs = tabular->column_of_cell(sel_cell_start);
248 int ce = tabular->column_of_cell(sel_cell_end);
251 cs = tabular->column_of_cell(sel_cell_end);
253 ce = tabular->right_column_of_cell(sel_cell_end);
256 int rs = tabular->row_of_cell(sel_cell_start);
257 int re = tabular->row_of_cell(sel_cell_end);
264 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
265 int w = tabular->GetWidthOfColumn(cell);
266 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
267 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
268 w, h, LColor::selection);
273 char const * InsetTabular::EditMessage() const
275 return _("Opened Tabular Inset");
279 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
281 UpdatableInset::Edit(bv, x, y, button);
283 if (!bv->lockInset(this)) {
284 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
288 the_locking_inset = 0;
289 inset_pos = inset_x = inset_y = 0;
290 setPos(bv->painter(), x, y);
291 sel_pos_start = sel_pos_end = cursor.pos();
292 sel_cell_start = sel_cell_end = actcell;
293 bv->text->FinishUndo();
294 if (InsetHit(bv, x, y)) {
295 ActivateCellInset(bv, x, y, button);
297 UpdateLocal(bv, false, false);
298 // bv->getOwner()->getPopups().updateFormTabular();
302 void InsetTabular::InsetUnlock(BufferView * bv)
305 if (the_locking_inset) {
306 the_locking_inset->InsetUnlock(bv);
307 the_locking_inset = 0;
310 if (hasSelection()) {
311 sel_pos_start = sel_pos_end = cursor.pos();
312 sel_cell_start = sel_cell_end = actcell;
313 UpdateLocal(bv, false, false);
315 no_selection = false;
320 void InsetTabular::UpdateLocal(BufferView * bv, bool what, bool mark_dirty)
323 bv->updateInset(this, mark_dirty);
325 resetPos(bv->painter());
328 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
330 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
334 if (inset == tabular->GetCellInset(actcell)) {
335 lyxerr[Debug::INSETS] << "OK" << endl;
336 the_locking_inset = tabular->GetCellInset(actcell);
337 resetPos(bv->painter());
338 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
339 inset_y = cursor.y();
340 inset_pos = cursor.pos();
342 } else if (the_locking_inset && (the_locking_inset == inset)) {
343 if (cursor.pos() == inset_pos) {
344 lyxerr[Debug::INSETS] << "OK" << endl;
345 resetPos(bv->painter());
346 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
347 inset_y = cursor.y();
349 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
351 } else if (the_locking_inset) {
352 lyxerr[Debug::INSETS] << "MAYBE" << endl;
353 return the_locking_inset->LockInsetInInset(bv, inset);
355 lyxerr[Debug::INSETS] << "NOT OK" << endl;
359 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
362 if (!the_locking_inset)
364 if (the_locking_inset == inset) {
365 the_locking_inset->InsetUnlock(bv);
366 the_locking_inset = 0;
368 moveRight(bv, false);
369 UpdateLocal(bv, true, false);
372 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
373 if ((inset->LyxCode() == TABULAR_CODE) &&
374 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
376 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
384 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
386 if (!the_locking_inset)
388 if (the_locking_inset != inset)
389 return the_locking_inset->UpdateInsetInInset(bv, inset);
390 UpdateLocal(bv, false, false);
395 int InsetTabular::InsetInInsetY()
397 if (!the_locking_inset)
400 return (inset_y + the_locking_inset->InsetInInsetY());
404 UpdatableInset * InsetTabular::GetLockingInset()
406 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
410 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
414 if (the_locking_inset)
415 return the_locking_inset->GetFirstLockingInsetOfType(c);
420 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
422 if (the_locking_inset)
423 return the_locking_inset->InsertInset(bv, inset);
428 void InsetTabular::InsetButtonRelease(BufferView * bv,
429 int x, int y, int button)
432 if (the_locking_inset) {
434 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
435 i->InsetButtonRelease(bv, x, y, button);
439 MenuLayoutTabular(true, this);
442 if (the_locking_inset) {
443 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
446 no_selection = false;
450 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
452 if (hasSelection()) {
453 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
454 UpdateLocal(bv, false, false);
456 no_selection = false;
460 setPos(bv->painter(), x, y);
461 sel_pos_start = sel_pos_end = cursor.pos();
462 sel_cell_start = sel_cell_end = actcell;
464 bool inset_hit = InsetHit(bv, x, y);
466 if ((ocell == actcell) && the_locking_inset && inset_hit) {
467 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
469 } else if (the_locking_inset) {
470 the_locking_inset->InsetUnlock(bv);
472 the_locking_inset = 0;
473 if (inset_hit && bv->the_locking_inset) {
474 ActivateCellInset(bv, x, y, button);
475 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
480 bview->getOwner()->getPopups().showFormTabular();
481 else if (ocell != actcell)
482 bview->getOwner()->getPopups().updateFormTabular();
487 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
489 if (the_locking_inset) {
490 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
491 y - inset_y, button);
495 // int ocell = actcell,
496 int old = sel_pos_end;
498 setPos(bv->painter(), x, y);
499 sel_pos_end = cursor.pos();
500 sel_cell_end = actcell;
501 if (old != sel_pos_end)
502 UpdateLocal(bv, false, false);
504 if (ocell != actcell)
505 bview->getOwner()->getPopups().updateFormTabular();
508 no_selection = false;
512 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
514 if (the_locking_inset) {
515 the_locking_inset->InsetKeyPress(xke);
521 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
524 UpdatableInset::RESULT
527 no_selection = false;
528 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
529 || (result == DISPATCHED_NOUPDATE)) {
531 resetPos(bv->painter());
536 if ((action < 0) && arg.empty())
539 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
540 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
542 if (the_locking_inset) {
543 result=the_locking_inset->LocalDispatch(bv, action, arg);
544 if (result == DISPATCHED_NOUPDATE)
546 else if (result == DISPATCHED) {
547 bool upd = SetCellDimensions(bv->painter(), actcell, actrow);
548 the_locking_inset->ToggleInsetCursor(bv);
549 UpdateLocal(bv, upd, false);
550 the_locking_inset->ToggleInsetCursor(bv);
552 } else if (result == FINISHED) {
553 if ((action == LFUN_RIGHT) || (action == -1)) {
554 cursor.pos(inset_pos + 1);
555 resetPos(bv->painter());
557 sel_pos_start = sel_pos_end = cursor.pos();
558 sel_cell_start = sel_cell_end = actcell;
565 bool hs = hasSelection();
568 // Normal chars not handled here
571 // --- Cursor Movements ---------------------------------------------
573 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
575 moveRight(bv, false);
576 sel_pos_end = cursor.pos();
577 if (!cellstart(cursor.pos())) {
578 if (tabular->right_column_of_cell(sel_cell_start) >
579 tabular->right_column_of_cell(actcell))
580 sel_cell_end = actcell+1;
582 sel_cell_end = actcell;
584 UpdateLocal(bv, false, false);
587 result = moveRight(bv);
588 sel_pos_start = sel_pos_end = cursor.pos();
589 sel_cell_start = sel_cell_end = actcell;
591 UpdateLocal(bv, false, false);
594 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
597 sel_pos_end = cursor.pos();
598 if (cellstart(cursor.pos())) {
599 if (tabular->column_of_cell(sel_cell_start) >=
600 tabular->column_of_cell(actcell))
601 sel_cell_end = actcell;
603 sel_cell_end = actcell-1;
605 UpdateLocal(bv, false, false);
608 result = moveLeft(bv);
609 sel_pos_start = sel_pos_end = cursor.pos();
610 sel_cell_start = sel_cell_end = actcell;
612 UpdateLocal(bv, false, false);
618 sel_pos_end = cursor.pos();
619 if ((ocell == sel_cell_end) ||
620 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
621 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
623 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
624 UpdateLocal(bv, false, false);
628 result= moveDown(bv);
629 sel_pos_start = sel_pos_end = cursor.pos();
630 sel_cell_start = sel_cell_end = actcell;
632 UpdateLocal(bv, false, false);
638 sel_pos_end = cursor.pos();
639 if ((ocell == sel_cell_end) ||
640 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
641 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
643 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
644 UpdateLocal(bv, false, false);
649 sel_pos_start = sel_pos_end = cursor.pos();
650 sel_cell_start = sel_cell_end = actcell;
652 UpdateLocal(bv, false, false);
664 if (the_locking_inset) {
665 the_locking_inset->InsetUnlock(bv);
667 the_locking_inset = 0;
668 if (action == LFUN_TAB)
672 sel_pos_start = sel_pos_end = cursor.pos();
673 sel_cell_start = sel_cell_end = actcell;
675 UpdateLocal(bv, false, false);
677 case LFUN_LAYOUT_TABLE:
679 int flag = (arg == "true");
680 MenuLayoutTabular(flag, this);
684 result = UNDISPATCHED;
687 if (result!=FINISHED) {
688 if (!the_locking_inset) {
690 if (ocell != actcell)
691 bview->getOwner()->getPopups().updateFormTabular();
696 bv->unlockInset(this);
701 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
703 return tabular->Latex(buf, os, fragile, fp);
707 int InsetTabular::Ascii(Buffer const *, ostream &) const
712 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
718 int InsetTabular::DocBook(Buffer const *, ostream &) const
724 void InsetTabular::Validate(LaTeXFeatures & features) const
726 tabular->Validate(features);
730 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
731 LyXFont const & font,
737 bool changed = false;
739 for(int i = 0; i < tabular->rows(); ++i) {
740 maxAsc = maxDesc = 0;
741 for(int j= 0; j < tabular->columns(); ++j) {
742 if (tabular->IsPartOfMultiColumn(i,j))
745 inset = tabular->GetCellInset(cell);
746 inset->update(bv, font, dodraw);
747 maxAsc = max(maxAsc, inset->ascent(bv->painter(), font));
748 maxDesc = max(maxDesc, inset->descent(bv->painter(), font));
749 changed = tabular->SetWidthOfCell(cell, inset->width(bv->painter(), font)) || changed;
751 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
752 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
758 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
760 x = cursor.x() - top_x;
765 void InsetTabular::ToggleInsetCursor(BufferView * bv)
767 if (the_locking_inset) {
768 the_locking_inset->ToggleInsetCursor(bv);
772 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
774 int asc = lyxfont::maxAscent(font);
775 int desc = lyxfont::maxDescent(font);
778 bv->hideLockedInsetCursor();
780 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
781 cursor_visible = !cursor_visible;
785 void InsetTabular::ShowInsetCursor(BufferView * bv)
787 if (!cursor_visible) {
788 LyXFont font; // = GetFont(par, cursor.pos);
790 int asc = lyxfont::maxAscent(font);
791 int desc = lyxfont::maxDescent(font);
792 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
793 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
794 cursor_visible = true;
799 void InsetTabular::HideInsetCursor(BufferView * bv)
802 ToggleInsetCursor(bv);
806 void InsetTabular::setPos(Painter & pain, int x, int y) const
811 actcell = actrow = actcol = 0;
812 int ly = tabular->GetDescentOfRow(actrow);
814 // first search the right row
815 while((ly < y) && (actrow < tabular->rows())) {
816 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
817 tabular->GetAscentOfRow(actrow+1) +
818 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
821 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
823 actcell = tabular->GetCellNumber(actrow, actcol);
825 // now search the right column
826 int lx = tabular->GetWidthOfColumn(actcell) -
827 tabular->GetAdditionalWidth(actcell);
828 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
829 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
830 tabular->GetAdditionalWidth(actcell - 1));
831 cursor.pos(((actcell+1) * 2) - 1);
833 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
834 cursor.x(lx + top_x - 2);
836 cursor.pos(cursor.pos() - 1);
837 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
842 int InsetTabular::getCellXPos(int cell) const
846 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
848 int lx = tabular->GetWidthOfColumn(cell);
849 for(; (c < cell); ++c) {
850 lx += tabular->GetWidthOfColumn(c);
852 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
853 ADD_TO_TABULAR_WIDTH);
856 void InsetTabular::resetPos(Painter & pain) const
860 actcol = tabular->column_of_cell(actcell);
865 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
866 if (tabular->IsLastCellInRow(cell)) {
867 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
868 tabular->GetAscentOfRow(actrow + 1) +
869 tabular->GetAdditionalHeight(cell + 1));
873 cursor.x(getCellXPos(actcell) + 2);
874 if (cursor.pos() % 2) {
875 LyXFont font(LyXFont::ALL_SANE);
876 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(pain,font) +
877 tabular->GetBeginningOfTextInCell(actcell));
879 if ((!the_locking_inset ||
880 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
881 (actcell != oldcell)) {
882 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
888 bool InsetTabular::SetCellDimensions(Painter & pain, int cell, int row)
890 InsetText * inset = tabular->GetCellInset(cell);
891 LyXFont font(LyXFont::ALL_SANE);
892 int asc = inset->ascent(pain, font) + ADD_TO_HEIGHT;
893 int desc = inset->descent(pain, font) + ADD_TO_HEIGHT;
894 int maxAsc = tabular->GetAscentOfRow(row);
895 int maxDesc = tabular->GetDescentOfRow(row);
896 bool ret = tabular->SetWidthOfCell(cell, inset->width(pain, font));
900 tabular->SetAscentOfRow(row, asc);
902 if (maxDesc < desc) {
904 tabular->SetDescentOfRow(row, desc);
910 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
912 if (!cellstart(cursor.pos())) {
913 if (tabular->IsLastCell(actcell))
916 cursor.pos(cursor.pos() + 1);
918 if (ActivateCellInset(bv))
920 } else { // before the inset
921 cursor.pos(cursor.pos() + 1);
923 resetPos(bv->painter());
924 return DISPATCHED_NOUPDATE;
928 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
935 cursor.pos(cursor.pos() - 1);
936 if (!cellstart(cursor.pos())) {
938 } else if (lock) { // behind the inset
939 if (ActivateCellInset(bv, 0, 0, 0, true))
942 resetPos(bv->painter());
943 return DISPATCHED_NOUPDATE;
947 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
950 actcell = tabular->GetCellAbove(actcell);
951 if (actcell == ocell) // we moved out of the inset
953 resetPos(bv->painter());
954 return DISPATCHED_NOUPDATE;
958 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
961 actcell = tabular->GetCellBelow(actcell);
962 if (actcell == ocell) // we moved out of the inset
964 resetPos(bv->painter());
965 return DISPATCHED_NOUPDATE;
969 bool InsetTabular::moveNextCell(BufferView * bv)
971 if (tabular->IsLastCell(actcell))
974 cursor.pos(cursor.pos() + 1);
975 if (!cellstart(cursor.pos()))
976 cursor.pos(cursor.pos() + 1);
977 resetPos(bv->painter());
982 bool InsetTabular::movePrevCell(BufferView * bv)
984 if (!actcell) // first cell
987 cursor.pos(cursor.pos() - 1);
988 if (cellstart(cursor.pos()))
989 cursor.pos(cursor.pos() - 1);
990 resetPos(bv->painter());
995 bool InsetTabular::Delete()
1001 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1003 if (the_locking_inset)
1004 the_locking_inset->SetFont(bv, font, tall);
1008 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1017 setAlign = LYX_ALIGN_LEFT,
1023 case LyXTabular::ALIGN_LEFT:
1024 setAlign=LYX_ALIGN_LEFT;
1026 case LyXTabular::ALIGN_RIGHT:
1027 setAlign=LYX_ALIGN_RIGHT;
1029 case LyXTabular::ALIGN_CENTER:
1030 setAlign=LYX_ALIGN_CENTER;
1035 if (hasSelection()) {
1037 sel_col_start = tabular->column_of_cell(sel_cell_start);
1038 sel_col_end = tabular->column_of_cell(sel_cell_end);
1039 if (sel_col_start > sel_col_end) {
1040 sel_col_end = sel_col_start;
1041 sel_col_start = tabular->column_of_cell(sel_cell_end);
1043 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1046 sel_row_start = tabular->row_of_cell(sel_cell_start);
1047 sel_row_end = tabular->row_of_cell(sel_cell_end);
1048 if (sel_row_start > sel_row_end) {
1049 tmp = sel_row_start;
1050 sel_row_start = sel_row_end;
1054 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1055 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1057 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1058 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1059 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1061 int row = tabular->row_of_cell(actcell);
1062 int column = tabular->column_of_cell(actcell);
1065 case LyXTabular::SET_PWIDTH:
1067 bool update = (tabular->GetPWidth(actcell) != val);
1068 tabular->SetPWidth(actcell,val);
1070 UpdateLocal(bv, true, true);
1073 case LyXTabular::SET_SPECIAL_COLUMN:
1074 case LyXTabular::SET_SPECIAL_MULTI:
1075 tabular->SetAlignSpecial(actcell,val,feature);
1077 case LyXTabular::APPEND_ROW:
1078 // append the row into the tabular
1079 UnlockInsetInInset(bv, the_locking_inset);
1080 tabular->AppendRow(actcell);
1081 UpdateLocal(bv, true, true);
1083 case LyXTabular::APPEND_COLUMN:
1084 // append the column into the tabular
1085 tabular->AppendColumn(actcell);
1086 actcell = tabular->GetCellNumber(row, column);
1087 UpdateLocal(bv, true, true);
1089 case LyXTabular::DELETE_ROW:
1090 tabular->DeleteRow(tabular->row_of_cell(actcell));
1091 if ((row+1) > tabular->rows())
1093 actcell = tabular->GetCellNumber(row, column);
1094 UpdateLocal(bv, true, true);
1096 case LyXTabular::DELETE_COLUMN:
1097 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1098 if ((column+1) > tabular->columns())
1100 actcell = tabular->GetCellNumber(row, column);
1101 UpdateLocal(bv, true, true);
1103 case LyXTabular::TOGGLE_LINE_TOP:
1104 lineSet = !tabular->TopLine(actcell);
1105 for(i=sel_row_start; i<=sel_row_end; ++i)
1106 for(j=sel_col_start; j<=sel_col_end; ++j)
1107 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1108 UpdateLocal(bv, true, true);
1111 case LyXTabular::TOGGLE_LINE_BOTTOM:
1112 lineSet = !tabular->BottomLine(actcell);
1113 for(i=sel_row_start; i<=sel_row_end; ++i)
1114 for(j=sel_col_start; j<=sel_col_end; ++j)
1115 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1116 UpdateLocal(bv, true, true);
1119 case LyXTabular::TOGGLE_LINE_LEFT:
1120 lineSet = !tabular->LeftLine(actcell);
1121 for(i=sel_row_start; i<=sel_row_end; ++i)
1122 for(j=sel_col_start; j<=sel_col_end; ++j)
1123 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1124 UpdateLocal(bv, true, true);
1127 case LyXTabular::TOGGLE_LINE_RIGHT:
1128 lineSet = !tabular->RightLine(actcell);
1129 for(i=sel_row_start; i<=sel_row_end; ++i)
1130 for(j=sel_col_start; j<=sel_col_end; ++j)
1131 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1132 UpdateLocal(bv, true, true);
1134 case LyXTabular::ALIGN_LEFT:
1135 case LyXTabular::ALIGN_RIGHT:
1136 case LyXTabular::ALIGN_CENTER:
1137 for(i=sel_row_start; i<=sel_row_end; ++i)
1138 for(j=sel_col_start; j<=sel_col_end; ++j)
1139 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1140 UpdateLocal(bv, true, true);
1142 case LyXTabular::MULTICOLUMN:
1144 if (sel_row_start != sel_row_end) {
1145 WriteAlert(_("Impossible Operation!"),
1146 _("Multicolumns can only be horizontally."),
1150 // just multicol for one Single Cell
1151 if (!hasSelection()) {
1152 // check wether we are completly in a multicol
1153 if (tabular->IsMultiColumn(actcell)) {
1154 tabular->UnsetMultiColumn(actcell);
1155 UpdateLocal(bv, true, true);
1157 tabular->SetMultiColumn(actcell, 1);
1158 UpdateLocal(bv, false, true);
1162 // we have a selection so this means we just add all this
1163 // cells to form a multicolumn cell
1167 if (sel_cell_start > sel_cell_end) {
1168 s_start = sel_cell_end;
1169 s_end = sel_cell_start;
1171 s_start = sel_cell_start;
1172 s_end = sel_cell_end;
1174 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1177 sel_cell_end = sel_cell_start;
1178 sel_pos_end = sel_pos_start;
1179 UpdateLocal(bv, true, true);
1182 case LyXTabular::SET_ALL_LINES:
1184 case LyXTabular::UNSET_ALL_LINES:
1185 for(i=sel_row_start; i<=sel_row_end; ++i)
1186 for(j=sel_col_start; j<=sel_col_end; ++j)
1187 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1188 UpdateLocal(bv, true, true);
1190 case LyXTabular::SET_LONGTABULAR:
1191 tabular->SetLongTabular(true);
1192 UpdateLocal(bv, true, true); // because this toggles displayed
1194 case LyXTabular::UNSET_LONGTABULAR:
1195 tabular->SetLongTabular(false);
1196 UpdateLocal(bv, true, true); // because this toggles displayed
1198 case LyXTabular::SET_ROTATE_TABULAR:
1199 tabular->SetRotateTabular(true);
1201 case LyXTabular::UNSET_ROTATE_TABULAR:
1202 tabular->SetRotateTabular(false);
1204 case LyXTabular::SET_ROTATE_CELL:
1205 for(i=sel_row_start; i<=sel_row_end; ++i)
1206 for(j=sel_col_start; j<=sel_col_end; ++j)
1207 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1209 case LyXTabular::UNSET_ROTATE_CELL:
1210 for(i=sel_row_start; i<=sel_row_end; ++i)
1211 for(j=sel_col_start; j<=sel_col_end; ++j)
1212 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1214 case LyXTabular::SET_LINEBREAKS:
1215 what = !tabular->GetLinebreaks(actcell);
1216 for(i=sel_row_start; i<=sel_row_end; ++i)
1217 for(j=sel_col_start; j<=sel_col_end; ++j)
1218 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1220 case LyXTabular::SET_LTFIRSTHEAD:
1221 tabular->SetLTHead(actcell,true);
1223 case LyXTabular::SET_LTHEAD:
1224 tabular->SetLTHead(actcell,false);
1226 case LyXTabular::SET_LTFOOT:
1227 tabular->SetLTFoot(actcell,false);
1229 case LyXTabular::SET_LTLASTFOOT:
1230 tabular->SetLTFoot(actcell,true);
1232 case LyXTabular::SET_LTNEWPAGE:
1233 what = !tabular->GetLTNewPage(actcell);
1234 tabular->SetLTNewPage(actcell,what);
1239 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1242 // the cursor.pos has to be before the inset so if it isn't now just
1243 // reset the curor pos first!
1244 if (cursor.pos() % 2) { // behind the inset
1245 cursor.pos(cursor.pos() - 1);
1246 resetPos(bv->painter());
1248 UpdatableInset * inset =
1249 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1250 LyXFont font(LyXFont::ALL_SANE);
1252 x = inset->x() + inset->width(bv->painter(), font);
1253 y = inset->descent(bv->painter(), font);
1255 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1256 inset_y = cursor.y();
1257 inset->Edit(bv, x - inset_x, y - inset_y, button);
1258 if (!the_locking_inset)
1260 UpdateLocal(bv, true, false);
1264 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1266 InsetText * inset = tabular->GetCellInset(actcell);
1269 if (cursor.pos() % 2) { // behind the inset
1270 return (((x + top_x) < cursor.x()) &&
1271 ((x + top_x) > (cursor.x() - inset->width(bv->painter(),
1272 LyXFont(LyXFont::ALL_SANE)))));
1274 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1275 return ((x1 > x2) &&
1276 (x1 < (x2 + inset->width(bv->painter(),
1277 LyXFont(LyXFont::ALL_SANE)))));
1281 // This returns paperWidth() if the cell-width is unlimited or the width
1282 // in pixels if we have a pwidth for this cell.
1283 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1285 string s = tabular->GetPWidth(cell);
1289 return VSpace(s).inPixels( 0, 0);
1292 int InsetTabular::getMaxWidth(Painter & pain,
1293 UpdatableInset const * inset) const
1296 int n = tabular->GetNumberOfCells();
1297 for(cell=0; cell < n; ++cell) {
1298 if (tabular->GetCellInset(cell) == inset)
1303 int w = GetMaxWidthOfCell(pain, cell);
1304 // this because text insets remove the xpos from the maxwidth because
1305 // otherwise the would not break good!!!
1306 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1311 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1317 for(int j= 0; j < tabular->columns(); ++j) {
1318 for(int i = 0; i < tabular->rows(); ++i) {
1319 if (tabular->IsPartOfMultiColumn(i,j))
1321 cell = tabular->GetCellNumber(i,j);
1322 inset = tabular->GetCellInset(cell);
1323 inset->update(bv, font);
1324 tabular->SetWidthOfCell(cell, inset->width(bv->painter(), font));
1326 // cell = tabular->GetCellNumber(0, j);
1327 // cx += tabular->GetWidthOfColumn(cell);