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(Painter & pain, LyXFont const & font) const
137 calculate_width_of_cells(pain, font);
140 return tabular->GetAscentOfRow(0);
144 int InsetTabular::descent(Painter & pain, LyXFont const & font) const
147 calculate_width_of_cells(pain, font);
150 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
154 int InsetTabular::width(Painter & pain, LyXFont const & font) const
157 calculate_width_of_cells(pain, font);
160 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
164 void InsetTabular::draw(Painter & pain, const LyXFont & font, int baseline,
172 UpdatableInset::draw(pain,font,baseline,x);
173 if (init_inset || (top_x != int(x)) || (top_baseline != baseline)) {
177 top_baseline = baseline;
179 // recomputeTextInsets(pain, font);
180 calculate_width_of_cells(pain, font);
184 x += ADD_TO_TABULAR_WIDTH;
185 for(i=0;i<tabular->rows();++i) {
187 for(j=0;j<tabular->columns();++j) {
188 if (tabular->IsPartOfMultiColumn(i,j))
190 cx = nx + tabular->GetBeginningOfTextInCell(cell);
192 DrawCellSelection(pain, nx, baseline, i, j, cell);
193 tabular->GetCellInset(cell)->draw(pain, font, baseline, cx);
194 DrawCellLines(pain, nx, baseline, i, cell);
195 nx += tabular->GetWidthOfColumn(cell);
198 baseline += tabular->GetDescentOfRow(i) + tabular->GetAscentOfRow(i+1)
199 + tabular->GetAdditionalHeight(cell+1);
201 x += width(pain, font);
205 void InsetTabular::update(BufferView * bv, LyXFont const & font) const
208 recomputeTextInsets(bv, font);
212 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
213 int row, int cell) const
215 int x2 = x + tabular->GetWidthOfColumn(cell);
218 if (!tabular->TopAlreadyDrawed(cell)) {
219 on_off = !tabular->TopLine(cell);
220 pain.line(x, baseline - tabular->GetAscentOfRow(row),
221 x2, baseline - tabular->GetAscentOfRow(row),
222 on_off ? LColor::tabularonoffline:LColor::tabularline,
223 on_off ? Painter::line_onoffdash:Painter::line_solid);
225 on_off = !tabular->BottomLine(cell);
226 pain.line(x,baseline + tabular->GetDescentOfRow(row),
227 x2, baseline + tabular->GetDescentOfRow(row),
228 on_off ? LColor::tabularonoffline:LColor::tabularline,
229 on_off ? Painter::line_onoffdash:Painter::line_solid);
230 if (!tabular->LeftAlreadyDrawed(cell)) {
231 on_off = !tabular->LeftLine(cell);
232 pain.line(x, baseline - tabular->GetAscentOfRow(row),
233 x, baseline + tabular->GetDescentOfRow(row),
234 on_off ? LColor::tabularonoffline:LColor::tabularline,
235 on_off ? Painter::line_onoffdash:Painter::line_solid);
237 on_off = !tabular->RightLine(cell);
238 pain.line(x2 - tabular->GetAdditionalWidth(cell),
239 baseline - tabular->GetAscentOfRow(row),
240 x2 - tabular->GetAdditionalWidth(cell),
241 baseline + tabular->GetDescentOfRow(row),
242 on_off ? LColor::tabularonoffline:LColor::tabularline,
243 on_off ? Painter::line_onoffdash:Painter::line_solid);
247 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
248 int row, int column, int cell) const
252 int cs = tabular->column_of_cell(sel_cell_start);
253 int ce = tabular->column_of_cell(sel_cell_end);
256 cs = tabular->column_of_cell(sel_cell_end);
258 ce = tabular->right_column_of_cell(sel_cell_end);
261 int rs = tabular->row_of_cell(sel_cell_start);
262 int re = tabular->row_of_cell(sel_cell_end);
269 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
270 int w = tabular->GetWidthOfColumn(cell);
271 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
272 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
273 w, h, LColor::selection);
278 char const * InsetTabular::EditMessage() const
280 return _("Opened Tabular Inset");
284 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
286 UpdatableInset::Edit(bv, x, y, button);
288 if (!bv->lockInset(this)) {
289 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
293 the_locking_inset = 0;
294 inset_pos = inset_x = inset_y = 0;
295 setPos(bv->painter(), x, y);
296 sel_pos_start = sel_pos_end = cursor.pos();
297 sel_cell_start = sel_cell_end = actcell;
298 bv->text->FinishUndo();
299 if (InsetHit(bv, x, y)) {
300 ActivateCellInset(bv, x, y, button);
302 UpdateLocal(bv, true, false);
303 // bv->getOwner()->getPopups().updateFormTabular();
307 void InsetTabular::InsetUnlock(BufferView * bv)
310 if (the_locking_inset) {
311 the_locking_inset->InsetUnlock(bv);
312 the_locking_inset = 0;
315 if (hasSelection()) {
316 sel_pos_start = sel_pos_end = cursor.pos();
317 sel_cell_start = sel_cell_end = actcell;
318 UpdateLocal(bv, false, false);
320 no_selection = false;
325 void InsetTabular::UpdateLocal(BufferView * bv, bool what, bool mark_dirty)
328 calculate_width_of_cells(bv->painter(), LyXFont(LyXFont::ALL_SANE));
329 bv->updateInset(this, mark_dirty);
331 resetPos(bv->painter());
334 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
336 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
340 if (inset == tabular->GetCellInset(actcell)) {
341 lyxerr[Debug::INSETS] << "OK" << endl;
342 the_locking_inset = tabular->GetCellInset(actcell);
343 resetPos(bv->painter());
344 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
345 inset_y = cursor.y();
346 inset_pos = cursor.pos();
348 } else if (the_locking_inset && (the_locking_inset == inset)) {
349 if (cursor.pos() == inset_pos) {
350 lyxerr[Debug::INSETS] << "OK" << endl;
351 resetPos(bv->painter());
352 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
353 inset_y = cursor.y();
355 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
357 } else if (the_locking_inset) {
358 lyxerr[Debug::INSETS] << "MAYBE" << endl;
359 return the_locking_inset->LockInsetInInset(bv, inset);
361 lyxerr[Debug::INSETS] << "NOT OK" << endl;
365 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
368 if (!the_locking_inset)
370 if (the_locking_inset == inset) {
371 the_locking_inset->InsetUnlock(bv);
372 the_locking_inset = 0;
374 moveRight(bv, false);
377 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
378 if ((inset->LyxCode() == TABULAR_CODE) &&
379 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
381 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
389 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
391 if (!the_locking_inset)
393 if (the_locking_inset != inset)
394 return the_locking_inset->UpdateInsetInInset(bv, inset);
395 UpdateLocal(bv, true, false);
400 int InsetTabular::InsetInInsetY()
402 if (!the_locking_inset)
405 return (inset_y + the_locking_inset->InsetInInsetY());
409 UpdatableInset * InsetTabular::GetLockingInset()
411 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
415 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
419 if (the_locking_inset)
420 return the_locking_inset->GetFirstLockingInsetOfType(c);
425 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
427 if (the_locking_inset)
428 return the_locking_inset->InsertInset(bv, inset);
433 void InsetTabular::InsetButtonRelease(BufferView * bv,
434 int x, int y, int button)
437 if (the_locking_inset) {
439 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
440 i->InsetButtonRelease(bv, x, y, button);
444 MenuLayoutTabular(true, this);
447 if (the_locking_inset) {
448 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
451 no_selection = false;
455 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
457 if (hasSelection()) {
458 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
459 UpdateLocal(bv, false, false);
461 no_selection = false;
465 setPos(bv->painter(), x, y);
466 sel_pos_start = sel_pos_end = cursor.pos();
467 sel_cell_start = sel_cell_end = actcell;
469 bool inset_hit = InsetHit(bv, x, y);
471 if ((ocell == actcell) && the_locking_inset && inset_hit) {
472 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
474 } else if (the_locking_inset) {
475 the_locking_inset->InsetUnlock(bv);
477 the_locking_inset = 0;
478 if (inset_hit && bv->the_locking_inset) {
479 ActivateCellInset(bv, x, y, button);
480 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
485 bview->getOwner()->getPopups().showFormTabular();
486 else if (ocell != actcell)
487 bview->getOwner()->getPopups().updateFormTabular();
492 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
494 if (the_locking_inset) {
495 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
496 y - inset_y, button);
500 // int ocell = actcell,
501 int old = sel_pos_end;
503 setPos(bv->painter(), x, y);
504 sel_pos_end = cursor.pos();
505 sel_cell_end = actcell;
506 if (old != sel_pos_end)
507 UpdateLocal(bv, false, false);
509 if (ocell != actcell)
510 bview->getOwner()->getPopups().updateFormTabular();
513 no_selection = false;
517 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
519 if (the_locking_inset) {
520 the_locking_inset->InsetKeyPress(xke);
526 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
529 UpdatableInset::RESULT
532 no_selection = false;
533 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
534 || (result == DISPATCHED_NOUPDATE)) {
536 resetPos(bv->painter());
541 if ((action < 0) && arg.empty())
544 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
545 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
547 if (the_locking_inset) {
548 result=the_locking_inset->LocalDispatch(bv, action, arg);
549 if (result == DISPATCHED_NOUPDATE)
551 else if (result == DISPATCHED) {
552 bool upd = SetCellDimensions(bv->painter(), actcell, actrow);
553 the_locking_inset->ToggleInsetCursor(bv);
554 UpdateLocal(bv, upd, false);
555 the_locking_inset->ToggleInsetCursor(bv);
557 } else if (result == FINISHED) {
558 if ((action == LFUN_RIGHT) || (action == -1)) {
559 cursor.pos(inset_pos + 1);
560 resetPos(bv->painter());
562 sel_pos_start = sel_pos_end = cursor.pos();
563 sel_cell_start = sel_cell_end = actcell;
570 bool hs = hasSelection();
573 // Normal chars not handled here
576 // --- Cursor Movements ---------------------------------------------
578 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
580 moveRight(bv, false);
581 sel_pos_end = cursor.pos();
582 if (!cellstart(cursor.pos())) {
583 if (tabular->right_column_of_cell(sel_cell_start) >
584 tabular->right_column_of_cell(actcell))
585 sel_cell_end = actcell+1;
587 sel_cell_end = actcell;
589 UpdateLocal(bv, false, false);
592 result = moveRight(bv);
593 sel_pos_start = sel_pos_end = cursor.pos();
594 sel_cell_start = sel_cell_end = actcell;
596 UpdateLocal(bv, false, false);
599 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
602 sel_pos_end = cursor.pos();
603 if (cellstart(cursor.pos())) {
604 if (tabular->column_of_cell(sel_cell_start) >=
605 tabular->column_of_cell(actcell))
606 sel_cell_end = actcell;
608 sel_cell_end = actcell-1;
610 UpdateLocal(bv, false, false);
613 result = moveLeft(bv);
614 sel_pos_start = sel_pos_end = cursor.pos();
615 sel_cell_start = sel_cell_end = actcell;
617 UpdateLocal(bv, false, false);
623 sel_pos_end = cursor.pos();
624 if ((ocell == sel_cell_end) ||
625 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
626 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
628 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
629 UpdateLocal(bv, false, false);
633 result= moveDown(bv);
634 sel_pos_start = sel_pos_end = cursor.pos();
635 sel_cell_start = sel_cell_end = actcell;
637 UpdateLocal(bv, false, false);
643 sel_pos_end = cursor.pos();
644 if ((ocell == sel_cell_end) ||
645 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
646 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
648 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
649 UpdateLocal(bv, false, false);
654 sel_pos_start = sel_pos_end = cursor.pos();
655 sel_cell_start = sel_cell_end = actcell;
657 UpdateLocal(bv, false, false);
669 if (the_locking_inset) {
670 the_locking_inset->InsetUnlock(bv);
672 the_locking_inset = 0;
673 if (action == LFUN_TAB)
677 sel_pos_start = sel_pos_end = cursor.pos();
678 sel_cell_start = sel_cell_end = actcell;
680 UpdateLocal(bv, false, false);
682 case LFUN_LAYOUT_TABLE:
684 int flag = (arg == "true");
685 MenuLayoutTabular(flag, this);
689 result = UNDISPATCHED;
692 if (result!=FINISHED) {
693 if (!the_locking_inset) {
695 if (ocell != actcell)
696 bview->getOwner()->getPopups().updateFormTabular();
701 bv->unlockInset(this);
706 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
708 return tabular->Latex(buf, os, fragile, fp);
712 int InsetTabular::Ascii(Buffer const *, ostream &) const
717 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
723 int InsetTabular::DocBook(Buffer const *, ostream &) const
729 void InsetTabular::Validate(LaTeXFeatures & features) const
731 tabular->Validate(features);
735 void InsetTabular::calculate_width_of_cells(Painter & pain,
736 LyXFont const & font) const
742 for(int i = 0; i < tabular->rows(); ++i) {
743 maxAsc = maxDesc = 0;
744 for(int j= 0; j < tabular->columns(); ++j) {
745 if (tabular->IsPartOfMultiColumn(i,j))
748 inset = tabular->GetCellInset(cell);
749 maxAsc = max(maxAsc, inset->ascent(pain, font));
750 maxDesc = max(maxDesc, inset->descent(pain, font));
751 tabular->SetWidthOfCell(cell, inset->width(pain, font));
753 tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
754 tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
759 void InsetTabular::GetCursorPos(int & x, int & y) const
761 x = cursor.x() - top_x;
766 void InsetTabular::ToggleInsetCursor(BufferView * bv)
768 if (the_locking_inset) {
769 the_locking_inset->ToggleInsetCursor(bv);
773 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
775 int asc = lyxfont::maxAscent(font);
776 int desc = lyxfont::maxDescent(font);
779 bv->hideLockedInsetCursor();
781 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
782 cursor_visible = !cursor_visible;
786 void InsetTabular::ShowInsetCursor(BufferView * bv)
788 if (!cursor_visible) {
789 LyXFont font; // = GetFont(par, cursor.pos);
791 int asc = lyxfont::maxAscent(font);
792 int desc = lyxfont::maxDescent(font);
793 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
794 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
795 cursor_visible = true;
800 void InsetTabular::HideInsetCursor(BufferView * bv)
803 ToggleInsetCursor(bv);
807 void InsetTabular::setPos(Painter & pain, int x, int y) const
812 actcell = actrow = actcol = 0;
813 int ly = tabular->GetDescentOfRow(actrow);
815 // first search the right row
816 while((ly < y) && (actrow < tabular->rows())) {
817 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
818 tabular->GetAscentOfRow(actrow+1) +
819 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
822 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
824 actcell = tabular->GetCellNumber(actrow, actcol);
826 // now search the right column
827 int lx = tabular->GetWidthOfColumn(actcell) -
828 tabular->GetAdditionalWidth(actcell);
829 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
830 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
831 tabular->GetAdditionalWidth(actcell - 1));
832 cursor.pos(((actcell+1) * 2) - 1);
834 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
835 cursor.x(lx + top_x - 2);
837 cursor.pos(cursor.pos() - 1);
838 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
843 int InsetTabular::getCellXPos(int cell) const
847 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
849 int lx = tabular->GetWidthOfColumn(cell);
850 for(; (c < cell); ++c) {
851 lx += tabular->GetWidthOfColumn(c);
853 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
854 ADD_TO_TABULAR_WIDTH);
857 void InsetTabular::resetPos(Painter & pain) const
861 actcol = tabular->column_of_cell(actcell);
866 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
867 if (tabular->IsLastCellInRow(cell)) {
868 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
869 tabular->GetAscentOfRow(actrow + 1) +
870 tabular->GetAdditionalHeight(cell + 1));
874 cursor.x(getCellXPos(actcell) + 2);
875 if (cursor.pos() % 2) {
876 LyXFont font(LyXFont::ALL_SANE);
877 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(pain,font) +
878 tabular->GetBeginningOfTextInCell(actcell));
880 if ((!the_locking_inset ||
881 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
882 (actcell != oldcell)) {
883 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
889 bool InsetTabular::SetCellDimensions(Painter & pain, int cell, int row)
891 InsetText * inset = tabular->GetCellInset(cell);
892 LyXFont font(LyXFont::ALL_SANE);
893 int asc = inset->ascent(pain, font) + ADD_TO_HEIGHT;
894 int desc = inset->descent(pain, font) + ADD_TO_HEIGHT;
895 int maxAsc = tabular->GetAscentOfRow(row);
896 int maxDesc = tabular->GetDescentOfRow(row);
897 bool ret = tabular->SetWidthOfCell(cell, inset->width(pain, font));
901 tabular->SetAscentOfRow(row, asc);
903 if (maxDesc < desc) {
905 tabular->SetDescentOfRow(row, desc);
911 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
913 if (!cellstart(cursor.pos())) {
914 if (tabular->IsLastCell(actcell))
917 cursor.pos(cursor.pos() + 1);
919 if (ActivateCellInset(bv))
921 } else { // before the inset
922 cursor.pos(cursor.pos() + 1);
924 resetPos(bv->painter());
925 return DISPATCHED_NOUPDATE;
929 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
936 cursor.pos(cursor.pos() - 1);
937 if (!cellstart(cursor.pos())) {
939 } else if (lock) { // behind the inset
940 if (ActivateCellInset(bv, 0, 0, 0, true))
943 resetPos(bv->painter());
944 return DISPATCHED_NOUPDATE;
948 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
951 actcell = tabular->GetCellAbove(actcell);
952 if (actcell == ocell) // we moved out of the inset
954 resetPos(bv->painter());
955 return DISPATCHED_NOUPDATE;
959 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
962 actcell = tabular->GetCellBelow(actcell);
963 if (actcell == ocell) // we moved out of the inset
965 resetPos(bv->painter());
966 return DISPATCHED_NOUPDATE;
970 bool InsetTabular::moveNextCell(BufferView * bv)
972 if (tabular->IsLastCell(actcell))
975 cursor.pos(cursor.pos() + 1);
976 if (!cellstart(cursor.pos()))
977 cursor.pos(cursor.pos() + 1);
978 resetPos(bv->painter());
983 bool InsetTabular::movePrevCell(BufferView * bv)
985 if (!actcell) // first cell
988 cursor.pos(cursor.pos() - 1);
989 if (cellstart(cursor.pos()))
990 cursor.pos(cursor.pos() - 1);
991 resetPos(bv->painter());
996 bool InsetTabular::Delete()
1002 void InsetTabular::SetFont(BufferView *, LyXFont const &, bool)
1007 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1016 setAlign = LYX_ALIGN_LEFT,
1022 case LyXTabular::ALIGN_LEFT:
1023 setAlign=LYX_ALIGN_LEFT;
1025 case LyXTabular::ALIGN_RIGHT:
1026 setAlign=LYX_ALIGN_RIGHT;
1028 case LyXTabular::ALIGN_CENTER:
1029 setAlign=LYX_ALIGN_CENTER;
1034 if (hasSelection()) {
1036 sel_col_start = tabular->column_of_cell(sel_cell_start);
1037 sel_col_end = tabular->column_of_cell(sel_cell_end);
1038 if (sel_col_start > sel_col_end) {
1039 sel_col_end = sel_col_start;
1040 sel_col_start = tabular->column_of_cell(sel_cell_end);
1042 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1045 sel_row_start = tabular->row_of_cell(sel_cell_start);
1046 sel_row_end = tabular->row_of_cell(sel_cell_end);
1047 if (sel_row_start > sel_row_end) {
1048 tmp = sel_row_start;
1049 sel_row_start = sel_row_end;
1053 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1054 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1056 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1057 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1058 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1060 int row = tabular->row_of_cell(actcell);
1061 int column = tabular->column_of_cell(actcell);
1064 case LyXTabular::SET_PWIDTH:
1066 bool update = (tabular->GetPWidth(actcell) != val);
1067 tabular->SetPWidth(actcell,val);
1069 UpdateLocal(bv, true, true);
1072 case LyXTabular::SET_SPECIAL_COLUMN:
1073 case LyXTabular::SET_SPECIAL_MULTI:
1074 tabular->SetAlignSpecial(actcell,val,feature);
1076 case LyXTabular::APPEND_ROW:
1077 // append the row into the tabular
1078 UnlockInsetInInset(bv, the_locking_inset);
1079 tabular->AppendRow(actcell);
1080 UpdateLocal(bv, true, true);
1082 case LyXTabular::APPEND_COLUMN:
1083 // append the column into the tabular
1084 tabular->AppendColumn(actcell);
1085 actcell = tabular->GetCellNumber(row, column);
1086 UpdateLocal(bv, true, true);
1088 case LyXTabular::DELETE_ROW:
1089 tabular->DeleteRow(tabular->row_of_cell(actcell));
1090 if ((row+1) > tabular->rows())
1092 actcell = tabular->GetCellNumber(row, column);
1093 UpdateLocal(bv, true, true);
1095 case LyXTabular::DELETE_COLUMN:
1096 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1097 if ((column+1) > tabular->columns())
1099 actcell = tabular->GetCellNumber(row, column);
1100 UpdateLocal(bv, true, true);
1102 case LyXTabular::TOGGLE_LINE_TOP:
1103 lineSet = !tabular->TopLine(actcell);
1104 for(i=sel_row_start; i<=sel_row_end; ++i)
1105 for(j=sel_col_start; j<=sel_col_end; ++j)
1106 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1107 UpdateLocal(bv, true, true);
1110 case LyXTabular::TOGGLE_LINE_BOTTOM:
1111 lineSet = !tabular->BottomLine(actcell);
1112 for(i=sel_row_start; i<=sel_row_end; ++i)
1113 for(j=sel_col_start; j<=sel_col_end; ++j)
1114 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1115 UpdateLocal(bv, true, true);
1118 case LyXTabular::TOGGLE_LINE_LEFT:
1119 lineSet = !tabular->LeftLine(actcell);
1120 for(i=sel_row_start; i<=sel_row_end; ++i)
1121 for(j=sel_col_start; j<=sel_col_end; ++j)
1122 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1123 UpdateLocal(bv, true, true);
1126 case LyXTabular::TOGGLE_LINE_RIGHT:
1127 lineSet = !tabular->RightLine(actcell);
1128 for(i=sel_row_start; i<=sel_row_end; ++i)
1129 for(j=sel_col_start; j<=sel_col_end; ++j)
1130 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1131 UpdateLocal(bv, true, true);
1133 case LyXTabular::ALIGN_LEFT:
1134 case LyXTabular::ALIGN_RIGHT:
1135 case LyXTabular::ALIGN_CENTER:
1136 for(i=sel_row_start; i<=sel_row_end; ++i)
1137 for(j=sel_col_start; j<=sel_col_end; ++j)
1138 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1139 UpdateLocal(bv, true, true);
1141 case LyXTabular::MULTICOLUMN:
1143 if (sel_row_start != sel_row_end) {
1144 WriteAlert(_("Impossible Operation!"),
1145 _("Multicolumns can only be horizontally."),
1149 // just multicol for one Single Cell
1150 if (!hasSelection()) {
1151 // check wether we are completly in a multicol
1152 if (tabular->IsMultiColumn(actcell)) {
1153 tabular->UnsetMultiColumn(actcell);
1154 UpdateLocal(bv, true, true);
1156 tabular->SetMultiColumn(actcell, 1);
1157 UpdateLocal(bv, false, true);
1161 // we have a selection so this means we just add all this
1162 // cells to form a multicolumn cell
1166 if (sel_cell_start > sel_cell_end) {
1167 s_start = sel_cell_end;
1168 s_end = sel_cell_start;
1170 s_start = sel_cell_start;
1171 s_end = sel_cell_end;
1173 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1176 sel_cell_end = sel_cell_start;
1177 sel_pos_end = sel_pos_start;
1178 UpdateLocal(bv, true, true);
1181 case LyXTabular::SET_ALL_LINES:
1183 case LyXTabular::UNSET_ALL_LINES:
1184 for(i=sel_row_start; i<=sel_row_end; ++i)
1185 for(j=sel_col_start; j<=sel_col_end; ++j)
1186 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1187 UpdateLocal(bv, true, true);
1189 case LyXTabular::SET_LONGTABULAR:
1190 tabular->SetLongTabular(true);
1191 UpdateLocal(bv, true, true); // because this toggles displayed
1193 case LyXTabular::UNSET_LONGTABULAR:
1194 tabular->SetLongTabular(false);
1195 UpdateLocal(bv, true, true); // because this toggles displayed
1197 case LyXTabular::SET_ROTATE_TABULAR:
1198 tabular->SetRotateTabular(true);
1200 case LyXTabular::UNSET_ROTATE_TABULAR:
1201 tabular->SetRotateTabular(false);
1203 case LyXTabular::SET_ROTATE_CELL:
1204 for(i=sel_row_start; i<=sel_row_end; ++i)
1205 for(j=sel_col_start; j<=sel_col_end; ++j)
1206 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1208 case LyXTabular::UNSET_ROTATE_CELL:
1209 for(i=sel_row_start; i<=sel_row_end; ++i)
1210 for(j=sel_col_start; j<=sel_col_end; ++j)
1211 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1213 case LyXTabular::SET_LINEBREAKS:
1214 what = !tabular->GetLinebreaks(actcell);
1215 for(i=sel_row_start; i<=sel_row_end; ++i)
1216 for(j=sel_col_start; j<=sel_col_end; ++j)
1217 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1219 case LyXTabular::SET_LTFIRSTHEAD:
1220 tabular->SetLTHead(actcell,true);
1222 case LyXTabular::SET_LTHEAD:
1223 tabular->SetLTHead(actcell,false);
1225 case LyXTabular::SET_LTFOOT:
1226 tabular->SetLTFoot(actcell,false);
1228 case LyXTabular::SET_LTLASTFOOT:
1229 tabular->SetLTFoot(actcell,true);
1231 case LyXTabular::SET_LTNEWPAGE:
1232 what = !tabular->GetLTNewPage(actcell);
1233 tabular->SetLTNewPage(actcell,what);
1238 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1241 // the cursor.pos has to be before the inset so if it isn't now just
1242 // reset the curor pos first!
1243 if (cursor.pos() % 2) { // behind the inset
1244 cursor.pos(cursor.pos() - 1);
1245 resetPos(bv->painter());
1247 UpdatableInset * inset =
1248 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1249 LyXFont font(LyXFont::ALL_SANE);
1251 x = inset->x() + inset->width(bv->painter(), font);
1252 y = inset->descent(bv->painter(), font);
1254 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1255 inset_y = cursor.y();
1256 inset->Edit(bv, x - inset_x, y - inset_y, button);
1257 if (!the_locking_inset)
1259 UpdateLocal(bv, true, false);
1263 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1265 InsetText * inset = tabular->GetCellInset(actcell);
1268 if (cursor.pos() % 2) { // behind the inset
1269 return (((x + top_x) < cursor.x()) &&
1270 ((x + top_x) > (cursor.x() - inset->width(bv->painter(),
1271 LyXFont(LyXFont::ALL_SANE)))));
1273 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1274 return ((x1 > x2) &&
1275 (x1 < (x2 + inset->width(bv->painter(),
1276 LyXFont(LyXFont::ALL_SANE)))));
1280 // This returns paperWidth() if the cell-width is unlimited or the width
1281 // in pixels if we have a pwidth for this cell.
1282 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1286 if ((w=tabular->GetPWidth(cell)).empty())
1288 return VSpace(w).inPixels( 0, 0);
1291 int InsetTabular::getMaxWidth(Painter & pain,
1292 UpdatableInset const * inset) const
1295 int n = tabular->GetNumberOfCells();
1296 for(cell=0; cell < n; ++cell) {
1297 if (tabular->GetCellInset(cell) == inset)
1302 int w = GetMaxWidthOfCell(pain, cell);
1303 // this because text insets remove the xpos from the maxwidth because
1304 // otherwise the would not break good!!!
1305 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1310 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1316 for(int j= 0; j < tabular->columns(); ++j) {
1317 for(int i = 0; i < tabular->rows(); ++i) {
1318 if (tabular->IsPartOfMultiColumn(i,j))
1320 cell = tabular->GetCellNumber(i,j);
1321 inset = tabular->GetCellInset(cell);
1322 inset->update(bv, font);
1323 inset->computeTextRows(bv->painter());
1324 tabular->SetWidthOfCell(cell, inset->width(bv->painter(), font));
1326 // cell = tabular->GetCellNumber(0, j);
1327 // cx += tabular->GetWidthOfColumn(cell);