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"
27 #include "LaTeXFeatures.h"
31 #include "lyx_gui_misc.h"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
40 const int ADD_TO_HEIGHT = 2;
41 const int ADD_TO_TABULAR_WIDTH = 2;
43 static LyXTabular * paste_tabular = 0;
44 bool InsetTabular::hasPasteBuffer() const
46 return (paste_tabular != 0);
57 struct tabular_features {
58 LyXTabular::Feature action;
62 //static tabular_features * tabularFeatures = 0;
64 static tabular_features tabularFeatures[] =
66 { LyXTabular::APPEND_ROW, "append-row" },
67 { LyXTabular::APPEND_COLUMN, "append-column" },
68 { LyXTabular::DELETE_ROW, "delete-row" },
69 { LyXTabular::DELETE_COLUMN, "delete-column" },
70 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
71 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
72 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
73 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
74 { LyXTabular::ALIGN_LEFT, "align-left" },
75 { LyXTabular::ALIGN_RIGHT, "align-right" },
76 { LyXTabular::ALIGN_CENTER, "align-center" },
77 { LyXTabular::VALIGN_TOP, "valign-top" },
78 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
79 { LyXTabular::VALIGN_CENTER, "valign-center" },
80 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
81 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
82 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
83 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
84 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
85 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
86 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
87 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
88 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
89 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
90 { LyXTabular::MULTICOLUMN, "multicolumn" },
91 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
92 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
93 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
94 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
95 { LyXTabular::SET_PWIDTH, "set-pwidth" },
96 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
97 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
98 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
99 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
100 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
101 { LyXTabular::SET_USEBOX, "set-usebox" },
102 { LyXTabular::SET_LTHEAD, "set-lthead" },
103 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
104 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
105 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
106 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
107 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
108 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
109 { LyXTabular::LAST_ACTION, "" }
112 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
119 tabular = new LyXTabular(this, rows,columns);
120 // for now make it always display as display() inset
122 the_locking_inset = 0;
123 locked = no_selection = cursor_visible = false;
125 actrow = actcell = 0;
132 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
135 tabular = new LyXTabular(this, *(tab.tabular));
136 the_locking_inset = 0;
137 locked = no_selection = cursor_visible = false;
139 actrow = actcell = 0;
140 sel_cell_start = sel_cell_end = 0;
146 InsetTabular::~InsetTabular()
153 Inset * InsetTabular::Clone(Buffer const & buf) const
155 InsetTabular * t = new InsetTabular(*this, buf);
157 t->tabular = tabular->Clone(t);
162 void InsetTabular::Write(Buffer const * buf, ostream & os) const
164 os << " Tabular" << endl;
165 tabular->Write(buf, os);
169 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
171 bool old_format = (lex.GetString() == "\\LyXTable");
176 tabular = new LyXTabular(buf, this, lex);
184 token = lex.GetString();
185 while (lex.IsOK() && (token != "\\end_inset")) {
187 token = lex.GetString();
189 if (token != "\\end_inset") {
190 lex.printError("Missing \\end_inset at this point. "
196 int InsetTabular::ascent(BufferView *, LyXFont const &) const
198 return tabular->GetAscentOfRow(0);
202 int InsetTabular::descent(BufferView *, LyXFont const &) const
204 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
208 int InsetTabular::width(BufferView *, LyXFont const &) const
210 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
214 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
215 float & x, bool cleared) const
220 Painter & pain = bv->painter();
225 UpdatableInset::draw(bv, font, baseline, x, cleared);
226 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
227 (top_x != int(x)) || (top_baseline != baseline))) {
228 int h = ascent(bv, font) + descent(bv, font);
229 int tx = display() || !owner() ? 0 : top_x;
230 int w = tx ? width(bv, font) : pain.paperWidth();
231 int ty = baseline - ascent(bv, font);
235 if ((ty + h) > pain.paperHeight())
236 h = pain.paperHeight();
237 if ((top_x + w) > pain.paperWidth())
238 w = pain.paperWidth();
239 pain.fillRectangle(tx, ty, w, h);
244 top_baseline = baseline;
245 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
248 x += ADD_TO_TABULAR_WIDTH;
252 first_visible_cell = -1;
253 for (i = 0; i < tabular->rows(); ++i) {
255 dodraw = ((baseline + tabular->GetDescentOfRow(i)) > 0) &&
256 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight();
257 for (j = 0; j < tabular->columns(); ++j) {
258 if (tabular->IsPartOfMultiColumn(i, j))
260 cx = nx + tabular->GetBeginningOfTextInCell(cell);
262 if (first_visible_cell < 0)
263 first_visible_cell = cell;
265 DrawCellSelection(pain, nx, baseline, i, j, cell);
266 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
268 DrawCellLines(pain, nx, baseline, i, cell);
270 nx += tabular->GetWidthOfColumn(cell);
273 baseline += tabular->GetDescentOfRow(i) +
274 tabular->GetAscentOfRow(i + 1) +
275 tabular->GetAdditionalHeight(cell);
277 } else if (need_update == CELL) {
280 if (the_locking_inset) {
281 Inset * inset = tabular->GetCellInset(cell);
282 for (i = 0; (inset != the_locking_inset) && (i < tabular->rows());
285 for (j = 0; (inset != the_locking_inset) && (j < tabular->columns()); ++j)
287 if (tabular->IsPartOfMultiColumn(i, j))
289 nx += tabular->GetWidthOfColumn(cell);
291 inset = tabular->GetCellInset(cell);
293 if (tabular->row_of_cell(cell) > i) {
295 baseline += tabular->GetDescentOfRow(i) +
296 tabular->GetAscentOfRow(i + 1) +
297 tabular->GetAdditionalHeight(cell);
301 for (i = 0; (cell < actcell) && (i < tabular->rows()); ++i) {
302 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
303 if (tabular->IsPartOfMultiColumn(i, j))
305 nx += tabular->GetWidthOfColumn(cell);
308 if (tabular->row_of_cell(cell) > i) {
310 baseline += tabular->GetDescentOfRow(i) +
311 tabular->GetAscentOfRow(i + 1) +
312 tabular->GetAdditionalHeight(cell);
316 i = tabular->row_of_cell(cell);
317 if (the_locking_inset != tabular->GetCellInset(cell)) {
318 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
323 cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
324 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
325 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
327 // clear only if we didn't have a change
328 if (need_update == CELL) {
329 // clear before the inset
332 baseline - tabular->GetAscentOfRow(i) + 1,
334 tabular->GetAscentOfRow(i) +
335 tabular->GetDescentOfRow(i) - 1);
336 // clear behind the inset
338 int(cx + the_locking_inset->width(bv,font) + 1),
339 baseline - tabular->GetAscentOfRow(i) + 1,
340 tabular->GetWidthOfColumn(cell) -
341 tabular->GetBeginningOfTextInCell(cell) -
342 the_locking_inset->width(bv,font) -
343 tabular->GetAdditionalWidth(cell) - 1,
344 tabular->GetAscentOfRow(i) +
345 tabular->GetDescentOfRow(i) - 1);
348 x -= ADD_TO_TABULAR_WIDTH;
349 x += width(bv, font);
350 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
357 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
358 int row, int cell) const
360 int x2 = x + tabular->GetWidthOfColumn(cell);
363 if (!tabular->TopAlreadyDrawed(cell)) {
364 on_off = !tabular->TopLine(cell);
365 pain.line(x, baseline - tabular->GetAscentOfRow(row),
366 x2, baseline - tabular->GetAscentOfRow(row),
367 on_off ? LColor::tabularonoffline : LColor::tabularline,
368 on_off ? Painter::line_onoffdash : Painter::line_solid);
370 on_off = !tabular->BottomLine(cell);
371 pain.line(x,baseline + tabular->GetDescentOfRow(row),
372 x2, baseline + tabular->GetDescentOfRow(row),
373 on_off ? LColor::tabularonoffline : LColor::tabularline,
374 on_off ? Painter::line_onoffdash : Painter::line_solid);
375 if (!tabular->LeftAlreadyDrawed(cell)) {
376 on_off = !tabular->LeftLine(cell);
377 pain.line(x, baseline - tabular->GetAscentOfRow(row),
378 x, baseline + tabular->GetDescentOfRow(row),
379 on_off ? LColor::tabularonoffline : LColor::tabularline,
380 on_off ? Painter::line_onoffdash : Painter::line_solid);
382 on_off = !tabular->RightLine(cell);
383 pain.line(x2 - tabular->GetAdditionalWidth(cell),
384 baseline - tabular->GetAscentOfRow(row),
385 x2 - tabular->GetAdditionalWidth(cell),
386 baseline + tabular->GetDescentOfRow(row),
387 on_off ? LColor::tabularonoffline : LColor::tabularline,
388 on_off ? Painter::line_onoffdash : Painter::line_solid);
392 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
393 int row, int column, int cell) const
395 int cs = tabular->column_of_cell(sel_cell_start);
396 int ce = tabular->column_of_cell(sel_cell_end);
399 cs = tabular->column_of_cell(sel_cell_end);
401 ce = tabular->right_column_of_cell(sel_cell_end);
404 int rs = tabular->row_of_cell(sel_cell_start);
405 int re = tabular->row_of_cell(sel_cell_end);
406 if (rs > re) swap(rs, re);
408 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
409 int w = tabular->GetWidthOfColumn(cell);
410 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
411 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
412 w, h, LColor::selection);
417 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
421 calculate_dimensions_of_cells(bv, font, true);
423 owner()->update(bv, font, true);
426 if (the_locking_inset) {
427 the_locking_inset->update(bv, font, reinit);
429 // inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
430 // inset_y = cursor.y();
432 switch (need_update) {
436 if (calculate_dimensions_of_cells(bv, font, false))
448 string const InsetTabular::EditMessage() const
450 return _("Opened Tabular Inset");
454 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
456 UpdatableInset::Edit(bv, x, y, button);
458 if (!bv->lockInset(this)) {
459 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
463 the_locking_inset = 0;
467 sel_cell_start = sel_cell_end = actcell;
468 bv->text->FinishUndo();
469 if (InsetHit(bv, x, y) && (button != 3)) {
470 ActivateCellInsetAbs(bv, x, y, button);
472 // UpdateLocal(bv, NONE, false);
473 // bv->getOwner()->getPopups().updateFormTabular();
477 void InsetTabular::InsetUnlock(BufferView * bv)
479 if (the_locking_inset) {
480 the_locking_inset->InsetUnlock(bv);
481 the_locking_inset = 0;
484 no_selection = false;
487 if (scroll() || hasSelection()) {
488 sel_cell_start = sel_cell_end = 0;
492 UpdateLocal(bv, FULL, false);
497 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
498 bool mark_dirty) const
500 if (need_update < what) // only set this if it has greater update
502 if ((what == INIT) && hasSelection())
505 if (need_update != NONE) {
506 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
507 if (locked) // && (what != NONE))
513 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
515 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
519 if (inset == tabular->GetCellInset(actcell)) {
520 lyxerr[Debug::INSETS] << "OK" << endl;
521 the_locking_inset = tabular->GetCellInset(actcell);
523 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
524 inset_y = cursor.y();
526 } else if (the_locking_inset && (the_locking_inset == inset)) {
527 lyxerr[Debug::INSETS] << "OK" << endl;
529 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
530 inset_y = cursor.y();
531 } else if (the_locking_inset) {
532 lyxerr[Debug::INSETS] << "MAYBE" << endl;
533 return the_locking_inset->LockInsetInInset(bv, inset);
535 lyxerr[Debug::INSETS] << "NOT OK" << endl;
540 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
543 if (!the_locking_inset)
545 if (the_locking_inset == inset) {
546 the_locking_inset->InsetUnlock(bv);
547 the_locking_inset = 0;
548 UpdateLocal(bv, CELL, false);
549 ShowInsetCursor(bv, false);
552 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
553 if (inset->LyxCode() == TABULAR_CODE &&
554 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
556 bv->owner()->getDialogs()->updateTabular(this);
565 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
567 if (!the_locking_inset)
569 if (the_locking_inset != inset) {
570 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
573 UpdateLocal(bv, CELL, false);
578 unsigned int InsetTabular::InsetInInsetY()
580 if (!the_locking_inset)
583 return (inset_y + the_locking_inset->InsetInInsetY());
587 UpdatableInset * InsetTabular::GetLockingInset()
589 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
593 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
597 if (the_locking_inset)
598 return the_locking_inset->GetFirstLockingInsetOfType(c);
603 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
605 if (the_locking_inset)
606 return the_locking_inset->InsertInset(bv, inset);
611 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
613 if (hasSelection() && (button == 3))
616 if (hasSelection()) {
618 UpdateLocal(bv, SELECTION, false);
621 no_selection = false;
623 int const ocell = actcell;
624 int const orow = actrow;
629 UpdateLocal(bv, NONE, false);
630 sel_cell_start = sel_cell_end = actcell;
632 if ((ocell != actcell) && the_locking_inset) {
633 the_locking_inset->InsetUnlock(bv);
634 the_locking_inset = 0;
640 bool const inset_hit = InsetHit(bv, x, y);
642 if ((ocell == actcell) && the_locking_inset && inset_hit) {
644 the_locking_inset->InsetButtonPress(bv,
645 x - inset_x, y - inset_y, button);
647 } else if (the_locking_inset) {
648 the_locking_inset->InsetUnlock(bv);
650 the_locking_inset = 0;
652 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
655 if (inset_hit && bv->theLockingInset()) {
656 if (ActivateCellInsetAbs(bv, x, y, button))
657 the_locking_inset->InsetButtonPress(bv, x - inset_x,
658 y - inset_y, button);
665 void InsetTabular::InsetButtonRelease(BufferView * bv,
666 int x, int y, int button)
669 if (the_locking_inset) {
671 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
672 i->InsetButtonRelease(bv, x, y, button);
676 bv->owner()->getDialogs()->showTabular(this);
679 if (the_locking_inset) {
680 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
683 no_selection = false;
687 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
689 if (the_locking_inset) {
690 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
691 y - inset_y, button);
696 int const old_cell = actcell;
699 sel_cell_end = actcell;
700 if (sel_cell_end != old_cell)
701 UpdateLocal(bv, SELECTION, false);
704 no_selection = false;
708 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
710 if (the_locking_inset) {
711 the_locking_inset->InsetKeyPress(xke);
717 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
720 // We need to save the value of the_locking_inset as the call to
721 // the_locking_inset->LocalDispatch might unlock it.
722 old_locking_inset = the_locking_inset;
723 no_selection = false;
724 UpdatableInset::RESULT result =
725 UpdatableInset::LocalDispatch(bv, action, arg);
726 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
731 if ((action < 0) && arg.empty())
734 if (the_locking_inset) {
735 result=the_locking_inset->LocalDispatch(bv, action, arg);
736 if (result == DISPATCHED_NOUPDATE) {
739 if (sc != scroll()) { // inset has been scrolled
740 the_locking_inset->ToggleInsetCursor(bv);
741 UpdateLocal(bv, FULL, false);
742 the_locking_inset->ToggleInsetCursor(bv);
745 } else if (result == DISPATCHED) {
746 the_locking_inset->ToggleInsetCursor(bv);
747 UpdateLocal(bv, CELL, false);
748 the_locking_inset->ToggleInsetCursor(bv);
750 } else if (result == FINISHED) {
754 bool hs = hasSelection();
758 // --- Cursor Movements ---------------------------------------------
760 if (tabular->IsLastCellInRow(actcell))
762 moveRight(bv, false);
763 sel_cell_end = actcell;
764 UpdateLocal(bv, SELECTION, false);
767 result = moveRight(bv);
768 sel_cell_start = sel_cell_end = actcell;
770 UpdateLocal(bv, SELECTION, false);
773 if (tabular->IsFirstCellInRow(actcell))
776 sel_cell_end = actcell;
777 UpdateLocal(bv, SELECTION, false);
780 result = moveLeft(bv);
781 sel_cell_start = sel_cell_end = actcell;
783 UpdateLocal(bv, SELECTION, false);
787 int const ocell = actcell;
789 if ((ocell == sel_cell_end) ||
790 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
791 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
793 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
794 UpdateLocal(bv, SELECTION, false);
798 result = moveDown(bv, old_locking_inset != 0);
799 sel_cell_start = sel_cell_end = actcell;
801 UpdateLocal(bv, SELECTION, false);
805 int const ocell = actcell;
807 if ((ocell == sel_cell_end) ||
808 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
809 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
811 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
812 UpdateLocal(bv, SELECTION, false);
816 result = moveUp(bv, old_locking_inset != 0);
817 sel_cell_start = sel_cell_end = actcell;
819 UpdateLocal(bv, SELECTION, false);
823 if (the_locking_inset) {
824 UnlockInsetInInset(bv, the_locking_inset);
825 the_locking_inset = 0;
827 if (bv->text->first + bv->painter().paperHeight() <
828 (top_baseline + tabular->GetHeightOfTabular()))
830 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
831 UpdateLocal(bv, FULL, false);
832 actcell = tabular->GetCellBelow(first_visible_cell) + column;
834 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
837 UpdateLocal(bv, CURSOR, false);
842 if (the_locking_inset) {
843 UnlockInsetInInset(bv, the_locking_inset);
844 the_locking_inset = 0;
846 if (top_baseline < 0) {
847 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
848 UpdateLocal(bv, FULL, false);
849 if (top_baseline > 0)
852 actcell = tabular->GetCellBelow(first_visible_cell) + column;
857 UpdateLocal(bv, CURSOR, false);
871 if (the_locking_inset) {
872 UnlockInsetInInset(bv, the_locking_inset);
873 the_locking_inset = 0;
875 if (action == LFUN_TAB)
876 moveNextCell(bv, old_locking_inset != 0);
878 movePrevCell(bv, old_locking_inset != 0);
879 sel_cell_start = sel_cell_end = actcell;
881 UpdateLocal(bv, SELECTION, false);
884 case LFUN_LAYOUT_TABULAR:
886 bv->owner()->getDialogs()->showTabular(this);
889 case LFUN_TABULAR_FEATURE:
890 if (!TabularFeatures(bv, arg))
891 result = UNDISPATCHED;
894 if (!copySelection(bv))
896 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
898 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
899 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
901 bv->text->cursor.par()->previous,
902 bv->text->cursor.par()->next
906 UpdateLocal(bv, INIT, true);
911 bv->text->FinishUndo();
914 case LFUN_PASTESELECTION:
916 string clip(bv->workarea()->getClipboard());
920 if (clip.find('\t') != string::npos) {
924 unsigned int len = clip.length();
925 string::size_type p = 0;
928 ((p = clip.find_first_of("\t\n", p)) != string::npos))
937 maxCols = max(cols, maxCols);
943 maxCols = max(cols, maxCols);
944 delete paste_tabular;
945 paste_tabular = new LyXTabular(this, rows, maxCols);
946 string::size_type op = 0;
948 int cells = paste_tabular->GetNumberOfCells();
950 while((cell < cells) && (p < len) &&
951 (p = clip.find_first_of("\t\n", p)) != string::npos)
957 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
962 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
963 while(cols++ < maxCols)
971 // check for the last cell if there is no trailing '\n'
972 if ((cell < cells) && (op < len))
973 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
975 // so that the clipboard is used and it goes on to default
976 // and executes LFUN_PASTESELECTION in insettext!
977 delete paste_tabular;
982 if (hasPasteBuffer()) {
983 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
985 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
986 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
988 bv->text->cursor.par()->previous,
989 bv->text->cursor.par()->next
993 UpdateLocal(bv, INIT, true);
996 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
998 // we try to activate the actual inset and put this event down to
999 // the insets dispatch function.
1000 result = UNDISPATCHED;
1001 if (the_locking_inset)
1004 if (ActivateCellInset(bv)) {
1005 result=the_locking_inset->LocalDispatch(bv, action, arg);
1006 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1007 UnlockInsetInInset(bv, the_locking_inset);
1009 the_locking_inset = 0;
1010 return UNDISPATCHED;
1013 the_locking_inset->ToggleInsetCursor(bv);
1014 UpdateLocal(bv, CELL, false);
1015 the_locking_inset->ToggleInsetCursor(bv);
1020 if (result!=FINISHED) {
1021 if (!the_locking_inset) {
1022 ShowInsetCursor(bv);
1025 bv->unlockInset(this);
1030 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1031 bool fragile, bool fp) const
1033 return tabular->Latex(buf, os, fragile, fp);
1037 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1039 // This should be changed to a real ascii export
1040 return tabular->Ascii(buf, os);
1044 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1050 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1052 return tabular->DocBook(buf,os);
1056 void InsetTabular::Validate(LaTeXFeatures & features) const
1058 tabular->Validate(features);
1062 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1063 LyXFont const & font,
1070 bool changed = false;
1072 // if we have a locking_inset we should have to check only this cell for
1073 // change so I'll try this to have a boost, but who knows ;)
1074 if ((need_update != INIT) &&
1075 (the_locking_inset == tabular->GetCellInset(actcell))) {
1076 for(int i = 0; i < tabular->columns(); ++i) {
1077 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1079 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1082 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1083 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1084 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1087 for (int i = 0; i < tabular->rows(); ++i) {
1090 for (int j= 0; j < tabular->columns(); ++j) {
1091 if (tabular->IsPartOfMultiColumn(i,j))
1094 inset = tabular->GetCellInset(cell);
1096 inset->update(bv, font, false);
1097 maxAsc = max(maxAsc, inset->ascent(bv, font));
1098 maxDesc = max(maxDesc, inset->descent(bv, font));
1099 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1101 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1102 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1108 void InsetTabular::GetCursorPos(BufferView *,
1109 int & x, int & y) const
1111 x = cursor.x() - top_x;
1116 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1118 if (the_locking_inset) {
1119 the_locking_inset->ToggleInsetCursor(bv);
1123 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1125 int const asc = lyxfont::maxAscent(font);
1126 int const desc = lyxfont::maxDescent(font);
1129 bv->hideLockedInsetCursor();
1131 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1132 cursor_visible = !cursor_visible;
1136 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1138 if (!cursor_visible) {
1139 LyXFont font; // = GetFont(par, cursor.pos);
1141 int const asc = lyxfont::maxAscent(font);
1142 int const desc = lyxfont::maxDescent(font);
1143 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1145 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1146 cursor_visible = true;
1151 void InsetTabular::HideInsetCursor(BufferView * bv)
1153 if (cursor_visible) {
1154 bv->hideLockedInsetCursor();
1155 cursor_visible = false;
1157 // if (cursor_visible)
1158 // ToggleInsetCursor(bv);
1162 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1166 actcell = actrow = actcol = 0;
1167 int ly = tabular->GetDescentOfRow(actrow);
1169 // first search the right row
1170 while((ly < y) && (actrow < tabular->rows())) {
1171 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1172 tabular->GetAscentOfRow(actrow + 1) +
1173 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1176 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1178 actcell = tabular->GetCellNumber(actrow, actcol);
1180 // now search the right column
1181 int lx = tabular->GetWidthOfColumn(actcell) -
1182 tabular->GetAdditionalWidth(actcell);
1184 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1185 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1186 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1187 tabular->GetAdditionalWidth(actcell - 1));
1189 // Jürgen, you should check that this is correct. (Lgb)
1190 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1191 lx += tabular->GetWidthOfColumn(actcell + 1)
1192 + tabular->GetAdditionalWidth(actcell);
1196 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1201 int InsetTabular::getCellXPos(int cell) const
1205 for (; !tabular->IsFirstCellInRow(c); --c)
1207 int lx = tabular->GetWidthOfColumn(cell);
1208 for (; c < cell; ++c) {
1209 lx += tabular->GetWidthOfColumn(c);
1211 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1215 void InsetTabular::resetPos(BufferView * bv) const
1219 actcol = tabular->column_of_cell(actcell);
1224 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1225 if (tabular->IsLastCellInRow(cell)) {
1226 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1227 tabular->GetAscentOfRow(actrow + 1) +
1228 tabular->GetAdditionalHeight(cell + 1));
1232 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1233 int new_x = getCellXPos(actcell);
1234 int old_x = cursor.x();
1237 // cursor.x(getCellXPos(actcell) + offset);
1238 if (scroll() && (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1240 else if (the_locking_inset &&
1241 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1243 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1244 if (xx > (bv->workWidth()-20))
1245 scroll(bv, -(xx - bv->workWidth() + 60));
1253 } else if (((cursor.x() - offset) > 20) &&
1254 ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1255 (bv->workWidth()-20)))
1257 scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1258 UpdateLocal(bv, FULL, false);
1259 } else if ((cursor.x() - offset) < 20) {
1260 scroll(bv, 20 - cursor.x() + offset);
1261 UpdateLocal(bv, FULL, false);
1262 } else if (scroll() && (top_x > 20) &&
1263 ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1264 scroll(bv, old_x - cursor.x());
1266 if ((!the_locking_inset ||
1267 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1268 (actcell != oldcell)) {
1269 InsetTabular * inset = const_cast<InsetTabular *>(this);
1270 bv->owner()->getDialogs()->updateTabular(inset);
1276 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1278 if (lock && !old_locking_inset) {
1279 if (ActivateCellInset(bv))
1282 bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1285 if (lock && ActivateCellInset(bv))
1289 return DISPATCHED_NOUPDATE;
1293 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1295 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1298 if (lock) { // behind the inset
1299 if (ActivateCellInset(bv, 0, 0, 0, true))
1303 return DISPATCHED_NOUPDATE;
1307 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1309 int const ocell = actcell;
1310 actcell = tabular->GetCellAbove(actcell);
1311 if (actcell == ocell) // we moved out of the inset
1317 if (old_locking_inset) {
1318 old_locking_inset->GetCursorPos(bv, x, y);
1319 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1321 if (ActivateCellInset(bv, x, 0))
1324 return DISPATCHED_NOUPDATE;
1328 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1330 int const ocell = actcell;
1331 actcell = tabular->GetCellBelow(actcell);
1332 if (actcell == ocell) // we moved out of the inset
1338 if (old_locking_inset) {
1339 old_locking_inset->GetCursorPos(bv, x, y);
1340 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1342 if (ActivateCellInset(bv, x, 0))
1345 return DISPATCHED_NOUPDATE;
1349 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1351 if (isRightToLeft(bv)) {
1352 if (tabular->IsFirstCellInRow(actcell)) {
1353 int row = tabular->row_of_cell(actcell);
1354 if (row == tabular->rows() - 1)
1356 actcell = tabular->GetLastCellInRow(row);
1357 actcell = tabular->GetCellBelow(actcell);
1364 if (tabular->IsLastCell(actcell))
1369 bool rtl = tabular->GetCellInset(actcell)->par->
1370 isRightToLeftPar(bv->buffer()->params);
1371 ActivateCellInset(bv, 0, 0, 0, !rtl);
1378 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1380 if (isRightToLeft(bv)) {
1381 if (tabular->IsLastCellInRow(actcell)) {
1382 int row = tabular->row_of_cell(actcell);
1385 actcell = tabular->GetFirstCellInRow(row);
1386 actcell = tabular->GetCellAbove(actcell);
1388 if (tabular->IsLastCell(actcell))
1393 if (!actcell) // first cell
1398 bool rtl = tabular->GetCellInset(actcell)->par->
1399 isRightToLeftPar(bv->buffer()->params);
1400 ActivateCellInset(bv, 0, 0, 0, !rtl);
1407 bool InsetTabular::Delete()
1413 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1415 if (the_locking_inset)
1416 the_locking_inset->SetFont(bv, font, tall);
1420 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1422 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1425 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1426 string const tmp = tabularFeatures[i].feature;
1428 if (tmp == what.substr(0, tmp.length())) {
1429 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1430 //tabularFeatures[i].feature.length())) {
1431 action = tabularFeatures[i].action;
1435 if (action == LyXTabular::LAST_ACTION)
1439 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1440 TabularFeatures(bv, action, val);
1445 void InsetTabular::TabularFeatures(BufferView * bv,
1446 LyXTabular::Feature feature,
1447 string const & value)
1456 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1457 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1462 case LyXTabular::M_ALIGN_LEFT:
1463 case LyXTabular::ALIGN_LEFT:
1464 setAlign=LYX_ALIGN_LEFT;
1466 case LyXTabular::M_ALIGN_RIGHT:
1467 case LyXTabular::ALIGN_RIGHT:
1468 setAlign=LYX_ALIGN_RIGHT;
1470 case LyXTabular::M_ALIGN_CENTER:
1471 case LyXTabular::ALIGN_CENTER:
1472 setAlign=LYX_ALIGN_CENTER;
1474 case LyXTabular::M_VALIGN_TOP:
1475 case LyXTabular::VALIGN_TOP:
1476 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1478 case LyXTabular::M_VALIGN_BOTTOM:
1479 case LyXTabular::VALIGN_BOTTOM:
1480 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1482 case LyXTabular::M_VALIGN_CENTER:
1483 case LyXTabular::VALIGN_CENTER:
1484 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1489 if (hasSelection()) {
1490 sel_col_start = tabular->column_of_cell(sel_cell_start);
1491 sel_col_end = tabular->column_of_cell(sel_cell_end);
1492 if (sel_col_start > sel_col_end) {
1493 sel_col_end = sel_col_start;
1494 sel_col_start = tabular->column_of_cell(sel_cell_end);
1496 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1499 sel_row_start = tabular->row_of_cell(sel_cell_start);
1500 sel_row_end = tabular->row_of_cell(sel_cell_end);
1501 if (sel_row_start > sel_row_end) {
1502 //int tmp = sel_row_start;
1503 //sel_row_start = sel_row_end;
1504 //sel_row_end = tmp;
1505 swap(sel_row_start, sel_row_end);
1508 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1509 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1511 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1513 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1514 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1516 bv->text->cursor.par()->previous,
1517 bv->text->cursor.par()->next
1521 int row = tabular->row_of_cell(actcell);
1522 int column = tabular->column_of_cell(actcell);
1526 case LyXTabular::SET_PWIDTH:
1528 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1529 tabular->SetColumnPWidth(actcell,value);
1531 for (int i=0; i < tabular->rows(); ++i) {
1532 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1535 UpdateLocal(bv, INIT, true);
1539 case LyXTabular::SET_MPWIDTH:
1541 bool const update = (tabular->GetPWidth(actcell) != value);
1542 tabular->SetMColumnPWidth(actcell,value);
1544 for (int i=0; i < tabular->rows(); ++i) {
1545 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1548 UpdateLocal(bv, INIT, true);
1552 case LyXTabular::SET_SPECIAL_COLUMN:
1553 case LyXTabular::SET_SPECIAL_MULTI:
1554 tabular->SetAlignSpecial(actcell,value,feature);
1556 case LyXTabular::APPEND_ROW:
1557 // append the row into the tabular
1558 UnlockInsetInInset(bv, the_locking_inset);
1559 tabular->AppendRow(actcell);
1560 UpdateLocal(bv, INIT, true);
1562 case LyXTabular::APPEND_COLUMN:
1563 // append the column into the tabular
1564 UnlockInsetInInset(bv, the_locking_inset);
1565 tabular->AppendColumn(actcell);
1566 actcell = tabular->GetCellNumber(row, column);
1567 UpdateLocal(bv, INIT, true);
1569 case LyXTabular::DELETE_ROW:
1570 UnlockInsetInInset(bv, the_locking_inset);
1571 tabular->DeleteRow(tabular->row_of_cell(actcell));
1572 if ((row+1) > tabular->rows())
1574 actcell = tabular->GetCellNumber(row, column);
1576 UpdateLocal(bv, INIT, true);
1578 case LyXTabular::DELETE_COLUMN:
1579 UnlockInsetInInset(bv, the_locking_inset);
1580 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1581 if ((column+1) > tabular->columns())
1583 actcell = tabular->GetCellNumber(row, column);
1585 UpdateLocal(bv, INIT, true);
1587 case LyXTabular::M_TOGGLE_LINE_TOP:
1589 case LyXTabular::TOGGLE_LINE_TOP:
1590 lineSet = !tabular->TopLine(actcell, flag);
1591 for (i=sel_row_start; i<=sel_row_end; ++i)
1592 for (j=sel_col_start; j<=sel_col_end; ++j)
1593 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1594 UpdateLocal(bv, INIT, true);
1597 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1599 case LyXTabular::TOGGLE_LINE_BOTTOM:
1600 lineSet = !tabular->BottomLine(actcell, flag);
1601 for (i=sel_row_start; i<=sel_row_end; ++i)
1602 for (j=sel_col_start; j<=sel_col_end; ++j)
1603 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1605 UpdateLocal(bv, INIT, true);
1608 case LyXTabular::M_TOGGLE_LINE_LEFT:
1610 case LyXTabular::TOGGLE_LINE_LEFT:
1611 lineSet = !tabular->LeftLine(actcell, flag);
1612 for (i=sel_row_start; i<=sel_row_end; ++i)
1613 for (j=sel_col_start; j<=sel_col_end; ++j)
1614 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1616 UpdateLocal(bv, INIT, true);
1619 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1621 case LyXTabular::TOGGLE_LINE_RIGHT:
1622 lineSet = !tabular->RightLine(actcell, flag);
1623 for (i=sel_row_start; i<=sel_row_end; ++i)
1624 for (j=sel_col_start; j<=sel_col_end; ++j)
1625 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1627 UpdateLocal(bv, INIT, true);
1629 case LyXTabular::M_ALIGN_LEFT:
1630 case LyXTabular::M_ALIGN_RIGHT:
1631 case LyXTabular::M_ALIGN_CENTER:
1633 case LyXTabular::ALIGN_LEFT:
1634 case LyXTabular::ALIGN_RIGHT:
1635 case LyXTabular::ALIGN_CENTER:
1636 for (i = sel_row_start; i <= sel_row_end; ++i)
1637 for (j = sel_col_start; j <= sel_col_end; ++j)
1638 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1640 UpdateLocal(bv, INIT, true);
1642 case LyXTabular::M_VALIGN_TOP:
1643 case LyXTabular::M_VALIGN_BOTTOM:
1644 case LyXTabular::M_VALIGN_CENTER:
1646 case LyXTabular::VALIGN_TOP:
1647 case LyXTabular::VALIGN_BOTTOM:
1648 case LyXTabular::VALIGN_CENTER:
1649 for (i = sel_row_start; i <= sel_row_end; ++i)
1650 for (j = sel_col_start; j <= sel_col_end; ++j)
1651 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1653 UpdateLocal(bv, INIT, true);
1655 case LyXTabular::MULTICOLUMN:
1657 if (sel_row_start != sel_row_end) {
1658 WriteAlert(_("Impossible Operation!"),
1659 _("Multicolumns can only be horizontally."),
1663 // just multicol for one Single Cell
1664 if (!hasSelection()) {
1665 // check wether we are completly in a multicol
1666 if (tabular->IsMultiColumn(actcell)) {
1667 tabular->UnsetMultiColumn(actcell);
1668 UpdateLocal(bv, INIT, true);
1670 tabular->SetMultiColumn(actcell, 1);
1671 UpdateLocal(bv, CELL, true);
1675 // we have a selection so this means we just add all this
1676 // cells to form a multicolumn cell
1680 if (sel_cell_start > sel_cell_end) {
1681 s_start = sel_cell_end;
1682 s_end = sel_cell_start;
1684 s_start = sel_cell_start;
1685 s_end = sel_cell_end;
1687 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1689 sel_cell_end = sel_cell_start;
1690 UpdateLocal(bv, INIT, true);
1693 case LyXTabular::SET_ALL_LINES:
1695 case LyXTabular::UNSET_ALL_LINES:
1696 for (i=sel_row_start; i<=sel_row_end; ++i)
1697 for (j=sel_col_start; j<=sel_col_end; ++j)
1698 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1699 UpdateLocal(bv, INIT, true);
1701 case LyXTabular::SET_LONGTABULAR:
1702 tabular->SetLongTabular(true);
1703 UpdateLocal(bv, INIT, true); // because this toggles displayed
1705 case LyXTabular::UNSET_LONGTABULAR:
1706 tabular->SetLongTabular(false);
1707 UpdateLocal(bv, INIT, true); // because this toggles displayed
1709 case LyXTabular::SET_ROTATE_TABULAR:
1710 tabular->SetRotateTabular(true);
1712 case LyXTabular::UNSET_ROTATE_TABULAR:
1713 tabular->SetRotateTabular(false);
1715 case LyXTabular::SET_ROTATE_CELL:
1716 for (i=sel_row_start; i<=sel_row_end; ++i)
1717 for (j=sel_col_start; j<=sel_col_end; ++j)
1718 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1720 case LyXTabular::UNSET_ROTATE_CELL:
1721 for (i = sel_row_start; i <= sel_row_end; ++i)
1722 for (j = sel_col_start; j <= sel_col_end; ++j)
1723 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1725 case LyXTabular::SET_USEBOX:
1727 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1728 if (val == tabular->GetUsebox(actcell))
1729 val = LyXTabular::BOX_NONE;
1730 for (i = sel_row_start; i <= sel_row_end; ++i)
1731 for (j = sel_col_start; j <= sel_col_end; ++j)
1732 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1735 case LyXTabular::SET_LTFIRSTHEAD:
1736 tabular->SetLTHead(actcell, true);
1738 case LyXTabular::SET_LTHEAD:
1739 tabular->SetLTHead(actcell, false);
1741 case LyXTabular::SET_LTFOOT:
1742 tabular->SetLTFoot(actcell, false);
1744 case LyXTabular::SET_LTLASTFOOT:
1745 tabular->SetLTFoot(actcell, true);
1747 case LyXTabular::SET_LTNEWPAGE:
1748 what = !tabular->GetLTNewPage(actcell);
1749 tabular->SetLTNewPage(actcell, what);
1751 // dummy stuff just to avoid warnings
1752 case LyXTabular::LAST_ACTION:
1758 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1761 UpdatableInset * inset =
1762 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1763 LyXFont font(LyXFont::ALL_SANE);
1765 x = inset->x() + inset->width(bv, font);
1766 y = inset->descent(bv, font);
1768 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1769 //inset_y = cursor.y();
1770 inset->Edit(bv, x, y, button);
1771 if (!the_locking_inset)
1773 UpdateLocal(bv, CELL, false);
1774 return (the_locking_inset != 0);
1778 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1781 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1782 inset_y = cursor.y();
1783 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1787 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1789 return x + top_x > cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1793 // This returns paperWidth() if the cell-width is unlimited or the width
1794 // in pixels if we have a pwidth for this cell.
1795 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1797 string const s = tabular->GetPWidth(cell);
1801 return VSpace(s).inPixels(0, 0);
1805 int InsetTabular::getMaxWidth(Painter & pain,
1806 UpdatableInset const * inset) const
1808 int const n = tabular->GetNumberOfCells();
1810 for (; cell < n; ++cell) {
1811 if (tabular->GetCellInset(cell) == inset)
1816 int w = GetMaxWidthOfCell(pain, cell);
1818 // because the inset then subtracts it's top_x and owner->x()
1819 w += (inset->x() - top_x);
1824 void InsetTabular::resizeLyXText(BufferView *) const
1830 LyXText * InsetTabular::getLyXText(BufferView const * bv, bool const recursive) const
1832 if (the_locking_inset)
1833 return the_locking_inset->getLyXText(bv, recursive);
1834 return Inset::getLyXText(bv, recursive);
1838 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1840 if (the_locking_inset) {
1841 InsetTabular * i = static_cast<InsetTabular *>
1842 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1844 i->OpenLayoutDialog(bv);
1848 bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1852 // functions returns:
1856 // 3 ... toggled off
1858 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1860 int action = LyXTabular::LAST_ACTION;
1861 LyXFunc::func_status status = LyXFunc::OK;
1864 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1865 string const tmp = tabularFeatures[i].feature;
1866 if (tmp == what.substr(0, tmp.length())) {
1867 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1868 // tabularFeatures[i].feature.length())) {
1869 action = tabularFeatures[i].action;
1873 if (action == LyXTabular::LAST_ACTION)
1874 return LyXFunc::Unknown;
1876 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1883 if (hasSelection()) {
1884 sel_row_start = tabular->row_of_cell(sel_cell_start);
1885 sel_row_end = tabular->row_of_cell(sel_cell_end);
1886 if (sel_row_start > sel_row_end) {
1887 //int tmp = sel_row_start;
1888 //sel_row_start = sel_row_end;
1889 //sel_row_end = tmp;
1890 swap(sel_row_start, sel_row_end);
1893 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1897 case LyXTabular::SET_PWIDTH:
1898 case LyXTabular::SET_MPWIDTH:
1899 case LyXTabular::SET_SPECIAL_COLUMN:
1900 case LyXTabular::SET_SPECIAL_MULTI:
1901 status |= LyXFunc::Disabled;
1904 case LyXTabular::APPEND_ROW:
1905 case LyXTabular::APPEND_COLUMN:
1906 case LyXTabular::DELETE_ROW:
1907 case LyXTabular::DELETE_COLUMN:
1908 case LyXTabular::SET_ALL_LINES:
1909 case LyXTabular::UNSET_ALL_LINES:
1910 status |= LyXFunc::OK;
1913 case LyXTabular::MULTICOLUMN:
1914 if (tabular->IsMultiColumn(actcell))
1915 status |= LyXFunc::ToggleOn;
1917 status |= LyXFunc::ToggleOff;
1919 case LyXTabular::M_TOGGLE_LINE_TOP:
1921 case LyXTabular::TOGGLE_LINE_TOP:
1922 if (tabular->TopLine(actcell, flag))
1923 status |= LyXFunc::ToggleOn;
1925 status |= LyXFunc::ToggleOff;
1927 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1929 case LyXTabular::TOGGLE_LINE_BOTTOM:
1930 if (tabular->BottomLine(actcell, flag))
1931 status |= LyXFunc::ToggleOn;
1933 status |= LyXFunc::ToggleOff;
1935 case LyXTabular::M_TOGGLE_LINE_LEFT:
1937 case LyXTabular::TOGGLE_LINE_LEFT:
1938 if (tabular->LeftLine(actcell, flag))
1939 status |= LyXFunc::ToggleOn;
1941 status |= LyXFunc::ToggleOff;
1943 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1945 case LyXTabular::TOGGLE_LINE_RIGHT:
1946 if (tabular->RightLine(actcell, flag))
1947 status |= LyXFunc::ToggleOn;
1949 status |= LyXFunc::ToggleOff;
1951 case LyXTabular::M_ALIGN_LEFT:
1953 case LyXTabular::ALIGN_LEFT:
1954 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1955 status |= LyXFunc::ToggleOn;
1957 status |= LyXFunc::ToggleOff;
1959 case LyXTabular::M_ALIGN_RIGHT:
1961 case LyXTabular::ALIGN_RIGHT:
1962 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1963 status |= LyXFunc::ToggleOn;
1965 status |= LyXFunc::ToggleOff;
1967 case LyXTabular::M_ALIGN_CENTER:
1969 case LyXTabular::ALIGN_CENTER:
1970 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1971 status |= LyXFunc::ToggleOn;
1973 status |= LyXFunc::ToggleOff;
1975 case LyXTabular::M_VALIGN_TOP:
1977 case LyXTabular::VALIGN_TOP:
1978 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1979 status |= LyXFunc::ToggleOn;
1981 status |= LyXFunc::ToggleOff;
1983 case LyXTabular::M_VALIGN_BOTTOM:
1985 case LyXTabular::VALIGN_BOTTOM:
1986 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1987 status |= LyXFunc::ToggleOn;
1989 status |= LyXFunc::ToggleOff;
1991 case LyXTabular::M_VALIGN_CENTER:
1993 case LyXTabular::VALIGN_CENTER:
1994 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1995 status |= LyXFunc::ToggleOn;
1997 status |= LyXFunc::ToggleOff;
1999 case LyXTabular::SET_LONGTABULAR:
2000 if (tabular->IsLongTabular())
2001 status |= LyXFunc::ToggleOn;
2003 status |= LyXFunc::ToggleOff;
2005 case LyXTabular::UNSET_LONGTABULAR:
2006 if (!tabular->IsLongTabular())
2007 status |= LyXFunc::ToggleOn;
2009 status |= LyXFunc::ToggleOff;
2011 case LyXTabular::SET_ROTATE_TABULAR:
2012 if (tabular->GetRotateTabular())
2013 status |= LyXFunc::ToggleOn;
2015 status |= LyXFunc::ToggleOff;
2017 case LyXTabular::UNSET_ROTATE_TABULAR:
2018 if (!tabular->GetRotateTabular())
2019 status |= LyXFunc::ToggleOn;
2021 status |= LyXFunc::ToggleOff;
2023 case LyXTabular::SET_ROTATE_CELL:
2024 if (tabular->GetRotateCell(actcell))
2025 status |= LyXFunc::ToggleOn;
2027 status |= LyXFunc::ToggleOff;
2029 case LyXTabular::UNSET_ROTATE_CELL:
2030 if (!tabular->GetRotateCell(actcell))
2031 status |= LyXFunc::ToggleOn;
2033 status |= LyXFunc::ToggleOff;
2035 case LyXTabular::SET_USEBOX:
2036 if (strToInt(argument) == tabular->GetUsebox(actcell))
2037 status |= LyXFunc::ToggleOn;
2039 status |= LyXFunc::ToggleOff;
2041 case LyXTabular::SET_LTFIRSTHEAD:
2042 if (tabular->GetRowOfLTHead(actcell, dummy))
2043 status |= LyXFunc::ToggleOn;
2045 status |= LyXFunc::ToggleOff;
2047 case LyXTabular::SET_LTHEAD:
2048 if (tabular->GetRowOfLTHead(actcell, dummy))
2049 status |= LyXFunc::ToggleOn;
2051 status |= LyXFunc::ToggleOff;
2053 case LyXTabular::SET_LTFOOT:
2054 if (tabular->GetRowOfLTFoot(actcell, dummy))
2055 status |= LyXFunc::ToggleOn;
2057 status |= LyXFunc::ToggleOff;
2059 case LyXTabular::SET_LTLASTFOOT:
2060 if (tabular->GetRowOfLTFoot(actcell, dummy))
2061 status |= LyXFunc::ToggleOn;
2063 status |= LyXFunc::ToggleOff;
2065 case LyXTabular::SET_LTNEWPAGE:
2066 if (tabular->GetLTNewPage(actcell))
2067 status |= LyXFunc::ToggleOn;
2069 status |= LyXFunc::ToggleOff;
2072 status = LyXFunc::Disabled;
2079 bool InsetTabular::copySelection(BufferView * bv)
2081 if (!hasSelection())
2083 delete paste_tabular;
2090 sel_col_start = tabular->column_of_cell(sel_cell_start);
2091 sel_col_end = tabular->column_of_cell(sel_cell_end);
2092 if (sel_col_start > sel_col_end) {
2093 sel_col_start = sel_col_end;
2094 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2096 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2098 int columns = sel_col_end - sel_col_start + 1;
2100 sel_row_start = tabular->row_of_cell(sel_cell_start);
2101 sel_row_end = tabular->row_of_cell(sel_cell_end);
2102 if (sel_row_start > sel_row_end) {
2103 //int tmp tmp = sel_row_start;
2104 //sel_row_start = sel_row_end;
2105 //sel_row_end = tmp;
2106 swap(sel_row_start, sel_row_end);
2108 int rows = sel_row_end - sel_row_start + 1;
2110 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2112 for (i=0; i < sel_row_start; ++i)
2113 paste_tabular->DeleteRow(0);
2114 while(paste_tabular->rows() > rows)
2115 paste_tabular->DeleteRow(rows);
2116 paste_tabular->SetTopLine(0, true, true);
2117 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2119 for (i=0; i < sel_col_start; ++i)
2120 paste_tabular->DeleteColumn(0);
2121 while(paste_tabular->columns() > columns)
2122 paste_tabular->DeleteColumn(columns);
2123 paste_tabular->SetLeftLine(0, true, true);
2124 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2127 paste_tabular->Ascii(bv->buffer(), sstr);
2128 bv->stuffClipboard(sstr.str().c_str());
2133 bool InsetTabular::pasteSelection(BufferView * bv)
2138 for (int r1 = 0, r2 = actrow;
2139 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2142 for(int c1 = 0, c2 = actcol;
2143 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2146 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2147 tabular->IsPartOfMultiColumn(r2,c2))
2149 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2153 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2157 int n1 = paste_tabular->GetCellNumber(r1, c1);
2158 int n2 = tabular->GetCellNumber(r2, c2);
2159 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2160 tabular->GetCellInset(n2)->setOwner(this);
2161 tabular->GetCellInset(n2)->deleteLyXText(bv);
2168 bool InsetTabular::cutSelection()
2170 if (!hasSelection())
2178 sel_col_start = tabular->column_of_cell(sel_cell_start);
2179 sel_col_end = tabular->column_of_cell(sel_cell_end);
2180 if (sel_col_start > sel_col_end) {
2181 sel_col_start = sel_col_end;
2182 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2184 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2186 sel_row_start = tabular->row_of_cell(sel_cell_start);
2187 sel_row_end = tabular->row_of_cell(sel_cell_end);
2188 if (sel_row_start > sel_row_end) {
2189 //int tmp = sel_row_start;
2190 //sel_row_start = sel_row_end;
2191 //sel_row_end = tmp;
2192 swap(sel_row_start, sel_row_end);
2194 if (sel_cell_start > sel_cell_end) {
2195 //int tmp = sel_cell_start;
2196 //sel_cell_start = sel_cell_end;
2197 //sel_cell_end = tmp;
2198 swap(sel_cell_start, sel_cell_end);
2200 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2201 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2202 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2208 bool InsetTabular::isRightToLeft(BufferView *bv )
2210 return bv->getParentLanguage(this)->RightToLeft();