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 &, LyXFont const &) const
136 return tabular->GetAscentOfRow(0);
140 int InsetTabular::descent(Painter &, LyXFont const &) const
142 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
146 int InsetTabular::width(Painter &, 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);
161 if ((need_update == INIT)|| (top_x != int(x)) || (top_baseline != baseline)) {
164 top_baseline = baseline;
166 if (locked) { // repaint this way as the background was not cleared
167 if (the_locking_inset)
168 the_locking_inset->update(bv, font, true);
170 bv->updateInset(const_cast<InsetTabular*>(this), false);
176 x += ADD_TO_TABULAR_WIDTH;
177 if (cleared || !locked || (need_update == FULL) || (need_update == CELL)) {
178 for(i=0;i<tabular->rows();++i) {
180 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
181 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
182 for(j=0;j<tabular->columns();++j) {
183 if (tabular->IsPartOfMultiColumn(i,j))
185 cx = nx + tabular->GetBeginningOfTextInCell(cell);
187 DrawCellSelection(pain, nx, baseline, i, j, cell);
188 if (dodraw && !cleared && locked && the_locking_inset) {
189 if (the_locking_inset == tabular->GetCellInset(cell))
190 tabular->GetCellInset(cell)->draw(bv, font,
194 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
196 DrawCellLines(pain, nx, baseline, i, cell);
198 nx += tabular->GetWidthOfColumn(cell);
201 baseline += tabular->GetDescentOfRow(i) +
202 tabular->GetAscentOfRow(i+1)+
203 tabular->GetAdditionalHeight(cell+1);
206 x += width(pain, font);
211 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
212 int row, int cell) const
214 int x2 = x + tabular->GetWidthOfColumn(cell);
217 if (!tabular->TopAlreadyDrawed(cell)) {
218 on_off = !tabular->TopLine(cell);
219 pain.line(x, baseline - tabular->GetAscentOfRow(row),
220 x2, baseline - tabular->GetAscentOfRow(row),
221 on_off ? LColor::tabularonoffline:LColor::tabularline,
222 on_off ? Painter::line_onoffdash:Painter::line_solid);
224 on_off = !tabular->BottomLine(cell);
225 pain.line(x,baseline + tabular->GetDescentOfRow(row),
226 x2, baseline + tabular->GetDescentOfRow(row),
227 on_off ? LColor::tabularonoffline:LColor::tabularline,
228 on_off ? Painter::line_onoffdash:Painter::line_solid);
229 if (!tabular->LeftAlreadyDrawed(cell)) {
230 on_off = !tabular->LeftLine(cell);
231 pain.line(x, baseline - tabular->GetAscentOfRow(row),
232 x, baseline + tabular->GetDescentOfRow(row),
233 on_off ? LColor::tabularonoffline:LColor::tabularline,
234 on_off ? Painter::line_onoffdash:Painter::line_solid);
236 on_off = !tabular->RightLine(cell);
237 pain.line(x2 - tabular->GetAdditionalWidth(cell),
238 baseline - tabular->GetAscentOfRow(row),
239 x2 - tabular->GetAdditionalWidth(cell),
240 baseline + tabular->GetDescentOfRow(row),
241 on_off ? LColor::tabularonoffline:LColor::tabularline,
242 on_off ? Painter::line_onoffdash:Painter::line_solid);
246 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
247 int row, int column, int cell) const
251 int cs = tabular->column_of_cell(sel_cell_start);
252 int ce = tabular->column_of_cell(sel_cell_end);
255 cs = tabular->column_of_cell(sel_cell_end);
257 ce = tabular->right_column_of_cell(sel_cell_end);
260 int rs = tabular->row_of_cell(sel_cell_start);
261 int re = tabular->row_of_cell(sel_cell_end);
268 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
269 int w = tabular->GetWidthOfColumn(cell);
270 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
271 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
272 w, h, LColor::selection);
277 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool dodraw)
279 if (the_locking_inset)
280 the_locking_inset->update(bv, font, dodraw);
281 switch(need_update) {
285 if (calculate_dimensions_of_cells(bv, font, dodraw))
297 char const * InsetTabular::EditMessage() const
299 return _("Opened Tabular Inset");
303 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
305 UpdatableInset::Edit(bv, x, y, button);
307 if (!bv->lockInset(this)) {
308 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
312 the_locking_inset = 0;
313 inset_pos = inset_x = inset_y = 0;
314 setPos(bv->painter(), x, y);
315 sel_pos_start = sel_pos_end = cursor.pos();
316 sel_cell_start = sel_cell_end = actcell;
317 bv->text->FinishUndo();
318 if (InsetHit(bv, x, y)) {
319 ActivateCellInset(bv, x, y, button);
321 UpdateLocal(bv, NONE, false);
322 // bv->getOwner()->getPopups().updateFormTabular();
326 void InsetTabular::InsetUnlock(BufferView * bv)
329 if (the_locking_inset) {
330 the_locking_inset->InsetUnlock(bv);
331 the_locking_inset = 0;
334 if (hasSelection()) {
335 sel_pos_start = sel_pos_end = 0;
336 sel_cell_start = sel_cell_end = 0;
337 UpdateLocal(bv, FULL, false);
339 no_selection = false;
344 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
348 bv->updateInset(this, mark_dirty);
350 resetPos(bv->painter());
353 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
355 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
359 if (inset == tabular->GetCellInset(actcell)) {
360 lyxerr[Debug::INSETS] << "OK" << endl;
361 the_locking_inset = tabular->GetCellInset(actcell);
362 resetPos(bv->painter());
363 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
364 inset_y = cursor.y();
365 inset_pos = cursor.pos();
367 } else if (the_locking_inset && (the_locking_inset == inset)) {
368 if (cursor.pos() == inset_pos) {
369 lyxerr[Debug::INSETS] << "OK" << endl;
370 resetPos(bv->painter());
371 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
372 inset_y = cursor.y();
374 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
376 } else if (the_locking_inset) {
377 lyxerr[Debug::INSETS] << "MAYBE" << endl;
378 return the_locking_inset->LockInsetInInset(bv, inset);
380 lyxerr[Debug::INSETS] << "NOT OK" << endl;
384 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
387 if (!the_locking_inset)
389 if (the_locking_inset == inset) {
390 the_locking_inset->InsetUnlock(bv);
391 the_locking_inset = 0;
393 moveRight(bv, false);
394 UpdateLocal(bv, CELL, false);
397 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
398 if ((inset->LyxCode() == TABULAR_CODE) &&
399 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
401 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
409 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
411 if (!the_locking_inset)
413 if (the_locking_inset != inset)
414 return the_locking_inset->UpdateInsetInInset(bv, inset);
415 UpdateLocal(bv, CELL, false);
420 int InsetTabular::InsetInInsetY()
422 if (!the_locking_inset)
425 return (inset_y + the_locking_inset->InsetInInsetY());
429 UpdatableInset * InsetTabular::GetLockingInset()
431 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
435 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
439 if (the_locking_inset)
440 return the_locking_inset->GetFirstLockingInsetOfType(c);
445 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
447 if (the_locking_inset)
448 return the_locking_inset->InsertInset(bv, inset);
453 void InsetTabular::InsetButtonRelease(BufferView * bv,
454 int x, int y, int button)
457 if (the_locking_inset) {
459 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
460 i->InsetButtonRelease(bv, x, y, button);
464 MenuLayoutTabular(true, this);
467 if (the_locking_inset) {
468 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
471 no_selection = false;
475 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
477 if (hasSelection()) {
478 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
479 UpdateLocal(bv, SELECTION, false);
481 no_selection = false;
485 setPos(bv->painter(), x, y);
486 sel_pos_start = sel_pos_end = cursor.pos();
487 sel_cell_start = sel_cell_end = actcell;
489 bool inset_hit = InsetHit(bv, x, y);
491 if ((ocell == actcell) && the_locking_inset && inset_hit) {
492 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
494 } else if (the_locking_inset) {
495 the_locking_inset->InsetUnlock(bv);
497 the_locking_inset = 0;
498 if (inset_hit && bv->the_locking_inset) {
499 ActivateCellInset(bv, x, y, button);
500 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
505 bview->getOwner()->getPopups().showFormTabular();
506 else if (ocell != actcell)
507 bview->getOwner()->getPopups().updateFormTabular();
512 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
514 if (the_locking_inset) {
515 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
516 y - inset_y, button);
520 // int ocell = actcell,
521 int old = sel_pos_end;
523 setPos(bv->painter(), x, y);
524 sel_pos_end = cursor.pos();
525 sel_cell_end = actcell;
526 if (old != sel_pos_end)
527 UpdateLocal(bv, SELECTION, false);
529 if (ocell != actcell)
530 bview->getOwner()->getPopups().updateFormTabular();
533 no_selection = false;
537 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
539 if (the_locking_inset) {
540 the_locking_inset->InsetKeyPress(xke);
546 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
549 UpdatableInset::RESULT
552 no_selection = false;
553 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
554 || (result == DISPATCHED_NOUPDATE)) {
556 resetPos(bv->painter());
561 if ((action < 0) && arg.empty())
564 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
565 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
567 if (the_locking_inset) {
568 result=the_locking_inset->LocalDispatch(bv, action, arg);
569 if (result == DISPATCHED_NOUPDATE)
571 else if (result == DISPATCHED) {
572 the_locking_inset->ToggleInsetCursor(bv);
573 UpdateLocal(bv, CELL, false);
574 the_locking_inset->ToggleInsetCursor(bv);
576 } else if (result == FINISHED) {
577 if ((action == LFUN_RIGHT) || (action == -1)) {
578 cursor.pos(inset_pos + 1);
579 resetPos(bv->painter());
581 sel_pos_start = sel_pos_end = cursor.pos();
582 sel_cell_start = sel_cell_end = actcell;
589 bool hs = hasSelection();
592 // Normal chars not handled here
595 // --- Cursor Movements ---------------------------------------------
597 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
599 moveRight(bv, false);
600 sel_pos_end = cursor.pos();
601 if (!cellstart(cursor.pos())) {
602 if (tabular->right_column_of_cell(sel_cell_start) >
603 tabular->right_column_of_cell(actcell))
604 sel_cell_end = actcell+1;
606 sel_cell_end = actcell;
608 UpdateLocal(bv, SELECTION, false);
611 result = moveRight(bv);
612 sel_pos_start = sel_pos_end = cursor.pos();
613 sel_cell_start = sel_cell_end = actcell;
615 UpdateLocal(bv, CURSOR, false);
618 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
621 sel_pos_end = cursor.pos();
622 if (cellstart(cursor.pos())) {
623 if (tabular->column_of_cell(sel_cell_start) >=
624 tabular->column_of_cell(actcell))
625 sel_cell_end = actcell;
627 sel_cell_end = actcell-1;
629 UpdateLocal(bv, SELECTION, false);
632 result = moveLeft(bv);
633 sel_pos_start = sel_pos_end = cursor.pos();
634 sel_cell_start = sel_cell_end = actcell;
636 UpdateLocal(bv, CURSOR, false);
642 sel_pos_end = cursor.pos();
643 if ((ocell == sel_cell_end) ||
644 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
645 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
647 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
648 UpdateLocal(bv, SELECTION, false);
652 result= moveDown(bv);
653 sel_pos_start = sel_pos_end = cursor.pos();
654 sel_cell_start = sel_cell_end = actcell;
656 UpdateLocal(bv, CURSOR, false);
662 sel_pos_end = cursor.pos();
663 if ((ocell == sel_cell_end) ||
664 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
665 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
667 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
668 UpdateLocal(bv, SELECTION, false);
673 sel_pos_start = sel_pos_end = cursor.pos();
674 sel_cell_start = sel_cell_end = actcell;
676 UpdateLocal(bv, CURSOR, false);
688 if (the_locking_inset) {
689 the_locking_inset->InsetUnlock(bv);
691 the_locking_inset = 0;
692 if (action == LFUN_TAB)
696 sel_pos_start = sel_pos_end = cursor.pos();
697 sel_cell_start = sel_cell_end = actcell;
699 UpdateLocal(bv, CURSOR, false);
701 case LFUN_LAYOUT_TABLE:
703 int flag = (arg == "true");
704 MenuLayoutTabular(flag, this);
708 result = UNDISPATCHED;
711 if (result!=FINISHED) {
712 if (!the_locking_inset) {
714 if (ocell != actcell)
715 bview->getOwner()->getPopups().updateFormTabular();
720 bv->unlockInset(this);
725 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
727 return tabular->Latex(buf, os, fragile, fp);
731 int InsetTabular::Ascii(Buffer const *, ostream &) const
736 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
742 int InsetTabular::DocBook(Buffer const *, ostream &) const
748 void InsetTabular::Validate(LaTeXFeatures & features) const
750 tabular->Validate(features);
754 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
755 LyXFont const & font,
761 bool changed = false;
763 for(int i = 0; i < tabular->rows(); ++i) {
764 maxAsc = maxDesc = 0;
765 for(int j= 0; j < tabular->columns(); ++j) {
766 if (tabular->IsPartOfMultiColumn(i,j))
769 inset = tabular->GetCellInset(cell);
770 inset->update(bv, font, dodraw);
771 maxAsc = max(maxAsc, inset->ascent(bv->painter(), font));
772 maxDesc = max(maxDesc, inset->descent(bv->painter(), font));
773 changed = tabular->SetWidthOfCell(cell, inset->width(bv->painter(), font)) || changed;
775 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
776 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
782 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
784 x = cursor.x() - top_x;
789 void InsetTabular::ToggleInsetCursor(BufferView * bv)
791 if (the_locking_inset) {
792 the_locking_inset->ToggleInsetCursor(bv);
796 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
798 int asc = lyxfont::maxAscent(font);
799 int desc = lyxfont::maxDescent(font);
802 bv->hideLockedInsetCursor();
804 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
805 cursor_visible = !cursor_visible;
809 void InsetTabular::ShowInsetCursor(BufferView * bv)
811 if (!cursor_visible) {
812 LyXFont font; // = GetFont(par, cursor.pos);
814 int asc = lyxfont::maxAscent(font);
815 int desc = lyxfont::maxDescent(font);
816 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
817 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
818 cursor_visible = true;
823 void InsetTabular::HideInsetCursor(BufferView * bv)
826 ToggleInsetCursor(bv);
830 void InsetTabular::setPos(Painter & pain, int x, int y) const
835 actcell = actrow = actcol = 0;
836 int ly = tabular->GetDescentOfRow(actrow);
838 // first search the right row
839 while((ly < y) && (actrow < tabular->rows())) {
840 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
841 tabular->GetAscentOfRow(actrow+1) +
842 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
845 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
847 actcell = tabular->GetCellNumber(actrow, actcol);
849 // now search the right column
850 int lx = tabular->GetWidthOfColumn(actcell) -
851 tabular->GetAdditionalWidth(actcell);
852 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
853 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
854 tabular->GetAdditionalWidth(actcell - 1));
855 cursor.pos(((actcell+1) * 2) - 1);
857 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
858 cursor.x(lx + top_x - 2);
860 cursor.pos(cursor.pos() - 1);
861 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
866 int InsetTabular::getCellXPos(int cell) const
870 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
872 int lx = tabular->GetWidthOfColumn(cell);
873 for(; (c < cell); ++c) {
874 lx += tabular->GetWidthOfColumn(c);
876 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
877 ADD_TO_TABULAR_WIDTH);
880 void InsetTabular::resetPos(Painter & pain) const
884 actcol = tabular->column_of_cell(actcell);
889 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
890 if (tabular->IsLastCellInRow(cell)) {
891 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
892 tabular->GetAscentOfRow(actrow + 1) +
893 tabular->GetAdditionalHeight(cell + 1));
897 cursor.x(getCellXPos(actcell) + 2);
898 if (cursor.pos() % 2) {
899 LyXFont font(LyXFont::ALL_SANE);
900 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(pain,font) +
901 tabular->GetBeginningOfTextInCell(actcell));
903 if ((!the_locking_inset ||
904 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
905 (actcell != oldcell)) {
906 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
912 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
914 if (!cellstart(cursor.pos())) {
915 if (tabular->IsLastCell(actcell))
918 cursor.pos(cursor.pos() + 1);
920 if (ActivateCellInset(bv))
922 } else { // before the inset
923 cursor.pos(cursor.pos() + 1);
925 resetPos(bv->painter());
926 return DISPATCHED_NOUPDATE;
930 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
937 cursor.pos(cursor.pos() - 1);
938 if (!cellstart(cursor.pos())) {
940 } else if (lock) { // behind the inset
941 if (ActivateCellInset(bv, 0, 0, 0, true))
944 resetPos(bv->painter());
945 return DISPATCHED_NOUPDATE;
949 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
952 actcell = tabular->GetCellAbove(actcell);
953 if (actcell == ocell) // we moved out of the inset
955 resetPos(bv->painter());
956 return DISPATCHED_NOUPDATE;
960 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
963 actcell = tabular->GetCellBelow(actcell);
964 if (actcell == ocell) // we moved out of the inset
966 resetPos(bv->painter());
967 return DISPATCHED_NOUPDATE;
971 bool InsetTabular::moveNextCell(BufferView * bv)
973 if (tabular->IsLastCell(actcell))
976 cursor.pos(cursor.pos() + 1);
977 if (!cellstart(cursor.pos()))
978 cursor.pos(cursor.pos() + 1);
979 resetPos(bv->painter());
984 bool InsetTabular::movePrevCell(BufferView * bv)
986 if (!actcell) // first cell
989 cursor.pos(cursor.pos() - 1);
990 if (cellstart(cursor.pos()))
991 cursor.pos(cursor.pos() - 1);
992 resetPos(bv->painter());
997 bool InsetTabular::Delete()
1003 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1005 if (the_locking_inset)
1006 the_locking_inset->SetFont(bv, font, tall);
1010 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1019 setAlign = LYX_ALIGN_LEFT,
1025 case LyXTabular::ALIGN_LEFT:
1026 setAlign=LYX_ALIGN_LEFT;
1028 case LyXTabular::ALIGN_RIGHT:
1029 setAlign=LYX_ALIGN_RIGHT;
1031 case LyXTabular::ALIGN_CENTER:
1032 setAlign=LYX_ALIGN_CENTER;
1037 if (hasSelection()) {
1039 sel_col_start = tabular->column_of_cell(sel_cell_start);
1040 sel_col_end = tabular->column_of_cell(sel_cell_end);
1041 if (sel_col_start > sel_col_end) {
1042 sel_col_end = sel_col_start;
1043 sel_col_start = tabular->column_of_cell(sel_cell_end);
1045 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1048 sel_row_start = tabular->row_of_cell(sel_cell_start);
1049 sel_row_end = tabular->row_of_cell(sel_cell_end);
1050 if (sel_row_start > sel_row_end) {
1051 tmp = sel_row_start;
1052 sel_row_start = sel_row_end;
1056 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1057 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1059 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1060 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1061 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1063 int row = tabular->row_of_cell(actcell);
1064 int column = tabular->column_of_cell(actcell);
1067 case LyXTabular::SET_PWIDTH:
1069 bool update = (tabular->GetPWidth(actcell) != val);
1070 tabular->SetPWidth(actcell,val);
1072 for (int i=0; i < tabular->rows(); ++i) {
1073 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1076 UpdateLocal(bv, INIT, true);
1080 case LyXTabular::SET_SPECIAL_COLUMN:
1081 case LyXTabular::SET_SPECIAL_MULTI:
1082 tabular->SetAlignSpecial(actcell,val,feature);
1084 case LyXTabular::APPEND_ROW:
1085 // append the row into the tabular
1086 UnlockInsetInInset(bv, the_locking_inset);
1087 tabular->AppendRow(actcell);
1088 UpdateLocal(bv, INIT, true);
1090 case LyXTabular::APPEND_COLUMN:
1091 // append the column into the tabular
1092 tabular->AppendColumn(actcell);
1093 actcell = tabular->GetCellNumber(row, column);
1094 UpdateLocal(bv, INIT, true);
1096 case LyXTabular::DELETE_ROW:
1097 tabular->DeleteRow(tabular->row_of_cell(actcell));
1098 if ((row+1) > tabular->rows())
1100 actcell = tabular->GetCellNumber(row, column);
1101 UpdateLocal(bv, INIT, true);
1103 case LyXTabular::DELETE_COLUMN:
1104 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1105 if ((column+1) > tabular->columns())
1107 actcell = tabular->GetCellNumber(row, column);
1108 UpdateLocal(bv, INIT, true);
1110 case LyXTabular::TOGGLE_LINE_TOP:
1111 lineSet = !tabular->TopLine(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->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1115 UpdateLocal(bv, INIT, true);
1118 case LyXTabular::TOGGLE_LINE_BOTTOM:
1119 lineSet = !tabular->BottomLine(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->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1123 UpdateLocal(bv, INIT, true);
1126 case LyXTabular::TOGGLE_LINE_LEFT:
1127 lineSet = !tabular->LeftLine(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->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1131 UpdateLocal(bv, INIT, true);
1134 case LyXTabular::TOGGLE_LINE_RIGHT:
1135 lineSet = !tabular->RightLine(actcell);
1136 for(i=sel_row_start; i<=sel_row_end; ++i)
1137 for(j=sel_col_start; j<=sel_col_end; ++j)
1138 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1139 UpdateLocal(bv, INIT, true);
1141 case LyXTabular::ALIGN_LEFT:
1142 case LyXTabular::ALIGN_RIGHT:
1143 case LyXTabular::ALIGN_CENTER:
1144 for(i=sel_row_start; i<=sel_row_end; ++i)
1145 for(j=sel_col_start; j<=sel_col_end; ++j)
1146 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1148 UpdateLocal(bv, INIT, true);
1150 UpdateLocal(bv, CELL, true);
1152 case LyXTabular::MULTICOLUMN:
1154 if (sel_row_start != sel_row_end) {
1155 WriteAlert(_("Impossible Operation!"),
1156 _("Multicolumns can only be horizontally."),
1160 // just multicol for one Single Cell
1161 if (!hasSelection()) {
1162 // check wether we are completly in a multicol
1163 if (tabular->IsMultiColumn(actcell)) {
1164 tabular->UnsetMultiColumn(actcell);
1165 UpdateLocal(bv, INIT, true);
1167 tabular->SetMultiColumn(actcell, 1);
1168 UpdateLocal(bv, CELL, true);
1172 // we have a selection so this means we just add all this
1173 // cells to form a multicolumn cell
1177 if (sel_cell_start > sel_cell_end) {
1178 s_start = sel_cell_end;
1179 s_end = sel_cell_start;
1181 s_start = sel_cell_start;
1182 s_end = sel_cell_end;
1184 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1187 sel_cell_end = sel_cell_start;
1188 sel_pos_end = sel_pos_start;
1189 UpdateLocal(bv, INIT, true);
1192 case LyXTabular::SET_ALL_LINES:
1194 case LyXTabular::UNSET_ALL_LINES:
1195 for(i=sel_row_start; i<=sel_row_end; ++i)
1196 for(j=sel_col_start; j<=sel_col_end; ++j)
1197 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1198 UpdateLocal(bv, INIT, true);
1200 case LyXTabular::SET_LONGTABULAR:
1201 tabular->SetLongTabular(true);
1202 UpdateLocal(bv, INIT, true); // because this toggles displayed
1204 case LyXTabular::UNSET_LONGTABULAR:
1205 tabular->SetLongTabular(false);
1206 UpdateLocal(bv, INIT, true); // because this toggles displayed
1208 case LyXTabular::SET_ROTATE_TABULAR:
1209 tabular->SetRotateTabular(true);
1211 case LyXTabular::UNSET_ROTATE_TABULAR:
1212 tabular->SetRotateTabular(false);
1214 case LyXTabular::SET_ROTATE_CELL:
1215 for(i=sel_row_start; i<=sel_row_end; ++i)
1216 for(j=sel_col_start; j<=sel_col_end; ++j)
1217 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1219 case LyXTabular::UNSET_ROTATE_CELL:
1220 for(i=sel_row_start; i<=sel_row_end; ++i)
1221 for(j=sel_col_start; j<=sel_col_end; ++j)
1222 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1224 case LyXTabular::SET_LINEBREAKS:
1225 what = !tabular->GetLinebreaks(actcell);
1226 for(i=sel_row_start; i<=sel_row_end; ++i)
1227 for(j=sel_col_start; j<=sel_col_end; ++j)
1228 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1230 case LyXTabular::SET_LTFIRSTHEAD:
1231 tabular->SetLTHead(actcell,true);
1233 case LyXTabular::SET_LTHEAD:
1234 tabular->SetLTHead(actcell,false);
1236 case LyXTabular::SET_LTFOOT:
1237 tabular->SetLTFoot(actcell,false);
1239 case LyXTabular::SET_LTLASTFOOT:
1240 tabular->SetLTFoot(actcell,true);
1242 case LyXTabular::SET_LTNEWPAGE:
1243 what = !tabular->GetLTNewPage(actcell);
1244 tabular->SetLTNewPage(actcell,what);
1249 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1252 // the cursor.pos has to be before the inset so if it isn't now just
1253 // reset the curor pos first!
1254 if (cursor.pos() % 2) { // behind the inset
1255 cursor.pos(cursor.pos() - 1);
1256 resetPos(bv->painter());
1258 UpdatableInset * inset =
1259 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1260 LyXFont font(LyXFont::ALL_SANE);
1262 x = inset->x() + inset->width(bv->painter(), font);
1263 y = inset->descent(bv->painter(), font);
1265 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1266 inset_y = cursor.y();
1267 inset->Edit(bv, x - inset_x, y - inset_y, button);
1268 if (!the_locking_inset)
1270 UpdateLocal(bv, CELL, false);
1274 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1276 InsetText * inset = tabular->GetCellInset(actcell);
1279 if (cursor.pos() % 2) { // behind the inset
1280 return (((x + top_x) < cursor.x()) &&
1281 ((x + top_x) > (cursor.x() - inset->width(bv->painter(),
1282 LyXFont(LyXFont::ALL_SANE)))));
1284 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1285 return ((x1 > x2) &&
1286 (x1 < (x2 + inset->width(bv->painter(),
1287 LyXFont(LyXFont::ALL_SANE)))));
1291 // This returns paperWidth() if the cell-width is unlimited or the width
1292 // in pixels if we have a pwidth for this cell.
1293 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1295 string s = tabular->GetPWidth(cell);
1299 return VSpace(s).inPixels( 0, 0);
1302 int InsetTabular::getMaxWidth(Painter & pain,
1303 UpdatableInset const * inset) const
1306 int n = tabular->GetNumberOfCells();
1307 for(cell=0; cell < n; ++cell) {
1308 if (tabular->GetCellInset(cell) == inset)
1313 int w = GetMaxWidthOfCell(pain, cell);
1314 // this because text insets remove the xpos from the maxwidth because
1315 // otherwise the would not break good!!!
1316 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1321 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1327 for(int j= 0; j < tabular->columns(); ++j) {
1328 for(int i = 0; i < tabular->rows(); ++i) {
1329 if (tabular->IsPartOfMultiColumn(i,j))
1331 cell = tabular->GetCellNumber(i,j);
1332 inset = tabular->GetCellInset(cell);
1333 inset->update(bv, font);
1334 tabular->SetWidthOfCell(cell, inset->width(bv->painter(), font));
1336 // cell = tabular->GetCellNumber(0, j);
1337 // cx += tabular->GetWidthOfColumn(cell);