1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 *======================================================
19 #pragma implementation
22 #include "insettabular.h"
25 #include "commandtags.h"
26 #include "LaTeXFeatures.h"
30 #include "lyx_gui_misc.h"
33 #include "insets/insettext.h"
34 #include "frontends/Dialogs.h"
36 extern void MenuLayoutTabular(bool, InsetTabular *);
37 extern bool UpdateLayoutTabular(bool, InsetTabular *);
38 extern void TabularOptClose();
40 const int ADD_TO_HEIGHT = 2;
41 const int ADD_TO_TABULAR_WIDTH = 2;
48 #define cellstart(p) ((p % 2) == 0)
50 //#define USE_NEW_LAYOUT 1
52 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
58 buffer = buf; // set this first!!!
59 tabular = new LyXTabular(this, rows,columns);
60 // for now make it always display as display() inset
62 the_locking_inset = 0;
63 locked = no_selection = cursor_visible = false;
68 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
74 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
76 buffer = buf; // set this first
77 tabular = new LyXTabular(this, *(tab.tabular));
78 the_locking_inset = 0;
79 locked = no_selection = cursor_visible = false;
84 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
90 InsetTabular::~InsetTabular()
94 if (buffer->getUser())
95 buffer->getUser()->owner()->getDialogs()->hideTabular(this);
97 dialogs_->hideTabular(this);
102 Inset * InsetTabular::Clone() const
104 InsetTabular * t = new InsetTabular(*this, buffer);
106 t->tabular = tabular->Clone(t);
111 void InsetTabular::Write(Buffer const * buf, ostream & os) const
113 os << " Tabular" << endl;
114 tabular->Write(buf, os);
118 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
120 bool old_format = (lex.GetString() == "\\LyXTable");
125 tabular = new LyXTabular(buf, this, lex);
133 token = lex.GetString();
134 while (lex.IsOK() && (token != "\\end_inset")) {
136 token = lex.GetString();
138 if (token != "\\end_inset") {
139 lex.printError("Missing \\end_inset at this point. "
145 int InsetTabular::ascent(BufferView *, LyXFont const &) const
147 return tabular->GetAscentOfRow(0);
151 int InsetTabular::descent(BufferView *, LyXFont const &) const
153 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0);
157 int InsetTabular::width(BufferView *, LyXFont const &) const
159 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
163 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
164 float & x, bool cleared) const
166 Painter & pain = bv->painter();
171 UpdatableInset::draw(bv,font,baseline,x,cleared);
172 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
173 (top_x != int(x)) || (top_baseline != baseline))) {
175 int h = ascent(bv, font) + descent(bv, font);
176 int tx = display()? 0:top_x;
177 int w = tx? width(bv, font):pain.paperWidth();
178 int ty = baseline - ascent(bv, font);
182 if ((ty + h) > pain.paperHeight())
183 h = pain.paperHeight();
184 if ((top_x + w) > pain.paperWidth())
185 w = pain.paperWidth();
186 pain.fillRectangle(tx, ty, w, h);
192 if (locked) { // repaint this way as the background was not cleared
193 if (the_locking_inset)
194 the_locking_inset->update(bv, font, true);
196 bv->updateInset(const_cast<InsetTabular*>(this), false);
203 top_baseline = baseline;
205 x += ADD_TO_TABULAR_WIDTH;
206 if (cleared || (need_update == FULL) || (need_update == CELL)) {
207 for(i=0;i<tabular->rows();++i) {
209 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
210 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
211 for(j=0;j<tabular->columns();++j) {
212 if (tabular->IsPartOfMultiColumn(i,j))
214 cx = nx + tabular->GetBeginningOfTextInCell(cell);
216 DrawCellSelection(pain, nx, baseline, i, j, cell);
217 if (dodraw && !cleared && locked && the_locking_inset) {
218 if (the_locking_inset == tabular->GetCellInset(cell))
219 tabular->GetCellInset(cell)->draw(bv, font,
223 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
225 DrawCellLines(pain, nx, baseline, i, cell);
227 nx += tabular->GetWidthOfColumn(cell);
230 baseline += tabular->GetDescentOfRow(i) +
231 tabular->GetAscentOfRow(i+1)+
232 tabular->GetAdditionalHeight(cell+1);
235 x += width(bv, font);
240 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
241 int row, int cell) const
243 int x2 = x + tabular->GetWidthOfColumn(cell);
246 if (!tabular->TopAlreadyDrawed(cell)) {
247 on_off = !tabular->TopLine(cell);
248 pain.line(x, baseline - tabular->GetAscentOfRow(row),
249 x2, baseline - tabular->GetAscentOfRow(row),
250 on_off ? LColor::tabularonoffline:LColor::tabularline,
251 on_off ? Painter::line_onoffdash:Painter::line_solid);
253 on_off = !tabular->BottomLine(cell);
254 pain.line(x,baseline + tabular->GetDescentOfRow(row),
255 x2, baseline + tabular->GetDescentOfRow(row),
256 on_off ? LColor::tabularonoffline:LColor::tabularline,
257 on_off ? Painter::line_onoffdash:Painter::line_solid);
258 if (!tabular->LeftAlreadyDrawed(cell)) {
259 on_off = !tabular->LeftLine(cell);
260 pain.line(x, baseline - tabular->GetAscentOfRow(row),
261 x, baseline + tabular->GetDescentOfRow(row),
262 on_off ? LColor::tabularonoffline:LColor::tabularline,
263 on_off ? Painter::line_onoffdash:Painter::line_solid);
265 on_off = !tabular->RightLine(cell);
266 pain.line(x2 - tabular->GetAdditionalWidth(cell),
267 baseline - tabular->GetAscentOfRow(row),
268 x2 - tabular->GetAdditionalWidth(cell),
269 baseline + tabular->GetDescentOfRow(row),
270 on_off ? LColor::tabularonoffline:LColor::tabularline,
271 on_off ? Painter::line_onoffdash:Painter::line_solid);
275 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
276 int row, int column, int cell) const
280 int cs = tabular->column_of_cell(sel_cell_start);
281 int ce = tabular->column_of_cell(sel_cell_end);
284 cs = tabular->column_of_cell(sel_cell_end);
286 ce = tabular->right_column_of_cell(sel_cell_end);
289 int rs = tabular->row_of_cell(sel_cell_start);
290 int re = tabular->row_of_cell(sel_cell_end);
297 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
298 int w = tabular->GetWidthOfColumn(cell);
299 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
300 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
301 w, h, LColor::selection);
306 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
310 calculate_dimensions_of_cells(bv, font, true);
312 owner()->update(bv, font, true);
315 if (the_locking_inset)
316 the_locking_inset->update(bv, font, reinit);
317 switch(need_update) {
321 if (calculate_dimensions_of_cells(bv, font, false))
333 char const * InsetTabular::EditMessage() const
335 return _("Opened Tabular Inset");
339 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
341 UpdatableInset::Edit(bv, x, y, button);
343 if (!bv->lockInset(this)) {
344 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
348 the_locking_inset = 0;
349 inset_pos = inset_x = inset_y = 0;
351 sel_pos_start = sel_pos_end = cursor.pos();
352 sel_cell_start = sel_cell_end = actcell;
353 bv->text->FinishUndo();
354 if (InsetHit(bv, x, y)) {
355 ActivateCellInset(bv, x, y, button);
357 UpdateLocal(bv, NONE, false);
358 // bv->getOwner()->getPopups().updateFormTabular();
362 void InsetTabular::InsetUnlock(BufferView * bv)
365 if (the_locking_inset) {
366 the_locking_inset->InsetUnlock(bv);
367 the_locking_inset = 0;
370 if (hasSelection()) {
371 sel_pos_start = sel_pos_end = 0;
372 sel_cell_start = sel_cell_end = 0;
373 UpdateLocal(bv, FULL, false);
375 no_selection = false;
380 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
384 bv->updateInset(this, mark_dirty);
389 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
391 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
395 if (inset == tabular->GetCellInset(actcell)) {
396 lyxerr[Debug::INSETS] << "OK" << endl;
397 the_locking_inset = tabular->GetCellInset(actcell);
399 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
400 inset_y = cursor.y();
401 inset_pos = cursor.pos();
403 } else if (the_locking_inset && (the_locking_inset == inset)) {
404 if (cursor.pos() == inset_pos) {
405 lyxerr[Debug::INSETS] << "OK" << endl;
407 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
408 inset_y = cursor.y();
410 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
412 } else if (the_locking_inset) {
413 lyxerr[Debug::INSETS] << "MAYBE" << endl;
414 return the_locking_inset->LockInsetInInset(bv, inset);
416 lyxerr[Debug::INSETS] << "NOT OK" << endl;
420 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
423 if (!the_locking_inset)
425 if (the_locking_inset == inset) {
426 the_locking_inset->InsetUnlock(bv);
427 the_locking_inset = 0;
429 moveRight(bv, false);
430 UpdateLocal(bv, CELL, false);
433 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
434 if ((inset->LyxCode() == TABULAR_CODE) &&
435 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
437 #ifdef USE_NEW_LAYOUT
438 dialogs_ = bv->owner()->getDialogs();
439 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
441 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
450 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
452 if (!the_locking_inset)
454 if (the_locking_inset != inset)
455 return the_locking_inset->UpdateInsetInInset(bv, inset);
456 UpdateLocal(bv, CELL, false);
461 int InsetTabular::InsetInInsetY()
463 if (!the_locking_inset)
466 return (inset_y + the_locking_inset->InsetInInsetY());
470 UpdatableInset * InsetTabular::GetLockingInset()
472 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
476 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
480 if (the_locking_inset)
481 return the_locking_inset->GetFirstLockingInsetOfType(c);
486 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
488 if (the_locking_inset)
489 return the_locking_inset->InsertInset(bv, inset);
494 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
496 if (hasSelection()) {
497 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
498 UpdateLocal(bv, SELECTION, false);
500 no_selection = false;
505 sel_pos_start = sel_pos_end = cursor.pos();
506 sel_cell_start = sel_cell_end = actcell;
508 bool inset_hit = InsetHit(bv, x, y);
510 if ((ocell == actcell) && the_locking_inset && inset_hit) {
511 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
513 } else if (the_locking_inset) {
514 the_locking_inset->InsetUnlock(bv);
516 the_locking_inset = 0;
517 if (inset_hit && bv->the_locking_inset) {
518 ActivateCellInset(bv, x, y, button);
519 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
524 void InsetTabular::InsetButtonRelease(BufferView * bv,
525 int x, int y, int button)
528 if (the_locking_inset) {
530 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
531 i->InsetButtonRelease(bv, x, y, button);
535 #ifdef USE_NEW_LAYOUT
536 dialogs_ = bv->owner()->getDialogs();
537 dialogs_->showTabular(this);
539 else if (ocell != actcell)
540 bview->getOwner()->getPopups().updateTabular();
543 MenuLayoutTabular(true, this);
547 if (the_locking_inset) {
548 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
551 no_selection = false;
555 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
557 if (the_locking_inset) {
558 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
559 y - inset_y, button);
563 // int ocell = actcell,
564 int old = sel_pos_end;
567 sel_pos_end = cursor.pos();
568 sel_cell_end = actcell;
569 if (old != sel_pos_end)
570 UpdateLocal(bv, SELECTION, false);
572 if (ocell != actcell)
573 bview->getOwner()->getPopups().updateFormTabular();
576 no_selection = false;
580 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
582 if (the_locking_inset) {
583 the_locking_inset->InsetKeyPress(xke);
589 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
592 UpdatableInset::RESULT
595 no_selection = false;
596 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
597 || (result == DISPATCHED_NOUPDATE)) {
604 if ((action < 0) && arg.empty())
607 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
608 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
610 if (the_locking_inset) {
611 result=the_locking_inset->LocalDispatch(bv, action, arg);
612 if (result == DISPATCHED_NOUPDATE)
614 else if (result == DISPATCHED) {
615 the_locking_inset->ToggleInsetCursor(bv);
616 UpdateLocal(bv, CELL, false);
617 the_locking_inset->ToggleInsetCursor(bv);
619 } else if (result == FINISHED) {
620 if ((action == LFUN_RIGHT) || (action == -1)) {
621 cursor.pos(inset_pos + 1);
624 sel_pos_start = sel_pos_end = cursor.pos();
625 sel_cell_start = sel_cell_end = actcell;
632 bool hs = hasSelection();
635 // Normal chars not handled here
638 // --- Cursor Movements ---------------------------------------------
640 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
642 moveRight(bv, false);
643 sel_pos_end = cursor.pos();
644 if (!cellstart(cursor.pos())) {
645 if (tabular->right_column_of_cell(sel_cell_start) >
646 tabular->right_column_of_cell(actcell))
647 sel_cell_end = actcell+1;
649 sel_cell_end = actcell;
651 UpdateLocal(bv, SELECTION, false);
654 result = moveRight(bv);
655 sel_pos_start = sel_pos_end = cursor.pos();
656 sel_cell_start = sel_cell_end = actcell;
658 UpdateLocal(bv, CURSOR, false);
661 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
664 sel_pos_end = cursor.pos();
665 if (cellstart(cursor.pos())) {
666 if (tabular->column_of_cell(sel_cell_start) >=
667 tabular->column_of_cell(actcell))
668 sel_cell_end = actcell;
670 sel_cell_end = actcell-1;
672 UpdateLocal(bv, SELECTION, false);
675 result = moveLeft(bv);
676 sel_pos_start = sel_pos_end = cursor.pos();
677 sel_cell_start = sel_cell_end = actcell;
679 UpdateLocal(bv, CURSOR, false);
685 sel_pos_end = cursor.pos();
686 if ((ocell == sel_cell_end) ||
687 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
688 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
690 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
691 UpdateLocal(bv, SELECTION, false);
695 result= moveDown(bv);
696 sel_pos_start = sel_pos_end = cursor.pos();
697 sel_cell_start = sel_cell_end = actcell;
699 UpdateLocal(bv, CURSOR, false);
705 sel_pos_end = cursor.pos();
706 if ((ocell == sel_cell_end) ||
707 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
708 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
710 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
711 UpdateLocal(bv, SELECTION, false);
716 sel_pos_start = sel_pos_end = cursor.pos();
717 sel_cell_start = sel_cell_end = actcell;
719 UpdateLocal(bv, CURSOR, false);
731 if (the_locking_inset) {
732 the_locking_inset->InsetUnlock(bv);
734 the_locking_inset = 0;
735 if (action == LFUN_TAB)
739 sel_pos_start = sel_pos_end = cursor.pos();
740 sel_cell_start = sel_cell_end = actcell;
742 UpdateLocal(bv, CURSOR, false);
744 case LFUN_LAYOUT_TABLE:
746 #ifdef USE_NEW_LAYOUT
747 dialogs_ = bv->owner()->getDialogs();
748 dialogs_->showTabular(this);
750 int flag = (arg == "true");
751 MenuLayoutTabular(flag, this);
756 result = UNDISPATCHED;
759 if (result!=FINISHED) {
760 if (!the_locking_inset) {
762 if (ocell != actcell)
763 bview->getOwner()->getPopups().updateFormTabular();
768 bv->unlockInset(this);
773 int InsetTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
775 return tabular->Latex(buf, os, fragile, fp);
779 int InsetTabular::Ascii(Buffer const *, ostream &) const
784 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
790 int InsetTabular::DocBook(Buffer const *, ostream &) const
796 void InsetTabular::Validate(LaTeXFeatures & features) const
798 tabular->Validate(features);
802 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
803 LyXFont const & font,
809 bool changed = false;
811 for(int i = 0; i < tabular->rows(); ++i) {
812 maxAsc = maxDesc = 0;
813 for(int j= 0; j < tabular->columns(); ++j) {
814 if (tabular->IsPartOfMultiColumn(i,j))
817 inset = tabular->GetCellInset(cell);
819 inset->update(bv, font, false);
820 maxAsc = max(maxAsc, inset->ascent(bv, font));
821 maxDesc = max(maxDesc, inset->descent(bv, font));
822 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
824 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
825 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
831 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
833 x = cursor.x() - top_x;
838 void InsetTabular::ToggleInsetCursor(BufferView * bv)
840 if (the_locking_inset) {
841 the_locking_inset->ToggleInsetCursor(bv);
845 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
847 int asc = lyxfont::maxAscent(font);
848 int desc = lyxfont::maxDescent(font);
851 bv->hideLockedInsetCursor();
853 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
854 cursor_visible = !cursor_visible;
858 void InsetTabular::ShowInsetCursor(BufferView * bv)
860 if (!cursor_visible) {
861 LyXFont font; // = GetFont(par, cursor.pos);
863 int asc = lyxfont::maxAscent(font);
864 int desc = lyxfont::maxDescent(font);
865 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
866 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
867 cursor_visible = true;
872 void InsetTabular::HideInsetCursor(BufferView * bv)
875 ToggleInsetCursor(bv);
879 void InsetTabular::setPos(BufferView * bv, int x, int y) const
884 actcell = actrow = actcol = 0;
885 int ly = tabular->GetDescentOfRow(actrow);
887 // first search the right row
888 while((ly < y) && (actrow < tabular->rows())) {
889 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
890 tabular->GetAscentOfRow(actrow+1) +
891 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
894 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
896 actcell = tabular->GetCellNumber(actrow, actcol);
898 // now search the right column
899 int lx = tabular->GetWidthOfColumn(actcell) -
900 tabular->GetAdditionalWidth(actcell);
901 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
902 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
903 tabular->GetAdditionalWidth(actcell - 1));
904 cursor.pos(((actcell+1) * 2) - 1);
906 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
907 cursor.x(lx + top_x - 2);
909 cursor.pos(cursor.pos() - 1);
910 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
915 int InsetTabular::getCellXPos(int cell) const
919 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
921 int lx = tabular->GetWidthOfColumn(cell);
922 for(; (c < cell); ++c) {
923 lx += tabular->GetWidthOfColumn(c);
925 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
926 ADD_TO_TABULAR_WIDTH);
929 void InsetTabular::resetPos(BufferView * bv) const
933 actcol = tabular->column_of_cell(actcell);
938 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
939 if (tabular->IsLastCellInRow(cell)) {
940 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
941 tabular->GetAscentOfRow(actrow + 1) +
942 tabular->GetAdditionalHeight(cell + 1));
946 cursor.x(getCellXPos(actcell) + 2);
947 if (cursor.pos() % 2) {
948 LyXFont font(LyXFont::ALL_SANE);
949 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
950 tabular->GetBeginningOfTextInCell(actcell));
952 if ((!the_locking_inset ||
953 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
954 (actcell != oldcell)) {
955 #ifdef USE_NEW_LAYOUT
956 dialogs_ = bv->owner()->getDialogs();
957 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
959 UpdateLayoutTabular(true, const_cast<InsetTabular *>(this));
966 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
968 if (!cellstart(cursor.pos())) {
969 if (tabular->IsLastCell(actcell))
972 cursor.pos(cursor.pos() + 1);
974 if (ActivateCellInset(bv))
976 } else { // before the inset
977 cursor.pos(cursor.pos() + 1);
980 return DISPATCHED_NOUPDATE;
984 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
991 cursor.pos(cursor.pos() - 1);
992 if (!cellstart(cursor.pos())) {
994 } else if (lock) { // behind the inset
995 if (ActivateCellInset(bv, 0, 0, 0, true))
999 return DISPATCHED_NOUPDATE;
1003 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1005 int ocell = actcell;
1006 actcell = tabular->GetCellAbove(actcell);
1007 if (actcell == ocell) // we moved out of the inset
1010 return DISPATCHED_NOUPDATE;
1014 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1016 int ocell = actcell;
1017 actcell = tabular->GetCellBelow(actcell);
1018 if (actcell == ocell) // we moved out of the inset
1021 return DISPATCHED_NOUPDATE;
1025 bool InsetTabular::moveNextCell(BufferView * bv)
1027 if (tabular->IsLastCell(actcell))
1030 cursor.pos(cursor.pos() + 1);
1031 if (!cellstart(cursor.pos()))
1032 cursor.pos(cursor.pos() + 1);
1038 bool InsetTabular::movePrevCell(BufferView * bv)
1040 if (!actcell) // first cell
1043 cursor.pos(cursor.pos() - 1);
1044 if (cellstart(cursor.pos()))
1045 cursor.pos(cursor.pos() - 1);
1051 bool InsetTabular::Delete()
1057 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1059 if (the_locking_inset)
1060 the_locking_inset->SetFont(bv, font, tall);
1064 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1073 setAlign = LYX_ALIGN_LEFT,
1079 case LyXTabular::ALIGN_LEFT:
1080 setAlign=LYX_ALIGN_LEFT;
1082 case LyXTabular::ALIGN_RIGHT:
1083 setAlign=LYX_ALIGN_RIGHT;
1085 case LyXTabular::ALIGN_CENTER:
1086 setAlign=LYX_ALIGN_CENTER;
1091 if (hasSelection()) {
1093 sel_col_start = tabular->column_of_cell(sel_cell_start);
1094 sel_col_end = tabular->column_of_cell(sel_cell_end);
1095 if (sel_col_start > sel_col_end) {
1096 sel_col_end = sel_col_start;
1097 sel_col_start = tabular->column_of_cell(sel_cell_end);
1099 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1102 sel_row_start = tabular->row_of_cell(sel_cell_start);
1103 sel_row_end = tabular->row_of_cell(sel_cell_end);
1104 if (sel_row_start > sel_row_end) {
1105 tmp = sel_row_start;
1106 sel_row_start = sel_row_end;
1110 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1111 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1113 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1114 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1115 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1117 int row = tabular->row_of_cell(actcell);
1118 int column = tabular->column_of_cell(actcell);
1121 case LyXTabular::SET_PWIDTH:
1123 bool update = (tabular->GetPWidth(actcell) != val);
1124 tabular->SetPWidth(actcell,val);
1126 for (int i=0; i < tabular->rows(); ++i) {
1127 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1130 UpdateLocal(bv, INIT, true);
1134 case LyXTabular::SET_SPECIAL_COLUMN:
1135 case LyXTabular::SET_SPECIAL_MULTI:
1136 tabular->SetAlignSpecial(actcell,val,feature);
1138 case LyXTabular::APPEND_ROW:
1139 // append the row into the tabular
1140 UnlockInsetInInset(bv, the_locking_inset);
1141 tabular->AppendRow(actcell);
1142 UpdateLocal(bv, INIT, true);
1144 case LyXTabular::APPEND_COLUMN:
1145 // append the column into the tabular
1146 tabular->AppendColumn(actcell);
1147 actcell = tabular->GetCellNumber(row, column);
1148 UpdateLocal(bv, INIT, true);
1150 case LyXTabular::DELETE_ROW:
1151 tabular->DeleteRow(tabular->row_of_cell(actcell));
1152 if ((row+1) > tabular->rows())
1154 actcell = tabular->GetCellNumber(row, column);
1155 UpdateLocal(bv, INIT, true);
1157 case LyXTabular::DELETE_COLUMN:
1158 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1159 if ((column+1) > tabular->columns())
1161 actcell = tabular->GetCellNumber(row, column);
1162 UpdateLocal(bv, INIT, true);
1164 case LyXTabular::TOGGLE_LINE_TOP:
1165 lineSet = !tabular->TopLine(actcell);
1166 for(i=sel_row_start; i<=sel_row_end; ++i)
1167 for(j=sel_col_start; j<=sel_col_end; ++j)
1168 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet);
1169 UpdateLocal(bv, INIT, true);
1172 case LyXTabular::TOGGLE_LINE_BOTTOM:
1173 lineSet = !tabular->BottomLine(actcell);
1174 for(i=sel_row_start; i<=sel_row_end; ++i)
1175 for(j=sel_col_start; j<=sel_col_end; ++j)
1176 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet);
1177 UpdateLocal(bv, INIT, true);
1180 case LyXTabular::TOGGLE_LINE_LEFT:
1181 lineSet = !tabular->LeftLine(actcell);
1182 for(i=sel_row_start; i<=sel_row_end; ++i)
1183 for(j=sel_col_start; j<=sel_col_end; ++j)
1184 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet);
1185 UpdateLocal(bv, INIT, true);
1188 case LyXTabular::TOGGLE_LINE_RIGHT:
1189 lineSet = !tabular->RightLine(actcell);
1190 for(i=sel_row_start; i<=sel_row_end; ++i)
1191 for(j=sel_col_start; j<=sel_col_end; ++j)
1192 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet);
1193 UpdateLocal(bv, INIT, true);
1195 case LyXTabular::ALIGN_LEFT:
1196 case LyXTabular::ALIGN_RIGHT:
1197 case LyXTabular::ALIGN_CENTER:
1198 for(i=sel_row_start; i<=sel_row_end; ++i)
1199 for(j=sel_col_start; j<=sel_col_end; ++j)
1200 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign);
1202 UpdateLocal(bv, INIT, true);
1204 UpdateLocal(bv, CELL, true);
1206 case LyXTabular::MULTICOLUMN:
1208 if (sel_row_start != sel_row_end) {
1209 WriteAlert(_("Impossible Operation!"),
1210 _("Multicolumns can only be horizontally."),
1214 // just multicol for one Single Cell
1215 if (!hasSelection()) {
1216 // check wether we are completly in a multicol
1217 if (tabular->IsMultiColumn(actcell)) {
1218 tabular->UnsetMultiColumn(actcell);
1219 UpdateLocal(bv, INIT, true);
1221 tabular->SetMultiColumn(actcell, 1);
1222 UpdateLocal(bv, CELL, true);
1226 // we have a selection so this means we just add all this
1227 // cells to form a multicolumn cell
1231 if (sel_cell_start > sel_cell_end) {
1232 s_start = sel_cell_end;
1233 s_end = sel_cell_start;
1235 s_start = sel_cell_start;
1236 s_end = sel_cell_end;
1238 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1241 sel_cell_end = sel_cell_start;
1242 sel_pos_end = sel_pos_start;
1243 UpdateLocal(bv, INIT, true);
1246 case LyXTabular::SET_ALL_LINES:
1248 case LyXTabular::UNSET_ALL_LINES:
1249 for(i=sel_row_start; i<=sel_row_end; ++i)
1250 for(j=sel_col_start; j<=sel_col_end; ++j)
1251 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1252 UpdateLocal(bv, INIT, true);
1254 case LyXTabular::SET_LONGTABULAR:
1255 tabular->SetLongTabular(true);
1256 UpdateLocal(bv, INIT, true); // because this toggles displayed
1258 case LyXTabular::UNSET_LONGTABULAR:
1259 tabular->SetLongTabular(false);
1260 UpdateLocal(bv, INIT, true); // because this toggles displayed
1262 case LyXTabular::SET_ROTATE_TABULAR:
1263 tabular->SetRotateTabular(true);
1265 case LyXTabular::UNSET_ROTATE_TABULAR:
1266 tabular->SetRotateTabular(false);
1268 case LyXTabular::SET_ROTATE_CELL:
1269 for(i=sel_row_start; i<=sel_row_end; ++i)
1270 for(j=sel_col_start; j<=sel_col_end; ++j)
1271 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1273 case LyXTabular::UNSET_ROTATE_CELL:
1274 for(i=sel_row_start; i<=sel_row_end; ++i)
1275 for(j=sel_col_start; j<=sel_col_end; ++j)
1276 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1278 case LyXTabular::SET_LINEBREAKS:
1279 what = !tabular->GetLinebreaks(actcell);
1280 for(i=sel_row_start; i<=sel_row_end; ++i)
1281 for(j=sel_col_start; j<=sel_col_end; ++j)
1282 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1284 case LyXTabular::SET_LTFIRSTHEAD:
1285 tabular->SetLTHead(actcell,true);
1287 case LyXTabular::SET_LTHEAD:
1288 tabular->SetLTHead(actcell,false);
1290 case LyXTabular::SET_LTFOOT:
1291 tabular->SetLTFoot(actcell,false);
1293 case LyXTabular::SET_LTLASTFOOT:
1294 tabular->SetLTFoot(actcell,true);
1296 case LyXTabular::SET_LTNEWPAGE:
1297 what = !tabular->GetLTNewPage(actcell);
1298 tabular->SetLTNewPage(actcell,what);
1303 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1306 // the cursor.pos has to be before the inset so if it isn't now just
1307 // reset the curor pos first!
1308 if (cursor.pos() % 2) { // behind the inset
1309 cursor.pos(cursor.pos() - 1);
1312 UpdatableInset * inset =
1313 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1314 LyXFont font(LyXFont::ALL_SANE);
1316 x = inset->x() + inset->width(bv, font);
1317 y = inset->descent(bv, font);
1319 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1320 inset_y = cursor.y();
1321 inset->Edit(bv, x - inset_x, y - inset_y, button);
1322 if (!the_locking_inset)
1324 UpdateLocal(bv, CELL, false);
1328 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1330 InsetText * inset = tabular->GetCellInset(actcell);
1333 if (cursor.pos() % 2) { // behind the inset
1334 return (((x + top_x) < cursor.x()) &&
1335 ((x + top_x) > (cursor.x() - inset->width(bv,
1336 LyXFont(LyXFont::ALL_SANE)))));
1338 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1339 return ((x1 > x2) &&
1340 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1344 // This returns paperWidth() if the cell-width is unlimited or the width
1345 // in pixels if we have a pwidth for this cell.
1346 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1348 string s = tabular->GetPWidth(cell);
1352 return VSpace(s).inPixels( 0, 0);
1355 int InsetTabular::getMaxWidth(Painter & pain,
1356 UpdatableInset const * inset) const
1359 int n = tabular->GetNumberOfCells();
1360 for(cell=0; cell < n; ++cell) {
1361 if (tabular->GetCellInset(cell) == inset)
1366 int w = GetMaxWidthOfCell(pain, cell);
1367 // this because text insets remove the xpos from the maxwidth because
1368 // otherwise the would not break good!!!
1369 // w += getCellXPos(cell) + tabular->GetBeginningOfTextInCell(cell);
1374 void InsetTabular::recomputeTextInsets(BufferView * bv, const LyXFont & font) const
1380 for(int j= 0; j < tabular->columns(); ++j) {
1381 for(int i = 0; i < tabular->rows(); ++i) {
1382 if (tabular->IsPartOfMultiColumn(i,j))
1384 cell = tabular->GetCellNumber(i,j);
1385 inset = tabular->GetCellInset(cell);
1386 inset->update(bv, font);
1387 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1389 // cell = tabular->GetCellNumber(0, j);
1390 // cx += tabular->GetWidthOfColumn(cell);