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"
33 #include "insets/insettext.h"
34 #include "frontends/Dialogs.h"
50 const int ADD_TO_HEIGHT = 2;
51 const int ADD_TO_TABULAR_WIDTH = 2;
53 LyXTabular * paste_tabular = 0;
56 struct tabular_features {
57 LyXTabular::Feature action;
61 //tabular_features * tabularFeatures = 0;
63 tabular_features tabularFeatures[] =
65 { LyXTabular::APPEND_ROW, "append-row" },
66 { LyXTabular::APPEND_COLUMN, "append-column" },
67 { LyXTabular::DELETE_ROW, "delete-row" },
68 { LyXTabular::DELETE_COLUMN, "delete-column" },
69 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
70 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
71 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
72 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
73 { LyXTabular::ALIGN_LEFT, "align-left" },
74 { LyXTabular::ALIGN_RIGHT, "align-right" },
75 { LyXTabular::ALIGN_CENTER, "align-center" },
76 { LyXTabular::VALIGN_TOP, "valign-top" },
77 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
78 { LyXTabular::VALIGN_CENTER, "valign-center" },
79 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
80 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
81 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
82 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
83 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
84 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
85 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
86 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
87 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
88 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
89 { LyXTabular::MULTICOLUMN, "multicolumn" },
90 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
91 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
92 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
93 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
94 { LyXTabular::SET_PWIDTH, "set-pwidth" },
95 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
96 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
97 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
98 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
99 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
100 { LyXTabular::SET_USEBOX, "set-usebox" },
101 { LyXTabular::SET_LTHEAD, "set-lthead" },
102 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
103 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
104 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
105 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
106 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
107 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
108 { LyXTabular::LAST_ACTION, "" }
114 bool InsetTabular::hasPasteBuffer() const
116 return (paste_tabular != 0);
120 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
127 tabular.reset(new LyXTabular(this, rows,columns));
128 // for now make it always display as display() inset
130 the_locking_inset = 0;
131 locked = no_selection = false;
133 actrow = actcell = 0;
140 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
143 tabular.reset(new LyXTabular(this, *(tab.tabular)));
144 the_locking_inset = 0;
145 locked = no_selection = false;
147 actrow = actcell = 0;
148 sel_cell_start = sel_cell_end = 0;
154 InsetTabular::~InsetTabular()
161 Inset * InsetTabular::Clone(Buffer const & buf) const
163 InsetTabular * t = new InsetTabular(*this, buf);
165 //t->tabular = tabular->Clone(t);
166 t->tabular.reset(tabular->Clone(t));
171 void InsetTabular::Write(Buffer const * buf, ostream & os) const
173 os << " Tabular" << endl;
174 tabular->Write(buf, os);
178 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
180 bool const old_format = (lex.GetString() == "\\LyXTable");
184 //tabular = new LyXTabular(buf, this, lex);
185 tabular.reset(new LyXTabular(buf, this, lex));
193 string token = lex.GetString();
194 while (lex.IsOK() && (token != "\\end_inset")) {
196 token = lex.GetString();
198 if (token != "\\end_inset") {
199 lex.printError("Missing \\end_inset at this point. "
205 int InsetTabular::ascent(BufferView *, LyXFont const &) const
207 return tabular->GetAscentOfRow(0);
211 int InsetTabular::descent(BufferView *, LyXFont const &) const
213 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
217 int InsetTabular::width(BufferView *, LyXFont const &) const
219 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
223 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
224 float & x, bool cleared) const
229 Painter & pain = bv->painter();
234 UpdatableInset::draw(bv, font, baseline, x, cleared);
235 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
236 (top_x != int(x)) || (top_baseline != baseline))) {
237 int h = ascent(bv, font) + descent(bv, font);
238 int const tx = display() || !owner() ? 0 : top_x;
239 int w = tx ? width(bv, font) : pain.paperWidth();
240 int ty = baseline - ascent(bv, font);
244 if ((ty + h) > pain.paperHeight())
245 h = pain.paperHeight();
246 if ((top_x + w) > pain.paperWidth())
247 w = pain.paperWidth();
248 pain.fillRectangle(tx, ty, w, h);
253 top_baseline = baseline;
254 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
257 x += ADD_TO_TABULAR_WIDTH;
261 first_visible_cell = -1;
262 for (i = 0; i < tabular->rows(); ++i) {
264 dodraw = ((baseline + tabular->GetDescentOfRow(i)) > 0) &&
265 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight();
266 for (j = 0; j < tabular->columns(); ++j) {
267 if (tabular->IsPartOfMultiColumn(i, j))
269 cx = nx + tabular->GetBeginningOfTextInCell(cell);
271 if (first_visible_cell < 0)
272 first_visible_cell = cell;
274 DrawCellSelection(pain, nx, baseline, i, j, cell);
275 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
277 DrawCellLines(pain, nx, baseline, i, cell);
279 nx += tabular->GetWidthOfColumn(cell);
282 baseline += tabular->GetDescentOfRow(i) +
283 tabular->GetAscentOfRow(i + 1) +
284 tabular->GetAdditionalHeight(cell);
286 } else if (need_update == CELL) {
289 if (the_locking_inset) {
290 Inset * inset = tabular->GetCellInset(cell);
292 inset != the_locking_inset && i < tabular->rows();
295 inset != the_locking_inset
296 && j < tabular->columns();
298 if (tabular->IsPartOfMultiColumn(i, j))
300 nx += tabular->GetWidthOfColumn(cell);
302 inset = tabular->GetCellInset(cell);
304 if (tabular->row_of_cell(cell) > i) {
306 baseline += tabular->GetDescentOfRow(i) +
307 tabular->GetAscentOfRow(i + 1) +
308 tabular->GetAdditionalHeight(cell);
313 cell < actcell && i < tabular->rows(); ++i) {
314 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
315 if (tabular->IsPartOfMultiColumn(i, j))
317 nx += tabular->GetWidthOfColumn(cell);
320 if (tabular->row_of_cell(cell) > i) {
322 baseline += tabular->GetDescentOfRow(i) +
323 tabular->GetAscentOfRow(i + 1) +
324 tabular->GetAdditionalHeight(cell);
328 i = tabular->row_of_cell(cell);
329 if (the_locking_inset != tabular->GetCellInset(cell)) {
330 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
333 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
335 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
336 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
337 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
339 // clear only if we didn't have a change
340 if (need_update == CELL) {
341 // clear before the inset
344 baseline - tabular->GetAscentOfRow(i) + 1,
346 tabular->GetAscentOfRow(i) +
347 tabular->GetDescentOfRow(i) - 1);
348 // clear behind the inset
350 int(cx + the_locking_inset->width(bv,font) + 1),
351 baseline - tabular->GetAscentOfRow(i) + 1,
352 tabular->GetWidthOfColumn(cell) -
353 tabular->GetBeginningOfTextInCell(cell) -
354 the_locking_inset->width(bv,font) -
355 tabular->GetAdditionalWidth(cell) - 1,
356 tabular->GetAscentOfRow(i) +
357 tabular->GetDescentOfRow(i) - 1);
360 x -= ADD_TO_TABULAR_WIDTH;
361 x += width(bv, font);
362 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
369 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
370 int row, int cell) const
372 int x2 = x + tabular->GetWidthOfColumn(cell);
375 if (!tabular->TopAlreadyDrawed(cell)) {
376 on_off = !tabular->TopLine(cell);
377 pain.line(x, baseline - tabular->GetAscentOfRow(row),
378 x2, baseline - tabular->GetAscentOfRow(row),
379 on_off ? LColor::tabularonoffline : LColor::tabularline,
380 on_off ? Painter::line_onoffdash : Painter::line_solid);
382 on_off = !tabular->BottomLine(cell);
383 pain.line(x,baseline + tabular->GetDescentOfRow(row),
384 x2, baseline + tabular->GetDescentOfRow(row),
385 on_off ? LColor::tabularonoffline : LColor::tabularline,
386 on_off ? Painter::line_onoffdash : Painter::line_solid);
387 if (!tabular->LeftAlreadyDrawed(cell)) {
388 on_off = !tabular->LeftLine(cell);
389 pain.line(x, baseline - tabular->GetAscentOfRow(row),
390 x, baseline + tabular->GetDescentOfRow(row),
391 on_off ? LColor::tabularonoffline : LColor::tabularline,
392 on_off ? Painter::line_onoffdash : Painter::line_solid);
394 on_off = !tabular->RightLine(cell);
395 pain.line(x2 - tabular->GetAdditionalWidth(cell),
396 baseline - tabular->GetAscentOfRow(row),
397 x2 - tabular->GetAdditionalWidth(cell),
398 baseline + tabular->GetDescentOfRow(row),
399 on_off ? LColor::tabularonoffline : LColor::tabularline,
400 on_off ? Painter::line_onoffdash : Painter::line_solid);
404 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
405 int row, int column, int cell) const
407 int cs = tabular->column_of_cell(sel_cell_start);
408 int ce = tabular->column_of_cell(sel_cell_end);
411 cs = tabular->column_of_cell(sel_cell_end);
413 ce = tabular->right_column_of_cell(sel_cell_end);
416 int rs = tabular->row_of_cell(sel_cell_start);
417 int re = tabular->row_of_cell(sel_cell_end);
418 if (rs > re) swap(rs, re);
420 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
421 int w = tabular->GetWidthOfColumn(cell);
422 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
423 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
424 w, h, LColor::selection);
429 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
433 calculate_dimensions_of_cells(bv, font, true);
435 owner()->update(bv, font, true);
438 if (the_locking_inset) {
439 the_locking_inset->update(bv, font, reinit);
441 // inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
442 // inset_y = cursor.y();
444 switch (need_update) {
448 if (calculate_dimensions_of_cells(bv, font, false))
460 string const InsetTabular::EditMessage() const
462 return _("Opened Tabular Inset");
466 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
468 UpdatableInset::Edit(bv, x, y, button);
470 if (!bv->lockInset(this)) {
471 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
475 the_locking_inset = 0;
479 sel_cell_start = sel_cell_end = actcell;
480 bv->text->FinishUndo();
481 if (InsetHit(bv, x, y) && (button != 3)) {
482 ActivateCellInsetAbs(bv, x, y, button);
484 // UpdateLocal(bv, NONE, false);
485 // bv->getOwner()->getPopups().updateFormTabular();
489 void InsetTabular::InsetUnlock(BufferView * bv)
491 if (the_locking_inset) {
492 the_locking_inset->InsetUnlock(bv);
493 the_locking_inset = 0;
496 no_selection = false;
499 if (scroll() || hasSelection()) {
500 sel_cell_start = sel_cell_end = 0;
504 UpdateLocal(bv, FULL, false);
509 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
510 bool mark_dirty) const
512 if (need_update < what) // only set this if it has greater update
514 if ((what == INIT) && hasSelection())
517 if (need_update != NONE) {
518 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
519 if (locked) // && (what != NONE))
525 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
527 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
532 if (inset == tabular->GetCellInset(actcell)) {
533 lyxerr[Debug::INSETS] << "OK" << endl;
534 the_locking_inset = tabular->GetCellInset(actcell);
536 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
537 inset_y = cursor.y();
539 } else if (the_locking_inset && (the_locking_inset == inset)) {
540 lyxerr[Debug::INSETS] << "OK" << endl;
542 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
543 inset_y = cursor.y();
544 } else if (the_locking_inset) {
545 lyxerr[Debug::INSETS] << "MAYBE" << endl;
546 return the_locking_inset->LockInsetInInset(bv, inset);
548 lyxerr[Debug::INSETS] << "NOT OK" << endl;
553 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
556 if (!the_locking_inset)
558 if (the_locking_inset == inset) {
559 the_locking_inset->InsetUnlock(bv);
560 the_locking_inset = 0;
561 UpdateLocal(bv, CELL, false);
562 ShowInsetCursor(bv, false);
565 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
566 if (inset->LyxCode() == TABULAR_CODE &&
567 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
568 bv->owner()->getDialogs()->updateTabular(this);
577 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
579 if (!the_locking_inset)
581 if (the_locking_inset != inset) {
582 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
585 UpdateLocal(bv, CELL, false);
590 unsigned int InsetTabular::InsetInInsetY()
592 if (!the_locking_inset)
595 return inset_y + the_locking_inset->InsetInInsetY();
599 UpdatableInset * InsetTabular::GetLockingInset()
601 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
605 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
609 if (the_locking_inset)
610 return the_locking_inset->GetFirstLockingInsetOfType(c);
615 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
617 if (the_locking_inset)
618 return the_locking_inset->InsertInset(bv, inset);
623 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
625 if (hasSelection() && (button == 3))
628 if (hasSelection()) {
630 UpdateLocal(bv, SELECTION, false);
633 no_selection = false;
635 int const ocell = actcell;
636 int const orow = actrow;
641 UpdateLocal(bv, NONE, false);
642 sel_cell_start = sel_cell_end = actcell;
644 if ((ocell != actcell) && the_locking_inset) {
645 the_locking_inset->InsetUnlock(bv);
646 the_locking_inset = 0;
652 bool const inset_hit = InsetHit(bv, x, y);
654 if ((ocell == actcell) && the_locking_inset && inset_hit) {
656 the_locking_inset->InsetButtonPress(bv,
657 x - inset_x, y - inset_y,
660 } else if (the_locking_inset) {
661 the_locking_inset->InsetUnlock(bv);
663 the_locking_inset = 0;
665 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
668 if (inset_hit && bv->theLockingInset()) {
669 if (ActivateCellInsetAbs(bv, x, y, button))
670 the_locking_inset->InsetButtonPress(bv,
680 void InsetTabular::InsetButtonRelease(BufferView * bv,
681 int x, int y, int button)
684 if (the_locking_inset) {
686 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
687 i->InsetButtonRelease(bv, x, y, button);
691 bv->owner()->getDialogs()->showTabular(this);
694 if (the_locking_inset) {
695 the_locking_inset->InsetButtonRelease(bv,
696 x - inset_x, y - inset_y,
700 no_selection = false;
704 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
706 if (the_locking_inset) {
707 the_locking_inset->InsetMotionNotify(bv,
715 int const old_cell = actcell;
718 sel_cell_end = actcell;
719 if (sel_cell_end != old_cell)
720 UpdateLocal(bv, SELECTION, false);
723 no_selection = false;
727 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
729 if (the_locking_inset) {
730 the_locking_inset->InsetKeyPress(xke);
736 UpdatableInset::RESULT
737 InsetTabular::LocalDispatch(BufferView * bv,
741 // We need to save the value of the_locking_inset as the call to
742 // the_locking_inset->LocalDispatch might unlock it.
743 old_locking_inset = the_locking_inset;
744 no_selection = false;
745 UpdatableInset::RESULT result =
746 UpdatableInset::LocalDispatch(bv, action, arg);
747 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
752 if ((action < 0) && arg.empty())
755 bool hs = hasSelection();
758 // this one have priority over the locked InsetText!
764 if (the_locking_inset) {
765 UnlockInsetInInset(bv, the_locking_inset);
766 the_locking_inset = 0;
768 if (action == LFUN_TAB)
769 moveNextCell(bv, old_locking_inset != 0);
771 movePrevCell(bv, old_locking_inset != 0);
772 sel_cell_start = sel_cell_end = actcell;
774 UpdateLocal(bv, SELECTION, false);
778 // this to avoid compiler warnings.
783 if (the_locking_inset) {
784 result=the_locking_inset->LocalDispatch(bv, action, arg);
785 if (result == DISPATCHED_NOUPDATE) {
788 if (sc != scroll()) { // inset has been scrolled
789 the_locking_inset->ToggleInsetCursor(bv);
790 UpdateLocal(bv, FULL, false);
791 the_locking_inset->ToggleInsetCursor(bv);
794 } else if (result == DISPATCHED) {
795 the_locking_inset->ToggleInsetCursor(bv);
796 UpdateLocal(bv, CELL, false);
797 the_locking_inset->ToggleInsetCursor(bv);
799 } else if (result == FINISHED) {
806 // --- Cursor Movements ----------------------------------
808 if (tabular->IsLastCellInRow(actcell))
810 moveRight(bv, false);
811 sel_cell_end = actcell;
812 UpdateLocal(bv, SELECTION, false);
815 result = moveRight(bv);
816 sel_cell_start = sel_cell_end = actcell;
818 UpdateLocal(bv, SELECTION, false);
821 if (tabular->IsFirstCellInRow(actcell))
824 sel_cell_end = actcell;
825 UpdateLocal(bv, SELECTION, false);
828 result = moveLeft(bv);
829 sel_cell_start = sel_cell_end = actcell;
831 UpdateLocal(bv, SELECTION, false);
835 int const ocell = actcell;
837 if ((ocell == sel_cell_end) ||
838 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
839 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
841 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
842 UpdateLocal(bv, SELECTION, false);
846 result = moveDown(bv, old_locking_inset != 0);
847 sel_cell_start = sel_cell_end = actcell;
849 UpdateLocal(bv, SELECTION, false);
853 int const ocell = actcell;
855 if ((ocell == sel_cell_end) ||
856 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
857 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
859 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
860 UpdateLocal(bv, SELECTION, false);
864 result = moveUp(bv, old_locking_inset != 0);
865 sel_cell_start = sel_cell_end = actcell;
867 UpdateLocal(bv, SELECTION, false);
871 if (the_locking_inset) {
872 UnlockInsetInInset(bv, the_locking_inset);
873 the_locking_inset = 0;
875 if (bv->text->first + bv->painter().paperHeight() <
876 (top_baseline + tabular->GetHeightOfTabular()))
878 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
879 UpdateLocal(bv, FULL, false);
880 actcell = tabular->GetCellBelow(first_visible_cell) + column;
882 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
885 UpdateLocal(bv, CURSOR, false);
890 if (the_locking_inset) {
891 UnlockInsetInInset(bv, the_locking_inset);
892 the_locking_inset = 0;
894 if (top_baseline < 0) {
895 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
896 UpdateLocal(bv, FULL, false);
897 if (top_baseline > 0)
900 actcell = tabular->GetCellBelow(first_visible_cell) + column;
905 UpdateLocal(bv, CURSOR, false);
916 case LFUN_LAYOUT_TABULAR:
918 bv->owner()->getDialogs()->showTabular(this);
921 case LFUN_TABULAR_FEATURE:
922 if (!TabularFeatures(bv, arg))
923 result = UNDISPATCHED;
926 if (!copySelection(bv))
928 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
930 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
931 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
933 bv->text->cursor.par()->previous(),
934 bv->text->cursor.par()->next()
938 UpdateLocal(bv, INIT, true);
943 bv->text->FinishUndo();
946 case LFUN_PASTESELECTION:
948 string clip(bv->workarea()->getClipboard());
952 if (clip.find('\t') != string::npos) {
956 unsigned int len = clip.length();
957 string::size_type p = 0;
960 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
968 maxCols = max(cols, maxCols);
974 maxCols = max(cols, maxCols);
975 delete paste_tabular;
976 paste_tabular = new LyXTabular(this, rows, maxCols);
977 string::size_type op = 0;
979 int cells = paste_tabular->GetNumberOfCells();
981 while((cell < cells) && (p < len) &&
982 (p = clip.find_first_of("\t\n", p)) != string::npos) {
987 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
992 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
993 while(cols++ < maxCols)
1001 // check for the last cell if there is no trailing '\n'
1002 if ((cell < cells) && (op < len))
1003 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1005 // so that the clipboard is used and it goes on
1007 // and executes LFUN_PASTESELECTION in insettext!
1008 delete paste_tabular;
1013 if (hasPasteBuffer()) {
1014 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1016 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1017 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1019 bv->text->cursor.par()->previous(),
1020 bv->text->cursor.par()->next()
1024 UpdateLocal(bv, INIT, true);
1027 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1029 // we try to activate the actual inset and put this event down to
1030 // the insets dispatch function.
1031 result = UNDISPATCHED;
1032 if (the_locking_inset)
1035 if (ActivateCellInset(bv)) {
1036 result = the_locking_inset->LocalDispatch(bv, action, arg);
1037 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1038 UnlockInsetInInset(bv, the_locking_inset);
1040 the_locking_inset = 0;
1041 return UNDISPATCHED;
1044 the_locking_inset->ToggleInsetCursor(bv);
1045 UpdateLocal(bv, CELL, false);
1046 the_locking_inset->ToggleInsetCursor(bv);
1051 if (result!=FINISHED) {
1052 if (!the_locking_inset) {
1053 ShowInsetCursor(bv);
1056 bv->unlockInset(this);
1061 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1062 bool fragile, bool fp) const
1064 return tabular->Latex(buf, os, fragile, fp);
1068 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1070 // This should be changed to a real ascii export
1071 return tabular->Ascii(buf, os);
1075 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1081 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1083 return tabular->DocBook(buf,os);
1087 void InsetTabular::Validate(LaTeXFeatures & features) const
1089 tabular->Validate(features);
1093 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1094 LyXFont const & font,
1101 bool changed = false;
1103 // if we have a locking_inset we should have to check only this cell for
1104 // change so I'll try this to have a boost, but who knows ;)
1105 if ((need_update != INIT) &&
1106 (the_locking_inset == tabular->GetCellInset(actcell))) {
1107 for(int i = 0; i < tabular->columns(); ++i) {
1108 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1110 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1113 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1114 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1115 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1118 for (int i = 0; i < tabular->rows(); ++i) {
1121 for (int j= 0; j < tabular->columns(); ++j) {
1122 if (tabular->IsPartOfMultiColumn(i,j))
1125 inset = tabular->GetCellInset(cell);
1127 inset->update(bv, font, false);
1128 maxAsc = max(maxAsc, inset->ascent(bv, font));
1129 maxDesc = max(maxDesc, inset->descent(bv, font));
1130 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1132 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1133 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1139 void InsetTabular::GetCursorPos(BufferView *,
1140 int & x, int & y) const
1142 x = cursor.x() - top_x;
1147 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1149 if (the_locking_inset) {
1150 the_locking_inset->ToggleInsetCursor(bv);
1154 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1156 int const asc = lyxfont::maxAscent(font);
1157 int const desc = lyxfont::maxDescent(font);
1159 if (isCursorVisible())
1160 bv->hideLockedInsetCursor();
1162 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1163 toggleCursorVisible();
1167 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1169 if (!isCursorVisible()) {
1170 LyXFont font; // = GetFont(par, cursor.pos);
1172 int const asc = lyxfont::maxAscent(font);
1173 int const desc = lyxfont::maxDescent(font);
1174 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1176 bv->showLockedInsetCursor(cursor.x(), cursor.y(),
1178 setCursorVisible(true);
1183 void InsetTabular::HideInsetCursor(BufferView * bv)
1185 if (isCursorVisible()) {
1186 bv->hideLockedInsetCursor();
1187 setCursorVisible(false);
1189 // if (cursor_visible)
1190 // ToggleInsetCursor(bv);
1194 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1198 actcell = actrow = actcol = 0;
1199 int ly = tabular->GetDescentOfRow(actrow);
1201 // first search the right row
1202 while((ly < y) && (actrow < tabular->rows())) {
1204 + tabular->GetDescentOfRow(actrow)
1205 + tabular->GetAscentOfRow(actrow + 1)
1207 GetAdditionalHeight(tabular->
1208 GetCellNumber(actrow + 1,
1211 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1213 actcell = tabular->GetCellNumber(actrow, actcol);
1215 // now search the right column
1216 int lx = tabular->GetWidthOfColumn(actcell) -
1217 tabular->GetAdditionalWidth(actcell);
1219 #ifdef WITH_WARNINGS
1220 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1222 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1223 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1224 tabular->GetAdditionalWidth(actcell - 1));
1226 // Jürgen, you should check that this is correct. (Lgb)
1227 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1228 lx += tabular->GetWidthOfColumn(actcell + 1)
1229 + tabular->GetAdditionalWidth(actcell);
1233 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1238 int InsetTabular::getCellXPos(int cell) const
1242 for (; !tabular->IsFirstCellInRow(c); --c)
1244 int lx = tabular->GetWidthOfColumn(cell);
1245 for (; c < cell; ++c) {
1246 lx += tabular->GetWidthOfColumn(c);
1248 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1252 void InsetTabular::resetPos(BufferView * bv) const
1256 actcol = tabular->column_of_cell(actcell);
1261 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1262 if (tabular->IsLastCellInRow(cell)) {
1264 + tabular->GetDescentOfRow(actrow)
1265 + tabular->GetAscentOfRow(actrow + 1)
1266 + tabular->GetAdditionalHeight(cell + 1));
1270 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1271 int new_x = getCellXPos(actcell);
1272 int old_x = cursor.x();
1275 // cursor.x(getCellXPos(actcell) + offset);
1276 if (scroll() && (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1278 else if (the_locking_inset &&
1279 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20)) {
1280 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1281 if (xx > (bv->workWidth()-20))
1282 scroll(bv, -(xx - bv->workWidth() + 60));
1290 } else if ((cursor.x() - offset) > 20 &&
1291 (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1292 > (bv->workWidth() - 20)) {
1293 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1294 UpdateLocal(bv, FULL, false);
1295 } else if ((cursor.x() - offset) < 20) {
1296 scroll(bv, 20 - cursor.x() + offset);
1297 UpdateLocal(bv, FULL, false);
1298 } else if (scroll() && top_x > 20 &&
1299 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1300 scroll(bv, old_x - cursor.x());
1302 if ((!the_locking_inset ||
1303 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1304 actcell != oldcell) {
1305 InsetTabular * inset = const_cast<InsetTabular *>(this);
1306 bv->owner()->getDialogs()->updateTabular(inset);
1312 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1314 if (lock && !old_locking_inset) {
1315 if (ActivateCellInset(bv))
1318 bool moved = isRightToLeft(bv)
1319 ? movePrevCell(bv) : moveNextCell(bv);
1322 if (lock && ActivateCellInset(bv))
1326 return DISPATCHED_NOUPDATE;
1330 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1332 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1335 if (lock) { // behind the inset
1336 if (ActivateCellInset(bv, 0, 0, 0, true))
1340 return DISPATCHED_NOUPDATE;
1344 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1346 int const ocell = actcell;
1347 actcell = tabular->GetCellAbove(actcell);
1348 if (actcell == ocell) // we moved out of the inset
1354 if (old_locking_inset) {
1355 old_locking_inset->GetCursorPos(bv, x, y);
1356 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1358 if (ActivateCellInset(bv, x, 0))
1361 return DISPATCHED_NOUPDATE;
1365 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1367 int const ocell = actcell;
1368 actcell = tabular->GetCellBelow(actcell);
1369 if (actcell == ocell) // we moved out of the inset
1375 if (old_locking_inset) {
1376 old_locking_inset->GetCursorPos(bv, x, y);
1377 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1379 if (ActivateCellInset(bv, x, 0))
1382 return DISPATCHED_NOUPDATE;
1386 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1388 if (isRightToLeft(bv)) {
1389 if (tabular->IsFirstCellInRow(actcell)) {
1390 int row = tabular->row_of_cell(actcell);
1391 if (row == tabular->rows() - 1)
1393 actcell = tabular->GetLastCellInRow(row);
1394 actcell = tabular->GetCellBelow(actcell);
1401 if (tabular->IsLastCell(actcell))
1406 bool rtl = tabular->GetCellInset(actcell)->par->
1407 isRightToLeftPar(bv->buffer()->params);
1408 ActivateCellInset(bv, 0, 0, 0, !rtl);
1415 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1417 if (isRightToLeft(bv)) {
1418 if (tabular->IsLastCellInRow(actcell)) {
1419 int row = tabular->row_of_cell(actcell);
1422 actcell = tabular->GetFirstCellInRow(row);
1423 actcell = tabular->GetCellAbove(actcell);
1425 if (tabular->IsLastCell(actcell))
1430 if (!actcell) // first cell
1435 bool rtl = tabular->GetCellInset(actcell)->par->
1436 isRightToLeftPar(bv->buffer()->params);
1437 ActivateCellInset(bv, 0, 0, 0, !rtl);
1444 bool InsetTabular::Delete()
1450 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1452 if (the_locking_inset)
1453 the_locking_inset->SetFont(bv, font, tall);
1457 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1459 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1462 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1463 string const tmp = tabularFeatures[i].feature;
1465 if (tmp == what.substr(0, tmp.length())) {
1466 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1467 //tabularFeatures[i].feature.length())) {
1468 action = tabularFeatures[i].action;
1472 if (action == LyXTabular::LAST_ACTION)
1476 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1477 TabularFeatures(bv, action, val);
1482 void InsetTabular::TabularFeatures(BufferView * bv,
1483 LyXTabular::Feature feature,
1484 string const & value)
1492 bool setLines = false;
1493 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1494 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1499 case LyXTabular::M_ALIGN_LEFT:
1500 case LyXTabular::ALIGN_LEFT:
1501 setAlign=LYX_ALIGN_LEFT;
1503 case LyXTabular::M_ALIGN_RIGHT:
1504 case LyXTabular::ALIGN_RIGHT:
1505 setAlign=LYX_ALIGN_RIGHT;
1507 case LyXTabular::M_ALIGN_CENTER:
1508 case LyXTabular::ALIGN_CENTER:
1509 setAlign=LYX_ALIGN_CENTER;
1511 case LyXTabular::M_VALIGN_TOP:
1512 case LyXTabular::VALIGN_TOP:
1513 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1515 case LyXTabular::M_VALIGN_BOTTOM:
1516 case LyXTabular::VALIGN_BOTTOM:
1517 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1519 case LyXTabular::M_VALIGN_CENTER:
1520 case LyXTabular::VALIGN_CENTER:
1521 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1526 if (hasSelection()) {
1527 sel_col_start = tabular->column_of_cell(sel_cell_start);
1528 sel_col_end = tabular->column_of_cell(sel_cell_end);
1529 if (sel_col_start > sel_col_end) {
1530 sel_col_end = sel_col_start;
1531 sel_col_start = tabular->column_of_cell(sel_cell_end);
1533 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1536 sel_row_start = tabular->row_of_cell(sel_cell_start);
1537 sel_row_end = tabular->row_of_cell(sel_cell_end);
1538 if (sel_row_start > sel_row_end) {
1539 //int tmp = sel_row_start;
1540 //sel_row_start = sel_row_end;
1541 //sel_row_end = tmp;
1542 swap(sel_row_start, sel_row_end);
1545 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1546 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1548 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1550 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1551 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1553 bv->text->cursor.par()->previous(),
1554 bv->text->cursor.par()->next()
1558 int row = tabular->row_of_cell(actcell);
1559 int column = tabular->column_of_cell(actcell);
1563 case LyXTabular::SET_PWIDTH:
1565 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1566 tabular->SetColumnPWidth(actcell,value);
1568 for (int i = 0; i < tabular->rows(); ++i) {
1569 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1572 UpdateLocal(bv, INIT, true);
1576 case LyXTabular::SET_MPWIDTH:
1578 bool const update = (tabular->GetPWidth(actcell) != value);
1579 tabular->SetMColumnPWidth(actcell,value);
1581 for (int i = 0; i < tabular->rows(); ++i) {
1582 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1585 UpdateLocal(bv, INIT, true);
1589 case LyXTabular::SET_SPECIAL_COLUMN:
1590 case LyXTabular::SET_SPECIAL_MULTI:
1591 tabular->SetAlignSpecial(actcell,value,feature);
1593 case LyXTabular::APPEND_ROW:
1594 // append the row into the tabular
1595 UnlockInsetInInset(bv, the_locking_inset);
1596 tabular->AppendRow(actcell);
1597 UpdateLocal(bv, INIT, true);
1599 case LyXTabular::APPEND_COLUMN:
1600 // append the column into the tabular
1601 UnlockInsetInInset(bv, the_locking_inset);
1602 tabular->AppendColumn(actcell);
1603 actcell = tabular->GetCellNumber(row, column);
1604 UpdateLocal(bv, INIT, true);
1606 case LyXTabular::DELETE_ROW:
1607 UnlockInsetInInset(bv, the_locking_inset);
1608 tabular->DeleteRow(tabular->row_of_cell(actcell));
1609 if ((row+1) > tabular->rows())
1611 actcell = tabular->GetCellNumber(row, column);
1613 UpdateLocal(bv, INIT, true);
1615 case LyXTabular::DELETE_COLUMN:
1616 UnlockInsetInInset(bv, the_locking_inset);
1617 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1618 if ((column+1) > tabular->columns())
1620 actcell = tabular->GetCellNumber(row, column);
1622 UpdateLocal(bv, INIT, true);
1624 case LyXTabular::M_TOGGLE_LINE_TOP:
1626 case LyXTabular::TOGGLE_LINE_TOP:
1628 bool lineSet = !tabular->TopLine(actcell, flag);
1629 for (int i = sel_row_start; i <= sel_row_end; ++i)
1630 for (int j = sel_col_start; j <= sel_col_end; ++j)
1631 tabular->SetTopLine(
1632 tabular->GetCellNumber(i, j),
1634 UpdateLocal(bv, INIT, true);
1638 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1640 case LyXTabular::TOGGLE_LINE_BOTTOM:
1642 bool lineSet = !tabular->BottomLine(actcell, flag);
1643 for (int i = sel_row_start; i <= sel_row_end; ++i)
1644 for (int j = sel_col_start; j <= sel_col_end; ++j)
1645 tabular->SetBottomLine(
1646 tabular->GetCellNumber(i, j),
1649 UpdateLocal(bv, INIT, true);
1653 case LyXTabular::M_TOGGLE_LINE_LEFT:
1655 case LyXTabular::TOGGLE_LINE_LEFT:
1657 bool lineSet = !tabular->LeftLine(actcell, flag);
1658 for (int i = sel_row_start; i <= sel_row_end; ++i)
1659 for (int j = sel_col_start; j <= sel_col_end; ++j)
1660 tabular->SetLeftLine(
1661 tabular->GetCellNumber(i,j),
1664 UpdateLocal(bv, INIT, true);
1668 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1670 case LyXTabular::TOGGLE_LINE_RIGHT:
1672 bool lineSet = !tabular->RightLine(actcell, flag);
1673 for (int i = sel_row_start; i <= sel_row_end; ++i)
1674 for (int j = sel_col_start; j <= sel_col_end; ++j)
1675 tabular->SetRightLine(
1676 tabular->GetCellNumber(i,j),
1679 UpdateLocal(bv, INIT, true);
1683 case LyXTabular::M_ALIGN_LEFT:
1684 case LyXTabular::M_ALIGN_RIGHT:
1685 case LyXTabular::M_ALIGN_CENTER:
1687 case LyXTabular::ALIGN_LEFT:
1688 case LyXTabular::ALIGN_RIGHT:
1689 case LyXTabular::ALIGN_CENTER:
1690 for (int i = sel_row_start; i <= sel_row_end; ++i)
1691 for (int j = sel_col_start; j <= sel_col_end; ++j)
1692 tabular->SetAlignment(
1693 tabular->GetCellNumber(i, j),
1696 UpdateLocal(bv, INIT, true);
1698 case LyXTabular::M_VALIGN_TOP:
1699 case LyXTabular::M_VALIGN_BOTTOM:
1700 case LyXTabular::M_VALIGN_CENTER:
1702 case LyXTabular::VALIGN_TOP:
1703 case LyXTabular::VALIGN_BOTTOM:
1704 case LyXTabular::VALIGN_CENTER:
1705 for (int i = sel_row_start; i <= sel_row_end; ++i)
1706 for (int j = sel_col_start; j <= sel_col_end; ++j)
1707 tabular->SetVAlignment(
1708 tabular->GetCellNumber(i, j),
1710 UpdateLocal(bv, INIT, true);
1712 case LyXTabular::MULTICOLUMN:
1714 if (sel_row_start != sel_row_end) {
1715 WriteAlert(_("Impossible Operation!"),
1716 _("Multicolumns can only be horizontally."),
1720 // just multicol for one Single Cell
1721 if (!hasSelection()) {
1722 // check wether we are completly in a multicol
1723 if (tabular->IsMultiColumn(actcell)) {
1724 tabular->UnsetMultiColumn(actcell);
1725 UpdateLocal(bv, INIT, true);
1727 tabular->SetMultiColumn(actcell, 1);
1728 UpdateLocal(bv, CELL, true);
1732 // we have a selection so this means we just add all this
1733 // cells to form a multicolumn cell
1737 if (sel_cell_start > sel_cell_end) {
1738 s_start = sel_cell_end;
1739 s_end = sel_cell_start;
1741 s_start = sel_cell_start;
1742 s_end = sel_cell_end;
1744 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1746 sel_cell_end = sel_cell_start;
1747 UpdateLocal(bv, INIT, true);
1750 case LyXTabular::SET_ALL_LINES:
1752 case LyXTabular::UNSET_ALL_LINES:
1753 for (int i = sel_row_start; i <= sel_row_end; ++i)
1754 for (int j = sel_col_start; j <= sel_col_end; ++j)
1755 tabular->SetAllLines(
1756 tabular->GetCellNumber(i,j), setLines);
1757 UpdateLocal(bv, INIT, true);
1759 case LyXTabular::SET_LONGTABULAR:
1760 tabular->SetLongTabular(true);
1761 UpdateLocal(bv, INIT, true); // because this toggles displayed
1763 case LyXTabular::UNSET_LONGTABULAR:
1764 tabular->SetLongTabular(false);
1765 UpdateLocal(bv, INIT, true); // because this toggles displayed
1767 case LyXTabular::SET_ROTATE_TABULAR:
1768 tabular->SetRotateTabular(true);
1770 case LyXTabular::UNSET_ROTATE_TABULAR:
1771 tabular->SetRotateTabular(false);
1773 case LyXTabular::SET_ROTATE_CELL:
1774 for (int i = sel_row_start; i <= sel_row_end; ++i)
1775 for (int j = sel_col_start; j<=sel_col_end; ++j)
1776 tabular->SetRotateCell(
1777 tabular->GetCellNumber(i, j),
1780 case LyXTabular::UNSET_ROTATE_CELL:
1781 for (int i = sel_row_start; i <= sel_row_end; ++i)
1782 for (int j = sel_col_start; j <= sel_col_end; ++j)
1783 tabular->SetRotateCell(
1784 tabular->GetCellNumber(i, j), false);
1786 case LyXTabular::SET_USEBOX:
1788 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1789 if (val == tabular->GetUsebox(actcell))
1790 val = LyXTabular::BOX_NONE;
1791 for (int i = sel_row_start; i <= sel_row_end; ++i)
1792 for (int j = sel_col_start; j <= sel_col_end; ++j)
1794 tabular->GetCellNumber(i, j), val);
1797 case LyXTabular::SET_LTFIRSTHEAD:
1798 tabular->SetLTHead(actcell, true);
1800 case LyXTabular::SET_LTHEAD:
1801 tabular->SetLTHead(actcell, false);
1803 case LyXTabular::SET_LTFOOT:
1804 tabular->SetLTFoot(actcell, false);
1806 case LyXTabular::SET_LTLASTFOOT:
1807 tabular->SetLTFoot(actcell, true);
1809 case LyXTabular::SET_LTNEWPAGE:
1811 bool what = !tabular->GetLTNewPage(actcell);
1812 tabular->SetLTNewPage(actcell, what);
1815 // dummy stuff just to avoid warnings
1816 case LyXTabular::LAST_ACTION:
1822 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1825 UpdatableInset * inset =
1826 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1827 LyXFont font(LyXFont::ALL_SANE);
1829 x = inset->x() + inset->width(bv, font);
1830 y = inset->descent(bv, font);
1832 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1833 //inset_y = cursor.y();
1834 inset->Edit(bv, x, y, button);
1835 if (!the_locking_inset)
1837 UpdateLocal(bv, CELL, false);
1838 return (the_locking_inset != 0);
1842 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1845 inset_x = cursor.x()
1846 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1847 inset_y = cursor.y();
1848 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1852 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1855 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1859 // This returns paperWidth() if the cell-width is unlimited or the width
1860 // in pixels if we have a pwidth for this cell.
1861 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1863 string const s = tabular->GetPWidth(cell);
1867 return VSpace(s).inPixels(bv);
1871 int InsetTabular::getMaxWidth(BufferView * bv,
1872 UpdatableInset const * inset) const
1874 int const n = tabular->GetNumberOfCells();
1876 for (; cell < n; ++cell) {
1877 if (tabular->GetCellInset(cell) == inset)
1882 int w = GetMaxWidthOfCell(bv, cell);
1884 // because the inset then subtracts it's top_x and owner->x()
1885 w += (inset->x() - top_x);
1890 void InsetTabular::resizeLyXText(BufferView *) const
1896 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1897 bool const recursive) const
1899 if (the_locking_inset)
1900 return the_locking_inset->getLyXText(bv, recursive);
1901 return Inset::getLyXText(bv, recursive);
1905 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1907 if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1908 bv->owner()->getDialogs()
1909 ->showTabular(const_cast<InsetTabular *>(this));
1914 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1916 if (the_locking_inset) {
1917 InsetTabular * i = static_cast<InsetTabular *>
1918 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1920 i->OpenLayoutDialog(bv);
1924 bv->owner()->getDialogs()->showTabular(
1925 const_cast<InsetTabular *>(this));
1929 // functions returns:
1933 // 3 ... toggled off
1935 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1937 int action = LyXTabular::LAST_ACTION;
1938 LyXFunc::func_status status = LyXFunc::OK;
1941 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1942 string const tmp = tabularFeatures[i].feature;
1943 if (tmp == what.substr(0, tmp.length())) {
1944 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1945 // tabularFeatures[i].feature.length())) {
1946 action = tabularFeatures[i].action;
1950 if (action == LyXTabular::LAST_ACTION)
1951 return LyXFunc::Unknown;
1953 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1960 if (hasSelection()) {
1961 sel_row_start = tabular->row_of_cell(sel_cell_start);
1962 sel_row_end = tabular->row_of_cell(sel_cell_end);
1963 if (sel_row_start > sel_row_end) {
1964 //int tmp = sel_row_start;
1965 //sel_row_start = sel_row_end;
1966 //sel_row_end = tmp;
1967 swap(sel_row_start, sel_row_end);
1970 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1974 case LyXTabular::SET_PWIDTH:
1975 case LyXTabular::SET_MPWIDTH:
1976 case LyXTabular::SET_SPECIAL_COLUMN:
1977 case LyXTabular::SET_SPECIAL_MULTI:
1978 status |= LyXFunc::Disabled;
1981 case LyXTabular::APPEND_ROW:
1982 case LyXTabular::APPEND_COLUMN:
1983 case LyXTabular::DELETE_ROW:
1984 case LyXTabular::DELETE_COLUMN:
1985 case LyXTabular::SET_ALL_LINES:
1986 case LyXTabular::UNSET_ALL_LINES:
1987 status |= LyXFunc::OK;
1990 case LyXTabular::MULTICOLUMN:
1991 if (tabular->IsMultiColumn(actcell))
1992 status |= LyXFunc::ToggleOn;
1994 status |= LyXFunc::ToggleOff;
1996 case LyXTabular::M_TOGGLE_LINE_TOP:
1998 case LyXTabular::TOGGLE_LINE_TOP:
1999 if (tabular->TopLine(actcell, flag))
2000 status |= LyXFunc::ToggleOn;
2002 status |= LyXFunc::ToggleOff;
2004 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2006 case LyXTabular::TOGGLE_LINE_BOTTOM:
2007 if (tabular->BottomLine(actcell, flag))
2008 status |= LyXFunc::ToggleOn;
2010 status |= LyXFunc::ToggleOff;
2012 case LyXTabular::M_TOGGLE_LINE_LEFT:
2014 case LyXTabular::TOGGLE_LINE_LEFT:
2015 if (tabular->LeftLine(actcell, flag))
2016 status |= LyXFunc::ToggleOn;
2018 status |= LyXFunc::ToggleOff;
2020 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2022 case LyXTabular::TOGGLE_LINE_RIGHT:
2023 if (tabular->RightLine(actcell, flag))
2024 status |= LyXFunc::ToggleOn;
2026 status |= LyXFunc::ToggleOff;
2028 case LyXTabular::M_ALIGN_LEFT:
2030 case LyXTabular::ALIGN_LEFT:
2031 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2032 status |= LyXFunc::ToggleOn;
2034 status |= LyXFunc::ToggleOff;
2036 case LyXTabular::M_ALIGN_RIGHT:
2038 case LyXTabular::ALIGN_RIGHT:
2039 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2040 status |= LyXFunc::ToggleOn;
2042 status |= LyXFunc::ToggleOff;
2044 case LyXTabular::M_ALIGN_CENTER:
2046 case LyXTabular::ALIGN_CENTER:
2047 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2048 status |= LyXFunc::ToggleOn;
2050 status |= LyXFunc::ToggleOff;
2052 case LyXTabular::M_VALIGN_TOP:
2054 case LyXTabular::VALIGN_TOP:
2055 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2056 status |= LyXFunc::ToggleOn;
2058 status |= LyXFunc::ToggleOff;
2060 case LyXTabular::M_VALIGN_BOTTOM:
2062 case LyXTabular::VALIGN_BOTTOM:
2063 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2064 status |= LyXFunc::ToggleOn;
2066 status |= LyXFunc::ToggleOff;
2068 case LyXTabular::M_VALIGN_CENTER:
2070 case LyXTabular::VALIGN_CENTER:
2071 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2072 status |= LyXFunc::ToggleOn;
2074 status |= LyXFunc::ToggleOff;
2076 case LyXTabular::SET_LONGTABULAR:
2077 if (tabular->IsLongTabular())
2078 status |= LyXFunc::ToggleOn;
2080 status |= LyXFunc::ToggleOff;
2082 case LyXTabular::UNSET_LONGTABULAR:
2083 if (!tabular->IsLongTabular())
2084 status |= LyXFunc::ToggleOn;
2086 status |= LyXFunc::ToggleOff;
2088 case LyXTabular::SET_ROTATE_TABULAR:
2089 if (tabular->GetRotateTabular())
2090 status |= LyXFunc::ToggleOn;
2092 status |= LyXFunc::ToggleOff;
2094 case LyXTabular::UNSET_ROTATE_TABULAR:
2095 if (!tabular->GetRotateTabular())
2096 status |= LyXFunc::ToggleOn;
2098 status |= LyXFunc::ToggleOff;
2100 case LyXTabular::SET_ROTATE_CELL:
2101 if (tabular->GetRotateCell(actcell))
2102 status |= LyXFunc::ToggleOn;
2104 status |= LyXFunc::ToggleOff;
2106 case LyXTabular::UNSET_ROTATE_CELL:
2107 if (!tabular->GetRotateCell(actcell))
2108 status |= LyXFunc::ToggleOn;
2110 status |= LyXFunc::ToggleOff;
2112 case LyXTabular::SET_USEBOX:
2113 if (strToInt(argument) == tabular->GetUsebox(actcell))
2114 status |= LyXFunc::ToggleOn;
2116 status |= LyXFunc::ToggleOff;
2118 case LyXTabular::SET_LTFIRSTHEAD:
2119 if (tabular->GetRowOfLTHead(actcell, dummy))
2120 status |= LyXFunc::ToggleOn;
2122 status |= LyXFunc::ToggleOff;
2124 case LyXTabular::SET_LTHEAD:
2125 if (tabular->GetRowOfLTHead(actcell, dummy))
2126 status |= LyXFunc::ToggleOn;
2128 status |= LyXFunc::ToggleOff;
2130 case LyXTabular::SET_LTFOOT:
2131 if (tabular->GetRowOfLTFoot(actcell, dummy))
2132 status |= LyXFunc::ToggleOn;
2134 status |= LyXFunc::ToggleOff;
2136 case LyXTabular::SET_LTLASTFOOT:
2137 if (tabular->GetRowOfLTFoot(actcell, dummy))
2138 status |= LyXFunc::ToggleOn;
2140 status |= LyXFunc::ToggleOff;
2142 case LyXTabular::SET_LTNEWPAGE:
2143 if (tabular->GetLTNewPage(actcell))
2144 status |= LyXFunc::ToggleOn;
2146 status |= LyXFunc::ToggleOff;
2149 status = LyXFunc::Disabled;
2156 std::vector<string> const InsetTabular::getLabelList() const
2158 return tabular->getLabelList();
2162 bool InsetTabular::copySelection(BufferView * bv)
2164 if (!hasSelection())
2166 //delete paste_tabular;
2168 //int sel_col_start;
2170 //int sel_row_start;
2173 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2174 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2175 if (sel_col_start > sel_col_end) {
2176 sel_col_start = sel_col_end;
2177 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2179 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2181 int const columns = sel_col_end - sel_col_start + 1;
2183 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2184 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2185 if (sel_row_start > sel_row_end) {
2186 //int tmp tmp = sel_row_start;
2187 //sel_row_start = sel_row_end;
2188 //sel_row_end = tmp;
2189 swap(sel_row_start, sel_row_end);
2191 int const rows = sel_row_end - sel_row_start + 1;
2193 delete paste_tabular;
2194 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2196 for (int i = 0; i < sel_row_start; ++i)
2197 paste_tabular->DeleteRow(0);
2198 while(paste_tabular->rows() > rows)
2199 paste_tabular->DeleteRow(rows);
2200 paste_tabular->SetTopLine(0, true, true);
2201 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2203 for (int i = 0; i < sel_col_start; ++i)
2204 paste_tabular->DeleteColumn(0);
2205 while (paste_tabular->columns() > columns)
2206 paste_tabular->DeleteColumn(columns);
2207 paste_tabular->SetLeftLine(0, true, true);
2208 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2212 paste_tabular->Ascii(bv->buffer(), sstr);
2213 bv->stuffClipboard(sstr.str().c_str());
2218 bool InsetTabular::pasteSelection(BufferView * bv)
2223 for (int r1 = 0, r2 = actrow;
2224 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2226 for(int c1 = 0, c2 = actcol;
2227 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2229 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2230 tabular->IsPartOfMultiColumn(r2,c2))
2232 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2236 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2240 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2241 int const n2 = tabular->GetCellNumber(r2, c2);
2242 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2243 tabular->GetCellInset(n2)->setOwner(this);
2244 tabular->GetCellInset(n2)->deleteLyXText(bv);
2251 bool InsetTabular::cutSelection()
2253 if (!hasSelection())
2256 //int sel_col_start;
2258 //int sel_row_start;
2261 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2262 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2263 if (sel_col_start > sel_col_end) {
2264 sel_col_start = sel_col_end;
2265 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2267 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2269 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2270 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2271 if (sel_row_start > sel_row_end) {
2272 //int tmp = sel_row_start;
2273 //sel_row_start = sel_row_end;
2274 //sel_row_end = tmp;
2275 swap(sel_row_start, sel_row_end);
2277 if (sel_cell_start > sel_cell_end) {
2278 //int tmp = sel_cell_start;
2279 //sel_cell_start = sel_cell_end;
2280 //sel_cell_end = tmp;
2281 swap(sel_cell_start, sel_cell_end);
2283 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2284 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2285 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2292 bool InsetTabular::isRightToLeft(BufferView *bv )
2294 return bv->getParentLanguage(this)->RightToLeft();