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(BufferView *, LyXFont const &) const
136 return tabular->GetAscentOfRow(0);
140 int InsetTabular::descent(BufferView *, LyXFont const &) const
142 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
146 int InsetTabular::width(BufferView *, LyXFont const &) const
148 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
152 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
153 float & x, bool cleared) const
155 Painter & pain = bv->painter();
160 UpdatableInset::draw(bv,font,baseline,x,cleared);
161 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
162 (top_x != int(x)) || (top_baseline != baseline))) {
164 int h = ascent(bv, font) + descent(bv, font);
165 int tx = display()? 0:top_x;
166 int w = tx? width(bv, font):pain.paperWidth();
167 int ty = baseline - ascent(bv, font);
171 if ((ty + h) > pain.paperHeight())
172 h = pain.paperHeight();
173 if ((top_x + w) > pain.paperWidth())
174 w = pain.paperWidth();
175 pain.fillRectangle(tx, ty, w, h);
181 if (locked) { // repaint this way as the background was not cleared
182 if (the_locking_inset)
183 the_locking_inset->update(bv, font, true);
185 bv->updateInset(const_cast<InsetTabular*>(this), false);
192 top_baseline = baseline;
194 x += ADD_TO_TABULAR_WIDTH;
195 if (cleared || (need_update == FULL) || (need_update == CELL)) {
196 for(i=0;i<tabular->rows();++i) {
198 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
199 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
200 for(j=0;j<tabular->columns();++j) {
201 if (tabular->IsPartOfMultiColumn(i,j))
203 cx = nx + tabular->GetBeginningOfTextInCell(cell);
205 DrawCellSelection(pain, nx, baseline, i, j, cell);
206 if (dodraw && !cleared && locked && the_locking_inset) {
207 if (the_locking_inset == tabular->GetCellInset(cell))
208 tabular->GetCellInset(cell)->draw(bv, font,
212 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
214 DrawCellLines(pain, nx, baseline, i, cell);
216 nx += tabular->GetWidthOfColumn(cell);
219 baseline += tabular->GetDescentOfRow(i) +
220 tabular->GetAscentOfRow(i+1)+
221 tabular->GetAdditionalHeight(cell+1);
224 x += width(bv, font);
229 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
230 int row, int cell) const
232 int x2 = x + tabular->GetWidthOfColumn(cell);
235 if (!tabular->TopAlreadyDrawed(cell)) {
236 on_off = !tabular->TopLine(cell);
237 pain.line(x, baseline - tabular->GetAscentOfRow(row),
238 x2, baseline - tabular->GetAscentOfRow(row),
239 on_off ? LColor::tabularonoffline:LColor::tabularline,
240 on_off ? Painter::line_onoffdash:Painter::line_solid);
242 on_off = !tabular->BottomLine(cell);
243 pain.line(x,baseline + tabular->GetDescentOfRow(row),
244 x2, baseline + tabular->GetDescentOfRow(row),
245 on_off ? LColor::tabularonoffline:LColor::tabularline,
246 on_off ? Painter::line_onoffdash:Painter::line_solid);
247 if (!tabular->LeftAlreadyDrawed(cell)) {
248 on_off = !tabular->LeftLine(cell);
249 pain.line(x, baseline - tabular->GetAscentOfRow(row),
250 x, baseline + tabular->GetDescentOfRow(row),
251 on_off ? LColor::tabularonoffline:LColor::tabularline,
252 on_off ? Painter::line_onoffdash:Painter::line_solid);
254 on_off = !tabular->RightLine(cell);
255 pain.line(x2 - tabular->GetAdditionalWidth(cell),
256 baseline - tabular->GetAscentOfRow(row),
257 x2 - tabular->GetAdditionalWidth(cell),
258 baseline + tabular->GetDescentOfRow(row),
259 on_off ? LColor::tabularonoffline:LColor::tabularline,
260 on_off ? Painter::line_onoffdash:Painter::line_solid);
264 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
265 int row, int column, int cell) const
269 int cs = tabular->column_of_cell(sel_cell_start);
270 int ce = tabular->column_of_cell(sel_cell_end);
273 cs = tabular->column_of_cell(sel_cell_end);
275 ce = tabular->right_column_of_cell(sel_cell_end);
278 int rs = tabular->row_of_cell(sel_cell_start);
279 int re = tabular->row_of_cell(sel_cell_end);
286 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
287 int w = tabular->GetWidthOfColumn(cell);
288 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
289 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
290 w, h, LColor::selection);
295 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
299 calculate_dimensions_of_cells(bv, font, true);
301 owner()->update(bv, font, true);
304 if (the_locking_inset)
305 the_locking_inset->update(bv, font, reinit);
306 switch(need_update) {
310 if (calculate_dimensions_of_cells(bv, font, false))
322 char const * InsetTabular::EditMessage() const
324 return _("Opened Tabular Inset");
328 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
330 UpdatableInset::Edit(bv, x, y, button);
332 if (!bv->lockInset(this)) {
333 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
337 the_locking_inset = 0;
338 inset_pos = inset_x = inset_y = 0;
340 sel_pos_start = sel_pos_end = cursor.pos();
341 sel_cell_start = sel_cell_end = actcell;
342 bv->text->FinishUndo();
343 if (InsetHit(bv, x, y)) {
344 ActivateCellInset(bv, x, y, button);
346 UpdateLocal(bv, NONE, false);
347 // bv->getOwner()->getPopups().updateFormTabular();
351 void InsetTabular::InsetUnlock(BufferView * bv)
354 if (the_locking_inset) {
355 the_locking_inset->InsetUnlock(bv);
356 the_locking_inset = 0;
359 if (hasSelection()) {
360 sel_pos_start = sel_pos_end = 0;
361 sel_cell_start = sel_cell_end = 0;
362 UpdateLocal(bv, FULL, false);
364 no_selection = false;
369 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
373 bv->updateInset(this, mark_dirty);
378 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
380 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
384 if (inset == tabular->GetCellInset(actcell)) {
385 lyxerr[Debug::INSETS] << "OK" << endl;
386 the_locking_inset = tabular->GetCellInset(actcell);
388 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
389 inset_y = cursor.y();
390 inset_pos = cursor.pos();
392 } else if (the_locking_inset && (the_locking_inset == inset)) {
393 if (cursor.pos() == inset_pos) {
394 lyxerr[Debug::INSETS] << "OK" << endl;
396 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
397 inset_y = cursor.y();
399 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
401 } else if (the_locking_inset) {
402 lyxerr[Debug::INSETS] << "MAYBE" << endl;
403 return the_locking_inset->LockInsetInInset(bv, inset);
405 lyxerr[Debug::INSETS] << "NOT OK" << endl;
409 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
412 if (!the_locking_inset)
414 if (the_locking_inset == inset) {
415 the_locking_inset->InsetUnlock(bv);
416 the_locking_inset = 0;
418 moveRight(bv, false);
419 UpdateLocal(bv, CELL, false);
422 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
423 if ((inset->LyxCode() == TABULAR_CODE) &&
424 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
426 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
434 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
436 if (!the_locking_inset)
438 if (the_locking_inset != inset)
439 return the_locking_inset->UpdateInsetInInset(bv, inset);
440 UpdateLocal(bv, CELL, false);
445 int InsetTabular::InsetInInsetY()
447 if (!the_locking_inset)
450 return (inset_y + the_locking_inset->InsetInInsetY());
454 UpdatableInset * InsetTabular::GetLockingInset()
456 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
460 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
464 if (the_locking_inset)
465 return the_locking_inset->GetFirstLockingInsetOfType(c);
470 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
472 if (the_locking_inset)
473 return the_locking_inset->InsertInset(bv, inset);
478 void InsetTabular::InsetButtonRelease(BufferView * bv,
479 int x, int y, int button)
482 if (the_locking_inset) {
484 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
485 i->InsetButtonRelease(bv, x, y, button);
489 MenuLayoutTabular(true, this);
492 if (the_locking_inset) {
493 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
496 no_selection = false;
500 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
502 if (hasSelection()) {
503 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
504 UpdateLocal(bv, SELECTION, false);
506 no_selection = false;
511 sel_pos_start = sel_pos_end = cursor.pos();
512 sel_cell_start = sel_cell_end = actcell;
514 bool inset_hit = InsetHit(bv, x, y);
516 if ((ocell == actcell) && the_locking_inset && inset_hit) {
517 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
519 } else if (the_locking_inset) {
520 the_locking_inset->InsetUnlock(bv);
522 the_locking_inset = 0;
523 if (inset_hit && bv->the_locking_inset) {
524 ActivateCellInset(bv, x, y, button);
525 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
530 bview->getOwner()->getPopups().showFormTabular();
531 else if (ocell != actcell)
532 bview->getOwner()->getPopups().updateFormTabular();
537 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
539 if (the_locking_inset) {
540 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
541 y - inset_y, button);
545 // int ocell = actcell,
546 int old = sel_pos_end;
549 sel_pos_end = cursor.pos();
550 sel_cell_end = actcell;
551 if (old != sel_pos_end)
552 UpdateLocal(bv, SELECTION, false);
554 if (ocell != actcell)
555 bview->getOwner()->getPopups().updateFormTabular();
558 no_selection = false;
562 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
564 if (the_locking_inset) {
565 the_locking_inset->InsetKeyPress(xke);
571 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
574 UpdatableInset::RESULT
577 no_selection = false;
578 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
579 || (result == DISPATCHED_NOUPDATE)) {
586 if ((action < 0) && arg.empty())
589 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
590 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
592 if (the_locking_inset) {
593 result=the_locking_inset->LocalDispatch(bv, action, arg);
594 if (result == DISPATCHED_NOUPDATE)
596 else if (result == DISPATCHED) {
597 the_locking_inset->ToggleInsetCursor(bv);
598 UpdateLocal(bv, CELL, false);
599 the_locking_inset->ToggleInsetCursor(bv);
601 } else if (result == FINISHED) {
602 if ((action == LFUN_RIGHT) || (action == -1)) {
603 cursor.pos(inset_pos + 1);
606 sel_pos_start = sel_pos_end = cursor.pos();
607 sel_cell_start = sel_cell_end = actcell;
614 bool hs = hasSelection();
617 // Normal chars not handled here
620 // --- Cursor Movements ---------------------------------------------
622 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
624 moveRight(bv, false);
625 sel_pos_end = cursor.pos();
626 if (!cellstart(cursor.pos())) {
627 if (tabular->right_column_of_cell(sel_cell_start) >
628 tabular->right_column_of_cell(actcell))
629 sel_cell_end = actcell+1;
631 sel_cell_end = actcell;
633 UpdateLocal(bv, SELECTION, false);
636 result = moveRight(bv);
637 sel_pos_start = sel_pos_end = cursor.pos();
638 sel_cell_start = sel_cell_end = actcell;
640 UpdateLocal(bv, CURSOR, false);
643 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
646 sel_pos_end = cursor.pos();
647 if (cellstart(cursor.pos())) {
648 if (tabular->column_of_cell(sel_cell_start) >=
649 tabular->column_of_cell(actcell))
650 sel_cell_end = actcell;
652 sel_cell_end = actcell-1;
654 UpdateLocal(bv, SELECTION, false);
657 result = moveLeft(bv);
658 sel_pos_start = sel_pos_end = cursor.pos();
659 sel_cell_start = sel_cell_end = actcell;
661 UpdateLocal(bv, CURSOR, false);
667 sel_pos_end = cursor.pos();
668 if ((ocell == sel_cell_end) ||
669 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
670 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
672 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
673 UpdateLocal(bv, SELECTION, false);
677 result= moveDown(bv);
678 sel_pos_start = sel_pos_end = cursor.pos();
679 sel_cell_start = sel_cell_end = actcell;
681 UpdateLocal(bv, CURSOR, false);
687 sel_pos_end = cursor.pos();
688 if ((ocell == sel_cell_end) ||
689 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
690 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
692 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
693 UpdateLocal(bv, SELECTION, false);
698 sel_pos_start = sel_pos_end = cursor.pos();
699 sel_cell_start = sel_cell_end = actcell;
701 UpdateLocal(bv, CURSOR, false);
713 if (the_locking_inset) {
714 the_locking_inset->InsetUnlock(bv);
716 the_locking_inset = 0;
717 if (action == LFUN_TAB)
721 sel_pos_start = sel_pos_end = cursor.pos();
722 sel_cell_start = sel_cell_end = actcell;
724 UpdateLocal(bv, CURSOR, false);
726 case LFUN_LAYOUT_TABLE:
728 int flag = (arg == "true");
729 MenuLayoutTabular(flag, this);
733 result = UNDISPATCHED;
736 if (result!=FINISHED) {
737 if (!the_locking_inset) {
739 if (ocell != actcell)
740 bview->getOwner()->getPopups().updateFormTabular();
745 bv->unlockInset(this);
750 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
752 return tabular->Latex(buf, os, fragile, fp);
756 int InsetTabular::Ascii(Buffer const *, ostream &) const
761 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
767 int InsetTabular::DocBook(Buffer const *, ostream &) const
773 void InsetTabular::Validate(LaTeXFeatures & features) const
775 tabular->Validate(features);
779 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
780 LyXFont const & font,
786 bool changed = false;
788 for(int i = 0; i < tabular->rows(); ++i) {
789 maxAsc = maxDesc = 0;
790 for(int j= 0; j < tabular->columns(); ++j) {
791 if (tabular->IsPartOfMultiColumn(i,j))
794 inset = tabular->GetCellInset(cell);
796 inset->update(bv, font, false);
797 maxAsc = max(maxAsc, inset->ascent(bv, font));
798 maxDesc = max(maxDesc, inset->descent(bv, font));
799 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
801 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
802 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
808 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
810 x = cursor.x() - top_x;
815 void InsetTabular::ToggleInsetCursor(BufferView * bv)
817 if (the_locking_inset) {
818 the_locking_inset->ToggleInsetCursor(bv);
822 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
824 int asc = lyxfont::maxAscent(font);
825 int desc = lyxfont::maxDescent(font);
828 bv->hideLockedInsetCursor();
830 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
831 cursor_visible = !cursor_visible;
835 void InsetTabular::ShowInsetCursor(BufferView * bv)
837 if (!cursor_visible) {
838 LyXFont font; // = GetFont(par, cursor.pos);
840 int asc = lyxfont::maxAscent(font);
841 int desc = lyxfont::maxDescent(font);
842 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
843 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
844 cursor_visible = true;
849 void InsetTabular::HideInsetCursor(BufferView * bv)
852 ToggleInsetCursor(bv);
856 void InsetTabular::setPos(BufferView * bv, int x, int y) const
861 actcell = actrow = actcol = 0;
862 int ly = tabular->GetDescentOfRow(actrow);
864 // first search the right row
865 while((ly < y) && (actrow < tabular->rows())) {
866 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
867 tabular->GetAscentOfRow(actrow+1) +
868 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
871 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
873 actcell = tabular->GetCellNumber(actrow, actcol);
875 // now search the right column
876 int lx = tabular->GetWidthOfColumn(actcell) -
877 tabular->GetAdditionalWidth(actcell);
878 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
879 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
880 tabular->GetAdditionalWidth(actcell - 1));
881 cursor.pos(((actcell+1) * 2) - 1);
883 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
884 cursor.x(lx + top_x - 2);
886 cursor.pos(cursor.pos() - 1);
887 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
892 int InsetTabular::getCellXPos(int cell) const
896 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
898 int lx = tabular->GetWidthOfColumn(cell);
899 for(; (c < cell); ++c) {
900 lx += tabular->GetWidthOfColumn(c);
902 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
903 ADD_TO_TABULAR_WIDTH);
906 void InsetTabular::resetPos(BufferView * bv) const
910 actcol = tabular->column_of_cell(actcell);
915 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
916 if (tabular->IsLastCellInRow(cell)) {
917 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
918 tabular->GetAscentOfRow(actrow + 1) +
919 tabular->GetAdditionalHeight(cell + 1));
923 cursor.x(getCellXPos(actcell) + 2);
924 if (cursor.pos() % 2) {
925 LyXFont font(LyXFont::ALL_SANE);
926 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
927 tabular->GetBeginningOfTextInCell(actcell));
929 if ((!the_locking_inset ||
930 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
931 (actcell != oldcell)) {
932 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
938 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
940 if (!cellstart(cursor.pos())) {
941 if (tabular->IsLastCell(actcell))
944 cursor.pos(cursor.pos() + 1);
946 if (ActivateCellInset(bv))
948 } else { // before the inset
949 cursor.pos(cursor.pos() + 1);
952 return DISPATCHED_NOUPDATE;
956 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
963 cursor.pos(cursor.pos() - 1);
964 if (!cellstart(cursor.pos())) {
966 } else if (lock) { // behind the inset
967 if (ActivateCellInset(bv, 0, 0, 0, true))
971 return DISPATCHED_NOUPDATE;
975 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
978 actcell = tabular->GetCellAbove(actcell);
979 if (actcell == ocell) // we moved out of the inset
982 return DISPATCHED_NOUPDATE;
986 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
989 actcell = tabular->GetCellBelow(actcell);
990 if (actcell == ocell) // we moved out of the inset
993 return DISPATCHED_NOUPDATE;
997 bool InsetTabular::moveNextCell(BufferView * bv)
999 if (tabular->IsLastCell(actcell))
1002 cursor.pos(cursor.pos() + 1);
1003 if (!cellstart(cursor.pos()))
1004 cursor.pos(cursor.pos() + 1);
1010 bool InsetTabular::movePrevCell(BufferView * bv)
1012 if (!actcell) // first cell
1015 cursor.pos(cursor.pos() - 1);
1016 if (cellstart(cursor.pos()))
1017 cursor.pos(cursor.pos() - 1);
1023 bool InsetTabular::Delete()
1029 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1031 if (the_locking_inset)
1032 the_locking_inset->SetFont(bv, font, tall);
1036 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1045 setAlign = LYX_ALIGN_LEFT,
1051 case LyXTabular::ALIGN_LEFT:
1052 setAlign=LYX_ALIGN_LEFT;
1054 case LyXTabular::ALIGN_RIGHT:
1055 setAlign=LYX_ALIGN_RIGHT;
1057 case LyXTabular::ALIGN_CENTER:
1058 setAlign=LYX_ALIGN_CENTER;
1063 if (hasSelection()) {
1065 sel_col_start = tabular->column_of_cell(sel_cell_start);
1066 sel_col_end = tabular->column_of_cell(sel_cell_end);
1067 if (sel_col_start > sel_col_end) {
1068 sel_col_end = sel_col_start;
1069 sel_col_start = tabular->column_of_cell(sel_cell_end);
1071 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1074 sel_row_start = tabular->row_of_cell(sel_cell_start);
1075 sel_row_end = tabular->row_of_cell(sel_cell_end);
1076 if (sel_row_start > sel_row_end) {
1077 tmp = sel_row_start;
1078 sel_row_start = sel_row_end;
1082 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1083 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1085 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1086 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1087 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1089 int row = tabular->row_of_cell(actcell);
1090 int column = tabular->column_of_cell(actcell);
1093 case LyXTabular::SET_PWIDTH:
1095 bool update = (tabular->GetPWidth(actcell) != val);
1096 tabular->SetPWidth(actcell,val);
1098 for (int i=0; i < tabular->rows(); ++i) {
1099 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1102 UpdateLocal(bv, INIT, true);
1106 case LyXTabular::SET_SPECIAL_COLUMN:
1107 case LyXTabular::SET_SPECIAL_MULTI:
1108 tabular->SetAlignSpecial(actcell,val,feature);
1110 case LyXTabular::APPEND_ROW:
1111 // append the row into the tabular
1112 UnlockInsetInInset(bv, the_locking_inset);
1113 tabular->AppendRow(actcell);
1114 UpdateLocal(bv, INIT, true);
1116 case LyXTabular::APPEND_COLUMN:
1117 // append the column into the tabular
1118 tabular->AppendColumn(actcell);
1119 actcell = tabular->GetCellNumber(row, column);
1120 UpdateLocal(bv, INIT, true);
1122 case LyXTabular::DELETE_ROW:
1123 tabular->DeleteRow(tabular->row_of_cell(actcell));
1124 if ((row+1) > tabular->rows())
1126 actcell = tabular->GetCellNumber(row, column);
1127 UpdateLocal(bv, INIT, true);
1129 case LyXTabular::DELETE_COLUMN:
1130 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1131 if ((column+1) > tabular->columns())
1133 actcell = tabular->GetCellNumber(row, column);
1134 UpdateLocal(bv, INIT, true);
1136 case LyXTabular::TOGGLE_LINE_TOP:
1137 lineSet = !tabular->TopLine(actcell);
1138 for(i=sel_row_start; i<=sel_row_end; ++i)
1139 for(j=sel_col_start; j<=sel_col_end; ++j)
1140 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1141 UpdateLocal(bv, INIT, true);
1144 case LyXTabular::TOGGLE_LINE_BOTTOM:
1145 lineSet = !tabular->BottomLine(actcell);
1146 for(i=sel_row_start; i<=sel_row_end; ++i)
1147 for(j=sel_col_start; j<=sel_col_end; ++j)
1148 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1149 UpdateLocal(bv, INIT, true);
1152 case LyXTabular::TOGGLE_LINE_LEFT:
1153 lineSet = !tabular->LeftLine(actcell);
1154 for(i=sel_row_start; i<=sel_row_end; ++i)
1155 for(j=sel_col_start; j<=sel_col_end; ++j)
1156 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1157 UpdateLocal(bv, INIT, true);
1160 case LyXTabular::TOGGLE_LINE_RIGHT:
1161 lineSet = !tabular->RightLine(actcell);
1162 for(i=sel_row_start; i<=sel_row_end; ++i)
1163 for(j=sel_col_start; j<=sel_col_end; ++j)
1164 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1165 UpdateLocal(bv, INIT, true);
1167 case LyXTabular::ALIGN_LEFT:
1168 case LyXTabular::ALIGN_RIGHT:
1169 case LyXTabular::ALIGN_CENTER:
1170 for(i=sel_row_start; i<=sel_row_end; ++i)
1171 for(j=sel_col_start; j<=sel_col_end; ++j)
1172 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1174 UpdateLocal(bv, INIT, true);
1176 UpdateLocal(bv, CELL, true);
1178 case LyXTabular::MULTICOLUMN:
1180 if (sel_row_start != sel_row_end) {
1181 WriteAlert(_("Impossible Operation!"),
1182 _("Multicolumns can only be horizontally."),
1186 // just multicol for one Single Cell
1187 if (!hasSelection()) {
1188 // check wether we are completly in a multicol
1189 if (tabular->IsMultiColumn(actcell)) {
1190 tabular->UnsetMultiColumn(actcell);
1191 UpdateLocal(bv, INIT, true);
1193 tabular->SetMultiColumn(actcell, 1);
1194 UpdateLocal(bv, CELL, true);
1198 // we have a selection so this means we just add all this
1199 // cells to form a multicolumn cell
1203 if (sel_cell_start > sel_cell_end) {
1204 s_start = sel_cell_end;
1205 s_end = sel_cell_start;
1207 s_start = sel_cell_start;
1208 s_end = sel_cell_end;
1210 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1213 sel_cell_end = sel_cell_start;
1214 sel_pos_end = sel_pos_start;
1215 UpdateLocal(bv, INIT, true);
1218 case LyXTabular::SET_ALL_LINES:
1220 case LyXTabular::UNSET_ALL_LINES:
1221 for(i=sel_row_start; i<=sel_row_end; ++i)
1222 for(j=sel_col_start; j<=sel_col_end; ++j)
1223 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1224 UpdateLocal(bv, INIT, true);
1226 case LyXTabular::SET_LONGTABULAR:
1227 tabular->SetLongTabular(true);
1228 UpdateLocal(bv, INIT, true); // because this toggles displayed
1230 case LyXTabular::UNSET_LONGTABULAR:
1231 tabular->SetLongTabular(false);
1232 UpdateLocal(bv, INIT, true); // because this toggles displayed
1234 case LyXTabular::SET_ROTATE_TABULAR:
1235 tabular->SetRotateTabular(true);
1237 case LyXTabular::UNSET_ROTATE_TABULAR:
1238 tabular->SetRotateTabular(false);
1240 case LyXTabular::SET_ROTATE_CELL:
1241 for(i=sel_row_start; i<=sel_row_end; ++i)
1242 for(j=sel_col_start; j<=sel_col_end; ++j)
1243 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1245 case LyXTabular::UNSET_ROTATE_CELL:
1246 for(i=sel_row_start; i<=sel_row_end; ++i)
1247 for(j=sel_col_start; j<=sel_col_end; ++j)
1248 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1250 case LyXTabular::SET_LINEBREAKS:
1251 what = !tabular->GetLinebreaks(actcell);
1252 for(i=sel_row_start; i<=sel_row_end; ++i)
1253 for(j=sel_col_start; j<=sel_col_end; ++j)
1254 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1256 case LyXTabular::SET_LTFIRSTHEAD:
1257 tabular->SetLTHead(actcell,true);
1259 case LyXTabular::SET_LTHEAD:
1260 tabular->SetLTHead(actcell,false);
1262 case LyXTabular::SET_LTFOOT:
1263 tabular->SetLTFoot(actcell,false);
1265 case LyXTabular::SET_LTLASTFOOT:
1266 tabular->SetLTFoot(actcell,true);
1268 case LyXTabular::SET_LTNEWPAGE:
1269 what = !tabular->GetLTNewPage(actcell);
1270 tabular->SetLTNewPage(actcell,what);
1275 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1278 // the cursor.pos has to be before the inset so if it isn't now just
1279 // reset the curor pos first!
1280 if (cursor.pos() % 2) { // behind the inset
1281 cursor.pos(cursor.pos() - 1);
1284 UpdatableInset * inset =
1285 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1286 LyXFont font(LyXFont::ALL_SANE);
1288 x = inset->x() + inset->width(bv, font);
1289 y = inset->descent(bv, font);
1291 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1292 inset_y = cursor.y();
1293 inset->Edit(bv, x - inset_x, y - inset_y, button);
1294 if (!the_locking_inset)
1296 UpdateLocal(bv, CELL, false);
1300 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1302 InsetText * inset = tabular->GetCellInset(actcell);
1305 if (cursor.pos() % 2) { // behind the inset
1306 return (((x + top_x) < cursor.x()) &&
1307 ((x + top_x) > (cursor.x() - inset->width(bv,
1308 LyXFont(LyXFont::ALL_SANE)))));
1310 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1311 return ((x1 > x2) &&
1312 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1316 // This returns paperWidth() if the cell-width is unlimited or the width
1317 // in pixels if we have a pwidth for this cell.
1318 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1320 string s = tabular->GetPWidth(cell);
1324 return VSpace(s).inPixels( 0, 0);
1327 int InsetTabular::getMaxWidth(Painter & pain,
1328 UpdatableInset const * inset) const
1331 int n = tabular->GetNumberOfCells();
1332 for(cell=0; cell < n; ++cell) {
1333 if (tabular->GetCellInset(cell) == inset)
1338 int w = GetMaxWidthOfCell(pain, cell);
1339 // this because text insets remove the xpos from the maxwidth because
1340 // otherwise the would not break good!!!
1341 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1346 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1352 for(int j= 0; j < tabular->columns(); ++j) {
1353 for(int i = 0; i < tabular->rows(); ++i) {
1354 if (tabular->IsPartOfMultiColumn(i,j))
1356 cell = tabular->GetCellNumber(i,j);
1357 inset = tabular->GetCellInset(cell);
1358 inset->update(bv, font);
1359 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1361 // cell = tabular->GetCellNumber(0, j);
1362 // cx += tabular->GetWidthOfColumn(cell);