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 dodraw)
297 if (the_locking_inset)
298 the_locking_inset->update(bv, font, dodraw);
299 switch(need_update) {
303 if (calculate_dimensions_of_cells(bv, font, dodraw))
315 char const * InsetTabular::EditMessage() const
317 return _("Opened Tabular Inset");
321 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
323 UpdatableInset::Edit(bv, x, y, button);
325 if (!bv->lockInset(this)) {
326 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
330 the_locking_inset = 0;
331 inset_pos = inset_x = inset_y = 0;
333 sel_pos_start = sel_pos_end = cursor.pos();
334 sel_cell_start = sel_cell_end = actcell;
335 bv->text->FinishUndo();
336 if (InsetHit(bv, x, y)) {
337 ActivateCellInset(bv, x, y, button);
339 UpdateLocal(bv, NONE, false);
340 // bv->getOwner()->getPopups().updateFormTabular();
344 void InsetTabular::InsetUnlock(BufferView * bv)
347 if (the_locking_inset) {
348 the_locking_inset->InsetUnlock(bv);
349 the_locking_inset = 0;
352 if (hasSelection()) {
353 sel_pos_start = sel_pos_end = 0;
354 sel_cell_start = sel_cell_end = 0;
355 UpdateLocal(bv, FULL, false);
357 no_selection = false;
362 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
366 bv->updateInset(this, mark_dirty);
371 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
373 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
377 if (inset == tabular->GetCellInset(actcell)) {
378 lyxerr[Debug::INSETS] << "OK" << endl;
379 the_locking_inset = tabular->GetCellInset(actcell);
381 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
382 inset_y = cursor.y();
383 inset_pos = cursor.pos();
385 } else if (the_locking_inset && (the_locking_inset == inset)) {
386 if (cursor.pos() == inset_pos) {
387 lyxerr[Debug::INSETS] << "OK" << endl;
389 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
390 inset_y = cursor.y();
392 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
394 } else if (the_locking_inset) {
395 lyxerr[Debug::INSETS] << "MAYBE" << endl;
396 return the_locking_inset->LockInsetInInset(bv, inset);
398 lyxerr[Debug::INSETS] << "NOT OK" << endl;
402 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
405 if (!the_locking_inset)
407 if (the_locking_inset == inset) {
408 the_locking_inset->InsetUnlock(bv);
409 the_locking_inset = 0;
411 moveRight(bv, false);
412 UpdateLocal(bv, CELL, false);
415 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
416 if ((inset->LyxCode() == TABULAR_CODE) &&
417 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
419 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
427 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
429 if (!the_locking_inset)
431 if (the_locking_inset != inset)
432 return the_locking_inset->UpdateInsetInInset(bv, inset);
433 UpdateLocal(bv, CELL, false);
438 int InsetTabular::InsetInInsetY()
440 if (!the_locking_inset)
443 return (inset_y + the_locking_inset->InsetInInsetY());
447 UpdatableInset * InsetTabular::GetLockingInset()
449 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
453 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
457 if (the_locking_inset)
458 return the_locking_inset->GetFirstLockingInsetOfType(c);
463 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
465 if (the_locking_inset)
466 return the_locking_inset->InsertInset(bv, inset);
471 void InsetTabular::InsetButtonRelease(BufferView * bv,
472 int x, int y, int button)
475 if (the_locking_inset) {
477 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
478 i->InsetButtonRelease(bv, x, y, button);
482 MenuLayoutTabular(true, this);
485 if (the_locking_inset) {
486 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
489 no_selection = false;
493 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
495 if (hasSelection()) {
496 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
497 UpdateLocal(bv, SELECTION, false);
499 no_selection = false;
504 sel_pos_start = sel_pos_end = cursor.pos();
505 sel_cell_start = sel_cell_end = actcell;
507 bool inset_hit = InsetHit(bv, x, y);
509 if ((ocell == actcell) && the_locking_inset && inset_hit) {
510 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
512 } else if (the_locking_inset) {
513 the_locking_inset->InsetUnlock(bv);
515 the_locking_inset = 0;
516 if (inset_hit && bv->the_locking_inset) {
517 ActivateCellInset(bv, x, y, button);
518 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
523 bview->getOwner()->getPopups().showFormTabular();
524 else if (ocell != actcell)
525 bview->getOwner()->getPopups().updateFormTabular();
530 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
532 if (the_locking_inset) {
533 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
534 y - inset_y, button);
538 // int ocell = actcell,
539 int old = sel_pos_end;
542 sel_pos_end = cursor.pos();
543 sel_cell_end = actcell;
544 if (old != sel_pos_end)
545 UpdateLocal(bv, SELECTION, false);
547 if (ocell != actcell)
548 bview->getOwner()->getPopups().updateFormTabular();
551 no_selection = false;
555 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
557 if (the_locking_inset) {
558 the_locking_inset->InsetKeyPress(xke);
564 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
567 UpdatableInset::RESULT
570 no_selection = false;
571 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
572 || (result == DISPATCHED_NOUPDATE)) {
579 if ((action < 0) && arg.empty())
582 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
583 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
585 if (the_locking_inset) {
586 result=the_locking_inset->LocalDispatch(bv, action, arg);
587 if (result == DISPATCHED_NOUPDATE)
589 else if (result == DISPATCHED) {
590 the_locking_inset->ToggleInsetCursor(bv);
591 UpdateLocal(bv, CELL, false);
592 the_locking_inset->ToggleInsetCursor(bv);
594 } else if (result == FINISHED) {
595 if ((action == LFUN_RIGHT) || (action == -1)) {
596 cursor.pos(inset_pos + 1);
599 sel_pos_start = sel_pos_end = cursor.pos();
600 sel_cell_start = sel_cell_end = actcell;
607 bool hs = hasSelection();
610 // Normal chars not handled here
613 // --- Cursor Movements ---------------------------------------------
615 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
617 moveRight(bv, false);
618 sel_pos_end = cursor.pos();
619 if (!cellstart(cursor.pos())) {
620 if (tabular->right_column_of_cell(sel_cell_start) >
621 tabular->right_column_of_cell(actcell))
622 sel_cell_end = actcell+1;
624 sel_cell_end = actcell;
626 UpdateLocal(bv, SELECTION, false);
629 result = moveRight(bv);
630 sel_pos_start = sel_pos_end = cursor.pos();
631 sel_cell_start = sel_cell_end = actcell;
633 UpdateLocal(bv, CURSOR, false);
636 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
639 sel_pos_end = cursor.pos();
640 if (cellstart(cursor.pos())) {
641 if (tabular->column_of_cell(sel_cell_start) >=
642 tabular->column_of_cell(actcell))
643 sel_cell_end = actcell;
645 sel_cell_end = actcell-1;
647 UpdateLocal(bv, SELECTION, false);
650 result = moveLeft(bv);
651 sel_pos_start = sel_pos_end = cursor.pos();
652 sel_cell_start = sel_cell_end = actcell;
654 UpdateLocal(bv, CURSOR, false);
660 sel_pos_end = cursor.pos();
661 if ((ocell == sel_cell_end) ||
662 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
663 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
665 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
666 UpdateLocal(bv, SELECTION, false);
670 result= moveDown(bv);
671 sel_pos_start = sel_pos_end = cursor.pos();
672 sel_cell_start = sel_cell_end = actcell;
674 UpdateLocal(bv, CURSOR, false);
680 sel_pos_end = cursor.pos();
681 if ((ocell == sel_cell_end) ||
682 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
683 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
685 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
686 UpdateLocal(bv, SELECTION, false);
691 sel_pos_start = sel_pos_end = cursor.pos();
692 sel_cell_start = sel_cell_end = actcell;
694 UpdateLocal(bv, CURSOR, false);
706 if (the_locking_inset) {
707 the_locking_inset->InsetUnlock(bv);
709 the_locking_inset = 0;
710 if (action == LFUN_TAB)
714 sel_pos_start = sel_pos_end = cursor.pos();
715 sel_cell_start = sel_cell_end = actcell;
717 UpdateLocal(bv, CURSOR, false);
719 case LFUN_LAYOUT_TABLE:
721 int flag = (arg == "true");
722 MenuLayoutTabular(flag, this);
726 result = UNDISPATCHED;
729 if (result!=FINISHED) {
730 if (!the_locking_inset) {
732 if (ocell != actcell)
733 bview->getOwner()->getPopups().updateFormTabular();
738 bv->unlockInset(this);
743 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
745 return tabular->Latex(buf, os, fragile, fp);
749 int InsetTabular::Ascii(Buffer const *, ostream &) const
754 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
760 int InsetTabular::DocBook(Buffer const *, ostream &) const
766 void InsetTabular::Validate(LaTeXFeatures & features) const
768 tabular->Validate(features);
772 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
773 LyXFont const & font,
779 bool changed = false;
781 for(int i = 0; i < tabular->rows(); ++i) {
782 maxAsc = maxDesc = 0;
783 for(int j= 0; j < tabular->columns(); ++j) {
784 if (tabular->IsPartOfMultiColumn(i,j))
787 inset = tabular->GetCellInset(cell);
788 inset->update(bv, font, dodraw);
789 maxAsc = max(maxAsc, inset->ascent(bv, font));
790 maxDesc = max(maxDesc, inset->descent(bv, font));
791 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
793 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
794 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
800 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
802 x = cursor.x() - top_x;
807 void InsetTabular::ToggleInsetCursor(BufferView * bv)
809 if (the_locking_inset) {
810 the_locking_inset->ToggleInsetCursor(bv);
814 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
816 int asc = lyxfont::maxAscent(font);
817 int desc = lyxfont::maxDescent(font);
820 bv->hideLockedInsetCursor();
822 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
823 cursor_visible = !cursor_visible;
827 void InsetTabular::ShowInsetCursor(BufferView * bv)
829 if (!cursor_visible) {
830 LyXFont font; // = GetFont(par, cursor.pos);
832 int asc = lyxfont::maxAscent(font);
833 int desc = lyxfont::maxDescent(font);
834 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
835 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
836 cursor_visible = true;
841 void InsetTabular::HideInsetCursor(BufferView * bv)
844 ToggleInsetCursor(bv);
848 void InsetTabular::setPos(BufferView * bv, int x, int y) const
853 actcell = actrow = actcol = 0;
854 int ly = tabular->GetDescentOfRow(actrow);
856 // first search the right row
857 while((ly < y) && (actrow < tabular->rows())) {
858 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
859 tabular->GetAscentOfRow(actrow+1) +
860 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
863 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
865 actcell = tabular->GetCellNumber(actrow, actcol);
867 // now search the right column
868 int lx = tabular->GetWidthOfColumn(actcell) -
869 tabular->GetAdditionalWidth(actcell);
870 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
871 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
872 tabular->GetAdditionalWidth(actcell - 1));
873 cursor.pos(((actcell+1) * 2) - 1);
875 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
876 cursor.x(lx + top_x - 2);
878 cursor.pos(cursor.pos() - 1);
879 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
884 int InsetTabular::getCellXPos(int cell) const
888 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
890 int lx = tabular->GetWidthOfColumn(cell);
891 for(; (c < cell); ++c) {
892 lx += tabular->GetWidthOfColumn(c);
894 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
895 ADD_TO_TABULAR_WIDTH);
898 void InsetTabular::resetPos(BufferView * bv) const
902 actcol = tabular->column_of_cell(actcell);
907 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
908 if (tabular->IsLastCellInRow(cell)) {
909 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
910 tabular->GetAscentOfRow(actrow + 1) +
911 tabular->GetAdditionalHeight(cell + 1));
915 cursor.x(getCellXPos(actcell) + 2);
916 if (cursor.pos() % 2) {
917 LyXFont font(LyXFont::ALL_SANE);
918 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
919 tabular->GetBeginningOfTextInCell(actcell));
921 if ((!the_locking_inset ||
922 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
923 (actcell != oldcell)) {
924 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
930 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
932 if (!cellstart(cursor.pos())) {
933 if (tabular->IsLastCell(actcell))
936 cursor.pos(cursor.pos() + 1);
938 if (ActivateCellInset(bv))
940 } else { // before the inset
941 cursor.pos(cursor.pos() + 1);
944 return DISPATCHED_NOUPDATE;
948 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
955 cursor.pos(cursor.pos() - 1);
956 if (!cellstart(cursor.pos())) {
958 } else if (lock) { // behind the inset
959 if (ActivateCellInset(bv, 0, 0, 0, true))
963 return DISPATCHED_NOUPDATE;
967 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
970 actcell = tabular->GetCellAbove(actcell);
971 if (actcell == ocell) // we moved out of the inset
974 return DISPATCHED_NOUPDATE;
978 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
981 actcell = tabular->GetCellBelow(actcell);
982 if (actcell == ocell) // we moved out of the inset
985 return DISPATCHED_NOUPDATE;
989 bool InsetTabular::moveNextCell(BufferView * bv)
991 if (tabular->IsLastCell(actcell))
994 cursor.pos(cursor.pos() + 1);
995 if (!cellstart(cursor.pos()))
996 cursor.pos(cursor.pos() + 1);
1002 bool InsetTabular::movePrevCell(BufferView * bv)
1004 if (!actcell) // first cell
1007 cursor.pos(cursor.pos() - 1);
1008 if (cellstart(cursor.pos()))
1009 cursor.pos(cursor.pos() - 1);
1015 bool InsetTabular::Delete()
1021 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1023 if (the_locking_inset)
1024 the_locking_inset->SetFont(bv, font, tall);
1028 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1037 setAlign = LYX_ALIGN_LEFT,
1043 case LyXTabular::ALIGN_LEFT:
1044 setAlign=LYX_ALIGN_LEFT;
1046 case LyXTabular::ALIGN_RIGHT:
1047 setAlign=LYX_ALIGN_RIGHT;
1049 case LyXTabular::ALIGN_CENTER:
1050 setAlign=LYX_ALIGN_CENTER;
1055 if (hasSelection()) {
1057 sel_col_start = tabular->column_of_cell(sel_cell_start);
1058 sel_col_end = tabular->column_of_cell(sel_cell_end);
1059 if (sel_col_start > sel_col_end) {
1060 sel_col_end = sel_col_start;
1061 sel_col_start = tabular->column_of_cell(sel_cell_end);
1063 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1066 sel_row_start = tabular->row_of_cell(sel_cell_start);
1067 sel_row_end = tabular->row_of_cell(sel_cell_end);
1068 if (sel_row_start > sel_row_end) {
1069 tmp = sel_row_start;
1070 sel_row_start = sel_row_end;
1074 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1075 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1077 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1078 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1079 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1081 int row = tabular->row_of_cell(actcell);
1082 int column = tabular->column_of_cell(actcell);
1085 case LyXTabular::SET_PWIDTH:
1087 bool update = (tabular->GetPWidth(actcell) != val);
1088 tabular->SetPWidth(actcell,val);
1090 for (int i=0; i < tabular->rows(); ++i) {
1091 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1094 UpdateLocal(bv, INIT, true);
1098 case LyXTabular::SET_SPECIAL_COLUMN:
1099 case LyXTabular::SET_SPECIAL_MULTI:
1100 tabular->SetAlignSpecial(actcell,val,feature);
1102 case LyXTabular::APPEND_ROW:
1103 // append the row into the tabular
1104 UnlockInsetInInset(bv, the_locking_inset);
1105 tabular->AppendRow(actcell);
1106 UpdateLocal(bv, INIT, true);
1108 case LyXTabular::APPEND_COLUMN:
1109 // append the column into the tabular
1110 tabular->AppendColumn(actcell);
1111 actcell = tabular->GetCellNumber(row, column);
1112 UpdateLocal(bv, INIT, true);
1114 case LyXTabular::DELETE_ROW:
1115 tabular->DeleteRow(tabular->row_of_cell(actcell));
1116 if ((row+1) > tabular->rows())
1118 actcell = tabular->GetCellNumber(row, column);
1119 UpdateLocal(bv, INIT, true);
1121 case LyXTabular::DELETE_COLUMN:
1122 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1123 if ((column+1) > tabular->columns())
1125 actcell = tabular->GetCellNumber(row, column);
1126 UpdateLocal(bv, INIT, true);
1128 case LyXTabular::TOGGLE_LINE_TOP:
1129 lineSet = !tabular->TopLine(actcell);
1130 for(i=sel_row_start; i<=sel_row_end; ++i)
1131 for(j=sel_col_start; j<=sel_col_end; ++j)
1132 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1133 UpdateLocal(bv, INIT, true);
1136 case LyXTabular::TOGGLE_LINE_BOTTOM:
1137 lineSet = !tabular->BottomLine(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->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1141 UpdateLocal(bv, INIT, true);
1144 case LyXTabular::TOGGLE_LINE_LEFT:
1145 lineSet = !tabular->LeftLine(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->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1149 UpdateLocal(bv, INIT, true);
1152 case LyXTabular::TOGGLE_LINE_RIGHT:
1153 lineSet = !tabular->RightLine(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->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1157 UpdateLocal(bv, INIT, true);
1159 case LyXTabular::ALIGN_LEFT:
1160 case LyXTabular::ALIGN_RIGHT:
1161 case LyXTabular::ALIGN_CENTER:
1162 for(i=sel_row_start; i<=sel_row_end; ++i)
1163 for(j=sel_col_start; j<=sel_col_end; ++j)
1164 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1166 UpdateLocal(bv, INIT, true);
1168 UpdateLocal(bv, CELL, true);
1170 case LyXTabular::MULTICOLUMN:
1172 if (sel_row_start != sel_row_end) {
1173 WriteAlert(_("Impossible Operation!"),
1174 _("Multicolumns can only be horizontally."),
1178 // just multicol for one Single Cell
1179 if (!hasSelection()) {
1180 // check wether we are completly in a multicol
1181 if (tabular->IsMultiColumn(actcell)) {
1182 tabular->UnsetMultiColumn(actcell);
1183 UpdateLocal(bv, INIT, true);
1185 tabular->SetMultiColumn(actcell, 1);
1186 UpdateLocal(bv, CELL, true);
1190 // we have a selection so this means we just add all this
1191 // cells to form a multicolumn cell
1195 if (sel_cell_start > sel_cell_end) {
1196 s_start = sel_cell_end;
1197 s_end = sel_cell_start;
1199 s_start = sel_cell_start;
1200 s_end = sel_cell_end;
1202 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1205 sel_cell_end = sel_cell_start;
1206 sel_pos_end = sel_pos_start;
1207 UpdateLocal(bv, INIT, true);
1210 case LyXTabular::SET_ALL_LINES:
1212 case LyXTabular::UNSET_ALL_LINES:
1213 for(i=sel_row_start; i<=sel_row_end; ++i)
1214 for(j=sel_col_start; j<=sel_col_end; ++j)
1215 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1216 UpdateLocal(bv, INIT, true);
1218 case LyXTabular::SET_LONGTABULAR:
1219 tabular->SetLongTabular(true);
1220 UpdateLocal(bv, INIT, true); // because this toggles displayed
1222 case LyXTabular::UNSET_LONGTABULAR:
1223 tabular->SetLongTabular(false);
1224 UpdateLocal(bv, INIT, true); // because this toggles displayed
1226 case LyXTabular::SET_ROTATE_TABULAR:
1227 tabular->SetRotateTabular(true);
1229 case LyXTabular::UNSET_ROTATE_TABULAR:
1230 tabular->SetRotateTabular(false);
1232 case LyXTabular::SET_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),true);
1237 case LyXTabular::UNSET_ROTATE_CELL:
1238 for(i=sel_row_start; i<=sel_row_end; ++i)
1239 for(j=sel_col_start; j<=sel_col_end; ++j)
1240 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1242 case LyXTabular::SET_LINEBREAKS:
1243 what = !tabular->GetLinebreaks(actcell);
1244 for(i=sel_row_start; i<=sel_row_end; ++i)
1245 for(j=sel_col_start; j<=sel_col_end; ++j)
1246 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1248 case LyXTabular::SET_LTFIRSTHEAD:
1249 tabular->SetLTHead(actcell,true);
1251 case LyXTabular::SET_LTHEAD:
1252 tabular->SetLTHead(actcell,false);
1254 case LyXTabular::SET_LTFOOT:
1255 tabular->SetLTFoot(actcell,false);
1257 case LyXTabular::SET_LTLASTFOOT:
1258 tabular->SetLTFoot(actcell,true);
1260 case LyXTabular::SET_LTNEWPAGE:
1261 what = !tabular->GetLTNewPage(actcell);
1262 tabular->SetLTNewPage(actcell,what);
1267 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1270 // the cursor.pos has to be before the inset so if it isn't now just
1271 // reset the curor pos first!
1272 if (cursor.pos() % 2) { // behind the inset
1273 cursor.pos(cursor.pos() - 1);
1276 UpdatableInset * inset =
1277 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1278 LyXFont font(LyXFont::ALL_SANE);
1280 x = inset->x() + inset->width(bv, font);
1281 y = inset->descent(bv, font);
1283 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1284 inset_y = cursor.y();
1285 inset->Edit(bv, x - inset_x, y - inset_y, button);
1286 if (!the_locking_inset)
1288 UpdateLocal(bv, CELL, false);
1292 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1294 InsetText * inset = tabular->GetCellInset(actcell);
1297 if (cursor.pos() % 2) { // behind the inset
1298 return (((x + top_x) < cursor.x()) &&
1299 ((x + top_x) > (cursor.x() - inset->width(bv,
1300 LyXFont(LyXFont::ALL_SANE)))));
1302 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1303 return ((x1 > x2) &&
1304 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1308 // This returns paperWidth() if the cell-width is unlimited or the width
1309 // in pixels if we have a pwidth for this cell.
1310 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1312 string s = tabular->GetPWidth(cell);
1316 return VSpace(s).inPixels( 0, 0);
1319 int InsetTabular::getMaxWidth(Painter & pain,
1320 UpdatableInset const * inset) const
1323 int n = tabular->GetNumberOfCells();
1324 for(cell=0; cell < n; ++cell) {
1325 if (tabular->GetCellInset(cell) == inset)
1330 int w = GetMaxWidthOfCell(pain, cell);
1331 // this because text insets remove the xpos from the maxwidth because
1332 // otherwise the would not break good!!!
1333 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1338 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1344 for(int j= 0; j < tabular->columns(); ++j) {
1345 for(int i = 0; i < tabular->rows(); ++i) {
1346 if (tabular->IsPartOfMultiColumn(i,j))
1348 cell = tabular->GetCellNumber(i,j);
1349 inset = tabular->GetCellInset(cell);
1350 inset->update(bv, font);
1351 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1353 // cell = tabular->GetCellNumber(0, j);
1354 // cx += tabular->GetWidthOfColumn(cell);