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::ALIGN_LEFT:
1042 setAlign=LYX_ALIGN_LEFT;
1044 case LyXTabular::ALIGN_RIGHT:
1045 setAlign=LYX_ALIGN_RIGHT;
1047 case LyXTabular::ALIGN_CENTER:
1048 setAlign=LYX_ALIGN_CENTER;
1053 if (hasSelection()) {
1055 sel_col_start = tabular->column_of_cell(sel_cell_start);
1056 sel_col_end = tabular->column_of_cell(sel_cell_end);
1057 if (sel_col_start > sel_col_end) {
1058 sel_col_end = sel_col_start;
1059 sel_col_start = tabular->column_of_cell(sel_cell_end);
1061 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1064 sel_row_start = tabular->row_of_cell(sel_cell_start);
1065 sel_row_end = tabular->row_of_cell(sel_cell_end);
1066 if (sel_row_start > sel_row_end) {
1067 tmp = sel_row_start;
1068 sel_row_start = sel_row_end;
1072 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1073 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1075 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1076 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1077 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1079 int row = tabular->row_of_cell(actcell);
1080 int column = tabular->column_of_cell(actcell);
1083 case LyXTabular::SET_PWIDTH:
1085 bool update = (tabular->GetPWidth(actcell) != val);
1086 tabular->SetPWidth(actcell,val);
1088 for (int i=0; i < tabular->rows(); ++i) {
1089 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1092 UpdateLocal(bv, INIT, true);
1096 case LyXTabular::SET_SPECIAL_COLUMN:
1097 case LyXTabular::SET_SPECIAL_MULTI:
1098 tabular->SetAlignSpecial(actcell,val,feature);
1100 case LyXTabular::APPEND_ROW:
1101 // append the row into the tabular
1102 UnlockInsetInInset(bv, the_locking_inset);
1103 tabular->AppendRow(actcell);
1104 UpdateLocal(bv, INIT, true);
1106 case LyXTabular::APPEND_COLUMN:
1107 // append the column into the tabular
1108 tabular->AppendColumn(actcell);
1109 actcell = tabular->GetCellNumber(row, column);
1110 UpdateLocal(bv, INIT, true);
1112 case LyXTabular::DELETE_ROW:
1113 tabular->DeleteRow(tabular->row_of_cell(actcell));
1114 if ((row+1) > tabular->rows())
1116 actcell = tabular->GetCellNumber(row, column);
1117 UpdateLocal(bv, INIT, true);
1119 case LyXTabular::DELETE_COLUMN:
1120 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1121 if ((column+1) > tabular->columns())
1123 actcell = tabular->GetCellNumber(row, column);
1124 UpdateLocal(bv, INIT, true);
1126 case LyXTabular::TOGGLE_LINE_TOP:
1127 lineSet = !tabular->TopLine(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->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1131 UpdateLocal(bv, INIT, true);
1134 case LyXTabular::TOGGLE_LINE_BOTTOM:
1135 lineSet = !tabular->BottomLine(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->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1139 UpdateLocal(bv, INIT, true);
1142 case LyXTabular::TOGGLE_LINE_LEFT:
1143 lineSet = !tabular->LeftLine(actcell);
1144 for(i=sel_row_start; i<=sel_row_end; ++i)
1145 for(j=sel_col_start; j<=sel_col_end; ++j)
1146 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1147 UpdateLocal(bv, INIT, true);
1150 case LyXTabular::TOGGLE_LINE_RIGHT:
1151 lineSet = !tabular->RightLine(actcell);
1152 for(i=sel_row_start; i<=sel_row_end; ++i)
1153 for(j=sel_col_start; j<=sel_col_end; ++j)
1154 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1155 UpdateLocal(bv, INIT, true);
1157 case LyXTabular::ALIGN_LEFT:
1158 case LyXTabular::ALIGN_RIGHT:
1159 case LyXTabular::ALIGN_CENTER:
1160 for(i=sel_row_start; i<=sel_row_end; ++i)
1161 for(j=sel_col_start; j<=sel_col_end; ++j)
1162 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1164 UpdateLocal(bv, INIT, true);
1166 UpdateLocal(bv, CELL, true);
1168 case LyXTabular::MULTICOLUMN:
1170 if (sel_row_start != sel_row_end) {
1171 WriteAlert(_("Impossible Operation!"),
1172 _("Multicolumns can only be horizontally."),
1176 // just multicol for one Single Cell
1177 if (!hasSelection()) {
1178 // check wether we are completly in a multicol
1179 if (tabular->IsMultiColumn(actcell)) {
1180 tabular->UnsetMultiColumn(actcell);
1181 UpdateLocal(bv, INIT, true);
1183 tabular->SetMultiColumn(actcell, 1);
1184 UpdateLocal(bv, CELL, true);
1188 // we have a selection so this means we just add all this
1189 // cells to form a multicolumn cell
1193 if (sel_cell_start > sel_cell_end) {
1194 s_start = sel_cell_end;
1195 s_end = sel_cell_start;
1197 s_start = sel_cell_start;
1198 s_end = sel_cell_end;
1200 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1203 sel_cell_end = sel_cell_start;
1204 sel_pos_end = sel_pos_start;
1205 UpdateLocal(bv, INIT, true);
1208 case LyXTabular::SET_ALL_LINES:
1210 case LyXTabular::UNSET_ALL_LINES:
1211 for(i=sel_row_start; i<=sel_row_end; ++i)
1212 for(j=sel_col_start; j<=sel_col_end; ++j)
1213 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1214 UpdateLocal(bv, INIT, true);
1216 case LyXTabular::SET_LONGTABULAR:
1217 tabular->SetLongTabular(true);
1218 UpdateLocal(bv, INIT, true); // because this toggles displayed
1220 case LyXTabular::UNSET_LONGTABULAR:
1221 tabular->SetLongTabular(false);
1222 UpdateLocal(bv, INIT, true); // because this toggles displayed
1224 case LyXTabular::SET_ROTATE_TABULAR:
1225 tabular->SetRotateTabular(true);
1227 case LyXTabular::UNSET_ROTATE_TABULAR:
1228 tabular->SetRotateTabular(false);
1230 case LyXTabular::SET_ROTATE_CELL:
1231 for(i=sel_row_start; i<=sel_row_end; ++i)
1232 for(j=sel_col_start; j<=sel_col_end; ++j)
1233 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1235 case LyXTabular::UNSET_ROTATE_CELL:
1236 for(i=sel_row_start; i<=sel_row_end; ++i)
1237 for(j=sel_col_start; j<=sel_col_end; ++j)
1238 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1240 case LyXTabular::SET_LINEBREAKS:
1241 what = !tabular->GetLinebreaks(actcell);
1242 for(i=sel_row_start; i<=sel_row_end; ++i)
1243 for(j=sel_col_start; j<=sel_col_end; ++j)
1244 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1246 case LyXTabular::SET_LTFIRSTHEAD:
1247 tabular->SetLTHead(actcell,true);
1249 case LyXTabular::SET_LTHEAD:
1250 tabular->SetLTHead(actcell,false);
1252 case LyXTabular::SET_LTFOOT:
1253 tabular->SetLTFoot(actcell,false);
1255 case LyXTabular::SET_LTLASTFOOT:
1256 tabular->SetLTFoot(actcell,true);
1258 case LyXTabular::SET_LTNEWPAGE:
1259 what = !tabular->GetLTNewPage(actcell);
1260 tabular->SetLTNewPage(actcell,what);
1266 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1269 // the cursor.pos has to be before the inset so if it isn't now just
1270 // reset the curor pos first!
1271 if (cursor.pos() % 2) { // behind the inset
1272 cursor.pos(cursor.pos() - 1);
1275 UpdatableInset * inset =
1276 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1277 LyXFont font(LyXFont::ALL_SANE);
1279 x = inset->x() + inset->width(bv, font);
1280 y = inset->descent(bv, font);
1282 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1283 inset_y = cursor.y();
1284 inset->Edit(bv, x - inset_x, y - inset_y, button);
1285 if (!the_locking_inset)
1287 UpdateLocal(bv, CELL, false);
1292 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1294 InsetText * inset = tabular->GetCellInset(actcell);
1297 if (cursor.pos() % 2) { // behind the inset
1298 return (((x + top_x) < cursor.x()) &&
1299 ((x + top_x) > (cursor.x() - inset->width(bv,
1300 LyXFont(LyXFont::ALL_SANE)))));
1302 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1303 return ((x1 > x2) &&
1304 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1309 // This returns paperWidth() if the cell-width is unlimited or the width
1310 // in pixels if we have a pwidth for this cell.
1311 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1313 string s = tabular->GetPWidth(cell);
1317 return VSpace(s).inPixels( 0, 0);
1321 int InsetTabular::getMaxWidth(Painter & pain,
1322 UpdatableInset const * inset) const
1325 int n = tabular->GetNumberOfCells();
1326 for(cell=0; cell < n; ++cell) {
1327 if (tabular->GetCellInset(cell) == inset)
1332 int w = GetMaxWidthOfCell(pain, cell);
1333 // this because text insets remove the xpos from the maxwidth because
1334 // otherwise the would not break good!!!
1335 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1341 void InsetTabular::recomputeTextInsets(BufferView * bv,
1342 LyXFont const & font) const
1348 for(int j= 0; j < tabular->columns(); ++j) {
1349 for(int i = 0; i < tabular->rows(); ++i) {
1350 if (tabular->IsPartOfMultiColumn(i,j))
1352 cell = tabular->GetCellNumber(i,j);
1353 inset = tabular->GetCellInset(cell);
1354 inset->update(bv, font);
1355 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1357 // cell = tabular->GetCellNumber(0, j);
1358 // cx += tabular->GetWidthOfColumn(cell);
1363 void InsetTabular::resizeLyXText(BufferView *) const