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;
63 actcell = cursor.pos = 0;
64 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
69 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
71 buffer = buf; // set this first
72 tabular = new LyXTabular(this, *(tab.tabular));
73 the_locking_inset = 0;
74 locked = no_selection = cursor_visible = false;
77 actcell = cursor.pos = 0;
78 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
83 InsetTabular::~InsetTabular()
89 Inset * InsetTabular::Clone() const
91 InsetTabular * t = new InsetTabular(*this, buffer);
93 t->tabular = tabular->Clone(t);
98 void InsetTabular::Write(ostream & os) const
100 os << " Tabular" << endl;
105 void InsetTabular::Read(LyXLex & lex)
107 bool old_format = (lex.GetString() == "\\LyXTable");
112 tabular = new LyXTabular(this, lex);
120 token = lex.GetString();
121 while (lex.IsOK() && (token != "\\end_inset")) {
123 token = lex.GetString();
125 if (token != "\\end_inset") {
126 lex.printError("Missing \\end_inset at this point. "
132 int InsetTabular::ascent(Painter & pain, LyXFont const & font) const
135 calculate_width_of_cells(pain, font);
138 return tabular->GetAscentOfRow(0);
142 int InsetTabular::descent(Painter & pain, LyXFont const & font) const
145 calculate_width_of_cells(pain, font);
148 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
152 int InsetTabular::width(Painter & pain, LyXFont const & font) const
155 calculate_width_of_cells(pain, font);
158 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
162 void InsetTabular::draw(Painter & pain, const LyXFont & font, int baseline,
170 UpdatableInset::draw(pain,font,baseline,x);
171 if (init_inset || (top_x != int(x)) || (top_baseline != baseline)) {
175 top_baseline = baseline;
177 recomputeTextInsets(pain, font);
178 calculate_width_of_cells(pain, font);
182 x += ADD_TO_TABULAR_WIDTH;
183 for(i=0;i<tabular->rows();++i) {
185 for(j=0;j<tabular->columns();++j) {
186 if (tabular->IsPartOfMultiColumn(i,j))
188 cx = nx + tabular->GetBeginningOfTextInCell(cell);
190 DrawCellSelection(pain, nx, baseline, i, j, cell);
191 tabular->GetCellInset(cell)->draw(pain, font, baseline, cx);
192 DrawCellLines(pain, nx, baseline, i, cell);
193 nx += tabular->GetWidthOfColumn(cell);
196 baseline += tabular->GetDescentOfRow(i) + tabular->GetAscentOfRow(i+1)
197 + tabular->GetAdditionalHeight(cell+1);
199 x += width(pain, font);
203 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
204 int row, int cell) const
206 int x2 = x + tabular->GetWidthOfColumn(cell);
209 if (!tabular->TopAlreadyDrawed(cell)) {
210 on_off = !tabular->TopLine(cell);
211 pain.line(x, baseline - tabular->GetAscentOfRow(row),
212 x2, baseline - tabular->GetAscentOfRow(row),
213 on_off ? LColor::tabularonoffline:LColor::tabularline,
214 on_off ? Painter::line_onoffdash:Painter::line_solid);
216 on_off = !tabular->BottomLine(cell);
217 pain.line(x,baseline + tabular->GetDescentOfRow(row),
218 x2, baseline + tabular->GetDescentOfRow(row),
219 on_off ? LColor::tabularonoffline:LColor::tabularline,
220 on_off ? Painter::line_onoffdash:Painter::line_solid);
221 if (!tabular->LeftAlreadyDrawed(cell)) {
222 on_off = !tabular->LeftLine(cell);
223 pain.line(x, baseline - tabular->GetAscentOfRow(row),
224 x, baseline + tabular->GetDescentOfRow(row),
225 on_off ? LColor::tabularonoffline:LColor::tabularline,
226 on_off ? Painter::line_onoffdash:Painter::line_solid);
228 on_off = !tabular->RightLine(cell);
229 pain.line(x2 - tabular->GetAdditionalWidth(cell),
230 baseline - tabular->GetAscentOfRow(row),
231 x2 - tabular->GetAdditionalWidth(cell),
232 baseline + tabular->GetDescentOfRow(row),
233 on_off ? LColor::tabularonoffline:LColor::tabularline,
234 on_off ? Painter::line_onoffdash:Painter::line_solid);
238 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
239 int row, int column, int cell) const
243 int cs = tabular->column_of_cell(sel_cell_start);
244 int ce = tabular->column_of_cell(sel_cell_end);
247 cs = tabular->column_of_cell(sel_cell_end);
249 ce = tabular->right_column_of_cell(sel_cell_end);
252 int rs = tabular->row_of_cell(sel_cell_start);
253 int re = tabular->row_of_cell(sel_cell_end);
260 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
261 int w = tabular->GetWidthOfColumn(cell);
262 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
263 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
264 w, h, LColor::selection);
269 char const * InsetTabular::EditMessage() const
271 return _("Opened Tabular Inset");
275 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
277 UpdatableInset::Edit(bv, x, y, button);
279 if (!bv->lockInset(this)) {
280 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
284 the_locking_inset = 0;
285 inset_pos = inset_x = inset_y = 0;
286 setPos(bv->painter(), x, y);
287 sel_pos_start = sel_pos_end = cursor.pos;
288 sel_cell_start = sel_cell_end = actcell;
289 bv->text->FinishUndo();
290 if (InsetHit(bv, x, y)) {
291 ActivateCellInset(bv, x, y, button);
293 UpdateLocal(bv, true);
294 // bv->getOwner()->getPopups().updateFormTabular();
298 void InsetTabular::InsetUnlock(BufferView * bv)
301 if (the_locking_inset) {
302 the_locking_inset->InsetUnlock(bv);
303 the_locking_inset = 0;
306 if (hasSelection()) {
307 sel_pos_start = sel_pos_end = cursor.pos;
308 sel_cell_start = sel_cell_end = actcell;
309 UpdateLocal(bv, false);
311 no_selection = false;
316 void InsetTabular::UpdateLocal(BufferView * bv, bool flag)
319 calculate_width_of_cells(bv->painter(), LyXFont(LyXFont::ALL_SANE));
320 bv->updateInset(this, flag);
322 resetPos(bv->painter());
325 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
327 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
331 if (inset == tabular->GetCellInset(actcell)) {
332 lyxerr[Debug::INSETS] << "OK" << endl;
333 the_locking_inset = tabular->GetCellInset(actcell);
334 resetPos(bv->painter());
335 inset_x = cursor.x - top_x + tabular->GetBeginningOfTextInCell(actcell);
337 inset_pos = cursor.pos;
339 } else if (the_locking_inset && (the_locking_inset == inset)) {
340 if (cursor.pos == inset_pos) {
341 lyxerr[Debug::INSETS] << "OK" << endl;
342 resetPos(bv->painter());
343 inset_x = cursor.x - top_x + tabular->GetBeginningOfTextInCell(actcell);
346 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
348 } else if (the_locking_inset) {
349 lyxerr[Debug::INSETS] << "MAYBE" << endl;
350 return the_locking_inset->LockInsetInInset(bv, inset);
352 lyxerr[Debug::INSETS] << "NOT OK" << endl;
356 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
359 if (!the_locking_inset)
361 if (the_locking_inset == inset) {
362 the_locking_inset->InsetUnlock(bv);
363 the_locking_inset = 0;
365 moveRight(bv, false);
368 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
369 if ((inset->LyxCode() == TABULAR_CODE) &&
370 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
372 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
380 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
382 if (!the_locking_inset)
384 if (the_locking_inset != inset)
385 return the_locking_inset->UpdateInsetInInset(bv, inset);
391 int InsetTabular::InsetInInsetY()
393 if (!the_locking_inset)
396 return (inset_y + the_locking_inset->InsetInInsetY());
400 UpdatableInset * InsetTabular::GetLockingInset()
402 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
406 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
410 if (the_locking_inset)
411 return the_locking_inset->GetFirstLockingInsetOfType(c);
416 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
418 if (the_locking_inset)
419 return the_locking_inset->InsertInset(bv, inset);
424 void InsetTabular::InsetButtonRelease(BufferView * bv,
425 int x, int y, int button)
428 if (the_locking_inset) {
430 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
431 i->InsetButtonRelease(bv, x, y, button);
435 MenuLayoutTabular(true, this);
438 if (the_locking_inset) {
439 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
442 no_selection = false;
446 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
448 if (hasSelection()) {
449 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
450 UpdateLocal(bv, false);
452 no_selection = false;
456 setPos(bv->painter(), x, y);
457 sel_pos_start = sel_pos_end = cursor.pos;
458 sel_cell_start = sel_cell_end = actcell;
460 bool inset_hit = InsetHit(bv, x, y);
462 if ((ocell == actcell) && the_locking_inset && inset_hit) {
463 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
465 } else if (the_locking_inset) {
466 the_locking_inset->InsetUnlock(bv);
468 the_locking_inset = 0;
469 if (inset_hit && bv->the_locking_inset) {
470 ActivateCellInset(bv, x, y, button);
471 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
476 bview->getOwner()->getPopups().showFormTabular();
477 else if (ocell != actcell)
478 bview->getOwner()->getPopups().updateFormTabular();
483 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
485 if (the_locking_inset) {
486 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
487 y - inset_y, button);
491 // int ocell = actcell,
492 int old = sel_pos_end;
494 setPos(bv->painter(), x, y);
495 sel_pos_end = cursor.pos;
496 sel_cell_end = actcell;
497 if (old != sel_pos_end)
498 UpdateLocal(bv, false);
500 if (ocell != actcell)
501 bview->getOwner()->getPopups().updateFormTabular();
504 no_selection = false;
508 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
510 if (the_locking_inset) {
511 the_locking_inset->InsetKeyPress(xke);
517 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
520 UpdatableInset::RESULT
523 no_selection = false;
524 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
525 || (result == DISPATCHED_NOUPDATE)) {
527 resetPos(bv->painter());
532 if ((action < 0) && arg.empty())
535 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
536 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
538 if (the_locking_inset) {
539 result=the_locking_inset->LocalDispatch(bv, action, arg);
540 if (result == DISPATCHED_NOUPDATE)
542 else if (result == DISPATCHED) {
543 bool upd = SetCellDimensions(bv->painter(), actcell, actrow);
544 the_locking_inset->ToggleInsetCursor(bv);
545 UpdateLocal(bv, upd);
546 the_locking_inset->ToggleInsetCursor(bv);
548 } else if (result == FINISHED) {
549 if ((action == LFUN_RIGHT) || (action == -1)) {
550 cursor.pos = inset_pos + 1;
551 resetPos(bv->painter());
553 sel_pos_start = sel_pos_end = cursor.pos;
554 sel_cell_start = sel_cell_end = actcell;
561 bool hs = hasSelection();
564 // Normal chars not handled here
567 // --- Cursor Movements ---------------------------------------------
569 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos))
571 moveRight(bv, false);
572 sel_pos_end = cursor.pos;
573 if (!cellstart(cursor.pos)) {
574 if (tabular->right_column_of_cell(sel_cell_start) >
575 tabular->right_column_of_cell(actcell))
576 sel_cell_end = actcell+1;
578 sel_cell_end = actcell;
580 UpdateLocal(bv, false);
583 result = moveRight(bv);
584 sel_pos_start = sel_pos_end = cursor.pos;
585 sel_cell_start = sel_cell_end = actcell;
587 UpdateLocal(bv, false);
590 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos))
593 sel_pos_end = cursor.pos;
594 if (cellstart(cursor.pos)) {
595 if (tabular->column_of_cell(sel_cell_start) >=
596 tabular->column_of_cell(actcell))
597 sel_cell_end = actcell;
599 sel_cell_end = actcell-1;
601 UpdateLocal(bv, false);
604 result = moveLeft(bv);
605 sel_pos_start = sel_pos_end = cursor.pos;
606 sel_cell_start = sel_cell_end = actcell;
608 UpdateLocal(bv, false);
614 sel_pos_end = cursor.pos;
615 if ((ocell == sel_cell_end) ||
616 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
617 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
619 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
620 UpdateLocal(bv, false);
624 result= moveDown(bv);
625 sel_pos_start = sel_pos_end = cursor.pos;
626 sel_cell_start = sel_cell_end = actcell;
628 UpdateLocal(bv, false);
634 sel_pos_end = cursor.pos;
635 if ((ocell == sel_cell_end) ||
636 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
637 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
639 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
640 UpdateLocal(bv, false);
645 sel_pos_start = sel_pos_end = cursor.pos;
646 sel_cell_start = sel_cell_end = actcell;
648 UpdateLocal(bv, false);
660 if (the_locking_inset) {
661 the_locking_inset->InsetUnlock(bv);
663 the_locking_inset = 0;
664 if (action == LFUN_TAB)
668 sel_pos_start = sel_pos_end = cursor.pos;
669 sel_cell_start = sel_cell_end = actcell;
671 UpdateLocal(bv, false);
673 case LFUN_LAYOUT_TABLE:
675 int flag = (arg == "true");
676 MenuLayoutTabular(flag, this);
680 result = UNDISPATCHED;
683 if (result!=FINISHED) {
684 if (!the_locking_inset) {
686 if (ocell != actcell)
687 bview->getOwner()->getPopups().updateFormTabular();
692 bv->unlockInset(this);
697 int InsetTabular::Latex(ostream & os, bool fragile, bool fp) const
699 return tabular->Latex(os, fragile, fp);
703 int InsetTabular::Ascii(ostream &) const
708 int InsetTabular::Linuxdoc(ostream &) const
714 int InsetTabular::DocBook(ostream &) const
720 void InsetTabular::Validate(LaTeXFeatures & features) const
722 tabular->Validate(features);
726 void InsetTabular::calculate_width_of_cells(Painter & pain,
727 LyXFont const & font) const
733 for(int i = 0; i < tabular->rows(); ++i) {
734 maxAsc = maxDesc = 0;
735 for(int j= 0; j < tabular->columns(); ++j) {
736 if (tabular->IsPartOfMultiColumn(i,j))
739 inset = tabular->GetCellInset(cell);
740 maxAsc = max(maxAsc, inset->ascent(pain, font));
741 maxDesc = max(maxDesc, inset->descent(pain, font));
742 tabular->SetWidthOfCell(cell, inset->width(pain, font));
744 tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
745 tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
750 void InsetTabular::GetCursorPos(int & x, int & y) const
757 void InsetTabular::ToggleInsetCursor(BufferView * bv)
759 if (the_locking_inset) {
760 the_locking_inset->ToggleInsetCursor(bv);
764 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
766 int asc = lyxfont::maxAscent(font);
767 int desc = lyxfont::maxDescent(font);
770 bv->hideLockedInsetCursor();
772 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
773 cursor_visible = !cursor_visible;
777 void InsetTabular::ShowInsetCursor(BufferView * bv)
779 if (!cursor_visible) {
780 LyXFont font; // = GetFont(par, cursor.pos);
782 int asc = lyxfont::maxAscent(font);
783 int desc = lyxfont::maxDescent(font);
784 bv->fitLockedInsetCursor(cursor.x, cursor.y, asc, desc);
785 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
786 cursor_visible = true;
791 void InsetTabular::HideInsetCursor(BufferView * bv)
794 ToggleInsetCursor(bv);
798 void InsetTabular::setPos(Painter & pain, int x, int y) const
800 cursor.y = cursor.pos = actcell = actrow = actcol = 0;
801 int ly = tabular->GetDescentOfRow(actrow);
803 // first search the right row
804 while((ly < y) && (actrow < tabular->rows())) {
805 cursor.y += tabular->GetDescentOfRow(actrow) +
806 tabular->GetAscentOfRow(actrow+1) +
807 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow+1,
810 ly = cursor.y + tabular->GetDescentOfRow(actrow);
812 actcell = tabular->GetCellNumber(actrow, actcol);
814 // now search the right column
815 int lx = tabular->GetWidthOfColumn(actcell) -
816 tabular->GetAdditionalWidth(actcell);
817 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
818 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
819 tabular->GetAdditionalWidth(actcell-1));
820 cursor.pos = ((actcell+1) * 2) - 1;
822 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
823 cursor.x = lx + top_x - 2;
826 cursor.x = lx - tabular->GetWidthOfColumn(actcell) + top_x + 2;
831 int InsetTabular::getCellXPos(int cell) const
835 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
837 int lx = tabular->GetWidthOfColumn(cell);
838 for(; (c < cell); ++c) {
839 lx += tabular->GetWidthOfColumn(c);
841 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
842 ADD_TO_TABULAR_WIDTH);
845 void InsetTabular::resetPos(Painter & pain) const
849 actcol = tabular->column_of_cell(actcell);
852 actrow = cursor.y = 0;
853 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
854 if (tabular->IsLastCellInRow(cell)) {
855 cursor.y += tabular->GetDescentOfRow(actrow) +
856 tabular->GetAscentOfRow(actrow+1) +
857 tabular->GetAdditionalHeight(cell+1);
861 cursor.x = getCellXPos(actcell) + 2;
862 if (cursor.pos % 2) {
863 LyXFont font(LyXFont::ALL_SANE);
864 cursor.x += tabular->GetCellInset(actcell)->width(pain,font) +
865 tabular->GetBeginningOfTextInCell(actcell);
867 if ((!the_locking_inset ||
868 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
869 (actcell != oldcell)) {
870 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
876 bool InsetTabular::SetCellDimensions(Painter & pain, int cell, int row)
878 InsetText * inset = tabular->GetCellInset(cell);
879 LyXFont font(LyXFont::ALL_SANE);
880 int asc = inset->ascent(pain, font) + ADD_TO_HEIGHT;
881 int desc = inset->descent(pain, font) + ADD_TO_HEIGHT;
882 int maxAsc = tabular->GetAscentOfRow(row);
883 int maxDesc = tabular->GetDescentOfRow(row);
884 bool ret = tabular->SetWidthOfCell(cell, inset->width(pain, font));
888 tabular->SetAscentOfRow(row, asc);
890 if (maxDesc < desc) {
892 tabular->SetDescentOfRow(row, desc);
898 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
900 if (!cellstart(cursor.pos)) {
901 if (tabular->IsLastCell(actcell))
906 if (ActivateCellInset(bv))
908 } else { // before the inset
911 resetPos(bv->painter());
912 return DISPATCHED_NOUPDATE;
916 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
924 if (!cellstart(cursor.pos)) {
926 } else if (lock) { // behind the inset
927 if (ActivateCellInset(bv, 0, 0, 0, true))
930 resetPos(bv->painter());
931 return DISPATCHED_NOUPDATE;
935 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
938 actcell = tabular->GetCellAbove(actcell);
939 if (actcell == ocell) // we moved out of the inset
941 resetPos(bv->painter());
942 return DISPATCHED_NOUPDATE;
946 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
949 actcell = tabular->GetCellBelow(actcell);
950 if (actcell == ocell) // we moved out of the inset
952 resetPos(bv->painter());
953 return DISPATCHED_NOUPDATE;
957 bool InsetTabular::moveNextCell(BufferView * bv)
959 if (tabular->IsLastCell(actcell))
963 if (!cellstart(cursor.pos))
965 resetPos(bv->painter());
970 bool InsetTabular::movePrevCell(BufferView * bv)
972 if (!actcell) // first cell
976 if (cellstart(cursor.pos))
978 resetPos(bv->painter());
983 bool InsetTabular::Delete()
989 void InsetTabular::SetFont(BufferView *, LyXFont const &, bool)
994 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1003 setAlign = LYX_ALIGN_LEFT,
1009 case LyXTabular::ALIGN_LEFT:
1010 setAlign=LYX_ALIGN_LEFT;
1012 case LyXTabular::ALIGN_RIGHT:
1013 setAlign=LYX_ALIGN_RIGHT;
1015 case LyXTabular::ALIGN_CENTER:
1016 setAlign=LYX_ALIGN_CENTER;
1021 if (hasSelection()) {
1023 sel_col_start = tabular->column_of_cell(sel_cell_start);
1024 sel_col_end = tabular->column_of_cell(sel_cell_end);
1025 if (sel_col_start > sel_col_end) {
1026 sel_col_end = sel_col_start;
1027 sel_col_start = tabular->column_of_cell(sel_cell_end);
1029 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1032 sel_row_start = tabular->row_of_cell(sel_cell_start);
1033 sel_row_end = tabular->row_of_cell(sel_cell_end);
1034 if (sel_row_start > sel_row_end) {
1035 tmp = sel_row_start;
1036 sel_row_start = sel_row_end;
1040 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1041 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1043 bv->text->SetUndo(Undo::FINISH,
1044 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
1045 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
1047 int row = tabular->row_of_cell(actcell);
1048 int column = tabular->column_of_cell(actcell);
1051 case LyXTabular::SET_PWIDTH:
1053 bool update = (tabular->GetPWidth(actcell) != val);
1054 tabular->SetPWidth(actcell,val);
1056 UpdateLocal(bv, true);
1059 case LyXTabular::SET_SPECIAL_COLUMN:
1060 case LyXTabular::SET_SPECIAL_MULTI:
1061 tabular->SetAlignSpecial(actcell,val,feature);
1063 case LyXTabular::APPEND_ROW:
1064 // append the row into the tabular
1065 tabular->AppendRow(actcell);
1066 UpdateLocal(bv, true);
1068 case LyXTabular::APPEND_COLUMN:
1069 // append the column into the tabular
1070 tabular->AppendColumn(actcell);
1071 actcell = tabular->GetCellNumber(row, column);
1072 UpdateLocal(bv, true);
1074 case LyXTabular::DELETE_ROW:
1075 tabular->DeleteRow(tabular->row_of_cell(actcell));
1076 if ((row+1) > tabular->rows())
1078 actcell = tabular->GetCellNumber(row, column);
1079 UpdateLocal(bv, true);
1081 case LyXTabular::DELETE_COLUMN:
1082 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1083 if ((column+1) > tabular->columns())
1085 actcell = tabular->GetCellNumber(row, column);
1086 UpdateLocal(bv, true);
1088 case LyXTabular::TOGGLE_LINE_TOP:
1089 lineSet = !tabular->TopLine(actcell);
1090 for(i=sel_row_start; i<=sel_row_end; ++i)
1091 for(j=sel_col_start; j<=sel_col_end; ++j)
1092 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1093 UpdateLocal(bv, true);
1096 case LyXTabular::TOGGLE_LINE_BOTTOM:
1097 lineSet = !tabular->BottomLine(actcell);
1098 for(i=sel_row_start; i<=sel_row_end; ++i)
1099 for(j=sel_col_start; j<=sel_col_end; ++j)
1100 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1101 UpdateLocal(bv, true);
1104 case LyXTabular::TOGGLE_LINE_LEFT:
1105 lineSet = !tabular->LeftLine(actcell);
1106 for(i=sel_row_start; i<=sel_row_end; ++i)
1107 for(j=sel_col_start; j<=sel_col_end; ++j)
1108 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1109 UpdateLocal(bv, true);
1112 case LyXTabular::TOGGLE_LINE_RIGHT:
1113 lineSet = !tabular->RightLine(actcell);
1114 for(i=sel_row_start; i<=sel_row_end; ++i)
1115 for(j=sel_col_start; j<=sel_col_end; ++j)
1116 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1117 UpdateLocal(bv, true);
1119 case LyXTabular::ALIGN_LEFT:
1120 case LyXTabular::ALIGN_RIGHT:
1121 case LyXTabular::ALIGN_CENTER:
1122 for(i=sel_row_start; i<=sel_row_end; ++i)
1123 for(j=sel_col_start; j<=sel_col_end; ++j)
1124 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1125 UpdateLocal(bv, true);
1127 case LyXTabular::MULTICOLUMN:
1129 if (sel_row_start != sel_row_end) {
1130 WriteAlert(_("Impossible Operation!"),
1131 _("Multicolumns can only be horizontally."),
1135 // just multicol for one Single Cell
1136 if (!hasSelection()) {
1137 // check wether we are completly in a multicol
1138 if (tabular->IsMultiColumn(actcell)) {
1139 tabular->UnsetMultiColumn(actcell);
1140 UpdateLocal(bv, true);
1142 tabular->SetMultiColumn(actcell, 1);
1143 UpdateLocal(bv, false);
1147 // we have a selection so this means we just add all this
1148 // cells to form a multicolumn cell
1152 if (sel_cell_start > sel_cell_end) {
1153 s_start = sel_cell_end;
1154 s_end = sel_cell_start;
1156 s_start = sel_cell_start;
1157 s_end = sel_cell_end;
1159 tabular->SetMultiColumn(s_start, s_end-s_start+1);
1162 sel_cell_end = sel_cell_start;
1163 sel_pos_end = sel_pos_start;
1164 UpdateLocal(bv, true);
1167 case LyXTabular::SET_ALL_LINES:
1169 case LyXTabular::UNSET_ALL_LINES:
1170 for(i=sel_row_start; i<=sel_row_end; ++i)
1171 for(j=sel_col_start; j<=sel_col_end; ++j)
1172 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1173 UpdateLocal(bv, true);
1175 case LyXTabular::SET_LONGTABULAR:
1176 tabular->SetLongTabular(true);
1177 UpdateLocal(bv, true); // because this toggles displayed
1179 case LyXTabular::UNSET_LONGTABULAR:
1180 tabular->SetLongTabular(false);
1181 UpdateLocal(bv, true); // because this toggles displayed
1183 case LyXTabular::SET_ROTATE_TABULAR:
1184 tabular->SetRotateTabular(true);
1186 case LyXTabular::UNSET_ROTATE_TABULAR:
1187 tabular->SetRotateTabular(false);
1189 case LyXTabular::SET_ROTATE_CELL:
1190 for(i=sel_row_start; i<=sel_row_end; ++i)
1191 for(j=sel_col_start; j<=sel_col_end; ++j)
1192 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1194 case LyXTabular::UNSET_ROTATE_CELL:
1195 for(i=sel_row_start; i<=sel_row_end; ++i)
1196 for(j=sel_col_start; j<=sel_col_end; ++j)
1197 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1199 case LyXTabular::SET_LINEBREAKS:
1200 what = !tabular->GetLinebreaks(actcell);
1201 for(i=sel_row_start; i<=sel_row_end; ++i)
1202 for(j=sel_col_start; j<=sel_col_end; ++j)
1203 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1205 case LyXTabular::SET_LTFIRSTHEAD:
1206 tabular->SetLTHead(actcell,true);
1208 case LyXTabular::SET_LTHEAD:
1209 tabular->SetLTHead(actcell,false);
1211 case LyXTabular::SET_LTFOOT:
1212 tabular->SetLTFoot(actcell,false);
1214 case LyXTabular::SET_LTLASTFOOT:
1215 tabular->SetLTFoot(actcell,true);
1217 case LyXTabular::SET_LTNEWPAGE:
1218 what = !tabular->GetLTNewPage(actcell);
1219 tabular->SetLTNewPage(actcell,what);
1224 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1227 // the cursor.pos has to be before the inset so if it isn't now just
1228 // reset the curor pos first!
1229 if (cursor.pos % 2) { // behind the inset
1231 resetPos(bv->painter());
1233 UpdatableInset * inset =
1234 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1235 LyXFont font(LyXFont::ALL_SANE);
1237 x = inset->x() + inset->width(bv->painter(), font);
1238 y = inset->descent(bv->painter(), font);
1240 inset_x = cursor.x - top_x + tabular->GetBeginningOfTextInCell(actcell);
1242 inset->Edit(bv, x-inset_x, y-inset_y, button);
1243 if (!the_locking_inset)
1245 UpdateLocal(bv, true);
1249 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1251 InsetText * inset = tabular->GetCellInset(actcell);
1254 if (cursor.pos % 2) { // behind the inset
1255 return (((x+top_x) < cursor.x) &&
1256 ((x+top_x) > (cursor.x - inset->width(bv->painter(),
1257 LyXFont(LyXFont::ALL_SANE)))));
1259 int x2 = cursor.x + tabular->GetBeginningOfTextInCell(actcell);
1260 return ((x1 > x2) &&
1261 (x1 < (x2 + inset->width(bv->painter(),
1262 LyXFont(LyXFont::ALL_SANE)))));
1266 // This returns paperWidth() if the cell-width is unlimited or the width
1267 // in pixels if we have a pwidth for this cell.
1268 int InsetTabular::GetMaxWidthOfCell(Painter & pain, int cell) const
1272 if ((w=tabular->GetPWidth(cell)).empty())
1273 return pain.paperWidth();
1274 return VSpace(w).inPixels( 0, 0);
1277 int InsetTabular::getMaxWidth(Painter & pain,
1278 UpdatableInset const * inset) const
1281 int n = tabular->GetNumberOfCells();
1282 for(cell=0; cell < n; ++cell) {
1283 if (tabular->GetCellInset(cell) == inset)
1287 return pain.paperWidth();
1288 int w = GetMaxWidthOfCell(pain, cell);
1289 // this because text insets remove the xpos from the maxwidth because
1290 // otherwise the would not break good!!!
1291 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1296 void InsetTabular::recomputeTextInsets(Painter & pain, const LyXFont & font) const
1302 for(int j= 0; j < tabular->columns(); ++j) {
1303 for(int i = 0; i < tabular->rows(); ++i) {
1304 if (tabular->IsPartOfMultiColumn(i,j))
1306 cell = tabular->GetCellNumber(i,j);
1307 inset = tabular->GetCellInset(cell);
1308 inset->computeTextRows(pain);
1309 tabular->SetWidthOfCell(cell, inset->width(pain, font));
1311 cell = tabular->GetCellNumber(0, j);
1312 cx += tabular->GetWidthOfColumn(cell);