1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettabular.h"
25 #include "commandtags.h"
26 #include "LaTeXFeatures.h"
30 #include "lyx_gui_misc.h"
33 #include "insets/insettext.h"
34 #include "frontends/Dialogs.h"
36 const int ADD_TO_HEIGHT = 2;
37 const int ADD_TO_TABULAR_WIDTH = 2;
45 #define cellstart(p) ((p % 2) == 0)
47 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
53 buffer = buf; // set this first!!!
54 tabular = new LyXTabular(this, rows,columns);
55 // for now make it always display as display() inset
57 the_locking_inset = 0;
58 locked = no_selection = cursor_visible = false;
63 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
69 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
71 buffer = buf; // set this first
72 tabular = new LyXTabular(this, *(tab.tabular));
73 the_locking_inset = 0;
74 locked = no_selection = cursor_visible = false;
79 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
85 InsetTabular::~InsetTabular()
89 dialogs_->hideTabular(this);
93 Inset * InsetTabular::Clone() const
95 InsetTabular * t = new InsetTabular(*this, buffer);
97 t->tabular = tabular->Clone(t);
102 void InsetTabular::Write(Buffer const * buf, ostream & os) const
104 os << " Tabular" << endl;
105 tabular->Write(buf, os);
109 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
111 bool old_format = (lex.GetString() == "\\LyXTable");
116 tabular = new LyXTabular(buf, this, lex);
124 token = lex.GetString();
125 while (lex.IsOK() && (token != "\\end_inset")) {
127 token = lex.GetString();
129 if (token != "\\end_inset") {
130 lex.printError("Missing \\end_inset at this point. "
136 int InsetTabular::ascent(BufferView *, LyXFont const &) const
138 return tabular->GetAscentOfRow(0);
142 int InsetTabular::descent(BufferView *, LyXFont const &) const
144 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
148 int InsetTabular::width(BufferView *, LyXFont const &) const
150 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
154 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
155 float & x, bool cleared) const
157 Painter & pain = bv->painter();
162 UpdatableInset::draw(bv,font,baseline,x,cleared);
163 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
164 (top_x != int(x)) || (top_baseline != baseline))) {
165 int h = ascent(bv, font) + descent(bv, font);
166 int tx = display()||!owner()? 0:top_x;
167 int w = tx? width(bv, font):pain.paperWidth();
168 int ty = baseline - ascent(bv, font);
172 if ((ty + h) > pain.paperHeight())
173 h = pain.paperHeight();
174 if ((top_x + w) > pain.paperWidth())
175 w = pain.paperWidth();
176 pain.fillRectangle(tx, ty, w, h);
181 top_baseline = baseline;
183 x += ADD_TO_TABULAR_WIDTH;
184 if (cleared || (need_update == FULL) || (need_update == CELL)) {
185 for(i=0;i<tabular->rows();++i) {
187 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
188 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
189 for(j=0;j<tabular->columns();++j) {
190 if (tabular->IsPartOfMultiColumn(i,j))
192 cx = nx + tabular->GetBeginningOfTextInCell(cell);
194 DrawCellSelection(pain, nx, baseline, i, j, cell);
195 if (dodraw && !cleared && locked && the_locking_inset) {
196 if (the_locking_inset == tabular->GetCellInset(cell))
197 tabular->GetCellInset(cell)->draw(bv, font,
201 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
203 DrawCellLines(pain, nx, baseline, i, cell);
205 nx += tabular->GetWidthOfColumn(cell);
208 baseline += tabular->GetDescentOfRow(i) +
209 tabular->GetAscentOfRow(i+1)+
210 tabular->GetAdditionalHeight(cell+1);
213 x += width(bv, font);
218 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
219 int row, int cell) const
221 int x2 = x + tabular->GetWidthOfColumn(cell);
224 if (!tabular->TopAlreadyDrawed(cell)) {
225 on_off = !tabular->TopLine(cell);
226 pain.line(x, baseline - tabular->GetAscentOfRow(row),
227 x2, baseline - tabular->GetAscentOfRow(row),
228 on_off ? LColor::tabularonoffline:LColor::tabularline,
229 on_off ? Painter::line_onoffdash:Painter::line_solid);
231 on_off = !tabular->BottomLine(cell);
232 pain.line(x,baseline + tabular->GetDescentOfRow(row),
233 x2, baseline + tabular->GetDescentOfRow(row),
234 on_off ? LColor::tabularonoffline:LColor::tabularline,
235 on_off ? Painter::line_onoffdash:Painter::line_solid);
236 if (!tabular->LeftAlreadyDrawed(cell)) {
237 on_off = !tabular->LeftLine(cell);
238 pain.line(x, baseline - tabular->GetAscentOfRow(row),
239 x, baseline + tabular->GetDescentOfRow(row),
240 on_off ? LColor::tabularonoffline:LColor::tabularline,
241 on_off ? Painter::line_onoffdash:Painter::line_solid);
243 on_off = !tabular->RightLine(cell);
244 pain.line(x2 - tabular->GetAdditionalWidth(cell),
245 baseline - tabular->GetAscentOfRow(row),
246 x2 - tabular->GetAdditionalWidth(cell),
247 baseline + tabular->GetDescentOfRow(row),
248 on_off ? LColor::tabularonoffline:LColor::tabularline,
249 on_off ? Painter::line_onoffdash:Painter::line_solid);
253 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
254 int row, int column, int cell) const
256 int cs = tabular->column_of_cell(sel_cell_start);
257 int ce = tabular->column_of_cell(sel_cell_end);
260 cs = tabular->column_of_cell(sel_cell_end);
262 ce = tabular->right_column_of_cell(sel_cell_end);
265 int rs = tabular->row_of_cell(sel_cell_start);
266 int re = tabular->row_of_cell(sel_cell_end);
267 if (rs > re) swap(rs, re);
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 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
282 calculate_dimensions_of_cells(bv, font, true);
284 owner()->update(bv, font, true);
287 if (the_locking_inset)
288 the_locking_inset->update(bv, font, reinit);
289 switch(need_update) {
293 if (calculate_dimensions_of_cells(bv, font, false))
305 char const * InsetTabular::EditMessage() const
307 return _("Opened Tabular Inset");
311 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
313 UpdatableInset::Edit(bv, x, y, button);
315 if (!bv->lockInset(this)) {
316 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
320 the_locking_inset = 0;
321 inset_pos = inset_x = inset_y = 0;
323 sel_pos_start = sel_pos_end = cursor.pos();
324 sel_cell_start = sel_cell_end = actcell;
325 bv->text->FinishUndo();
326 if (InsetHit(bv, x, y)) {
327 ActivateCellInset(bv, x, y, button);
329 UpdateLocal(bv, NONE, false);
330 // bv->getOwner()->getPopups().updateFormTabular();
334 void InsetTabular::InsetUnlock(BufferView * bv)
336 if (the_locking_inset) {
337 the_locking_inset->InsetUnlock(bv);
338 the_locking_inset = 0;
341 if (hasSelection()) {
342 sel_pos_start = sel_pos_end = 0;
343 sel_cell_start = sel_cell_end = 0;
344 UpdateLocal(bv, FULL, false);
346 no_selection = false;
352 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
356 bv->updateInset(this, mark_dirty);
362 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
364 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
368 if (inset == tabular->GetCellInset(actcell)) {
369 lyxerr[Debug::INSETS] << "OK" << endl;
370 the_locking_inset = tabular->GetCellInset(actcell);
372 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
373 inset_y = cursor.y();
374 inset_pos = cursor.pos();
376 } else if (the_locking_inset && (the_locking_inset == inset)) {
377 if (cursor.pos() == inset_pos) {
378 lyxerr[Debug::INSETS] << "OK" << endl;
380 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
381 inset_y = cursor.y();
383 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
385 } else if (the_locking_inset) {
386 lyxerr[Debug::INSETS] << "MAYBE" << endl;
387 return the_locking_inset->LockInsetInInset(bv, inset);
389 lyxerr[Debug::INSETS] << "NOT OK" << endl;
394 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
397 if (!the_locking_inset)
399 if (the_locking_inset == inset) {
400 the_locking_inset->InsetUnlock(bv);
401 the_locking_inset = 0;
403 moveRight(bv, false);
404 UpdateLocal(bv, CELL, false);
407 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
408 if ((inset->LyxCode() == TABULAR_CODE) &&
409 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
411 dialogs_ = bv->owner()->getDialogs();
412 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
421 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
423 if (!the_locking_inset)
425 if (the_locking_inset != inset)
426 return the_locking_inset->UpdateInsetInInset(bv, inset);
427 UpdateLocal(bv, CELL, false);
432 int InsetTabular::InsetInInsetY()
434 if (!the_locking_inset)
437 return (inset_y + the_locking_inset->InsetInInsetY());
441 UpdatableInset * InsetTabular::GetLockingInset()
443 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
447 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
451 if (the_locking_inset)
452 return the_locking_inset->GetFirstLockingInsetOfType(c);
457 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
459 if (the_locking_inset)
460 return the_locking_inset->InsertInset(bv, inset);
465 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
467 if (hasSelection()) {
468 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
469 UpdateLocal(bv, SELECTION, false);
471 no_selection = false;
476 sel_pos_start = sel_pos_end = cursor.pos();
477 sel_cell_start = sel_cell_end = actcell;
479 bool inset_hit = InsetHit(bv, x, y);
481 if ((ocell == actcell) && the_locking_inset && inset_hit) {
482 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
484 } else if (the_locking_inset) {
485 the_locking_inset->InsetUnlock(bv);
487 the_locking_inset = 0;
488 if (inset_hit && bv->the_locking_inset) {
489 ActivateCellInset(bv, x, y, button);
490 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
495 void InsetTabular::InsetButtonRelease(BufferView * bv,
496 int x, int y, int button)
499 if (the_locking_inset) {
501 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
502 i->InsetButtonRelease(bv, x, y, button);
506 dialogs_ = bv->owner()->getDialogs();
507 dialogs_->showTabular(this);
509 else if (ocell != actcell)
510 bview->getOwner()->getPopups().updateTabular();
514 if (the_locking_inset) {
515 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
518 no_selection = false;
522 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
524 if (the_locking_inset) {
525 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
526 y - inset_y, button);
530 // int ocell = actcell,
531 int old = sel_pos_end;
534 sel_pos_end = cursor.pos();
535 sel_cell_end = actcell;
536 if (old != sel_pos_end)
537 UpdateLocal(bv, SELECTION, false);
539 if (ocell != actcell)
540 bview->getOwner()->getPopups().updateFormTabular();
543 no_selection = false;
547 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
549 if (the_locking_inset) {
550 the_locking_inset->InsetKeyPress(xke);
556 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
559 UpdatableInset::RESULT
562 no_selection = false;
563 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
564 || (result == DISPATCHED_NOUPDATE)) {
570 if ((action < 0) && arg.empty())
573 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
574 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
576 if (the_locking_inset) {
577 result=the_locking_inset->LocalDispatch(bv, action, arg);
578 if (result == DISPATCHED_NOUPDATE)
580 else if (result == DISPATCHED) {
581 the_locking_inset->ToggleInsetCursor(bv);
582 UpdateLocal(bv, CELL, false);
583 the_locking_inset->ToggleInsetCursor(bv);
585 } else if (result == FINISHED) {
586 if ((action == LFUN_RIGHT) || (action == -1)) {
587 cursor.pos(inset_pos + 1);
590 sel_pos_start = sel_pos_end = cursor.pos();
591 sel_cell_start = sel_cell_end = actcell;
598 bool hs = hasSelection();
602 // Normal chars not handled here
605 // --- Cursor Movements ---------------------------------------------
607 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
609 moveRight(bv, false);
610 sel_pos_end = cursor.pos();
611 if (!cellstart(cursor.pos())) {
612 if (tabular->right_column_of_cell(sel_cell_start) >
613 tabular->right_column_of_cell(actcell))
614 sel_cell_end = actcell+1;
616 sel_cell_end = actcell;
618 UpdateLocal(bv, SELECTION, false);
621 result = moveRight(bv);
622 sel_pos_start = sel_pos_end = cursor.pos();
623 sel_cell_start = sel_cell_end = actcell;
625 UpdateLocal(bv, SELECTION, false);
628 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
631 sel_pos_end = cursor.pos();
632 if (cellstart(cursor.pos())) {
633 if (tabular->column_of_cell(sel_cell_start) >=
634 tabular->column_of_cell(actcell))
635 sel_cell_end = actcell;
637 sel_cell_end = actcell-1;
639 UpdateLocal(bv, SELECTION, false);
642 result = moveLeft(bv);
643 sel_pos_start = sel_pos_end = cursor.pos();
644 sel_cell_start = sel_cell_end = actcell;
646 UpdateLocal(bv, SELECTION, false);
652 sel_pos_end = cursor.pos();
653 if ((ocell == sel_cell_end) ||
654 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
655 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
657 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
658 UpdateLocal(bv, SELECTION, false);
662 result= moveDown(bv);
663 sel_pos_start = sel_pos_end = cursor.pos();
664 sel_cell_start = sel_cell_end = actcell;
666 UpdateLocal(bv, SELECTION, false);
672 sel_pos_end = cursor.pos();
673 if ((ocell == sel_cell_end) ||
674 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
675 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
677 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
678 UpdateLocal(bv, SELECTION, false);
683 sel_pos_start = sel_pos_end = cursor.pos();
684 sel_cell_start = sel_cell_end = actcell;
686 UpdateLocal(bv, SELECTION, false);
698 if (the_locking_inset) {
699 UnlockInsetInInset(bv, the_locking_inset);
700 the_locking_inset = 0;
702 if (action == LFUN_TAB)
706 sel_pos_start = sel_pos_end = cursor.pos();
707 sel_cell_start = sel_cell_end = actcell;
709 UpdateLocal(bv, SELECTION, false);
711 case LFUN_LAYOUT_TABLE:
713 dialogs_ = bv->owner()->getDialogs();
714 dialogs_->showTabular(this);
718 result = UNDISPATCHED;
721 if (result!=FINISHED) {
722 if (!the_locking_inset) {
724 if (ocell != actcell)
725 bview->getOwner()->getPopups().updateFormTabular();
730 bv->unlockInset(this);
735 int InsetTabular::Latex(Buffer const * buf, ostream & os,
736 bool fragile, bool fp) const
738 return tabular->Latex(buf, os, fragile, fp);
742 int InsetTabular::Ascii(Buffer const *, ostream &) const
748 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
754 int InsetTabular::DocBook(Buffer const *, ostream &) const
760 void InsetTabular::Validate(LaTeXFeatures & features) const
762 tabular->Validate(features);
766 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
767 LyXFont const & font,
773 bool changed = false;
775 for(int i = 0; i < tabular->rows(); ++i) {
776 maxAsc = maxDesc = 0;
777 for(int j= 0; j < tabular->columns(); ++j) {
778 if (tabular->IsPartOfMultiColumn(i,j))
781 inset = tabular->GetCellInset(cell);
783 inset->update(bv, font, false);
784 maxAsc = max(maxAsc, inset->ascent(bv, font));
785 maxDesc = max(maxDesc, inset->descent(bv, font));
786 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
788 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
789 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
795 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
797 x = cursor.x() - top_x;
802 void InsetTabular::ToggleInsetCursor(BufferView * bv)
804 if (the_locking_inset) {
805 the_locking_inset->ToggleInsetCursor(bv);
809 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
811 int asc = lyxfont::maxAscent(font);
812 int desc = lyxfont::maxDescent(font);
815 bv->hideLockedInsetCursor();
817 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
818 cursor_visible = !cursor_visible;
822 void InsetTabular::ShowInsetCursor(BufferView * bv)
824 if (!cursor_visible) {
825 LyXFont font; // = GetFont(par, cursor.pos);
827 int asc = lyxfont::maxAscent(font);
828 int desc = lyxfont::maxDescent(font);
829 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
830 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
831 cursor_visible = true;
836 void InsetTabular::HideInsetCursor(BufferView * bv)
839 ToggleInsetCursor(bv);
843 void InsetTabular::setPos(BufferView * bv, int x, int y) const
848 actcell = actrow = actcol = 0;
849 int ly = tabular->GetDescentOfRow(actrow);
851 // first search the right row
852 while((ly < y) && (actrow < tabular->rows())) {
853 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
854 tabular->GetAscentOfRow(actrow+1) +
855 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
858 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
860 actcell = tabular->GetCellNumber(actrow, actcol);
862 // now search the right column
863 int lx = tabular->GetWidthOfColumn(actcell) -
864 tabular->GetAdditionalWidth(actcell);
865 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
866 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
867 tabular->GetAdditionalWidth(actcell - 1));
868 cursor.pos(((actcell+1) * 2) - 1);
870 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
871 cursor.x(lx + top_x - 2);
873 cursor.pos(cursor.pos() - 1);
874 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
880 int InsetTabular::getCellXPos(int cell) const
884 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
886 int lx = tabular->GetWidthOfColumn(cell);
887 for(; (c < cell); ++c) {
888 lx += tabular->GetWidthOfColumn(c);
890 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
891 ADD_TO_TABULAR_WIDTH);
895 void InsetTabular::resetPos(BufferView * bv) const
899 actcol = tabular->column_of_cell(actcell);
904 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
905 if (tabular->IsLastCellInRow(cell)) {
906 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
907 tabular->GetAscentOfRow(actrow + 1) +
908 tabular->GetAdditionalHeight(cell + 1));
912 cursor.x(getCellXPos(actcell) + 2);
913 if (cursor.pos() % 2) {
914 LyXFont font(LyXFont::ALL_SANE);
915 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
916 tabular->GetBeginningOfTextInCell(actcell));
918 if ((!the_locking_inset ||
919 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
920 (actcell != oldcell)) {
921 dialogs_ = bv->owner()->getDialogs();
922 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
928 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
930 if (!cellstart(cursor.pos())) {
931 if (tabular->IsLastCell(actcell))
934 cursor.pos(cursor.pos() + 1);
936 if (ActivateCellInset(bv))
938 } else { // before the inset
939 cursor.pos(cursor.pos() + 1);
942 return DISPATCHED_NOUPDATE;
946 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
953 cursor.pos(cursor.pos() - 1);
954 if (!cellstart(cursor.pos())) {
956 } else if (lock) { // behind the inset
957 if (ActivateCellInset(bv, 0, 0, 0, true))
961 return DISPATCHED_NOUPDATE;
965 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
968 actcell = tabular->GetCellAbove(actcell);
969 if (actcell == ocell) // we moved out of the inset
972 return DISPATCHED_NOUPDATE;
976 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
979 actcell = tabular->GetCellBelow(actcell);
980 if (actcell == ocell) // we moved out of the inset
983 return DISPATCHED_NOUPDATE;
987 bool InsetTabular::moveNextCell(BufferView * bv)
989 if (tabular->IsLastCell(actcell))
992 cursor.pos(cursor.pos() + 1);
993 if (!cellstart(cursor.pos()))
994 cursor.pos(cursor.pos() + 1);
1000 bool InsetTabular::movePrevCell(BufferView * bv)
1002 if (!actcell) // first cell
1005 cursor.pos(cursor.pos() - 1);
1006 if (cellstart(cursor.pos()))
1007 cursor.pos(cursor.pos() - 1);
1013 bool InsetTabular::Delete()
1019 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1021 if (the_locking_inset)
1022 the_locking_inset->SetFont(bv, font, tall);
1026 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1035 setAlign = LYX_ALIGN_LEFT,
1041 case LyXTabular::M_ALIGN_LEFT:
1042 case LyXTabular::ALIGN_LEFT:
1043 setAlign=LYX_ALIGN_LEFT;
1045 case LyXTabular::M_ALIGN_RIGHT:
1046 case LyXTabular::ALIGN_RIGHT:
1047 setAlign=LYX_ALIGN_RIGHT;
1049 case LyXTabular::M_ALIGN_CENTER:
1050 case LyXTabular::ALIGN_CENTER:
1051 setAlign=LYX_ALIGN_CENTER;
1053 case LyXTabular::M_VALIGN_TOP:
1054 case LyXTabular::VALIGN_TOP:
1055 setAlign=LyXTabular::LYX_VALIGN_TOP;
1057 case LyXTabular::M_VALIGN_BOTTOM:
1058 case LyXTabular::VALIGN_BOTTOM:
1059 setAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1061 case LyXTabular::M_VALIGN_CENTER:
1062 case LyXTabular::VALIGN_CENTER:
1063 setAlign=LyXTabular::LYX_VALIGN_CENTER;
1068 if (hasSelection()) {
1070 sel_col_start = tabular->column_of_cell(sel_cell_start);
1071 sel_col_end = tabular->column_of_cell(sel_cell_end);
1072 if (sel_col_start > sel_col_end) {
1073 sel_col_end = sel_col_start;
1074 sel_col_start = tabular->column_of_cell(sel_cell_end);
1076 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1079 sel_row_start = tabular->row_of_cell(sel_cell_start);
1080 sel_row_end = tabular->row_of_cell(sel_cell_end);
1081 if (sel_row_start > sel_row_end) {
1082 tmp = sel_row_start;
1083 sel_row_start = sel_row_end;
1087 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1088 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1090 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1091 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1092 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1094 int row = tabular->row_of_cell(actcell);
1095 int column = tabular->column_of_cell(actcell);
1099 case LyXTabular::SET_PWIDTH:
1101 bool update = (tabular->GetColumnPWidth(actcell) != val);
1102 tabular->SetColumnPWidth(actcell,val);
1104 for (int i=0; i < tabular->rows(); ++i) {
1105 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1108 UpdateLocal(bv, INIT, true);
1112 case LyXTabular::SET_MPWIDTH:
1114 bool update = (tabular->GetPWidth(actcell) != val);
1115 tabular->SetMColumnPWidth(actcell,val);
1117 for (int i=0; i < tabular->rows(); ++i) {
1118 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1121 UpdateLocal(bv, INIT, true);
1125 case LyXTabular::SET_SPECIAL_COLUMN:
1126 case LyXTabular::SET_SPECIAL_MULTI:
1127 tabular->SetAlignSpecial(actcell,val,feature);
1129 case LyXTabular::APPEND_ROW:
1130 // append the row into the tabular
1131 UnlockInsetInInset(bv, the_locking_inset);
1132 tabular->AppendRow(actcell);
1133 UpdateLocal(bv, INIT, true);
1135 case LyXTabular::APPEND_COLUMN:
1136 // append the column into the tabular
1137 tabular->AppendColumn(actcell);
1138 actcell = tabular->GetCellNumber(row, column);
1139 UpdateLocal(bv, INIT, true);
1141 case LyXTabular::DELETE_ROW:
1142 tabular->DeleteRow(tabular->row_of_cell(actcell));
1143 if ((row+1) > tabular->rows())
1145 actcell = tabular->GetCellNumber(row, column);
1146 UpdateLocal(bv, INIT, true);
1148 case LyXTabular::DELETE_COLUMN:
1149 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1150 if ((column+1) > tabular->columns())
1152 actcell = tabular->GetCellNumber(row, column);
1153 UpdateLocal(bv, INIT, true);
1155 case LyXTabular::M_TOGGLE_LINE_TOP:
1157 case LyXTabular::TOGGLE_LINE_TOP:
1158 lineSet = !tabular->TopLine(actcell, flag);
1159 for(i=sel_row_start; i<=sel_row_end; ++i)
1160 for(j=sel_col_start; j<=sel_col_end; ++j)
1161 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1162 UpdateLocal(bv, INIT, true);
1165 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1167 case LyXTabular::TOGGLE_LINE_BOTTOM:
1168 lineSet = !tabular->BottomLine(actcell, flag);
1169 for(i=sel_row_start; i<=sel_row_end; ++i)
1170 for(j=sel_col_start; j<=sel_col_end; ++j)
1171 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1173 UpdateLocal(bv, INIT, true);
1176 case LyXTabular::M_TOGGLE_LINE_LEFT:
1178 case LyXTabular::TOGGLE_LINE_LEFT:
1179 lineSet = !tabular->LeftLine(actcell, flag);
1180 for(i=sel_row_start; i<=sel_row_end; ++i)
1181 for(j=sel_col_start; j<=sel_col_end; ++j)
1182 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1184 UpdateLocal(bv, INIT, true);
1187 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1189 case LyXTabular::TOGGLE_LINE_RIGHT:
1190 lineSet = !tabular->RightLine(actcell, flag);
1191 for(i=sel_row_start; i<=sel_row_end; ++i)
1192 for(j=sel_col_start; j<=sel_col_end; ++j)
1193 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1195 UpdateLocal(bv, INIT, true);
1197 case LyXTabular::M_ALIGN_LEFT:
1198 case LyXTabular::M_ALIGN_RIGHT:
1199 case LyXTabular::M_ALIGN_CENTER:
1201 case LyXTabular::ALIGN_LEFT:
1202 case LyXTabular::ALIGN_RIGHT:
1203 case LyXTabular::ALIGN_CENTER:
1204 for(i=sel_row_start; i<=sel_row_end; ++i)
1205 for(j=sel_col_start; j<=sel_col_end; ++j)
1206 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign,
1209 UpdateLocal(bv, INIT, true);
1211 UpdateLocal(bv, CELL, true);
1213 case LyXTabular::M_VALIGN_TOP:
1214 case LyXTabular::M_VALIGN_BOTTOM:
1215 case LyXTabular::M_VALIGN_CENTER:
1217 case LyXTabular::VALIGN_TOP:
1218 case LyXTabular::VALIGN_BOTTOM:
1219 case LyXTabular::VALIGN_CENTER:
1220 for(i=sel_row_start; i<=sel_row_end; ++i)
1221 for(j=sel_col_start; j<=sel_col_end; ++j)
1222 tabular->SetVAlignment(tabular->GetCellNumber(i,j), setAlign,
1225 UpdateLocal(bv, INIT, true);
1227 UpdateLocal(bv, CELL, true);
1229 case LyXTabular::MULTICOLUMN:
1231 if (sel_row_start != sel_row_end) {
1232 WriteAlert(_("Impossible Operation!"),
1233 _("Multicolumns can only be horizontally."),
1237 // just multicol for one Single Cell
1238 if (!hasSelection()) {
1239 // check wether we are completly in a multicol
1240 if (tabular->IsMultiColumn(actcell)) {
1241 tabular->UnsetMultiColumn(actcell);
1242 UpdateLocal(bv, INIT, true);
1244 tabular->SetMultiColumn(actcell, 1);
1245 UpdateLocal(bv, CELL, true);
1249 // we have a selection so this means we just add all this
1250 // cells to form a multicolumn cell
1254 if (sel_cell_start > sel_cell_end) {
1255 s_start = sel_cell_end;
1256 s_end = sel_cell_start;
1258 s_start = sel_cell_start;
1259 s_end = sel_cell_end;
1261 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1264 sel_cell_end = sel_cell_start;
1265 sel_pos_end = sel_pos_start;
1266 UpdateLocal(bv, INIT, true);
1269 case LyXTabular::SET_ALL_LINES:
1271 case LyXTabular::UNSET_ALL_LINES:
1272 for(i=sel_row_start; i<=sel_row_end; ++i)
1273 for(j=sel_col_start; j<=sel_col_end; ++j)
1274 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1275 UpdateLocal(bv, INIT, true);
1277 case LyXTabular::SET_LONGTABULAR:
1278 tabular->SetLongTabular(true);
1279 UpdateLocal(bv, INIT, true); // because this toggles displayed
1281 case LyXTabular::UNSET_LONGTABULAR:
1282 tabular->SetLongTabular(false);
1283 UpdateLocal(bv, INIT, true); // because this toggles displayed
1285 case LyXTabular::SET_ROTATE_TABULAR:
1286 tabular->SetRotateTabular(true);
1288 case LyXTabular::UNSET_ROTATE_TABULAR:
1289 tabular->SetRotateTabular(false);
1291 case LyXTabular::SET_ROTATE_CELL:
1292 for(i=sel_row_start; i<=sel_row_end; ++i)
1293 for(j=sel_col_start; j<=sel_col_end; ++j)
1294 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1296 case LyXTabular::UNSET_ROTATE_CELL:
1297 for(i=sel_row_start; i<=sel_row_end; ++i)
1298 for(j=sel_col_start; j<=sel_col_end; ++j)
1299 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1301 case LyXTabular::SET_LINEBREAKS:
1302 what = !tabular->GetLinebreaks(actcell);
1303 for(i=sel_row_start; i<=sel_row_end; ++i)
1304 for(j=sel_col_start; j<=sel_col_end; ++j)
1305 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1307 case LyXTabular::SET_LTFIRSTHEAD:
1308 tabular->SetLTHead(actcell,true);
1310 case LyXTabular::SET_LTHEAD:
1311 tabular->SetLTHead(actcell,false);
1313 case LyXTabular::SET_LTFOOT:
1314 tabular->SetLTFoot(actcell,false);
1316 case LyXTabular::SET_LTLASTFOOT:
1317 tabular->SetLTFoot(actcell,true);
1319 case LyXTabular::SET_LTNEWPAGE:
1320 what = !tabular->GetLTNewPage(actcell);
1321 tabular->SetLTNewPage(actcell,what);
1327 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1330 // the cursor.pos has to be before the inset so if it isn't now just
1331 // reset the curor pos first!
1332 if (cursor.pos() % 2) { // behind the inset
1333 cursor.pos(cursor.pos() - 1);
1336 UpdatableInset * inset =
1337 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1338 LyXFont font(LyXFont::ALL_SANE);
1340 x = inset->x() + inset->width(bv, font);
1341 y = inset->descent(bv, font);
1343 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1344 inset_y = cursor.y();
1345 inset->Edit(bv, x - inset_x, y - inset_y, button);
1346 if (!the_locking_inset)
1348 UpdateLocal(bv, CELL, false);
1353 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1355 InsetText * inset = tabular->GetCellInset(actcell);
1358 if (cursor.pos() % 2) { // behind the inset
1359 return (((x + top_x) < cursor.x()) &&
1360 ((x + top_x) > (cursor.x() - inset->width(bv,
1361 LyXFont(LyXFont::ALL_SANE)))));
1363 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1364 return ((x1 > x2) &&
1365 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1370 // This returns paperWidth() if the cell-width is unlimited or the width
1371 // in pixels if we have a pwidth for this cell.
1372 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1374 string s = tabular->GetPWidth(cell);
1378 return VSpace(s).inPixels( 0, 0);
1382 int InsetTabular::getMaxWidth(Painter & pain,
1383 UpdatableInset const * inset) const
1386 int n = tabular->GetNumberOfCells();
1387 for(cell=0; cell < n; ++cell) {
1388 if (tabular->GetCellInset(cell) == inset)
1393 int w = GetMaxWidthOfCell(pain, cell);
1394 // this because text insets remove the xpos from the maxwidth because
1395 // otherwise the would not break good!!!
1396 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1402 void InsetTabular::recomputeTextInsets(BufferView * bv,
1403 LyXFont const & font) const
1409 for(int j= 0; j < tabular->columns(); ++j) {
1410 for(int i = 0; i < tabular->rows(); ++i) {
1411 if (tabular->IsPartOfMultiColumn(i,j))
1413 cell = tabular->GetCellNumber(i,j);
1414 inset = tabular->GetCellInset(cell);
1415 inset->update(bv, font);
1416 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1418 // cell = tabular->GetCellNumber(0, j);
1419 // cx += tabular->GetWidthOfColumn(cell);
1424 void InsetTabular::resizeLyXText(BufferView *) const