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 & pain, LyXFont const & font) const
137 calculate_width_of_cells(pain, font);
140 return tabular->GetAscentOfRow(0);
144 int InsetTabular::descent(Painter & pain, LyXFont const & font) const
147 calculate_width_of_cells(pain, font);
150 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
154 int InsetTabular::width(Painter & pain, LyXFont const & font) const
157 calculate_width_of_cells(pain, font);
160 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
164 void InsetTabular::draw(Painter & pain, const LyXFont & font, int baseline,
172 UpdatableInset::draw(pain,font,baseline,x);
173 if (init_inset || (top_x != int(x)) || (top_baseline != baseline)) {
177 top_baseline = baseline;
179 recomputeTextInsets(pain, font);
180 calculate_width_of_cells(pain, font);
184 x += ADD_TO_TABULAR_WIDTH;
185 for(i=0;i<tabular->rows();++i) {
187 for(j=0;j<tabular->columns();++j) {
188 if (tabular->IsPartOfMultiColumn(i,j))
190 cx = nx + tabular->GetBeginningOfTextInCell(cell);
192 DrawCellSelection(pain, nx, baseline, i, j, cell);
193 tabular->GetCellInset(cell)->draw(pain, font, baseline, cx);
194 DrawCellLines(pain, nx, baseline, i, cell);
195 nx += tabular->GetWidthOfColumn(cell);
198 baseline += tabular->GetDescentOfRow(i) + tabular->GetAscentOfRow(i+1)
199 + tabular->GetAdditionalHeight(cell+1);
201 x += width(pain, font);
205 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
206 int row, int cell) const
208 int x2 = x + tabular->GetWidthOfColumn(cell);
211 if (!tabular->TopAlreadyDrawed(cell)) {
212 on_off = !tabular->TopLine(cell);
213 pain.line(x, baseline - tabular->GetAscentOfRow(row),
214 x2, baseline - tabular->GetAscentOfRow(row),
215 on_off ? LColor::tabularonoffline:LColor::tabularline,
216 on_off ? Painter::line_onoffdash:Painter::line_solid);
218 on_off = !tabular->BottomLine(cell);
219 pain.line(x,baseline + tabular->GetDescentOfRow(row),
220 x2, baseline + tabular->GetDescentOfRow(row),
221 on_off ? LColor::tabularonoffline:LColor::tabularline,
222 on_off ? Painter::line_onoffdash:Painter::line_solid);
223 if (!tabular->LeftAlreadyDrawed(cell)) {
224 on_off = !tabular->LeftLine(cell);
225 pain.line(x, baseline - tabular->GetAscentOfRow(row),
226 x, baseline + tabular->GetDescentOfRow(row),
227 on_off ? LColor::tabularonoffline:LColor::tabularline,
228 on_off ? Painter::line_onoffdash:Painter::line_solid);
230 on_off = !tabular->RightLine(cell);
231 pain.line(x2 - tabular->GetAdditionalWidth(cell),
232 baseline - tabular->GetAscentOfRow(row),
233 x2 - tabular->GetAdditionalWidth(cell),
234 baseline + tabular->GetDescentOfRow(row),
235 on_off ? LColor::tabularonoffline:LColor::tabularline,
236 on_off ? Painter::line_onoffdash:Painter::line_solid);
240 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
241 int row, int column, int cell) const
245 int cs = tabular->column_of_cell(sel_cell_start);
246 int ce = tabular->column_of_cell(sel_cell_end);
249 cs = tabular->column_of_cell(sel_cell_end);
251 ce = tabular->right_column_of_cell(sel_cell_end);
254 int rs = tabular->row_of_cell(sel_cell_start);
255 int re = tabular->row_of_cell(sel_cell_end);
262 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
263 int w = tabular->GetWidthOfColumn(cell);
264 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
265 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
266 w, h, LColor::selection);
271 char const * InsetTabular::EditMessage() const
273 return _("Opened Tabular Inset");
277 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
279 UpdatableInset::Edit(bv, x, y, button);
281 if (!bv->lockInset(this)) {
282 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
286 the_locking_inset = 0;
287 inset_pos = inset_x = inset_y = 0;
288 setPos(bv->painter(), x, y);
289 sel_pos_start = sel_pos_end = cursor.pos();
290 sel_cell_start = sel_cell_end = actcell;
291 bv->text->FinishUndo();
292 if (InsetHit(bv, x, y)) {
293 ActivateCellInset(bv, x, y, button);
295 UpdateLocal(bv, true);
296 // bv->getOwner()->getPopups().updateFormTabular();
300 void InsetTabular::InsetUnlock(BufferView * bv)
303 if (the_locking_inset) {
304 the_locking_inset->InsetUnlock(bv);
305 the_locking_inset = 0;
308 if (hasSelection()) {
309 sel_pos_start = sel_pos_end = cursor.pos();
310 sel_cell_start = sel_cell_end = actcell;
311 UpdateLocal(bv, false);
313 no_selection = false;
318 void InsetTabular::UpdateLocal(BufferView * bv, bool flag)
321 calculate_width_of_cells(bv->painter(), LyXFont(LyXFont::ALL_SANE));
322 bv->updateInset(this, flag);
324 resetPos(bv->painter());
327 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
329 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
333 if (inset == tabular->GetCellInset(actcell)) {
334 lyxerr[Debug::INSETS] << "OK" << endl;
335 the_locking_inset = tabular->GetCellInset(actcell);
336 resetPos(bv->painter());
337 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
338 inset_y = cursor.y();
339 inset_pos = cursor.pos();
341 } else if (the_locking_inset && (the_locking_inset == inset)) {
342 if (cursor.pos() == inset_pos) {
343 lyxerr[Debug::INSETS] << "OK" << endl;
344 resetPos(bv->painter());
345 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
346 inset_y = cursor.y();
348 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
350 } else if (the_locking_inset) {
351 lyxerr[Debug::INSETS] << "MAYBE" << endl;
352 return the_locking_inset->LockInsetInInset(bv, inset);
354 lyxerr[Debug::INSETS] << "NOT OK" << endl;
358 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
361 if (!the_locking_inset)
363 if (the_locking_inset == inset) {
364 the_locking_inset->InsetUnlock(bv);
365 the_locking_inset = 0;
367 moveRight(bv, false);
370 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
371 if ((inset->LyxCode() == TABULAR_CODE) &&
372 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
374 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
382 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
384 if (!the_locking_inset)
386 if (the_locking_inset != inset)
387 return the_locking_inset->UpdateInsetInInset(bv, inset);
393 int InsetTabular::InsetInInsetY()
395 if (!the_locking_inset)
398 return (inset_y + the_locking_inset->InsetInInsetY());
402 UpdatableInset * InsetTabular::GetLockingInset()
404 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
408 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
412 if (the_locking_inset)
413 return the_locking_inset->GetFirstLockingInsetOfType(c);
418 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
420 if (the_locking_inset)
421 return the_locking_inset->InsertInset(bv, inset);
426 void InsetTabular::InsetButtonRelease(BufferView * bv,
427 int x, int y, int button)
430 if (the_locking_inset) {
432 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
433 i->InsetButtonRelease(bv, x, y, button);
437 MenuLayoutTabular(true, this);
440 if (the_locking_inset) {
441 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
444 no_selection = false;
448 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
450 if (hasSelection()) {
451 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
452 UpdateLocal(bv, false);
454 no_selection = false;
458 setPos(bv->painter(), x, y);
459 sel_pos_start = sel_pos_end = cursor.pos();
460 sel_cell_start = sel_cell_end = actcell;
462 bool inset_hit = InsetHit(bv, x, y);
464 if ((ocell == actcell) && the_locking_inset && inset_hit) {
465 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
467 } else if (the_locking_inset) {
468 the_locking_inset->InsetUnlock(bv);
470 the_locking_inset = 0;
471 if (inset_hit && bv->the_locking_inset) {
472 ActivateCellInset(bv, x, y, button);
473 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
478 bview->getOwner()->getPopups().showFormTabular();
479 else if (ocell != actcell)
480 bview->getOwner()->getPopups().updateFormTabular();
485 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
487 if (the_locking_inset) {
488 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
489 y - inset_y, button);
493 // int ocell = actcell,
494 int old = sel_pos_end;
496 setPos(bv->painter(), x, y);
497 sel_pos_end = cursor.pos();
498 sel_cell_end = actcell;
499 if (old != sel_pos_end)
500 UpdateLocal(bv, false);
502 if (ocell != actcell)
503 bview->getOwner()->getPopups().updateFormTabular();
506 no_selection = false;
510 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
512 if (the_locking_inset) {
513 the_locking_inset->InsetKeyPress(xke);
519 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
522 UpdatableInset::RESULT
525 no_selection = false;
526 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
527 || (result == DISPATCHED_NOUPDATE)) {
529 resetPos(bv->painter());
534 if ((action < 0) && arg.empty())
537 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
538 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
540 if (the_locking_inset) {
541 result=the_locking_inset->LocalDispatch(bv, action, arg);
542 if (result == DISPATCHED_NOUPDATE)
544 else if (result == DISPATCHED) {
545 bool upd = SetCellDimensions(bv->painter(), actcell, actrow);
546 the_locking_inset->ToggleInsetCursor(bv);
547 UpdateLocal(bv, upd);
548 the_locking_inset->ToggleInsetCursor(bv);
550 } else if (result == FINISHED) {
551 if ((action == LFUN_RIGHT) || (action == -1)) {
552 cursor.pos(inset_pos + 1);
553 resetPos(bv->painter());
555 sel_pos_start = sel_pos_end = cursor.pos();
556 sel_cell_start = sel_cell_end = actcell;
563 bool hs = hasSelection();
566 // Normal chars not handled here
569 // --- Cursor Movements ---------------------------------------------
571 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
573 moveRight(bv, false);
574 sel_pos_end = cursor.pos();
575 if (!cellstart(cursor.pos())) {
576 if (tabular->right_column_of_cell(sel_cell_start) >
577 tabular->right_column_of_cell(actcell))
578 sel_cell_end = actcell+1;
580 sel_cell_end = actcell;
582 UpdateLocal(bv, false);
585 result = moveRight(bv);
586 sel_pos_start = sel_pos_end = cursor.pos();
587 sel_cell_start = sel_cell_end = actcell;
589 UpdateLocal(bv, false);
592 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
595 sel_pos_end = cursor.pos();
596 if (cellstart(cursor.pos())) {
597 if (tabular->column_of_cell(sel_cell_start) >=
598 tabular->column_of_cell(actcell))
599 sel_cell_end = actcell;
601 sel_cell_end = actcell-1;
603 UpdateLocal(bv, false);
606 result = moveLeft(bv);
607 sel_pos_start = sel_pos_end = cursor.pos();
608 sel_cell_start = sel_cell_end = actcell;
610 UpdateLocal(bv, false);
616 sel_pos_end = cursor.pos();
617 if ((ocell == sel_cell_end) ||
618 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
619 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
621 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
622 UpdateLocal(bv, false);
626 result= moveDown(bv);
627 sel_pos_start = sel_pos_end = cursor.pos();
628 sel_cell_start = sel_cell_end = actcell;
630 UpdateLocal(bv, false);
636 sel_pos_end = cursor.pos();
637 if ((ocell == sel_cell_end) ||
638 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
639 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
641 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
642 UpdateLocal(bv, false);
647 sel_pos_start = sel_pos_end = cursor.pos();
648 sel_cell_start = sel_cell_end = actcell;
650 UpdateLocal(bv, false);
662 if (the_locking_inset) {
663 the_locking_inset->InsetUnlock(bv);
665 the_locking_inset = 0;
666 if (action == LFUN_TAB)
670 sel_pos_start = sel_pos_end = cursor.pos();
671 sel_cell_start = sel_cell_end = actcell;
673 UpdateLocal(bv, false);
675 case LFUN_LAYOUT_TABLE:
677 int flag = (arg == "true");
678 MenuLayoutTabular(flag, this);
682 result = UNDISPATCHED;
685 if (result!=FINISHED) {
686 if (!the_locking_inset) {
688 if (ocell != actcell)
689 bview->getOwner()->getPopups().updateFormTabular();
694 bv->unlockInset(this);
699 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
701 return tabular->Latex(buf, os, fragile, fp);
705 int InsetTabular::Ascii(Buffer const *, ostream &) const
710 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
716 int InsetTabular::DocBook(Buffer const *, ostream &) const
722 void InsetTabular::Validate(LaTeXFeatures & features) const
724 tabular->Validate(features);
728 void InsetTabular::calculate_width_of_cells(Painter & pain,
729 LyXFont const & font) const
735 for(int i = 0; i < tabular->rows(); ++i) {
736 maxAsc = maxDesc = 0;
737 for(int j= 0; j < tabular->columns(); ++j) {
738 if (tabular->IsPartOfMultiColumn(i,j))
741 inset = tabular->GetCellInset(cell);
742 maxAsc = max(maxAsc, inset->ascent(pain, font));
743 maxDesc = max(maxDesc, inset->descent(pain, font));
744 tabular->SetWidthOfCell(cell, inset->width(pain, font));
746 tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
747 tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
752 void InsetTabular::GetCursorPos(int & x, int & y) const
754 x = cursor.x() - top_x;
759 void InsetTabular::ToggleInsetCursor(BufferView * bv)
761 if (the_locking_inset) {
762 the_locking_inset->ToggleInsetCursor(bv);
766 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
768 int asc = lyxfont::maxAscent(font);
769 int desc = lyxfont::maxDescent(font);
772 bv->hideLockedInsetCursor();
774 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
775 cursor_visible = !cursor_visible;
779 void InsetTabular::ShowInsetCursor(BufferView * bv)
781 if (!cursor_visible) {
782 LyXFont font; // = GetFont(par, cursor.pos);
784 int asc = lyxfont::maxAscent(font);
785 int desc = lyxfont::maxDescent(font);
786 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
787 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
788 cursor_visible = true;
793 void InsetTabular::HideInsetCursor(BufferView * bv)
796 ToggleInsetCursor(bv);
800 void InsetTabular::setPos(Painter & pain, int x, int y) const
805 actcell = actrow = actcol = 0;
806 int ly = tabular->GetDescentOfRow(actrow);
808 // first search the right row
809 while((ly < y) && (actrow < tabular->rows())) {
810 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
811 tabular->GetAscentOfRow(actrow+1) +
812 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
815 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
817 actcell = tabular->GetCellNumber(actrow, actcol);
819 // now search the right column
820 int lx = tabular->GetWidthOfColumn(actcell) -
821 tabular->GetAdditionalWidth(actcell);
822 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
823 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
824 tabular->GetAdditionalWidth(actcell - 1));
825 cursor.pos(((actcell+1) * 2) - 1);
827 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
828 cursor.x(lx + top_x - 2);
830 cursor.pos(cursor.pos() - 1);
831 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
836 int InsetTabular::getCellXPos(int cell) const
840 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
842 int lx = tabular->GetWidthOfColumn(cell);
843 for(; (c < cell); ++c) {
844 lx += tabular->GetWidthOfColumn(c);
846 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
847 ADD_TO_TABULAR_WIDTH);
850 void InsetTabular::resetPos(Painter & pain) const
854 actcol = tabular->column_of_cell(actcell);
859 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
860 if (tabular->IsLastCellInRow(cell)) {
861 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
862 tabular->GetAscentOfRow(actrow + 1) +
863 tabular->GetAdditionalHeight(cell + 1));
867 cursor.x(getCellXPos(actcell) + 2);
868 if (cursor.pos() % 2) {
869 LyXFont font(LyXFont::ALL_SANE);
870 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(pain,font) +
871 tabular->GetBeginningOfTextInCell(actcell));
873 if ((!the_locking_inset ||
874 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
875 (actcell != oldcell)) {
876 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
882 bool InsetTabular::SetCellDimensions(Painter & pain, int cell, int row)
884 InsetText * inset = tabular->GetCellInset(cell);
885 LyXFont font(LyXFont::ALL_SANE);
886 int asc = inset->ascent(pain, font) + ADD_TO_HEIGHT;
887 int desc = inset->descent(pain, font) + ADD_TO_HEIGHT;
888 int maxAsc = tabular->GetAscentOfRow(row);
889 int maxDesc = tabular->GetDescentOfRow(row);
890 bool ret = tabular->SetWidthOfCell(cell, inset->width(pain, font));
894 tabular->SetAscentOfRow(row, asc);
896 if (maxDesc < desc) {
898 tabular->SetDescentOfRow(row, desc);
904 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
906 if (!cellstart(cursor.pos())) {
907 if (tabular->IsLastCell(actcell))
910 cursor.pos(cursor.pos() + 1);
912 if (ActivateCellInset(bv))
914 } else { // before the inset
915 cursor.pos(cursor.pos() + 1);
917 resetPos(bv->painter());
918 return DISPATCHED_NOUPDATE;
922 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
929 cursor.pos(cursor.pos() - 1);
930 if (!cellstart(cursor.pos())) {
932 } else if (lock) { // behind the inset
933 if (ActivateCellInset(bv, 0, 0, 0, true))
936 resetPos(bv->painter());
937 return DISPATCHED_NOUPDATE;
941 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
944 actcell = tabular->GetCellAbove(actcell);
945 if (actcell == ocell) // we moved out of the inset
947 resetPos(bv->painter());
948 return DISPATCHED_NOUPDATE;
952 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
955 actcell = tabular->GetCellBelow(actcell);
956 if (actcell == ocell) // we moved out of the inset
958 resetPos(bv->painter());
959 return DISPATCHED_NOUPDATE;
963 bool InsetTabular::moveNextCell(BufferView * bv)
965 if (tabular->IsLastCell(actcell))
968 cursor.pos(cursor.pos() + 1);
969 if (!cellstart(cursor.pos()))
970 cursor.pos(cursor.pos() + 1);
971 resetPos(bv->painter());
976 bool InsetTabular::movePrevCell(BufferView * bv)
978 if (!actcell) // first cell
981 cursor.pos(cursor.pos() - 1);
982 if (cellstart(cursor.pos()))
983 cursor.pos(cursor.pos() - 1);
984 resetPos(bv->painter());
989 bool InsetTabular::Delete()
995 void InsetTabular::SetFont(BufferView *, LyXFont const &, bool)
1000 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1009 setAlign = LYX_ALIGN_LEFT,
1015 case LyXTabular::ALIGN_LEFT:
1016 setAlign=LYX_ALIGN_LEFT;
1018 case LyXTabular::ALIGN_RIGHT:
1019 setAlign=LYX_ALIGN_RIGHT;
1021 case LyXTabular::ALIGN_CENTER:
1022 setAlign=LYX_ALIGN_CENTER;
1027 if (hasSelection()) {
1029 sel_col_start = tabular->column_of_cell(sel_cell_start);
1030 sel_col_end = tabular->column_of_cell(sel_cell_end);
1031 if (sel_col_start > sel_col_end) {
1032 sel_col_end = sel_col_start;
1033 sel_col_start = tabular->column_of_cell(sel_cell_end);
1035 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1038 sel_row_start = tabular->row_of_cell(sel_cell_start);
1039 sel_row_end = tabular->row_of_cell(sel_cell_end);
1040 if (sel_row_start > sel_row_end) {
1041 tmp = sel_row_start;
1042 sel_row_start = sel_row_end;
1046 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1047 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1049 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1050 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1051 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1053 int row = tabular->row_of_cell(actcell);
1054 int column = tabular->column_of_cell(actcell);
1057 case LyXTabular::SET_PWIDTH:
1059 bool update = (tabular->GetPWidth(actcell) != val);
1060 tabular->SetPWidth(actcell,val);
1062 UpdateLocal(bv, true);
1065 case LyXTabular::SET_SPECIAL_COLUMN:
1066 case LyXTabular::SET_SPECIAL_MULTI:
1067 tabular->SetAlignSpecial(actcell,val,feature);
1069 case LyXTabular::APPEND_ROW:
1070 // append the row into the tabular
1071 UnlockInsetInInset(bv, the_locking_inset);
1072 tabular->AppendRow(actcell);
1073 UpdateLocal(bv, true);
1075 case LyXTabular::APPEND_COLUMN:
1076 // append the column into the tabular
1077 tabular->AppendColumn(actcell);
1078 actcell = tabular->GetCellNumber(row, column);
1079 UpdateLocal(bv, true);
1081 case LyXTabular::DELETE_ROW:
1082 tabular->DeleteRow(tabular->row_of_cell(actcell));
1083 if ((row+1) > tabular->rows())
1085 actcell = tabular->GetCellNumber(row, column);
1086 UpdateLocal(bv, true);
1088 case LyXTabular::DELETE_COLUMN:
1089 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1090 if ((column+1) > tabular->columns())
1092 actcell = tabular->GetCellNumber(row, column);
1093 UpdateLocal(bv, true);
1095 case LyXTabular::TOGGLE_LINE_TOP:
1096 lineSet = !tabular->TopLine(actcell);
1097 for(i=sel_row_start; i<=sel_row_end; ++i)
1098 for(j=sel_col_start; j<=sel_col_end; ++j)
1099 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1100 UpdateLocal(bv, true);
1103 case LyXTabular::TOGGLE_LINE_BOTTOM:
1104 lineSet = !tabular->BottomLine(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->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1108 UpdateLocal(bv, true);
1111 case LyXTabular::TOGGLE_LINE_LEFT:
1112 lineSet = !tabular->LeftLine(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->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1116 UpdateLocal(bv, true);
1119 case LyXTabular::TOGGLE_LINE_RIGHT:
1120 lineSet = !tabular->RightLine(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->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1124 UpdateLocal(bv, true);
1126 case LyXTabular::ALIGN_LEFT:
1127 case LyXTabular::ALIGN_RIGHT:
1128 case LyXTabular::ALIGN_CENTER:
1129 for(i=sel_row_start; i<=sel_row_end; ++i)
1130 for(j=sel_col_start; j<=sel_col_end; ++j)
1131 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1132 UpdateLocal(bv, true);
1134 case LyXTabular::MULTICOLUMN:
1136 if (sel_row_start != sel_row_end) {
1137 WriteAlert(_("Impossible Operation!"),
1138 _("Multicolumns can only be horizontally."),
1142 // just multicol for one Single Cell
1143 if (!hasSelection()) {
1144 // check wether we are completly in a multicol
1145 if (tabular->IsMultiColumn(actcell)) {
1146 tabular->UnsetMultiColumn(actcell);
1147 UpdateLocal(bv, true);
1149 tabular->SetMultiColumn(actcell, 1);
1150 UpdateLocal(bv, false);
1154 // we have a selection so this means we just add all this
1155 // cells to form a multicolumn cell
1159 if (sel_cell_start > sel_cell_end) {
1160 s_start = sel_cell_end;
1161 s_end = sel_cell_start;
1163 s_start = sel_cell_start;
1164 s_end = sel_cell_end;
1166 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1169 sel_cell_end = sel_cell_start;
1170 sel_pos_end = sel_pos_start;
1171 UpdateLocal(bv, true);
1174 case LyXTabular::SET_ALL_LINES:
1176 case LyXTabular::UNSET_ALL_LINES:
1177 for(i=sel_row_start; i<=sel_row_end; ++i)
1178 for(j=sel_col_start; j<=sel_col_end; ++j)
1179 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1180 UpdateLocal(bv, true);
1182 case LyXTabular::SET_LONGTABULAR:
1183 tabular->SetLongTabular(true);
1184 UpdateLocal(bv, true); // because this toggles displayed
1186 case LyXTabular::UNSET_LONGTABULAR:
1187 tabular->SetLongTabular(false);
1188 UpdateLocal(bv, true); // because this toggles displayed
1190 case LyXTabular::SET_ROTATE_TABULAR:
1191 tabular->SetRotateTabular(true);
1193 case LyXTabular::UNSET_ROTATE_TABULAR:
1194 tabular->SetRotateTabular(false);
1196 case LyXTabular::SET_ROTATE_CELL:
1197 for(i=sel_row_start; i<=sel_row_end; ++i)
1198 for(j=sel_col_start; j<=sel_col_end; ++j)
1199 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1201 case LyXTabular::UNSET_ROTATE_CELL:
1202 for(i=sel_row_start; i<=sel_row_end; ++i)
1203 for(j=sel_col_start; j<=sel_col_end; ++j)
1204 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1206 case LyXTabular::SET_LINEBREAKS:
1207 what = !tabular->GetLinebreaks(actcell);
1208 for(i=sel_row_start; i<=sel_row_end; ++i)
1209 for(j=sel_col_start; j<=sel_col_end; ++j)
1210 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1212 case LyXTabular::SET_LTFIRSTHEAD:
1213 tabular->SetLTHead(actcell,true);
1215 case LyXTabular::SET_LTHEAD:
1216 tabular->SetLTHead(actcell,false);
1218 case LyXTabular::SET_LTFOOT:
1219 tabular->SetLTFoot(actcell,false);
1221 case LyXTabular::SET_LTLASTFOOT:
1222 tabular->SetLTFoot(actcell,true);
1224 case LyXTabular::SET_LTNEWPAGE:
1225 what = !tabular->GetLTNewPage(actcell);
1226 tabular->SetLTNewPage(actcell,what);
1231 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1234 // the cursor.pos has to be before the inset so if it isn't now just
1235 // reset the curor pos first!
1236 if (cursor.pos() % 2) { // behind the inset
1237 cursor.pos(cursor.pos() - 1);
1238 resetPos(bv->painter());
1240 UpdatableInset * inset =
1241 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1242 LyXFont font(LyXFont::ALL_SANE);
1244 x = inset->x() + inset->width(bv->painter(), font);
1245 y = inset->descent(bv->painter(), font);
1247 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1248 inset_y = cursor.y();
1249 inset->Edit(bv, x - inset_x, y - inset_y, button);
1250 if (!the_locking_inset)
1252 UpdateLocal(bv, true);
1256 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1258 InsetText * inset = tabular->GetCellInset(actcell);
1261 if (cursor.pos() % 2) { // behind the inset
1262 return (((x + top_x) < cursor.x()) &&
1263 ((x + top_x) > (cursor.x() - inset->width(bv->painter(),
1264 LyXFont(LyXFont::ALL_SANE)))));
1266 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1267 return ((x1 > x2) &&
1268 (x1 < (x2 + inset->width(bv->painter(),
1269 LyXFont(LyXFont::ALL_SANE)))));
1273 // This returns paperWidth() if the cell-width is unlimited or the width
1274 // in pixels if we have a pwidth for this cell.
1275 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1279 if ((w=tabular->GetPWidth(cell)).empty())
1281 return VSpace(w).inPixels( 0, 0);
1284 int InsetTabular::getMaxWidth(Painter & pain,
1285 UpdatableInset const * inset) const
1288 int n = tabular->GetNumberOfCells();
1289 for(cell=0; cell < n; ++cell) {
1290 if (tabular->GetCellInset(cell) == inset)
1295 int w = GetMaxWidthOfCell(pain, cell);
1296 // this because text insets remove the xpos from the maxwidth because
1297 // otherwise the would not break good!!!
1298 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1303 void InsetTabular::recomputeTextInsets(Painter & pain, const LyXFont & font) const
1309 for(int j= 0; j < tabular->columns(); ++j) {
1310 for(int i = 0; i < tabular->rows(); ++i) {
1311 if (tabular->IsPartOfMultiColumn(i,j))
1313 cell = tabular->GetCellNumber(i,j);
1314 inset = tabular->GetCellInset(cell);
1315 inset->computeTextRows(pain);
1316 tabular->SetWidthOfCell(cell, inset->width(pain, font));
1318 cell = tabular->GetCellNumber(0, j);
1319 cx += tabular->GetWidthOfColumn(cell);