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 extern void MenuLayoutTabular(bool, InsetTabular *);
37 extern bool UpdateLayoutTabular(bool, InsetTabular *);
38 extern void TabularOptClose();
40 const int ADD_TO_HEIGHT = 2;
41 const int ADD_TO_TABULAR_WIDTH = 2;
48 #define cellstart(p) ((p % 2) == 0)
50 //#define USE_NEW_LAYOUT 1
52 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
58 buffer = buf; // set this first!!!
59 tabular = new LyXTabular(this, rows,columns);
60 // for now make it always display as display() inset
62 the_locking_inset = 0;
63 locked = no_selection = cursor_visible = false;
68 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
73 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
75 buffer = buf; // set this first
76 tabular = new LyXTabular(this, *(tab.tabular));
77 the_locking_inset = 0;
78 locked = no_selection = cursor_visible = false;
83 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
88 InsetTabular::~InsetTabular()
92 if (buffer->getUser())
93 buffer->getUser()->owner()->getDialogs()->hideTabular(this);
98 Inset * InsetTabular::Clone() const
100 InsetTabular * t = new InsetTabular(*this, buffer);
102 t->tabular = tabular->Clone(t);
107 void InsetTabular::Write(Buffer const * buf, ostream & os) const
109 os << " Tabular" << endl;
110 tabular->Write(buf, os);
114 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
116 bool old_format = (lex.GetString() == "\\LyXTable");
121 tabular = new LyXTabular(buf, this, lex);
129 token = lex.GetString();
130 while (lex.IsOK() && (token != "\\end_inset")) {
132 token = lex.GetString();
134 if (token != "\\end_inset") {
135 lex.printError("Missing \\end_inset at this point. "
141 int InsetTabular::ascent(BufferView *, LyXFont const &) const
143 return tabular->GetAscentOfRow(0);
147 int InsetTabular::descent(BufferView *, LyXFont const &) const
149 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
153 int InsetTabular::width(BufferView *, LyXFont const &) const
155 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
159 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
160 float & x, bool cleared) const
162 Painter & pain = bv->painter();
167 UpdatableInset::draw(bv,font,baseline,x,cleared);
168 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
169 (top_x != int(x)) || (top_baseline != baseline))) {
171 int h = ascent(bv, font) + descent(bv, font);
172 int tx = display()? 0:top_x;
173 int w = tx? width(bv, font):pain.paperWidth();
174 int ty = baseline - ascent(bv, font);
178 if ((ty + h) > pain.paperHeight())
179 h = pain.paperHeight();
180 if ((top_x + w) > pain.paperWidth())
181 w = pain.paperWidth();
182 pain.fillRectangle(tx, ty, w, h);
188 if (locked) { // repaint this way as the background was not cleared
189 if (the_locking_inset)
190 the_locking_inset->update(bv, font, true);
192 bv->updateInset(const_cast<InsetTabular*>(this), false);
199 top_baseline = baseline;
201 x += ADD_TO_TABULAR_WIDTH;
202 if (cleared || (need_update == FULL) || (need_update == CELL)) {
203 for(i=0;i<tabular->rows();++i) {
205 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
206 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
207 for(j=0;j<tabular->columns();++j) {
208 if (tabular->IsPartOfMultiColumn(i,j))
210 cx = nx + tabular->GetBeginningOfTextInCell(cell);
212 DrawCellSelection(pain, nx, baseline, i, j, cell);
213 if (dodraw && !cleared && locked && the_locking_inset) {
214 if (the_locking_inset == tabular->GetCellInset(cell))
215 tabular->GetCellInset(cell)->draw(bv, font,
219 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
221 DrawCellLines(pain, nx, baseline, i, cell);
223 nx += tabular->GetWidthOfColumn(cell);
226 baseline += tabular->GetDescentOfRow(i) +
227 tabular->GetAscentOfRow(i+1)+
228 tabular->GetAdditionalHeight(cell+1);
231 x += width(bv, font);
236 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
237 int row, int cell) const
239 int x2 = x + tabular->GetWidthOfColumn(cell);
242 if (!tabular->TopAlreadyDrawed(cell)) {
243 on_off = !tabular->TopLine(cell);
244 pain.line(x, baseline - tabular->GetAscentOfRow(row),
245 x2, baseline - tabular->GetAscentOfRow(row),
246 on_off ? LColor::tabularonoffline:LColor::tabularline,
247 on_off ? Painter::line_onoffdash:Painter::line_solid);
249 on_off = !tabular->BottomLine(cell);
250 pain.line(x,baseline + tabular->GetDescentOfRow(row),
251 x2, baseline + tabular->GetDescentOfRow(row),
252 on_off ? LColor::tabularonoffline:LColor::tabularline,
253 on_off ? Painter::line_onoffdash:Painter::line_solid);
254 if (!tabular->LeftAlreadyDrawed(cell)) {
255 on_off = !tabular->LeftLine(cell);
256 pain.line(x, baseline - tabular->GetAscentOfRow(row),
257 x, baseline + tabular->GetDescentOfRow(row),
258 on_off ? LColor::tabularonoffline:LColor::tabularline,
259 on_off ? Painter::line_onoffdash:Painter::line_solid);
261 on_off = !tabular->RightLine(cell);
262 pain.line(x2 - tabular->GetAdditionalWidth(cell),
263 baseline - tabular->GetAscentOfRow(row),
264 x2 - tabular->GetAdditionalWidth(cell),
265 baseline + tabular->GetDescentOfRow(row),
266 on_off ? LColor::tabularonoffline:LColor::tabularline,
267 on_off ? Painter::line_onoffdash:Painter::line_solid);
271 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
272 int row, int column, int cell) const
276 int cs = tabular->column_of_cell(sel_cell_start);
277 int ce = tabular->column_of_cell(sel_cell_end);
280 cs = tabular->column_of_cell(sel_cell_end);
282 ce = tabular->right_column_of_cell(sel_cell_end);
285 int rs = tabular->row_of_cell(sel_cell_start);
286 int re = tabular->row_of_cell(sel_cell_end);
293 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
294 int w = tabular->GetWidthOfColumn(cell);
295 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
296 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
297 w, h, LColor::selection);
302 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
306 calculate_dimensions_of_cells(bv, font, true);
308 owner()->update(bv, font, true);
311 if (the_locking_inset)
312 the_locking_inset->update(bv, font, reinit);
313 switch(need_update) {
317 if (calculate_dimensions_of_cells(bv, font, false))
329 char const * InsetTabular::EditMessage() const
331 return _("Opened Tabular Inset");
335 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
337 UpdatableInset::Edit(bv, x, y, button);
339 if (!bv->lockInset(this)) {
340 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
344 the_locking_inset = 0;
345 inset_pos = inset_x = inset_y = 0;
347 sel_pos_start = sel_pos_end = cursor.pos();
348 sel_cell_start = sel_cell_end = actcell;
349 bv->text->FinishUndo();
350 if (InsetHit(bv, x, y)) {
351 ActivateCellInset(bv, x, y, button);
353 UpdateLocal(bv, NONE, false);
354 // bv->getOwner()->getPopups().updateFormTabular();
358 void InsetTabular::InsetUnlock(BufferView * bv)
361 if (the_locking_inset) {
362 the_locking_inset->InsetUnlock(bv);
363 the_locking_inset = 0;
366 if (hasSelection()) {
367 sel_pos_start = sel_pos_end = 0;
368 sel_cell_start = sel_cell_end = 0;
369 UpdateLocal(bv, FULL, false);
371 no_selection = false;
376 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
380 bv->updateInset(this, mark_dirty);
385 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
387 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
391 if (inset == tabular->GetCellInset(actcell)) {
392 lyxerr[Debug::INSETS] << "OK" << endl;
393 the_locking_inset = tabular->GetCellInset(actcell);
395 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
396 inset_y = cursor.y();
397 inset_pos = cursor.pos();
399 } else if (the_locking_inset && (the_locking_inset == inset)) {
400 if (cursor.pos() == inset_pos) {
401 lyxerr[Debug::INSETS] << "OK" << endl;
403 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
404 inset_y = cursor.y();
406 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
408 } else if (the_locking_inset) {
409 lyxerr[Debug::INSETS] << "MAYBE" << endl;
410 return the_locking_inset->LockInsetInInset(bv, inset);
412 lyxerr[Debug::INSETS] << "NOT OK" << endl;
416 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
419 if (!the_locking_inset)
421 if (the_locking_inset == inset) {
422 the_locking_inset->InsetUnlock(bv);
423 the_locking_inset = 0;
425 moveRight(bv, false);
426 UpdateLocal(bv, CELL, false);
429 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
430 if ((inset->LyxCode() == TABULAR_CODE) &&
431 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
433 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
441 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
443 if (!the_locking_inset)
445 if (the_locking_inset != inset)
446 return the_locking_inset->UpdateInsetInInset(bv, inset);
447 UpdateLocal(bv, CELL, false);
452 int InsetTabular::InsetInInsetY()
454 if (!the_locking_inset)
457 return (inset_y + the_locking_inset->InsetInInsetY());
461 UpdatableInset * InsetTabular::GetLockingInset()
463 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
467 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
471 if (the_locking_inset)
472 return the_locking_inset->GetFirstLockingInsetOfType(c);
477 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
479 if (the_locking_inset)
480 return the_locking_inset->InsertInset(bv, inset);
485 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
487 if (hasSelection()) {
488 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
489 UpdateLocal(bv, SELECTION, false);
491 no_selection = false;
496 sel_pos_start = sel_pos_end = cursor.pos();
497 sel_cell_start = sel_cell_end = actcell;
499 bool inset_hit = InsetHit(bv, x, y);
501 if ((ocell == actcell) && the_locking_inset && inset_hit) {
502 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
504 } else if (the_locking_inset) {
505 the_locking_inset->InsetUnlock(bv);
507 the_locking_inset = 0;
508 if (inset_hit && bv->the_locking_inset) {
509 ActivateCellInset(bv, x, y, button);
510 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
515 void InsetTabular::InsetButtonRelease(BufferView * bv,
516 int x, int y, int button)
519 if (the_locking_inset) {
521 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
522 i->InsetButtonRelease(bv, x, y, button);
526 #ifdef USE_NEW_LAYOUT
527 bv->owner()->getDialogs()->showTabular(this);
529 else if (ocell != actcell)
530 bview->getOwner()->getPopups().updateTabular();
533 MenuLayoutTabular(true, this);
537 if (the_locking_inset) {
538 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
541 no_selection = false;
545 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
547 if (the_locking_inset) {
548 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
549 y - inset_y, button);
553 // int ocell = actcell,
554 int old = sel_pos_end;
557 sel_pos_end = cursor.pos();
558 sel_cell_end = actcell;
559 if (old != sel_pos_end)
560 UpdateLocal(bv, SELECTION, false);
562 if (ocell != actcell)
563 bview->getOwner()->getPopups().updateFormTabular();
566 no_selection = false;
570 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
572 if (the_locking_inset) {
573 the_locking_inset->InsetKeyPress(xke);
579 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
582 UpdatableInset::RESULT
585 no_selection = false;
586 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
587 || (result == DISPATCHED_NOUPDATE)) {
594 if ((action < 0) && arg.empty())
597 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
598 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
600 if (the_locking_inset) {
601 result=the_locking_inset->LocalDispatch(bv, action, arg);
602 if (result == DISPATCHED_NOUPDATE)
604 else if (result == DISPATCHED) {
605 the_locking_inset->ToggleInsetCursor(bv);
606 UpdateLocal(bv, CELL, false);
607 the_locking_inset->ToggleInsetCursor(bv);
609 } else if (result == FINISHED) {
610 if ((action == LFUN_RIGHT) || (action == -1)) {
611 cursor.pos(inset_pos + 1);
614 sel_pos_start = sel_pos_end = cursor.pos();
615 sel_cell_start = sel_cell_end = actcell;
622 bool hs = hasSelection();
625 // Normal chars not handled here
628 // --- Cursor Movements ---------------------------------------------
630 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
632 moveRight(bv, false);
633 sel_pos_end = cursor.pos();
634 if (!cellstart(cursor.pos())) {
635 if (tabular->right_column_of_cell(sel_cell_start) >
636 tabular->right_column_of_cell(actcell))
637 sel_cell_end = actcell+1;
639 sel_cell_end = actcell;
641 UpdateLocal(bv, SELECTION, false);
644 result = moveRight(bv);
645 sel_pos_start = sel_pos_end = cursor.pos();
646 sel_cell_start = sel_cell_end = actcell;
648 UpdateLocal(bv, CURSOR, false);
651 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
654 sel_pos_end = cursor.pos();
655 if (cellstart(cursor.pos())) {
656 if (tabular->column_of_cell(sel_cell_start) >=
657 tabular->column_of_cell(actcell))
658 sel_cell_end = actcell;
660 sel_cell_end = actcell-1;
662 UpdateLocal(bv, SELECTION, false);
665 result = moveLeft(bv);
666 sel_pos_start = sel_pos_end = cursor.pos();
667 sel_cell_start = sel_cell_end = actcell;
669 UpdateLocal(bv, CURSOR, false);
675 sel_pos_end = cursor.pos();
676 if ((ocell == sel_cell_end) ||
677 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
678 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
680 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
681 UpdateLocal(bv, SELECTION, false);
685 result= moveDown(bv);
686 sel_pos_start = sel_pos_end = cursor.pos();
687 sel_cell_start = sel_cell_end = actcell;
689 UpdateLocal(bv, CURSOR, false);
695 sel_pos_end = cursor.pos();
696 if ((ocell == sel_cell_end) ||
697 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
698 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
700 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
701 UpdateLocal(bv, SELECTION, false);
706 sel_pos_start = sel_pos_end = cursor.pos();
707 sel_cell_start = sel_cell_end = actcell;
709 UpdateLocal(bv, CURSOR, false);
721 if (the_locking_inset) {
722 the_locking_inset->InsetUnlock(bv);
724 the_locking_inset = 0;
725 if (action == LFUN_TAB)
729 sel_pos_start = sel_pos_end = cursor.pos();
730 sel_cell_start = sel_cell_end = actcell;
732 UpdateLocal(bv, CURSOR, false);
734 case LFUN_LAYOUT_TABLE:
736 int flag = (arg == "true");
737 MenuLayoutTabular(flag, this);
741 result = UNDISPATCHED;
744 if (result!=FINISHED) {
745 if (!the_locking_inset) {
747 if (ocell != actcell)
748 bview->getOwner()->getPopups().updateFormTabular();
753 bv->unlockInset(this);
758 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
760 return tabular->Latex(buf, os, fragile, fp);
764 int InsetTabular::Ascii(Buffer const *, ostream &) const
769 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
775 int InsetTabular::DocBook(Buffer const *, ostream &) const
781 void InsetTabular::Validate(LaTeXFeatures & features) const
783 tabular->Validate(features);
787 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
788 LyXFont const & font,
794 bool changed = false;
796 for(int i = 0; i < tabular->rows(); ++i) {
797 maxAsc = maxDesc = 0;
798 for(int j= 0; j < tabular->columns(); ++j) {
799 if (tabular->IsPartOfMultiColumn(i,j))
802 inset = tabular->GetCellInset(cell);
804 inset->update(bv, font, false);
805 maxAsc = max(maxAsc, inset->ascent(bv, font));
806 maxDesc = max(maxDesc, inset->descent(bv, font));
807 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
809 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
810 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
816 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
818 x = cursor.x() - top_x;
823 void InsetTabular::ToggleInsetCursor(BufferView * bv)
825 if (the_locking_inset) {
826 the_locking_inset->ToggleInsetCursor(bv);
830 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
832 int asc = lyxfont::maxAscent(font);
833 int desc = lyxfont::maxDescent(font);
836 bv->hideLockedInsetCursor();
838 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
839 cursor_visible = !cursor_visible;
843 void InsetTabular::ShowInsetCursor(BufferView * bv)
845 if (!cursor_visible) {
846 LyXFont font; // = GetFont(par, cursor.pos);
848 int asc = lyxfont::maxAscent(font);
849 int desc = lyxfont::maxDescent(font);
850 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
851 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
852 cursor_visible = true;
857 void InsetTabular::HideInsetCursor(BufferView * bv)
860 ToggleInsetCursor(bv);
864 void InsetTabular::setPos(BufferView * bv, int x, int y) const
869 actcell = actrow = actcol = 0;
870 int ly = tabular->GetDescentOfRow(actrow);
872 // first search the right row
873 while((ly < y) && (actrow < tabular->rows())) {
874 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
875 tabular->GetAscentOfRow(actrow+1) +
876 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
879 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
881 actcell = tabular->GetCellNumber(actrow, actcol);
883 // now search the right column
884 int lx = tabular->GetWidthOfColumn(actcell) -
885 tabular->GetAdditionalWidth(actcell);
886 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
887 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
888 tabular->GetAdditionalWidth(actcell - 1));
889 cursor.pos(((actcell+1) * 2) - 1);
891 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
892 cursor.x(lx + top_x - 2);
894 cursor.pos(cursor.pos() - 1);
895 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
900 int InsetTabular::getCellXPos(int cell) const
904 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
906 int lx = tabular->GetWidthOfColumn(cell);
907 for(; (c < cell); ++c) {
908 lx += tabular->GetWidthOfColumn(c);
910 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
911 ADD_TO_TABULAR_WIDTH);
914 void InsetTabular::resetPos(BufferView * bv) const
918 actcol = tabular->column_of_cell(actcell);
923 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
924 if (tabular->IsLastCellInRow(cell)) {
925 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
926 tabular->GetAscentOfRow(actrow + 1) +
927 tabular->GetAdditionalHeight(cell + 1));
931 cursor.x(getCellXPos(actcell) + 2);
932 if (cursor.pos() % 2) {
933 LyXFont font(LyXFont::ALL_SANE);
934 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
935 tabular->GetBeginningOfTextInCell(actcell));
937 if ((!the_locking_inset ||
938 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
939 (actcell != oldcell)) {
940 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
946 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
948 if (!cellstart(cursor.pos())) {
949 if (tabular->IsLastCell(actcell))
952 cursor.pos(cursor.pos() + 1);
954 if (ActivateCellInset(bv))
956 } else { // before the inset
957 cursor.pos(cursor.pos() + 1);
960 return DISPATCHED_NOUPDATE;
964 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
971 cursor.pos(cursor.pos() - 1);
972 if (!cellstart(cursor.pos())) {
974 } else if (lock) { // behind the inset
975 if (ActivateCellInset(bv, 0, 0, 0, true))
979 return DISPATCHED_NOUPDATE;
983 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
986 actcell = tabular->GetCellAbove(actcell);
987 if (actcell == ocell) // we moved out of the inset
990 return DISPATCHED_NOUPDATE;
994 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
997 actcell = tabular->GetCellBelow(actcell);
998 if (actcell == ocell) // we moved out of the inset
1001 return DISPATCHED_NOUPDATE;
1005 bool InsetTabular::moveNextCell(BufferView * bv)
1007 if (tabular->IsLastCell(actcell))
1010 cursor.pos(cursor.pos() + 1);
1011 if (!cellstart(cursor.pos()))
1012 cursor.pos(cursor.pos() + 1);
1018 bool InsetTabular::movePrevCell(BufferView * bv)
1020 if (!actcell) // first cell
1023 cursor.pos(cursor.pos() - 1);
1024 if (cellstart(cursor.pos()))
1025 cursor.pos(cursor.pos() - 1);
1031 bool InsetTabular::Delete()
1037 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1039 if (the_locking_inset)
1040 the_locking_inset->SetFont(bv, font, tall);
1044 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1053 setAlign = LYX_ALIGN_LEFT,
1059 case LyXTabular::ALIGN_LEFT:
1060 setAlign=LYX_ALIGN_LEFT;
1062 case LyXTabular::ALIGN_RIGHT:
1063 setAlign=LYX_ALIGN_RIGHT;
1065 case LyXTabular::ALIGN_CENTER:
1066 setAlign=LYX_ALIGN_CENTER;
1071 if (hasSelection()) {
1073 sel_col_start = tabular->column_of_cell(sel_cell_start);
1074 sel_col_end = tabular->column_of_cell(sel_cell_end);
1075 if (sel_col_start > sel_col_end) {
1076 sel_col_end = sel_col_start;
1077 sel_col_start = tabular->column_of_cell(sel_cell_end);
1079 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1082 sel_row_start = tabular->row_of_cell(sel_cell_start);
1083 sel_row_end = tabular->row_of_cell(sel_cell_end);
1084 if (sel_row_start > sel_row_end) {
1085 tmp = sel_row_start;
1086 sel_row_start = sel_row_end;
1090 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1091 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1093 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1094 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1095 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1097 int row = tabular->row_of_cell(actcell);
1098 int column = tabular->column_of_cell(actcell);
1101 case LyXTabular::SET_PWIDTH:
1103 bool update = (tabular->GetPWidth(actcell) != val);
1104 tabular->SetPWidth(actcell,val);
1106 for (int i=0; i < tabular->rows(); ++i) {
1107 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1110 UpdateLocal(bv, INIT, true);
1114 case LyXTabular::SET_SPECIAL_COLUMN:
1115 case LyXTabular::SET_SPECIAL_MULTI:
1116 tabular->SetAlignSpecial(actcell,val,feature);
1118 case LyXTabular::APPEND_ROW:
1119 // append the row into the tabular
1120 UnlockInsetInInset(bv, the_locking_inset);
1121 tabular->AppendRow(actcell);
1122 UpdateLocal(bv, INIT, true);
1124 case LyXTabular::APPEND_COLUMN:
1125 // append the column into the tabular
1126 tabular->AppendColumn(actcell);
1127 actcell = tabular->GetCellNumber(row, column);
1128 UpdateLocal(bv, INIT, true);
1130 case LyXTabular::DELETE_ROW:
1131 tabular->DeleteRow(tabular->row_of_cell(actcell));
1132 if ((row+1) > tabular->rows())
1134 actcell = tabular->GetCellNumber(row, column);
1135 UpdateLocal(bv, INIT, true);
1137 case LyXTabular::DELETE_COLUMN:
1138 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1139 if ((column+1) > tabular->columns())
1141 actcell = tabular->GetCellNumber(row, column);
1142 UpdateLocal(bv, INIT, true);
1144 case LyXTabular::TOGGLE_LINE_TOP:
1145 lineSet = !tabular->TopLine(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->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1149 UpdateLocal(bv, INIT, true);
1152 case LyXTabular::TOGGLE_LINE_BOTTOM:
1153 lineSet = !tabular->BottomLine(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->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1157 UpdateLocal(bv, INIT, true);
1160 case LyXTabular::TOGGLE_LINE_LEFT:
1161 lineSet = !tabular->LeftLine(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->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1165 UpdateLocal(bv, INIT, true);
1168 case LyXTabular::TOGGLE_LINE_RIGHT:
1169 lineSet = !tabular->RightLine(actcell);
1170 for(i=sel_row_start; i<=sel_row_end; ++i)
1171 for(j=sel_col_start; j<=sel_col_end; ++j)
1172 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1173 UpdateLocal(bv, INIT, true);
1175 case LyXTabular::ALIGN_LEFT:
1176 case LyXTabular::ALIGN_RIGHT:
1177 case LyXTabular::ALIGN_CENTER:
1178 for(i=sel_row_start; i<=sel_row_end; ++i)
1179 for(j=sel_col_start; j<=sel_col_end; ++j)
1180 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1182 UpdateLocal(bv, INIT, true);
1184 UpdateLocal(bv, CELL, true);
1186 case LyXTabular::MULTICOLUMN:
1188 if (sel_row_start != sel_row_end) {
1189 WriteAlert(_("Impossible Operation!"),
1190 _("Multicolumns can only be horizontally."),
1194 // just multicol for one Single Cell
1195 if (!hasSelection()) {
1196 // check wether we are completly in a multicol
1197 if (tabular->IsMultiColumn(actcell)) {
1198 tabular->UnsetMultiColumn(actcell);
1199 UpdateLocal(bv, INIT, true);
1201 tabular->SetMultiColumn(actcell, 1);
1202 UpdateLocal(bv, CELL, true);
1206 // we have a selection so this means we just add all this
1207 // cells to form a multicolumn cell
1211 if (sel_cell_start > sel_cell_end) {
1212 s_start = sel_cell_end;
1213 s_end = sel_cell_start;
1215 s_start = sel_cell_start;
1216 s_end = sel_cell_end;
1218 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1221 sel_cell_end = sel_cell_start;
1222 sel_pos_end = sel_pos_start;
1223 UpdateLocal(bv, INIT, true);
1226 case LyXTabular::SET_ALL_LINES:
1228 case LyXTabular::UNSET_ALL_LINES:
1229 for(i=sel_row_start; i<=sel_row_end; ++i)
1230 for(j=sel_col_start; j<=sel_col_end; ++j)
1231 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1232 UpdateLocal(bv, INIT, true);
1234 case LyXTabular::SET_LONGTABULAR:
1235 tabular->SetLongTabular(true);
1236 UpdateLocal(bv, INIT, true); // because this toggles displayed
1238 case LyXTabular::UNSET_LONGTABULAR:
1239 tabular->SetLongTabular(false);
1240 UpdateLocal(bv, INIT, true); // because this toggles displayed
1242 case LyXTabular::SET_ROTATE_TABULAR:
1243 tabular->SetRotateTabular(true);
1245 case LyXTabular::UNSET_ROTATE_TABULAR:
1246 tabular->SetRotateTabular(false);
1248 case LyXTabular::SET_ROTATE_CELL:
1249 for(i=sel_row_start; i<=sel_row_end; ++i)
1250 for(j=sel_col_start; j<=sel_col_end; ++j)
1251 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1253 case LyXTabular::UNSET_ROTATE_CELL:
1254 for(i=sel_row_start; i<=sel_row_end; ++i)
1255 for(j=sel_col_start; j<=sel_col_end; ++j)
1256 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1258 case LyXTabular::SET_LINEBREAKS:
1259 what = !tabular->GetLinebreaks(actcell);
1260 for(i=sel_row_start; i<=sel_row_end; ++i)
1261 for(j=sel_col_start; j<=sel_col_end; ++j)
1262 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1264 case LyXTabular::SET_LTFIRSTHEAD:
1265 tabular->SetLTHead(actcell,true);
1267 case LyXTabular::SET_LTHEAD:
1268 tabular->SetLTHead(actcell,false);
1270 case LyXTabular::SET_LTFOOT:
1271 tabular->SetLTFoot(actcell,false);
1273 case LyXTabular::SET_LTLASTFOOT:
1274 tabular->SetLTFoot(actcell,true);
1276 case LyXTabular::SET_LTNEWPAGE:
1277 what = !tabular->GetLTNewPage(actcell);
1278 tabular->SetLTNewPage(actcell,what);
1283 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1286 // the cursor.pos has to be before the inset so if it isn't now just
1287 // reset the curor pos first!
1288 if (cursor.pos() % 2) { // behind the inset
1289 cursor.pos(cursor.pos() - 1);
1292 UpdatableInset * inset =
1293 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1294 LyXFont font(LyXFont::ALL_SANE);
1296 x = inset->x() + inset->width(bv, font);
1297 y = inset->descent(bv, font);
1299 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1300 inset_y = cursor.y();
1301 inset->Edit(bv, x - inset_x, y - inset_y, button);
1302 if (!the_locking_inset)
1304 UpdateLocal(bv, CELL, false);
1308 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1310 InsetText * inset = tabular->GetCellInset(actcell);
1313 if (cursor.pos() % 2) { // behind the inset
1314 return (((x + top_x) < cursor.x()) &&
1315 ((x + top_x) > (cursor.x() - inset->width(bv,
1316 LyXFont(LyXFont::ALL_SANE)))));
1318 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1319 return ((x1 > x2) &&
1320 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1324 // This returns paperWidth() if the cell-width is unlimited or the width
1325 // in pixels if we have a pwidth for this cell.
1326 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1328 string s = tabular->GetPWidth(cell);
1332 return VSpace(s).inPixels( 0, 0);
1335 int InsetTabular::getMaxWidth(Painter & pain,
1336 UpdatableInset const * inset) const
1339 int n = tabular->GetNumberOfCells();
1340 for(cell=0; cell < n; ++cell) {
1341 if (tabular->GetCellInset(cell) == inset)
1346 int w = GetMaxWidthOfCell(pain, cell);
1347 // this because text insets remove the xpos from the maxwidth because
1348 // otherwise the would not break good!!!
1349 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1354 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1360 for(int j= 0; j < tabular->columns(); ++j) {
1361 for(int i = 0; i < tabular->rows(); ++i) {
1362 if (tabular->IsPartOfMultiColumn(i,j))
1364 cell = tabular->GetCellNumber(i,j);
1365 inset = tabular->GetCellInset(cell);
1366 inset->update(bv, font);
1367 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1369 // cell = tabular->GetCellNumber(0, j);
1370 // cx += tabular->GetWidthOfColumn(cell);