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"
34 #include "frontends/Dialogs.h"
36 const int ADD_TO_HEIGHT = 2;
37 const int ADD_TO_TABULAR_WIDTH = 2;
44 #define cellstart(p) ((p % 2) == 0)
46 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
52 buffer = buf; // set this first!!!
53 tabular = new LyXTabular(this, rows,columns);
54 // for now make it always display as display() inset
56 the_locking_inset = 0;
57 locked = no_selection = cursor_visible = false;
62 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
68 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
70 buffer = buf; // set this first
71 tabular = new LyXTabular(this, *(tab.tabular));
72 the_locking_inset = 0;
73 locked = no_selection = cursor_visible = false;
78 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
84 InsetTabular::~InsetTabular()
88 dialogs_->hideTabular(this);
92 Inset * InsetTabular::Clone() const
94 InsetTabular * t = new InsetTabular(*this, buffer);
96 t->tabular = tabular->Clone(t);
101 void InsetTabular::Write(Buffer const * buf, ostream & os) const
103 os << " Tabular" << endl;
104 tabular->Write(buf, os);
108 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
110 bool old_format = (lex.GetString() == "\\LyXTable");
115 tabular = new LyXTabular(buf, this, lex);
123 token = lex.GetString();
124 while (lex.IsOK() && (token != "\\end_inset")) {
126 token = lex.GetString();
128 if (token != "\\end_inset") {
129 lex.printError("Missing \\end_inset at this point. "
135 int InsetTabular::ascent(BufferView *, LyXFont const &) const
137 return tabular->GetAscentOfRow(0);
141 int InsetTabular::descent(BufferView *, LyXFont const &) const
143 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
147 int InsetTabular::width(BufferView *, LyXFont const &) const
149 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
153 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
154 float & x, bool cleared) const
156 Painter & pain = bv->painter();
161 UpdatableInset::draw(bv,font,baseline,x,cleared);
162 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
163 (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);
180 top_baseline = baseline;
182 x += ADD_TO_TABULAR_WIDTH;
183 if (cleared || (need_update == FULL) || (need_update == CELL)) {
184 for(i=0;i<tabular->rows();++i) {
186 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
187 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
188 for(j=0;j<tabular->columns();++j) {
189 if (tabular->IsPartOfMultiColumn(i,j))
191 cx = nx + tabular->GetBeginningOfTextInCell(cell);
193 DrawCellSelection(pain, nx, baseline, i, j, cell);
194 if (dodraw && !cleared && locked && the_locking_inset) {
195 if (the_locking_inset == tabular->GetCellInset(cell))
196 tabular->GetCellInset(cell)->draw(bv, font,
200 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
202 DrawCellLines(pain, nx, baseline, i, cell);
204 nx += tabular->GetWidthOfColumn(cell);
207 baseline += tabular->GetDescentOfRow(i) +
208 tabular->GetAscentOfRow(i+1)+
209 tabular->GetAdditionalHeight(cell+1);
212 x += width(bv, font);
217 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
218 int row, int cell) const
220 int x2 = x + tabular->GetWidthOfColumn(cell);
223 if (!tabular->TopAlreadyDrawed(cell)) {
224 on_off = !tabular->TopLine(cell);
225 pain.line(x, baseline - tabular->GetAscentOfRow(row),
226 x2, baseline - tabular->GetAscentOfRow(row),
227 on_off ? LColor::tabularonoffline:LColor::tabularline,
228 on_off ? Painter::line_onoffdash:Painter::line_solid);
230 on_off = !tabular->BottomLine(cell);
231 pain.line(x,baseline + tabular->GetDescentOfRow(row),
232 x2, baseline + tabular->GetDescentOfRow(row),
233 on_off ? LColor::tabularonoffline:LColor::tabularline,
234 on_off ? Painter::line_onoffdash:Painter::line_solid);
235 if (!tabular->LeftAlreadyDrawed(cell)) {
236 on_off = !tabular->LeftLine(cell);
237 pain.line(x, baseline - tabular->GetAscentOfRow(row),
238 x, baseline + tabular->GetDescentOfRow(row),
239 on_off ? LColor::tabularonoffline:LColor::tabularline,
240 on_off ? Painter::line_onoffdash:Painter::line_solid);
242 on_off = !tabular->RightLine(cell);
243 pain.line(x2 - tabular->GetAdditionalWidth(cell),
244 baseline - tabular->GetAscentOfRow(row),
245 x2 - tabular->GetAdditionalWidth(cell),
246 baseline + tabular->GetDescentOfRow(row),
247 on_off ? LColor::tabularonoffline:LColor::tabularline,
248 on_off ? Painter::line_onoffdash:Painter::line_solid);
252 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
253 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);
274 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
275 int w = tabular->GetWidthOfColumn(cell);
276 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
277 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
278 w, h, LColor::selection);
283 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
287 calculate_dimensions_of_cells(bv, font, true);
289 owner()->update(bv, font, true);
292 if (the_locking_inset)
293 the_locking_inset->update(bv, font, reinit);
294 switch(need_update) {
298 if (calculate_dimensions_of_cells(bv, font, false))
310 char const * InsetTabular::EditMessage() const
312 return _("Opened Tabular Inset");
316 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
318 UpdatableInset::Edit(bv, x, y, button);
320 if (!bv->lockInset(this)) {
321 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
325 the_locking_inset = 0;
326 inset_pos = inset_x = inset_y = 0;
328 sel_pos_start = sel_pos_end = cursor.pos();
329 sel_cell_start = sel_cell_end = actcell;
330 bv->text->FinishUndo();
331 if (InsetHit(bv, x, y)) {
332 ActivateCellInset(bv, x, y, button);
334 UpdateLocal(bv, NONE, false);
335 // bv->getOwner()->getPopups().updateFormTabular();
339 void InsetTabular::InsetUnlock(BufferView * bv)
341 if (the_locking_inset) {
342 the_locking_inset->InsetUnlock(bv);
343 the_locking_inset = 0;
346 if (hasSelection()) {
347 sel_pos_start = sel_pos_end = 0;
348 sel_cell_start = sel_cell_end = 0;
349 UpdateLocal(bv, FULL, false);
351 no_selection = false;
356 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
360 bv->updateInset(this, mark_dirty);
365 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
367 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
371 if (inset == tabular->GetCellInset(actcell)) {
372 lyxerr[Debug::INSETS] << "OK" << endl;
373 the_locking_inset = tabular->GetCellInset(actcell);
375 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
376 inset_y = cursor.y();
377 inset_pos = cursor.pos();
379 } else if (the_locking_inset && (the_locking_inset == inset)) {
380 if (cursor.pos() == inset_pos) {
381 lyxerr[Debug::INSETS] << "OK" << endl;
383 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
384 inset_y = cursor.y();
386 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
388 } else if (the_locking_inset) {
389 lyxerr[Debug::INSETS] << "MAYBE" << endl;
390 return the_locking_inset->LockInsetInInset(bv, inset);
392 lyxerr[Debug::INSETS] << "NOT OK" << endl;
396 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
399 if (!the_locking_inset)
401 if (the_locking_inset == inset) {
402 the_locking_inset->InsetUnlock(bv);
403 the_locking_inset = 0;
405 moveRight(bv, false);
406 UpdateLocal(bv, CELL, false);
409 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
410 if ((inset->LyxCode() == TABULAR_CODE) &&
411 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
413 dialogs_ = bv->owner()->getDialogs();
414 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)) {
572 if ((action < 0) && arg.empty())
575 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
576 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
578 if (the_locking_inset) {
579 result=the_locking_inset->LocalDispatch(bv, action, arg);
580 if (result == DISPATCHED_NOUPDATE)
582 else if (result == DISPATCHED) {
583 the_locking_inset->ToggleInsetCursor(bv);
584 UpdateLocal(bv, CELL, false);
585 the_locking_inset->ToggleInsetCursor(bv);
587 } else if (result == FINISHED) {
588 if ((action == LFUN_RIGHT) || (action == -1)) {
589 cursor.pos(inset_pos + 1);
592 sel_pos_start = sel_pos_end = cursor.pos();
593 sel_cell_start = sel_cell_end = actcell;
600 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, CURSOR, 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, CURSOR, 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, CURSOR, 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, CURSOR, false);
699 if (the_locking_inset) {
700 the_locking_inset->InsetUnlock(bv);
702 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, CURSOR, 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, bool fragile, bool fp) const
738 return tabular->Latex(buf, os, fragile, fp);
742 int InsetTabular::Ascii(Buffer const *, ostream &) const
747 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
753 int InsetTabular::DocBook(Buffer const *, ostream &) const
759 void InsetTabular::Validate(LaTeXFeatures & features) const
761 tabular->Validate(features);
765 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
766 LyXFont const & font,
772 bool changed = false;
774 for(int i = 0; i < tabular->rows(); ++i) {
775 maxAsc = maxDesc = 0;
776 for(int j= 0; j < tabular->columns(); ++j) {
777 if (tabular->IsPartOfMultiColumn(i,j))
780 inset = tabular->GetCellInset(cell);
782 inset->update(bv, font, false);
783 maxAsc = max(maxAsc, inset->ascent(bv, font));
784 maxDesc = max(maxDesc, inset->descent(bv, font));
785 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
787 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
788 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
794 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
796 x = cursor.x() - top_x;
801 void InsetTabular::ToggleInsetCursor(BufferView * bv)
803 if (the_locking_inset) {
804 the_locking_inset->ToggleInsetCursor(bv);
808 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
810 int asc = lyxfont::maxAscent(font);
811 int desc = lyxfont::maxDescent(font);
814 bv->hideLockedInsetCursor();
816 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
817 cursor_visible = !cursor_visible;
821 void InsetTabular::ShowInsetCursor(BufferView * bv)
823 if (!cursor_visible) {
824 LyXFont font; // = GetFont(par, cursor.pos);
826 int asc = lyxfont::maxAscent(font);
827 int desc = lyxfont::maxDescent(font);
828 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
829 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
830 cursor_visible = true;
835 void InsetTabular::HideInsetCursor(BufferView * bv)
838 ToggleInsetCursor(bv);
842 void InsetTabular::setPos(BufferView * bv, int x, int y) const
847 actcell = actrow = actcol = 0;
848 int ly = tabular->GetDescentOfRow(actrow);
850 // first search the right row
851 while((ly < y) && (actrow < tabular->rows())) {
852 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
853 tabular->GetAscentOfRow(actrow+1) +
854 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
857 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
859 actcell = tabular->GetCellNumber(actrow, actcol);
861 // now search the right column
862 int lx = tabular->GetWidthOfColumn(actcell) -
863 tabular->GetAdditionalWidth(actcell);
864 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
865 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
866 tabular->GetAdditionalWidth(actcell - 1));
867 cursor.pos(((actcell+1) * 2) - 1);
869 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
870 cursor.x(lx + top_x - 2);
872 cursor.pos(cursor.pos() - 1);
873 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
878 int InsetTabular::getCellXPos(int cell) const
882 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
884 int lx = tabular->GetWidthOfColumn(cell);
885 for(; (c < cell); ++c) {
886 lx += tabular->GetWidthOfColumn(c);
888 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
889 ADD_TO_TABULAR_WIDTH);
892 void InsetTabular::resetPos(BufferView * bv) const
896 actcol = tabular->column_of_cell(actcell);
901 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
902 if (tabular->IsLastCellInRow(cell)) {
903 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
904 tabular->GetAscentOfRow(actrow + 1) +
905 tabular->GetAdditionalHeight(cell + 1));
909 cursor.x(getCellXPos(actcell) + 2);
910 if (cursor.pos() % 2) {
911 LyXFont font(LyXFont::ALL_SANE);
912 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
913 tabular->GetBeginningOfTextInCell(actcell));
915 if ((!the_locking_inset ||
916 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
917 (actcell != oldcell)) {
918 dialogs_ = bv->owner()->getDialogs();
919 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
925 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
927 if (!cellstart(cursor.pos())) {
928 if (tabular->IsLastCell(actcell))
931 cursor.pos(cursor.pos() + 1);
933 if (ActivateCellInset(bv))
935 } else { // before the inset
936 cursor.pos(cursor.pos() + 1);
939 return DISPATCHED_NOUPDATE;
943 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
950 cursor.pos(cursor.pos() - 1);
951 if (!cellstart(cursor.pos())) {
953 } else if (lock) { // behind the inset
954 if (ActivateCellInset(bv, 0, 0, 0, true))
958 return DISPATCHED_NOUPDATE;
962 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
965 actcell = tabular->GetCellAbove(actcell);
966 if (actcell == ocell) // we moved out of the inset
969 return DISPATCHED_NOUPDATE;
973 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
976 actcell = tabular->GetCellBelow(actcell);
977 if (actcell == ocell) // we moved out of the inset
980 return DISPATCHED_NOUPDATE;
984 bool InsetTabular::moveNextCell(BufferView * bv)
986 if (tabular->IsLastCell(actcell))
989 cursor.pos(cursor.pos() + 1);
990 if (!cellstart(cursor.pos()))
991 cursor.pos(cursor.pos() + 1);
997 bool InsetTabular::movePrevCell(BufferView * bv)
999 if (!actcell) // first cell
1002 cursor.pos(cursor.pos() - 1);
1003 if (cellstart(cursor.pos()))
1004 cursor.pos(cursor.pos() - 1);
1010 bool InsetTabular::Delete()
1016 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1018 if (the_locking_inset)
1019 the_locking_inset->SetFont(bv, font, tall);
1023 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1032 setAlign = LYX_ALIGN_LEFT,
1038 case LyXTabular::ALIGN_LEFT:
1039 setAlign=LYX_ALIGN_LEFT;
1041 case LyXTabular::ALIGN_RIGHT:
1042 setAlign=LYX_ALIGN_RIGHT;
1044 case LyXTabular::ALIGN_CENTER:
1045 setAlign=LYX_ALIGN_CENTER;
1050 if (hasSelection()) {
1052 sel_col_start = tabular->column_of_cell(sel_cell_start);
1053 sel_col_end = tabular->column_of_cell(sel_cell_end);
1054 if (sel_col_start > sel_col_end) {
1055 sel_col_end = sel_col_start;
1056 sel_col_start = tabular->column_of_cell(sel_cell_end);
1058 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1061 sel_row_start = tabular->row_of_cell(sel_cell_start);
1062 sel_row_end = tabular->row_of_cell(sel_cell_end);
1063 if (sel_row_start > sel_row_end) {
1064 tmp = sel_row_start;
1065 sel_row_start = sel_row_end;
1069 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1070 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1072 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1073 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1074 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1076 int row = tabular->row_of_cell(actcell);
1077 int column = tabular->column_of_cell(actcell);
1080 case LyXTabular::SET_PWIDTH:
1082 bool update = (tabular->GetPWidth(actcell) != val);
1083 tabular->SetPWidth(actcell,val);
1085 for (int i=0; i < tabular->rows(); ++i) {
1086 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1089 UpdateLocal(bv, INIT, true);
1093 case LyXTabular::SET_SPECIAL_COLUMN:
1094 case LyXTabular::SET_SPECIAL_MULTI:
1095 tabular->SetAlignSpecial(actcell,val,feature);
1097 case LyXTabular::APPEND_ROW:
1098 // append the row into the tabular
1099 UnlockInsetInInset(bv, the_locking_inset);
1100 tabular->AppendRow(actcell);
1101 UpdateLocal(bv, INIT, true);
1103 case LyXTabular::APPEND_COLUMN:
1104 // append the column into the tabular
1105 tabular->AppendColumn(actcell);
1106 actcell = tabular->GetCellNumber(row, column);
1107 UpdateLocal(bv, INIT, true);
1109 case LyXTabular::DELETE_ROW:
1110 tabular->DeleteRow(tabular->row_of_cell(actcell));
1111 if ((row+1) > tabular->rows())
1113 actcell = tabular->GetCellNumber(row, column);
1114 UpdateLocal(bv, INIT, true);
1116 case LyXTabular::DELETE_COLUMN:
1117 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1118 if ((column+1) > tabular->columns())
1120 actcell = tabular->GetCellNumber(row, column);
1121 UpdateLocal(bv, INIT, true);
1123 case LyXTabular::TOGGLE_LINE_TOP:
1124 lineSet = !tabular->TopLine(actcell);
1125 for(i=sel_row_start; i<=sel_row_end; ++i)
1126 for(j=sel_col_start; j<=sel_col_end; ++j)
1127 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1128 UpdateLocal(bv, INIT, true);
1131 case LyXTabular::TOGGLE_LINE_BOTTOM:
1132 lineSet = !tabular->BottomLine(actcell);
1133 for(i=sel_row_start; i<=sel_row_end; ++i)
1134 for(j=sel_col_start; j<=sel_col_end; ++j)
1135 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1136 UpdateLocal(bv, INIT, true);
1139 case LyXTabular::TOGGLE_LINE_LEFT:
1140 lineSet = !tabular->LeftLine(actcell);
1141 for(i=sel_row_start; i<=sel_row_end; ++i)
1142 for(j=sel_col_start; j<=sel_col_end; ++j)
1143 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1144 UpdateLocal(bv, INIT, true);
1147 case LyXTabular::TOGGLE_LINE_RIGHT:
1148 lineSet = !tabular->RightLine(actcell);
1149 for(i=sel_row_start; i<=sel_row_end; ++i)
1150 for(j=sel_col_start; j<=sel_col_end; ++j)
1151 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1152 UpdateLocal(bv, INIT, true);
1154 case LyXTabular::ALIGN_LEFT:
1155 case LyXTabular::ALIGN_RIGHT:
1156 case LyXTabular::ALIGN_CENTER:
1157 for(i=sel_row_start; i<=sel_row_end; ++i)
1158 for(j=sel_col_start; j<=sel_col_end; ++j)
1159 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1161 UpdateLocal(bv, INIT, true);
1163 UpdateLocal(bv, CELL, true);
1165 case LyXTabular::MULTICOLUMN:
1167 if (sel_row_start != sel_row_end) {
1168 WriteAlert(_("Impossible Operation!"),
1169 _("Multicolumns can only be horizontally."),
1173 // just multicol for one Single Cell
1174 if (!hasSelection()) {
1175 // check wether we are completly in a multicol
1176 if (tabular->IsMultiColumn(actcell)) {
1177 tabular->UnsetMultiColumn(actcell);
1178 UpdateLocal(bv, INIT, true);
1180 tabular->SetMultiColumn(actcell, 1);
1181 UpdateLocal(bv, CELL, true);
1185 // we have a selection so this means we just add all this
1186 // cells to form a multicolumn cell
1190 if (sel_cell_start > sel_cell_end) {
1191 s_start = sel_cell_end;
1192 s_end = sel_cell_start;
1194 s_start = sel_cell_start;
1195 s_end = sel_cell_end;
1197 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1200 sel_cell_end = sel_cell_start;
1201 sel_pos_end = sel_pos_start;
1202 UpdateLocal(bv, INIT, true);
1205 case LyXTabular::SET_ALL_LINES:
1207 case LyXTabular::UNSET_ALL_LINES:
1208 for(i=sel_row_start; i<=sel_row_end; ++i)
1209 for(j=sel_col_start; j<=sel_col_end; ++j)
1210 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1211 UpdateLocal(bv, INIT, true);
1213 case LyXTabular::SET_LONGTABULAR:
1214 tabular->SetLongTabular(true);
1215 UpdateLocal(bv, INIT, true); // because this toggles displayed
1217 case LyXTabular::UNSET_LONGTABULAR:
1218 tabular->SetLongTabular(false);
1219 UpdateLocal(bv, INIT, true); // because this toggles displayed
1221 case LyXTabular::SET_ROTATE_TABULAR:
1222 tabular->SetRotateTabular(true);
1224 case LyXTabular::UNSET_ROTATE_TABULAR:
1225 tabular->SetRotateTabular(false);
1227 case LyXTabular::SET_ROTATE_CELL:
1228 for(i=sel_row_start; i<=sel_row_end; ++i)
1229 for(j=sel_col_start; j<=sel_col_end; ++j)
1230 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1232 case LyXTabular::UNSET_ROTATE_CELL:
1233 for(i=sel_row_start; i<=sel_row_end; ++i)
1234 for(j=sel_col_start; j<=sel_col_end; ++j)
1235 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1237 case LyXTabular::SET_LINEBREAKS:
1238 what = !tabular->GetLinebreaks(actcell);
1239 for(i=sel_row_start; i<=sel_row_end; ++i)
1240 for(j=sel_col_start; j<=sel_col_end; ++j)
1241 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1243 case LyXTabular::SET_LTFIRSTHEAD:
1244 tabular->SetLTHead(actcell,true);
1246 case LyXTabular::SET_LTHEAD:
1247 tabular->SetLTHead(actcell,false);
1249 case LyXTabular::SET_LTFOOT:
1250 tabular->SetLTFoot(actcell,false);
1252 case LyXTabular::SET_LTLASTFOOT:
1253 tabular->SetLTFoot(actcell,true);
1255 case LyXTabular::SET_LTNEWPAGE:
1256 what = !tabular->GetLTNewPage(actcell);
1257 tabular->SetLTNewPage(actcell,what);
1262 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1265 // the cursor.pos has to be before the inset so if it isn't now just
1266 // reset the curor pos first!
1267 if (cursor.pos() % 2) { // behind the inset
1268 cursor.pos(cursor.pos() - 1);
1271 UpdatableInset * inset =
1272 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1273 LyXFont font(LyXFont::ALL_SANE);
1275 x = inset->x() + inset->width(bv, font);
1276 y = inset->descent(bv, font);
1278 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1279 inset_y = cursor.y();
1280 inset->Edit(bv, x - inset_x, y - inset_y, button);
1281 if (!the_locking_inset)
1283 UpdateLocal(bv, CELL, false);
1287 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1289 InsetText * inset = tabular->GetCellInset(actcell);
1292 if (cursor.pos() % 2) { // behind the inset
1293 return (((x + top_x) < cursor.x()) &&
1294 ((x + top_x) > (cursor.x() - inset->width(bv,
1295 LyXFont(LyXFont::ALL_SANE)))));
1297 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1298 return ((x1 > x2) &&
1299 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1303 // This returns paperWidth() if the cell-width is unlimited or the width
1304 // in pixels if we have a pwidth for this cell.
1305 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1307 string s = tabular->GetPWidth(cell);
1311 return VSpace(s).inPixels( 0, 0);
1314 int InsetTabular::getMaxWidth(Painter & pain,
1315 UpdatableInset const * inset) const
1318 int n = tabular->GetNumberOfCells();
1319 for(cell=0; cell < n; ++cell) {
1320 if (tabular->GetCellInset(cell) == inset)
1325 int w = GetMaxWidthOfCell(pain, cell);
1326 // this because text insets remove the xpos from the maxwidth because
1327 // otherwise the would not break good!!!
1328 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1333 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1339 for(int j= 0; j < tabular->columns(); ++j) {
1340 for(int i = 0; i < tabular->rows(); ++i) {
1341 if (tabular->IsPartOfMultiColumn(i,j))
1343 cell = tabular->GetCellNumber(i,j);
1344 inset = tabular->GetCellInset(cell);
1345 inset->update(bv, font);
1346 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1348 // cell = tabular->GetCellNumber(0, j);
1349 // cx += tabular->GetWidthOfColumn(cell);