1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettabular.h"
25 #include "commandtags.h"
27 #include "LaTeXFeatures.h"
31 #include "lyx_gui_misc.h"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
37 const int ADD_TO_HEIGHT = 2;
38 const int ADD_TO_TABULAR_WIDTH = 2;
46 #define cellstart(p) ((p % 2) == 0)
48 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
54 buffer = buf; // set this first!!!
55 tabular = new LyXTabular(this, rows,columns);
56 // for now make it always display as display() inset
58 the_locking_inset = 0;
59 locked = no_selection = cursor_visible = false;
64 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;
86 InsetTabular::~InsetTabular()
90 dialogs_->hideTabular(this);
94 Inset * InsetTabular::Clone() const
96 InsetTabular * t = new InsetTabular(*this, buffer);
98 t->tabular = tabular->Clone(t);
103 void InsetTabular::Write(Buffer const * buf, ostream & os) const
105 os << " Tabular" << endl;
106 tabular->Write(buf, os);
110 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
112 bool old_format = (lex.GetString() == "\\LyXTable");
117 tabular = new LyXTabular(buf, this, lex);
125 token = lex.GetString();
126 while (lex.IsOK() && (token != "\\end_inset")) {
128 token = lex.GetString();
130 if (token != "\\end_inset") {
131 lex.printError("Missing \\end_inset at this point. "
137 int InsetTabular::ascent(BufferView *, LyXFont const &) const
139 return tabular->GetAscentOfRow(0);
143 int InsetTabular::descent(BufferView *, LyXFont const &) const
145 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
149 int InsetTabular::width(BufferView *, LyXFont const &) const
151 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
155 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
156 float & x, bool cleared) const
158 Painter & pain = bv->painter();
163 UpdatableInset::draw(bv,font,baseline,x,cleared);
164 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
165 (top_x != int(x)) || (top_baseline != baseline))) {
166 int h = ascent(bv, font) + descent(bv, font);
167 int tx = display()||!owner()? 0:top_x;
168 int w = tx? width(bv, font):pain.paperWidth();
169 int ty = baseline - ascent(bv, font);
173 if ((ty + h) > pain.paperHeight())
174 h = pain.paperHeight();
175 if ((top_x + w) > pain.paperWidth())
176 w = pain.paperWidth();
177 pain.fillRectangle(tx, ty, w, h);
182 top_baseline = baseline;
184 x += ADD_TO_TABULAR_WIDTH;
185 if (cleared || (need_update == FULL) || (need_update == CELL)) {
186 for(i=0;i<tabular->rows();++i) {
188 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
189 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
190 for(j=0;j<tabular->columns();++j) {
191 if (tabular->IsPartOfMultiColumn(i,j))
193 cx = nx + tabular->GetBeginningOfTextInCell(cell);
195 DrawCellSelection(pain, nx, baseline, i, j, cell);
196 if (dodraw && !cleared && locked && the_locking_inset) {
197 if (the_locking_inset == tabular->GetCellInset(cell))
198 tabular->GetCellInset(cell)->draw(bv, font,
202 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
204 DrawCellLines(pain, nx, baseline, i, cell);
206 nx += tabular->GetWidthOfColumn(cell);
209 baseline += tabular->GetDescentOfRow(i) +
210 tabular->GetAscentOfRow(i+1)+
211 tabular->GetAdditionalHeight(cell+1);
214 x += width(bv, font);
219 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
220 int row, int cell) const
222 int x2 = x + tabular->GetWidthOfColumn(cell);
225 if (!tabular->TopAlreadyDrawed(cell)) {
226 on_off = !tabular->TopLine(cell);
227 pain.line(x, baseline - tabular->GetAscentOfRow(row),
228 x2, baseline - tabular->GetAscentOfRow(row),
229 on_off ? LColor::tabularonoffline:LColor::tabularline,
230 on_off ? Painter::line_onoffdash:Painter::line_solid);
232 on_off = !tabular->BottomLine(cell);
233 pain.line(x,baseline + tabular->GetDescentOfRow(row),
234 x2, baseline + tabular->GetDescentOfRow(row),
235 on_off ? LColor::tabularonoffline:LColor::tabularline,
236 on_off ? Painter::line_onoffdash:Painter::line_solid);
237 if (!tabular->LeftAlreadyDrawed(cell)) {
238 on_off = !tabular->LeftLine(cell);
239 pain.line(x, baseline - tabular->GetAscentOfRow(row),
240 x, baseline + tabular->GetDescentOfRow(row),
241 on_off ? LColor::tabularonoffline:LColor::tabularline,
242 on_off ? Painter::line_onoffdash:Painter::line_solid);
244 on_off = !tabular->RightLine(cell);
245 pain.line(x2 - tabular->GetAdditionalWidth(cell),
246 baseline - tabular->GetAscentOfRow(row),
247 x2 - tabular->GetAdditionalWidth(cell),
248 baseline + tabular->GetDescentOfRow(row),
249 on_off ? LColor::tabularonoffline:LColor::tabularline,
250 on_off ? Painter::line_onoffdash:Painter::line_solid);
254 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
255 int row, int column, int cell) const
257 int cs = tabular->column_of_cell(sel_cell_start);
258 int ce = tabular->column_of_cell(sel_cell_end);
261 cs = tabular->column_of_cell(sel_cell_end);
263 ce = tabular->right_column_of_cell(sel_cell_end);
266 int rs = tabular->row_of_cell(sel_cell_start);
267 int re = tabular->row_of_cell(sel_cell_end);
268 if (rs > re) swap(rs, re);
270 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
271 int w = tabular->GetWidthOfColumn(cell);
272 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
273 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
274 w, h, LColor::selection);
279 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
283 calculate_dimensions_of_cells(bv, font, true);
285 owner()->update(bv, font, true);
288 if (the_locking_inset)
289 the_locking_inset->update(bv, font, reinit);
290 switch(need_update) {
294 if (calculate_dimensions_of_cells(bv, font, false))
306 char const * InsetTabular::EditMessage() const
308 return _("Opened Tabular Inset");
312 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
314 UpdatableInset::Edit(bv, x, y, button);
316 if (!bv->lockInset(this)) {
317 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
321 the_locking_inset = 0;
322 inset_pos = inset_x = inset_y = 0;
324 sel_pos_start = sel_pos_end = cursor.pos();
325 sel_cell_start = sel_cell_end = actcell;
326 bv->text->FinishUndo();
327 if (InsetHit(bv, x, y)) {
328 ActivateCellInset(bv, x, y, button);
330 UpdateLocal(bv, NONE, false);
331 // bv->getOwner()->getPopups().updateFormTabular();
335 void InsetTabular::InsetUnlock(BufferView * bv)
337 if (the_locking_inset) {
338 the_locking_inset->InsetUnlock(bv);
339 the_locking_inset = 0;
342 if (hasSelection()) {
343 sel_pos_start = sel_pos_end = 0;
344 sel_cell_start = sel_cell_end = 0;
345 UpdateLocal(bv, FULL, false);
347 no_selection = false;
353 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
357 bv->updateInset(this, mark_dirty);
363 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
365 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
369 if (inset == tabular->GetCellInset(actcell)) {
370 lyxerr[Debug::INSETS] << "OK" << endl;
371 the_locking_inset = tabular->GetCellInset(actcell);
373 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
374 inset_y = cursor.y();
375 inset_pos = cursor.pos();
377 } else if (the_locking_inset && (the_locking_inset == inset)) {
378 if (cursor.pos() == inset_pos) {
379 lyxerr[Debug::INSETS] << "OK" << endl;
381 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
382 inset_y = cursor.y();
384 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
386 } else if (the_locking_inset) {
387 lyxerr[Debug::INSETS] << "MAYBE" << endl;
388 return the_locking_inset->LockInsetInInset(bv, inset);
390 lyxerr[Debug::INSETS] << "NOT OK" << endl;
395 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
398 if (!the_locking_inset)
400 if (the_locking_inset == inset) {
401 the_locking_inset->InsetUnlock(bv);
402 the_locking_inset = 0;
404 moveRight(bv, false);
405 UpdateLocal(bv, CELL, false);
408 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
409 if ((inset->LyxCode() == TABULAR_CODE) &&
410 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
412 dialogs_ = bv->owner()->getDialogs();
413 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
422 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
424 if (!the_locking_inset)
426 if (the_locking_inset != inset)
427 return the_locking_inset->UpdateInsetInInset(bv, inset);
428 UpdateLocal(bv, CELL, false);
433 int InsetTabular::InsetInInsetY()
435 if (!the_locking_inset)
438 return (inset_y + the_locking_inset->InsetInInsetY());
442 UpdatableInset * InsetTabular::GetLockingInset()
444 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
448 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
452 if (the_locking_inset)
453 return the_locking_inset->GetFirstLockingInsetOfType(c);
458 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
460 if (the_locking_inset)
461 return the_locking_inset->InsertInset(bv, inset);
466 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
468 if (hasSelection()) {
469 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
470 UpdateLocal(bv, SELECTION, false);
472 no_selection = false;
477 sel_pos_start = sel_pos_end = cursor.pos();
478 sel_cell_start = sel_cell_end = actcell;
480 bool inset_hit = InsetHit(bv, x, y);
482 if ((ocell == actcell) && the_locking_inset && inset_hit) {
483 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
485 } else if (the_locking_inset) {
486 the_locking_inset->InsetUnlock(bv);
488 the_locking_inset = 0;
489 if (inset_hit && bv->the_locking_inset) {
490 ActivateCellInset(bv, x, y, button);
491 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
496 void InsetTabular::InsetButtonRelease(BufferView * bv,
497 int x, int y, int button)
500 if (the_locking_inset) {
502 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
503 i->InsetButtonRelease(bv, x, y, button);
507 dialogs_ = bv->owner()->getDialogs();
508 dialogs_->showTabular(this);
510 else if (ocell != actcell)
511 bview->getOwner()->getPopups().updateTabular();
515 if (the_locking_inset) {
516 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
519 no_selection = false;
523 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
525 if (the_locking_inset) {
526 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
527 y - inset_y, button);
531 // int ocell = actcell,
532 int old = sel_pos_end;
535 sel_pos_end = cursor.pos();
536 sel_cell_end = actcell;
537 if (old != sel_pos_end)
538 UpdateLocal(bv, SELECTION, false);
540 if (ocell != actcell)
541 bview->getOwner()->getPopups().updateFormTabular();
544 no_selection = false;
548 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
550 if (the_locking_inset) {
551 the_locking_inset->InsetKeyPress(xke);
557 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
560 UpdatableInset::RESULT
563 no_selection = false;
564 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
565 || (result == DISPATCHED_NOUPDATE)) {
571 if ((action < 0) && arg.empty())
574 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
575 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
577 if (the_locking_inset) {
578 result=the_locking_inset->LocalDispatch(bv, action, arg);
579 if (result == DISPATCHED_NOUPDATE)
581 else if (result == DISPATCHED) {
582 the_locking_inset->ToggleInsetCursor(bv);
583 UpdateLocal(bv, CELL, false);
584 the_locking_inset->ToggleInsetCursor(bv);
586 } else if (result == FINISHED) {
587 if ((action == LFUN_RIGHT) || (action == -1)) {
588 cursor.pos(inset_pos + 1);
591 sel_pos_start = sel_pos_end = cursor.pos();
592 sel_cell_start = sel_cell_end = actcell;
599 bool hs = hasSelection();
603 // Normal chars not handled here
606 // --- Cursor Movements ---------------------------------------------
608 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
610 moveRight(bv, false);
611 sel_pos_end = cursor.pos();
612 if (!cellstart(cursor.pos())) {
613 if (tabular->right_column_of_cell(sel_cell_start) >
614 tabular->right_column_of_cell(actcell))
615 sel_cell_end = actcell+1;
617 sel_cell_end = actcell;
619 UpdateLocal(bv, SELECTION, false);
622 result = moveRight(bv);
623 sel_pos_start = sel_pos_end = cursor.pos();
624 sel_cell_start = sel_cell_end = actcell;
626 UpdateLocal(bv, SELECTION, false);
629 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
632 sel_pos_end = cursor.pos();
633 if (cellstart(cursor.pos())) {
634 if (tabular->column_of_cell(sel_cell_start) >=
635 tabular->column_of_cell(actcell))
636 sel_cell_end = actcell;
638 sel_cell_end = actcell-1;
640 UpdateLocal(bv, SELECTION, false);
643 result = moveLeft(bv);
644 sel_pos_start = sel_pos_end = cursor.pos();
645 sel_cell_start = sel_cell_end = actcell;
647 UpdateLocal(bv, SELECTION, false);
653 sel_pos_end = cursor.pos();
654 if ((ocell == sel_cell_end) ||
655 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
656 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
658 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
659 UpdateLocal(bv, SELECTION, false);
663 result= moveDown(bv);
664 sel_pos_start = sel_pos_end = cursor.pos();
665 sel_cell_start = sel_cell_end = actcell;
667 UpdateLocal(bv, SELECTION, false);
673 sel_pos_end = cursor.pos();
674 if ((ocell == sel_cell_end) ||
675 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
676 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
678 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
679 UpdateLocal(bv, SELECTION, false);
684 sel_pos_start = sel_pos_end = cursor.pos();
685 sel_cell_start = sel_cell_end = actcell;
687 UpdateLocal(bv, SELECTION, false);
699 if (the_locking_inset) {
700 UnlockInsetInInset(bv, the_locking_inset);
701 the_locking_inset = 0;
703 if (action == LFUN_TAB)
707 sel_pos_start = sel_pos_end = cursor.pos();
708 sel_cell_start = sel_cell_end = actcell;
710 UpdateLocal(bv, SELECTION, false);
712 case LFUN_LAYOUT_TABLE:
714 dialogs_ = bv->owner()->getDialogs();
715 dialogs_->showTabular(this);
719 result = UNDISPATCHED;
722 if (result!=FINISHED) {
723 if (!the_locking_inset) {
725 if (ocell != actcell)
726 bview->getOwner()->getPopups().updateFormTabular();
731 bv->unlockInset(this);
736 int InsetTabular::Latex(Buffer const * buf, ostream & os,
737 bool fragile, bool fp) const
739 return tabular->Latex(buf, os, fragile, fp);
743 int InsetTabular::Ascii(Buffer const *, ostream &) const
749 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
755 int InsetTabular::DocBook(Buffer const *, ostream &) const
761 void InsetTabular::Validate(LaTeXFeatures & features) const
763 tabular->Validate(features);
767 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
768 LyXFont const & font,
774 bool changed = false;
776 for(int i = 0; i < tabular->rows(); ++i) {
777 maxAsc = maxDesc = 0;
778 for(int j= 0; j < tabular->columns(); ++j) {
779 if (tabular->IsPartOfMultiColumn(i,j))
782 inset = tabular->GetCellInset(cell);
784 inset->update(bv, font, false);
785 maxAsc = max(maxAsc, inset->ascent(bv, font));
786 maxDesc = max(maxDesc, inset->descent(bv, font));
787 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
789 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
790 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
796 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
798 x = cursor.x() - top_x;
803 void InsetTabular::ToggleInsetCursor(BufferView * bv)
805 if (the_locking_inset) {
806 the_locking_inset->ToggleInsetCursor(bv);
810 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
812 int asc = lyxfont::maxAscent(font);
813 int desc = lyxfont::maxDescent(font);
816 bv->hideLockedInsetCursor();
818 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
819 cursor_visible = !cursor_visible;
823 void InsetTabular::ShowInsetCursor(BufferView * bv)
825 if (!cursor_visible) {
826 LyXFont font; // = GetFont(par, cursor.pos);
828 int asc = lyxfont::maxAscent(font);
829 int desc = lyxfont::maxDescent(font);
830 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
831 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
832 cursor_visible = true;
837 void InsetTabular::HideInsetCursor(BufferView * bv)
840 ToggleInsetCursor(bv);
844 void InsetTabular::setPos(BufferView * bv, int x, int y) const
849 actcell = actrow = actcol = 0;
850 int ly = tabular->GetDescentOfRow(actrow);
852 // first search the right row
853 while((ly < y) && (actrow < tabular->rows())) {
854 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
855 tabular->GetAscentOfRow(actrow+1) +
856 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
859 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
861 actcell = tabular->GetCellNumber(actrow, actcol);
863 // now search the right column
864 int lx = tabular->GetWidthOfColumn(actcell) -
865 tabular->GetAdditionalWidth(actcell);
866 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
867 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
868 tabular->GetAdditionalWidth(actcell - 1));
869 cursor.pos(((actcell+1) * 2) - 1);
871 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
872 cursor.x(lx + top_x - 2);
874 cursor.pos(cursor.pos() - 1);
875 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
881 int InsetTabular::getCellXPos(int cell) const
885 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
887 int lx = tabular->GetWidthOfColumn(cell);
888 for(; (c < cell); ++c) {
889 lx += tabular->GetWidthOfColumn(c);
891 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
892 ADD_TO_TABULAR_WIDTH);
896 void InsetTabular::resetPos(BufferView * bv) const
900 actcol = tabular->column_of_cell(actcell);
905 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
906 if (tabular->IsLastCellInRow(cell)) {
907 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
908 tabular->GetAscentOfRow(actrow + 1) +
909 tabular->GetAdditionalHeight(cell + 1));
913 cursor.x(getCellXPos(actcell) + 2);
914 if (cursor.pos() % 2) {
915 LyXFont font(LyXFont::ALL_SANE);
916 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
917 tabular->GetBeginningOfTextInCell(actcell));
919 if ((!the_locking_inset ||
920 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
921 (actcell != oldcell)) {
922 dialogs_ = bv->owner()->getDialogs();
923 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
929 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
931 if (!cellstart(cursor.pos())) {
932 if (tabular->IsLastCell(actcell))
935 cursor.pos(cursor.pos() + 1);
937 if (ActivateCellInset(bv))
939 } else { // before the inset
940 cursor.pos(cursor.pos() + 1);
943 return DISPATCHED_NOUPDATE;
947 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
954 cursor.pos(cursor.pos() - 1);
955 if (!cellstart(cursor.pos())) {
957 } else if (lock) { // behind the inset
958 if (ActivateCellInset(bv, 0, 0, 0, true))
962 return DISPATCHED_NOUPDATE;
966 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
969 actcell = tabular->GetCellAbove(actcell);
970 if (actcell == ocell) // we moved out of the inset
973 return DISPATCHED_NOUPDATE;
977 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
980 actcell = tabular->GetCellBelow(actcell);
981 if (actcell == ocell) // we moved out of the inset
984 return DISPATCHED_NOUPDATE;
988 bool InsetTabular::moveNextCell(BufferView * bv)
990 if (tabular->IsLastCell(actcell))
993 cursor.pos(cursor.pos() + 1);
994 if (!cellstart(cursor.pos()))
995 cursor.pos(cursor.pos() + 1);
1001 bool InsetTabular::movePrevCell(BufferView * bv)
1003 if (!actcell) // first cell
1006 cursor.pos(cursor.pos() - 1);
1007 if (cellstart(cursor.pos()))
1008 cursor.pos(cursor.pos() - 1);
1014 bool InsetTabular::Delete()
1020 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1022 if (the_locking_inset)
1023 the_locking_inset->SetFont(bv, font, tall);
1027 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1036 setAlign = LYX_ALIGN_LEFT,
1042 case LyXTabular::M_ALIGN_LEFT:
1043 case LyXTabular::ALIGN_LEFT:
1044 setAlign=LYX_ALIGN_LEFT;
1046 case LyXTabular::M_ALIGN_RIGHT:
1047 case LyXTabular::ALIGN_RIGHT:
1048 setAlign=LYX_ALIGN_RIGHT;
1050 case LyXTabular::M_ALIGN_CENTER:
1051 case LyXTabular::ALIGN_CENTER:
1052 setAlign=LYX_ALIGN_CENTER;
1054 case LyXTabular::M_VALIGN_TOP:
1055 case LyXTabular::VALIGN_TOP:
1056 setAlign=LyXTabular::LYX_VALIGN_TOP;
1058 case LyXTabular::M_VALIGN_BOTTOM:
1059 case LyXTabular::VALIGN_BOTTOM:
1060 setAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1062 case LyXTabular::M_VALIGN_CENTER:
1063 case LyXTabular::VALIGN_CENTER:
1064 setAlign=LyXTabular::LYX_VALIGN_CENTER;
1069 if (hasSelection()) {
1071 sel_col_start = tabular->column_of_cell(sel_cell_start);
1072 sel_col_end = tabular->column_of_cell(sel_cell_end);
1073 if (sel_col_start > sel_col_end) {
1074 sel_col_end = sel_col_start;
1075 sel_col_start = tabular->column_of_cell(sel_cell_end);
1077 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1080 sel_row_start = tabular->row_of_cell(sel_cell_start);
1081 sel_row_end = tabular->row_of_cell(sel_cell_end);
1082 if (sel_row_start > sel_row_end) {
1083 tmp = sel_row_start;
1084 sel_row_start = sel_row_end;
1088 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1089 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1091 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1092 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1093 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1095 int row = tabular->row_of_cell(actcell);
1096 int column = tabular->column_of_cell(actcell);
1100 case LyXTabular::SET_PWIDTH:
1102 bool update = (tabular->GetColumnPWidth(actcell) != val);
1103 tabular->SetColumnPWidth(actcell,val);
1105 for (int i=0; i < tabular->rows(); ++i) {
1106 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1109 UpdateLocal(bv, INIT, true);
1113 case LyXTabular::SET_MPWIDTH:
1115 bool update = (tabular->GetPWidth(actcell) != val);
1116 tabular->SetMColumnPWidth(actcell,val);
1118 for (int i=0; i < tabular->rows(); ++i) {
1119 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1122 UpdateLocal(bv, INIT, true);
1126 case LyXTabular::SET_SPECIAL_COLUMN:
1127 case LyXTabular::SET_SPECIAL_MULTI:
1128 tabular->SetAlignSpecial(actcell,val,feature);
1130 case LyXTabular::APPEND_ROW:
1131 // append the row into the tabular
1132 UnlockInsetInInset(bv, the_locking_inset);
1133 tabular->AppendRow(actcell);
1134 UpdateLocal(bv, INIT, true);
1136 case LyXTabular::APPEND_COLUMN:
1137 // append the column into the tabular
1138 tabular->AppendColumn(actcell);
1139 actcell = tabular->GetCellNumber(row, column);
1140 UpdateLocal(bv, INIT, true);
1142 case LyXTabular::DELETE_ROW:
1143 tabular->DeleteRow(tabular->row_of_cell(actcell));
1144 if ((row+1) > tabular->rows())
1146 actcell = tabular->GetCellNumber(row, column);
1147 UpdateLocal(bv, INIT, true);
1149 case LyXTabular::DELETE_COLUMN:
1150 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1151 if ((column+1) > tabular->columns())
1153 actcell = tabular->GetCellNumber(row, column);
1154 UpdateLocal(bv, INIT, true);
1156 case LyXTabular::M_TOGGLE_LINE_TOP:
1158 case LyXTabular::TOGGLE_LINE_TOP:
1159 lineSet = !tabular->TopLine(actcell, flag);
1160 for(i=sel_row_start; i<=sel_row_end; ++i)
1161 for(j=sel_col_start; j<=sel_col_end; ++j)
1162 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1163 UpdateLocal(bv, INIT, true);
1166 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1168 case LyXTabular::TOGGLE_LINE_BOTTOM:
1169 lineSet = !tabular->BottomLine(actcell, flag);
1170 for(i=sel_row_start; i<=sel_row_end; ++i)
1171 for(j=sel_col_start; j<=sel_col_end; ++j)
1172 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1174 UpdateLocal(bv, INIT, true);
1177 case LyXTabular::M_TOGGLE_LINE_LEFT:
1179 case LyXTabular::TOGGLE_LINE_LEFT:
1180 lineSet = !tabular->LeftLine(actcell, flag);
1181 for(i=sel_row_start; i<=sel_row_end; ++i)
1182 for(j=sel_col_start; j<=sel_col_end; ++j)
1183 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1185 UpdateLocal(bv, INIT, true);
1188 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1190 case LyXTabular::TOGGLE_LINE_RIGHT:
1191 lineSet = !tabular->RightLine(actcell, flag);
1192 for(i=sel_row_start; i<=sel_row_end; ++i)
1193 for(j=sel_col_start; j<=sel_col_end; ++j)
1194 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1196 UpdateLocal(bv, INIT, true);
1198 case LyXTabular::M_ALIGN_LEFT:
1199 case LyXTabular::M_ALIGN_RIGHT:
1200 case LyXTabular::M_ALIGN_CENTER:
1202 case LyXTabular::ALIGN_LEFT:
1203 case LyXTabular::ALIGN_RIGHT:
1204 case LyXTabular::ALIGN_CENTER:
1205 for(i=sel_row_start; i<=sel_row_end; ++i)
1206 for(j=sel_col_start; j<=sel_col_end; ++j)
1207 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign,
1210 UpdateLocal(bv, INIT, true);
1212 UpdateLocal(bv, CELL, true);
1214 case LyXTabular::M_VALIGN_TOP:
1215 case LyXTabular::M_VALIGN_BOTTOM:
1216 case LyXTabular::M_VALIGN_CENTER:
1218 case LyXTabular::VALIGN_TOP:
1219 case LyXTabular::VALIGN_BOTTOM:
1220 case LyXTabular::VALIGN_CENTER:
1221 for(i=sel_row_start; i<=sel_row_end; ++i)
1222 for(j=sel_col_start; j<=sel_col_end; ++j)
1223 tabular->SetVAlignment(tabular->GetCellNumber(i,j), setAlign,
1226 UpdateLocal(bv, INIT, true);
1228 UpdateLocal(bv, CELL, true);
1230 case LyXTabular::MULTICOLUMN:
1232 if (sel_row_start != sel_row_end) {
1233 WriteAlert(_("Impossible Operation!"),
1234 _("Multicolumns can only be horizontally."),
1238 // just multicol for one Single Cell
1239 if (!hasSelection()) {
1240 // check wether we are completly in a multicol
1241 if (tabular->IsMultiColumn(actcell)) {
1242 tabular->UnsetMultiColumn(actcell);
1243 UpdateLocal(bv, INIT, true);
1245 tabular->SetMultiColumn(actcell, 1);
1246 UpdateLocal(bv, CELL, true);
1250 // we have a selection so this means we just add all this
1251 // cells to form a multicolumn cell
1255 if (sel_cell_start > sel_cell_end) {
1256 s_start = sel_cell_end;
1257 s_end = sel_cell_start;
1259 s_start = sel_cell_start;
1260 s_end = sel_cell_end;
1262 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1265 sel_cell_end = sel_cell_start;
1266 sel_pos_end = sel_pos_start;
1267 UpdateLocal(bv, INIT, true);
1270 case LyXTabular::SET_ALL_LINES:
1272 case LyXTabular::UNSET_ALL_LINES:
1273 for(i=sel_row_start; i<=sel_row_end; ++i)
1274 for(j=sel_col_start; j<=sel_col_end; ++j)
1275 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1276 UpdateLocal(bv, INIT, true);
1278 case LyXTabular::SET_LONGTABULAR:
1279 tabular->SetLongTabular(true);
1280 UpdateLocal(bv, INIT, true); // because this toggles displayed
1282 case LyXTabular::UNSET_LONGTABULAR:
1283 tabular->SetLongTabular(false);
1284 UpdateLocal(bv, INIT, true); // because this toggles displayed
1286 case LyXTabular::SET_ROTATE_TABULAR:
1287 tabular->SetRotateTabular(true);
1289 case LyXTabular::UNSET_ROTATE_TABULAR:
1290 tabular->SetRotateTabular(false);
1292 case LyXTabular::SET_ROTATE_CELL:
1293 for(i=sel_row_start; i<=sel_row_end; ++i)
1294 for(j=sel_col_start; j<=sel_col_end; ++j)
1295 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1297 case LyXTabular::UNSET_ROTATE_CELL:
1298 for(i=sel_row_start; i<=sel_row_end; ++i)
1299 for(j=sel_col_start; j<=sel_col_end; ++j)
1300 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1302 case LyXTabular::SET_LINEBREAKS:
1303 what = !tabular->GetLinebreaks(actcell);
1304 for(i=sel_row_start; i<=sel_row_end; ++i)
1305 for(j=sel_col_start; j<=sel_col_end; ++j)
1306 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1308 case LyXTabular::SET_LTFIRSTHEAD:
1309 tabular->SetLTHead(actcell,true);
1311 case LyXTabular::SET_LTHEAD:
1312 tabular->SetLTHead(actcell,false);
1314 case LyXTabular::SET_LTFOOT:
1315 tabular->SetLTFoot(actcell,false);
1317 case LyXTabular::SET_LTLASTFOOT:
1318 tabular->SetLTFoot(actcell,true);
1320 case LyXTabular::SET_LTNEWPAGE:
1321 what = !tabular->GetLTNewPage(actcell);
1322 tabular->SetLTNewPage(actcell,what);
1328 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1331 // the cursor.pos has to be before the inset so if it isn't now just
1332 // reset the curor pos first!
1333 if (cursor.pos() % 2) { // behind the inset
1334 cursor.pos(cursor.pos() - 1);
1337 UpdatableInset * inset =
1338 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1339 LyXFont font(LyXFont::ALL_SANE);
1341 x = inset->x() + inset->width(bv, font);
1342 y = inset->descent(bv, font);
1344 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1345 inset_y = cursor.y();
1346 inset->Edit(bv, x - inset_x, y - inset_y, button);
1347 if (!the_locking_inset)
1349 UpdateLocal(bv, CELL, false);
1354 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1356 InsetText * inset = tabular->GetCellInset(actcell);
1359 if (cursor.pos() % 2) { // behind the inset
1360 return (((x + top_x) < cursor.x()) &&
1361 ((x + top_x) > (cursor.x() - inset->width(bv,
1362 LyXFont(LyXFont::ALL_SANE)))));
1364 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1365 return ((x1 > x2) &&
1366 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1371 // This returns paperWidth() if the cell-width is unlimited or the width
1372 // in pixels if we have a pwidth for this cell.
1373 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1375 string s = tabular->GetPWidth(cell);
1379 return VSpace(s).inPixels( 0, 0);
1383 int InsetTabular::getMaxWidth(Painter & pain,
1384 UpdatableInset const * inset) const
1387 int n = tabular->GetNumberOfCells();
1388 for(cell=0; cell < n; ++cell) {
1389 if (tabular->GetCellInset(cell) == inset)
1394 int w = GetMaxWidthOfCell(pain, cell);
1395 // this because text insets remove the xpos from the maxwidth because
1396 // otherwise the would not break good!!!
1397 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1403 void InsetTabular::recomputeTextInsets(BufferView * bv,
1404 LyXFont const & font) const
1410 for(int j= 0; j < tabular->columns(); ++j) {
1411 for(int i = 0; i < tabular->rows(); ++i) {
1412 if (tabular->IsPartOfMultiColumn(i,j))
1414 cell = tabular->GetCellNumber(i,j);
1415 inset = tabular->GetCellInset(cell);
1416 inset->update(bv, font);
1417 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1419 // cell = tabular->GetCellNumber(0, j);
1420 // cx += tabular->GetWidthOfColumn(cell);
1425 void InsetTabular::resizeLyXText(BufferView *) const
1430 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1432 if (the_locking_inset)
1433 return the_locking_inset->getLyXText(bv);
1434 return Inset::getLyXText(bv);