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"
52 const int ADD_TO_HEIGHT = 2;
53 const int ADD_TO_TABULAR_WIDTH = 2;
55 LyXTabular * paste_tabular = 0;
58 struct tabular_features {
59 LyXTabular::Feature action;
63 //tabular_features * tabularFeatures = 0;
65 tabular_features tabularFeatures[] =
67 { LyXTabular::APPEND_ROW, "append-row" },
68 { LyXTabular::APPEND_COLUMN, "append-column" },
69 { LyXTabular::DELETE_ROW, "delete-row" },
70 { LyXTabular::DELETE_COLUMN, "delete-column" },
71 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
72 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
73 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
74 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
75 { LyXTabular::ALIGN_LEFT, "align-left" },
76 { LyXTabular::ALIGN_RIGHT, "align-right" },
77 { LyXTabular::ALIGN_CENTER, "align-center" },
78 { LyXTabular::VALIGN_TOP, "valign-top" },
79 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
80 { LyXTabular::VALIGN_CENTER, "valign-center" },
81 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
82 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
83 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
84 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
85 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
86 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
87 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
88 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
89 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
90 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
91 { LyXTabular::MULTICOLUMN, "multicolumn" },
92 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
93 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
94 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
95 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
96 { LyXTabular::SET_PWIDTH, "set-pwidth" },
97 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
98 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
99 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
100 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
101 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
102 { LyXTabular::SET_USEBOX, "set-usebox" },
103 { LyXTabular::SET_LTHEAD, "set-lthead" },
104 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
105 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
106 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
107 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
108 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
109 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
110 { LyXTabular::LAST_ACTION, "" }
116 bool InsetTabular::hasPasteBuffer() const
118 return (paste_tabular != 0);
122 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
129 tabular.reset(new LyXTabular(this, rows,columns));
130 // for now make it always display as display() inset
132 the_locking_inset = 0;
133 locked = no_selection = false;
135 actrow = actcell = 0;
141 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
144 tabular.reset(new LyXTabular(this, *(tab.tabular)));
145 the_locking_inset = 0;
146 locked = no_selection = false;
148 actrow = actcell = 0;
149 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
228 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
231 // lyxerr << "InsetTabular::draw(" << need_update << ")\n";
233 Painter & pain = bv->painter();
239 UpdatableInset::draw(bv, font, baseline, x, cleared);
242 x += static_cast<float>(scroll());
244 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
245 (top_x != int(x)) || (top_baseline != baseline))) {
246 int h = ascent(bv, font) + descent(bv, font);
247 int const tx = display() || !owner() ? 0 : top_x;
248 int w = tx ? width(bv, font) : pain.paperWidth();
249 int ty = baseline - ascent(bv, font);
253 if ((ty + h) > pain.paperHeight())
254 h = pain.paperHeight();
255 if ((top_x + w) > pain.paperWidth())
256 w = pain.paperWidth();
257 pain.fillRectangle(tx, ty, w, h);
262 top_baseline = baseline;
263 x += ADD_TO_TABULAR_WIDTH;
267 first_visible_cell = -1;
268 for (i = 0; i < tabular->rows(); ++i) {
270 cell = tabular->GetCellNumber(i, 0);
271 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
272 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
274 baseline += tabular->GetDescentOfRow(i) +
275 tabular->GetAscentOfRow(i + 1) +
276 tabular->GetAdditionalHeight(i + 1);
279 for (j = 0; j < tabular->columns(); ++j) {
280 if (nx > bv->workWidth())
282 if (tabular->IsPartOfMultiColumn(i, j))
284 cx = nx + tabular->GetBeginningOfTextInCell(cell);
285 if (first_visible_cell < 0)
286 first_visible_cell = cell;
288 DrawCellSelection(pain, nx, baseline, i, j, cell);
289 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
291 DrawCellLines(pain, nx, baseline, i, cell);
292 nx += tabular->GetWidthOfColumn(cell);
295 baseline += tabular->GetDescentOfRow(i) +
296 tabular->GetAscentOfRow(i + 1) +
297 tabular->GetAdditionalHeight(i + 1);
299 } else if (need_update == CELL) {
302 if (the_locking_inset &&
303 tabular->GetCellInset(actcell) != the_locking_inset)
305 Inset * inset = tabular->GetCellInset(cell);
307 inset != the_locking_inset && i < tabular->rows();
310 inset != the_locking_inset
311 && j < tabular->columns();
313 if (tabular->IsPartOfMultiColumn(i, j))
315 nx += tabular->GetWidthOfColumn(cell);
317 inset = tabular->GetCellInset(cell);
319 if (tabular->row_of_cell(cell) > i) {
321 baseline += tabular->GetDescentOfRow(i) +
322 tabular->GetAscentOfRow(i + 1) +
323 tabular->GetAdditionalHeight(i + 1);
327 // copute baseline for actual row
328 for (i = 0; i < actrow; ++i) {
329 baseline += tabular->GetDescentOfRow(i) +
330 tabular->GetAscentOfRow(i + 1) +
331 tabular->GetAdditionalHeight(i + 1);
333 // now compute the right x position
334 cell = tabular->GetCellNumber(actrow, 0);
335 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
336 if (tabular->IsPartOfMultiColumn(actrow, j))
338 nx += tabular->GetWidthOfColumn(cell);
342 i = tabular->row_of_cell(cell);
343 if (the_locking_inset != tabular->GetCellInset(cell)) {
344 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
347 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
349 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
350 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
351 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
353 // clear only if we didn't have a change
354 if (need_update == CELL) {
355 // clear before the inset
358 baseline - tabular->GetAscentOfRow(i) + 1,
360 tabular->GetAscentOfRow(i) +
361 tabular->GetDescentOfRow(i) - 1);
362 // clear behind the inset
364 int(cx + the_locking_inset->width(bv,font) + 1),
365 baseline - tabular->GetAscentOfRow(i) + 1,
366 tabular->GetWidthOfColumn(cell) -
367 tabular->GetBeginningOfTextInCell(cell) -
368 the_locking_inset->width(bv,font) -
369 tabular->GetAdditionalWidth(cell) - 1,
370 tabular->GetAscentOfRow(i) +
371 tabular->GetDescentOfRow(i) - 1);
374 x -= ADD_TO_TABULAR_WIDTH;
375 x += width(bv, font);
376 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
383 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
384 int row, int cell) const
386 int x2 = x + tabular->GetWidthOfColumn(cell);
389 if (!tabular->TopAlreadyDrawed(cell)) {
390 on_off = !tabular->TopLine(cell);
391 pain.line(x, baseline - tabular->GetAscentOfRow(row),
392 x2, baseline - tabular->GetAscentOfRow(row),
393 on_off ? LColor::tabularonoffline : LColor::tabularline,
394 on_off ? Painter::line_onoffdash : Painter::line_solid);
396 on_off = !tabular->BottomLine(cell);
397 pain.line(x,baseline + tabular->GetDescentOfRow(row),
398 x2, baseline + tabular->GetDescentOfRow(row),
399 on_off ? LColor::tabularonoffline : LColor::tabularline,
400 on_off ? Painter::line_onoffdash : Painter::line_solid);
401 if (!tabular->LeftAlreadyDrawed(cell)) {
402 on_off = !tabular->LeftLine(cell);
403 pain.line(x, baseline - tabular->GetAscentOfRow(row),
404 x, baseline + tabular->GetDescentOfRow(row),
405 on_off ? LColor::tabularonoffline : LColor::tabularline,
406 on_off ? Painter::line_onoffdash : Painter::line_solid);
408 on_off = !tabular->RightLine(cell);
409 pain.line(x2 - tabular->GetAdditionalWidth(cell),
410 baseline - tabular->GetAscentOfRow(row),
411 x2 - tabular->GetAdditionalWidth(cell),
412 baseline + tabular->GetDescentOfRow(row),
413 on_off ? LColor::tabularonoffline : LColor::tabularline,
414 on_off ? Painter::line_onoffdash : Painter::line_solid);
418 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
419 int row, int column, int cell) const
421 int cs = tabular->column_of_cell(sel_cell_start);
422 int ce = tabular->column_of_cell(sel_cell_end);
425 cs = tabular->column_of_cell(sel_cell_end);
427 ce = tabular->right_column_of_cell(sel_cell_end);
430 int rs = tabular->row_of_cell(sel_cell_start);
431 int re = tabular->row_of_cell(sel_cell_end);
432 if (rs > re) swap(rs, re);
434 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
435 int w = tabular->GetWidthOfColumn(cell);
436 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
437 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
438 w, h, LColor::selection);
443 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
447 calculate_dimensions_of_cells(bv, font, true);
449 owner()->update(bv, font, true);
452 if (the_locking_inset)
453 the_locking_inset->update(bv, font, reinit);
455 switch (need_update) {
459 if (calculate_dimensions_of_cells(bv, font, false))
471 string const InsetTabular::EditMessage() const
473 return _("Opened Tabular Inset");
477 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
479 UpdatableInset::Edit(bv, x, y, button);
481 if (!bv->lockInset(this)) {
482 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
486 the_locking_inset = 0;
490 sel_cell_start = sel_cell_end = actcell;
491 bv->text->FinishUndo();
492 if (InsetHit(bv, x, y) && (button != 3)) {
493 ActivateCellInsetAbs(bv, x, y, button);
495 // UpdateLocal(bv, NONE, false);
496 // bv->getOwner()->getPopups().updateFormTabular();
500 void InsetTabular::InsetUnlock(BufferView * bv)
502 if (the_locking_inset) {
503 the_locking_inset->InsetUnlock(bv);
504 the_locking_inset = 0;
507 no_selection = false;
510 if (scroll(false) || hasSelection()) {
511 sel_cell_start = sel_cell_end = 0;
515 UpdateLocal(bv, FULL, false);
520 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
521 bool mark_dirty) const
523 if (need_update < what) // only set this if it has greater update
525 if ((what == INIT) && hasSelection())
528 if (need_update != NONE) {
529 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
530 if (locked) // && (what != NONE))
536 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
538 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
543 if (inset == tabular->GetCellInset(actcell)) {
544 lyxerr[Debug::INSETS] << "OK" << endl;
545 the_locking_inset = tabular->GetCellInset(actcell);
547 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
548 inset_y = cursor.y();
550 } else if (the_locking_inset && (the_locking_inset == inset)) {
551 lyxerr[Debug::INSETS] << "OK" << endl;
553 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
554 inset_y = cursor.y();
555 } else if (the_locking_inset) {
556 lyxerr[Debug::INSETS] << "MAYBE" << endl;
557 return the_locking_inset->LockInsetInInset(bv, inset);
559 lyxerr[Debug::INSETS] << "NOT OK" << endl;
564 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
567 if (!the_locking_inset)
569 if (the_locking_inset == inset) {
570 the_locking_inset->InsetUnlock(bv);
571 the_locking_inset = 0;
575 UpdateLocal(bv, CELL, false);
576 ShowInsetCursor(bv, false);
579 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
580 if (inset->LyxCode() == TABULAR_CODE &&
581 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
582 bv->owner()->getDialogs()->updateTabular(this);
591 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
593 if (!the_locking_inset)
595 if (the_locking_inset != inset) {
596 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
599 UpdateLocal(bv, CELL, false);
604 unsigned int InsetTabular::InsetInInsetY()
606 if (!the_locking_inset)
609 return inset_y + the_locking_inset->InsetInInsetY();
613 UpdatableInset * InsetTabular::GetLockingInset()
615 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
619 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
623 if (the_locking_inset)
624 return the_locking_inset->GetFirstLockingInsetOfType(c);
629 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
631 if (the_locking_inset)
632 return the_locking_inset->InsertInset(bv, inset);
637 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
639 if (hasSelection() && (button == 3))
642 if (hasSelection()) {
644 UpdateLocal(bv, SELECTION, false);
647 no_selection = false;
649 int const ocell = actcell;
650 int const orow = actrow;
655 UpdateLocal(bv, NONE, false);
656 sel_cell_start = sel_cell_end = actcell;
658 if ((ocell != actcell) && the_locking_inset) {
659 the_locking_inset->InsetUnlock(bv);
660 the_locking_inset = 0;
666 bool const inset_hit = InsetHit(bv, x, y);
668 if ((ocell == actcell) && the_locking_inset && inset_hit) {
670 the_locking_inset->InsetButtonPress(bv,
671 x - inset_x, y - inset_y,
674 } else if (the_locking_inset) {
675 the_locking_inset->InsetUnlock(bv);
677 the_locking_inset = 0;
679 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
682 if (inset_hit && bv->theLockingInset()) {
683 if (ActivateCellInsetAbs(bv, x, y, button))
684 the_locking_inset->InsetButtonPress(bv,
694 void InsetTabular::InsetButtonRelease(BufferView * bv,
695 int x, int y, int button)
698 if (the_locking_inset) {
700 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
701 i->InsetButtonRelease(bv, x, y, button);
705 bv->owner()->getDialogs()->showTabular(this);
708 if (the_locking_inset) {
709 the_locking_inset->InsetButtonRelease(bv,
710 x - inset_x, y - inset_y,
714 no_selection = false;
718 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
720 if (the_locking_inset) {
721 the_locking_inset->InsetMotionNotify(bv,
729 int const old_cell = actcell;
732 sel_cell_end = actcell;
733 if (sel_cell_end != old_cell)
734 UpdateLocal(bv, SELECTION, false);
737 no_selection = false;
741 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
743 if (the_locking_inset) {
744 the_locking_inset->InsetKeyPress(xke);
750 UpdatableInset::RESULT
751 InsetTabular::LocalDispatch(BufferView * bv,
755 // We need to save the value of the_locking_inset as the call to
756 // the_locking_inset->LocalDispatch might unlock it.
757 old_locking_inset = the_locking_inset;
758 no_selection = false;
759 UpdatableInset::RESULT result =
760 UpdatableInset::LocalDispatch(bv, action, arg);
761 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
766 if ((action < 0) && arg.empty())
769 bool hs = hasSelection();
772 // this one have priority over the locked InsetText!
778 if (the_locking_inset) {
779 UnlockInsetInInset(bv, the_locking_inset);
780 the_locking_inset = 0;
782 if (action == LFUN_TAB)
783 moveNextCell(bv, old_locking_inset != 0);
785 movePrevCell(bv, old_locking_inset != 0);
786 sel_cell_start = sel_cell_end = actcell;
788 UpdateLocal(bv, SELECTION, false);
789 if (!the_locking_inset) {
791 return DISPATCHED_NOUPDATE;
795 // this to avoid compiler warnings.
800 if (the_locking_inset) {
801 result=the_locking_inset->LocalDispatch(bv, action, arg);
802 if (result == DISPATCHED_NOUPDATE) {
805 if (sc != scroll()) { // inset has been scrolled
806 the_locking_inset->ToggleInsetCursor(bv);
807 UpdateLocal(bv, FULL, false);
808 the_locking_inset->ToggleInsetCursor(bv);
811 } else if (result == DISPATCHED) {
812 the_locking_inset->ToggleInsetCursor(bv);
813 UpdateLocal(bv, CELL, false);
814 the_locking_inset->ToggleInsetCursor(bv);
816 } else if (result == FINISHED) {
823 // --- Cursor Movements ----------------------------------
825 if (tabular->IsLastCellInRow(actcell))
827 moveRight(bv, false);
828 sel_cell_end = actcell;
829 UpdateLocal(bv, SELECTION, false);
832 result = moveRight(bv);
833 sel_cell_start = sel_cell_end = actcell;
835 UpdateLocal(bv, SELECTION, false);
838 if (tabular->IsFirstCellInRow(actcell))
841 sel_cell_end = actcell;
842 UpdateLocal(bv, SELECTION, false);
845 result = moveLeft(bv);
846 sel_cell_start = sel_cell_end = actcell;
848 UpdateLocal(bv, SELECTION, false);
852 int const ocell = actcell;
854 if ((ocell == sel_cell_end) ||
855 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
856 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
858 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
859 UpdateLocal(bv, SELECTION, false);
863 result = moveDown(bv, old_locking_inset != 0);
864 sel_cell_start = sel_cell_end = actcell;
866 UpdateLocal(bv, SELECTION, false);
870 int const ocell = actcell;
872 if ((ocell == sel_cell_end) ||
873 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
874 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
876 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
877 UpdateLocal(bv, SELECTION, false);
881 result = moveUp(bv, old_locking_inset != 0);
882 sel_cell_start = sel_cell_end = actcell;
884 UpdateLocal(bv, SELECTION, false);
888 if (the_locking_inset) {
889 UnlockInsetInInset(bv, the_locking_inset);
890 the_locking_inset = 0;
892 if (bv->text->first + bv->painter().paperHeight() <
893 (top_baseline + tabular->GetHeightOfTabular()))
895 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
896 UpdateLocal(bv, FULL, false);
897 actcell = tabular->GetCellBelow(first_visible_cell) + column;
899 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
902 UpdateLocal(bv, CURSOR, false);
907 if (the_locking_inset) {
908 UnlockInsetInInset(bv, the_locking_inset);
909 the_locking_inset = 0;
911 if (top_baseline < 0) {
912 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
913 UpdateLocal(bv, FULL, false);
914 if (top_baseline > 0)
917 actcell = tabular->GetCellBelow(first_visible_cell) + column;
922 UpdateLocal(bv, CURSOR, false);
933 case LFUN_LAYOUT_TABULAR:
935 bv->owner()->getDialogs()->showTabular(this);
938 case LFUN_TABULAR_FEATURE:
939 if (!TabularFeatures(bv, arg))
940 result = UNDISPATCHED;
943 if (!copySelection(bv))
945 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
947 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
948 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
950 bv->text->cursor.par()->previous(),
951 bv->text->cursor.par()->next()
955 UpdateLocal(bv, INIT, true);
960 bv->text->FinishUndo();
963 case LFUN_PASTESELECTION:
965 string clip(bv->workarea()->getClipboard());
969 if (clip.find('\t') != string::npos) {
973 unsigned int len = clip.length();
974 string::size_type p = 0;
977 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
985 maxCols = max(cols, maxCols);
991 maxCols = max(cols, maxCols);
992 delete paste_tabular;
993 paste_tabular = new LyXTabular(this, rows, maxCols);
994 string::size_type op = 0;
996 int cells = paste_tabular->GetNumberOfCells();
998 while((cell < cells) && (p < len) &&
999 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1004 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1009 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1010 while(cols++ < maxCols)
1018 // check for the last cell if there is no trailing '\n'
1019 if ((cell < cells) && (op < len))
1020 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1022 // so that the clipboard is used and it goes on
1024 // and executes LFUN_PASTESELECTION in insettext!
1025 delete paste_tabular;
1030 if (hasPasteBuffer()) {
1031 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1033 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1034 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1036 bv->text->cursor.par()->previous(),
1037 bv->text->cursor.par()->next()
1041 UpdateLocal(bv, INIT, true);
1044 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1046 // we try to activate the actual inset and put this event down to
1047 // the insets dispatch function.
1048 result = UNDISPATCHED;
1049 if (the_locking_inset)
1052 if (ActivateCellInset(bv)) {
1053 result = the_locking_inset->LocalDispatch(bv, action, arg);
1054 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1055 UnlockInsetInInset(bv, the_locking_inset);
1057 the_locking_inset = 0;
1058 return UNDISPATCHED;
1061 // the_locking_inset->ToggleInsetCursor(bv);
1062 UpdateLocal(bv, CELL, false);
1063 // the_locking_inset->ToggleInsetCursor(bv);
1068 if (result!=FINISHED) {
1069 if (!the_locking_inset) {
1070 ShowInsetCursor(bv);
1073 bv->unlockInset(this);
1078 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1079 bool fragile, bool fp) const
1081 return tabular->Latex(buf, os, fragile, fp);
1085 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1087 // This should be changed to a real ascii export
1088 return tabular->Ascii(buf, os);
1092 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1098 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1100 return tabular->DocBook(buf,os);
1104 void InsetTabular::Validate(LaTeXFeatures & features) const
1106 tabular->Validate(features);
1110 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1111 LyXFont const & font,
1118 bool changed = false;
1120 // if we have a locking_inset we should have to check only this cell for
1121 // change so I'll try this to have a boost, but who knows ;)
1122 if ((need_update != INIT) &&
1123 (the_locking_inset == tabular->GetCellInset(actcell))) {
1124 for(int i = 0; i < tabular->columns(); ++i) {
1125 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1127 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1130 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1131 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1132 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1135 for (int i = 0; i < tabular->rows(); ++i) {
1138 for (int j= 0; j < tabular->columns(); ++j) {
1139 if (tabular->IsPartOfMultiColumn(i,j))
1142 inset = tabular->GetCellInset(cell);
1144 inset->update(bv, font, false);
1145 maxAsc = max(maxAsc, inset->ascent(bv, font));
1146 maxDesc = max(maxDesc, inset->descent(bv, font));
1147 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1149 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1150 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1156 void InsetTabular::GetCursorPos(BufferView *,
1157 int & x, int & y) const
1159 x = cursor.x() - top_x;
1164 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1166 if (the_locking_inset) {
1167 the_locking_inset->ToggleInsetCursor(bv);
1171 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1173 int const asc = lyxfont::maxAscent(font);
1174 int const desc = lyxfont::maxDescent(font);
1176 if (isCursorVisible())
1177 bv->hideLockedInsetCursor();
1179 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1180 toggleCursorVisible();
1184 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1186 if (!isCursorVisible()) {
1187 LyXFont font; // = GetFont(par, cursor.pos);
1189 int const asc = lyxfont::maxAscent(font);
1190 int const desc = lyxfont::maxDescent(font);
1191 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1193 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1194 setCursorVisible(true);
1199 void InsetTabular::HideInsetCursor(BufferView * bv)
1201 if (isCursorVisible()) {
1202 bv->hideLockedInsetCursor();
1203 setCursorVisible(false);
1205 // if (cursor_visible)
1206 // ToggleInsetCursor(bv);
1210 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1214 actcell = actrow = actcol = 0;
1215 int ly = tabular->GetDescentOfRow(actrow);
1217 // first search the right row
1218 while((ly < y) && (actrow < tabular->rows())) {
1219 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1220 tabular->GetAscentOfRow(actrow + 1) +
1221 tabular->GetAdditionalHeight(actrow + 1));
1223 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1225 actcell = tabular->GetCellNumber(actrow, actcol);
1227 // now search the right column
1228 int lx = tabular->GetWidthOfColumn(actcell) -
1229 tabular->GetAdditionalWidth(actcell);
1231 #ifdef WITH_WARNINGS
1232 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1234 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1235 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1236 tabular->GetAdditionalWidth(actcell - 1));
1238 // Jürgen, you should check that this is correct. (Lgb)
1239 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1240 lx += tabular->GetWidthOfColumn(actcell + 1)
1241 + tabular->GetAdditionalWidth(actcell);
1245 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1250 int InsetTabular::getCellXPos(int cell) const
1254 for (; !tabular->IsFirstCellInRow(c); --c)
1256 int lx = tabular->GetWidthOfColumn(cell);
1257 for (; c < cell; ++c) {
1258 lx += tabular->GetWidthOfColumn(c);
1260 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1264 void InsetTabular::resetPos(BufferView * bv) const
1268 actcol = tabular->column_of_cell(actcell);
1273 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1274 if (tabular->IsLastCellInRow(cell)) {
1275 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1276 tabular->GetAscentOfRow(actrow + 1) +
1277 tabular->GetAdditionalHeight(actrow + 1));
1281 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1282 int new_x = getCellXPos(actcell);
1283 int old_x = cursor.x();
1286 // cursor.x(getCellXPos(actcell) + offset);
1287 if (scroll(false) && (tabular->GetWidthOfTabular() < bv->workWidth()-20)) {
1289 UpdateLocal(bv, FULL, false);
1290 } else if (the_locking_inset &&
1291 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20)) {
1292 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1293 if (xx > (bv->workWidth()-20))
1294 scroll(bv, -(xx - bv->workWidth() + 60));
1302 UpdateLocal(bv, FULL, false);
1303 } else if ((cursor.x() - offset) > 20 &&
1304 (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1305 > (bv->workWidth() - 20)) {
1306 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1307 UpdateLocal(bv, FULL, false);
1308 } else if ((cursor.x() - offset) < 20) {
1309 scroll(bv, 20 - cursor.x() + offset);
1310 UpdateLocal(bv, FULL, false);
1311 } else if (scroll(false) && top_x > 20 &&
1312 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1313 scroll(bv, old_x - cursor.x());
1314 UpdateLocal(bv, FULL, false);
1316 if ((!the_locking_inset ||
1317 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1318 actcell != oldcell) {
1319 InsetTabular * inset = const_cast<InsetTabular *>(this);
1320 bv->owner()->getDialogs()->updateTabular(inset);
1326 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1328 if (lock && !old_locking_inset) {
1329 if (ActivateCellInset(bv))
1332 bool moved = isRightToLeft(bv)
1333 ? movePrevCell(bv) : moveNextCell(bv);
1336 if (lock && ActivateCellInset(bv))
1340 return DISPATCHED_NOUPDATE;
1344 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1346 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1349 if (lock) { // behind the inset
1350 if (ActivateCellInset(bv, 0, 0, 0, true))
1354 return DISPATCHED_NOUPDATE;
1358 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1360 int const ocell = actcell;
1361 actcell = tabular->GetCellAbove(actcell);
1362 if (actcell == ocell) // we moved out of the inset
1368 if (old_locking_inset) {
1369 old_locking_inset->GetCursorPos(bv, x, y);
1370 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1372 if (ActivateCellInset(bv, x, 0))
1375 return DISPATCHED_NOUPDATE;
1379 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1381 int const ocell = actcell;
1382 actcell = tabular->GetCellBelow(actcell);
1383 if (actcell == ocell) // we moved out of the inset
1389 if (old_locking_inset) {
1390 old_locking_inset->GetCursorPos(bv, x, y);
1391 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1393 if (ActivateCellInset(bv, x, 0))
1396 return DISPATCHED_NOUPDATE;
1400 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1402 if (isRightToLeft(bv)) {
1403 if (tabular->IsFirstCellInRow(actcell)) {
1404 int row = tabular->row_of_cell(actcell);
1405 if (row == tabular->rows() - 1)
1407 actcell = tabular->GetLastCellInRow(row);
1408 actcell = tabular->GetCellBelow(actcell);
1415 if (tabular->IsLastCell(actcell))
1420 bool rtl = tabular->GetCellInset(actcell)->par->
1421 isRightToLeftPar(bv->buffer()->params);
1422 ActivateCellInset(bv, 0, 0, 0, !rtl);
1429 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1431 if (isRightToLeft(bv)) {
1432 if (tabular->IsLastCellInRow(actcell)) {
1433 int row = tabular->row_of_cell(actcell);
1436 actcell = tabular->GetFirstCellInRow(row);
1437 actcell = tabular->GetCellAbove(actcell);
1439 if (tabular->IsLastCell(actcell))
1444 if (!actcell) // first cell
1449 bool rtl = tabular->GetCellInset(actcell)->par->
1450 isRightToLeftPar(bv->buffer()->params);
1451 ActivateCellInset(bv, 0, 0, 0, !rtl);
1458 bool InsetTabular::Delete()
1464 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1466 if (the_locking_inset)
1467 the_locking_inset->SetFont(bv, font, tall);
1471 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1473 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1476 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1477 string const tmp = tabularFeatures[i].feature;
1479 if (tmp == what.substr(0, tmp.length())) {
1480 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1481 //tabularFeatures[i].feature.length())) {
1482 action = tabularFeatures[i].action;
1486 if (action == LyXTabular::LAST_ACTION)
1490 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1491 TabularFeatures(bv, action, val);
1496 void InsetTabular::TabularFeatures(BufferView * bv,
1497 LyXTabular::Feature feature,
1498 string const & value)
1506 bool setLines = false;
1507 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1508 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1513 case LyXTabular::M_ALIGN_LEFT:
1514 case LyXTabular::ALIGN_LEFT:
1515 setAlign=LYX_ALIGN_LEFT;
1517 case LyXTabular::M_ALIGN_RIGHT:
1518 case LyXTabular::ALIGN_RIGHT:
1519 setAlign=LYX_ALIGN_RIGHT;
1521 case LyXTabular::M_ALIGN_CENTER:
1522 case LyXTabular::ALIGN_CENTER:
1523 setAlign=LYX_ALIGN_CENTER;
1525 case LyXTabular::M_VALIGN_TOP:
1526 case LyXTabular::VALIGN_TOP:
1527 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1529 case LyXTabular::M_VALIGN_BOTTOM:
1530 case LyXTabular::VALIGN_BOTTOM:
1531 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1533 case LyXTabular::M_VALIGN_CENTER:
1534 case LyXTabular::VALIGN_CENTER:
1535 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1540 if (hasSelection()) {
1541 sel_col_start = tabular->column_of_cell(sel_cell_start);
1542 sel_col_end = tabular->column_of_cell(sel_cell_end);
1543 if (sel_col_start > sel_col_end) {
1544 sel_col_end = sel_col_start;
1545 sel_col_start = tabular->column_of_cell(sel_cell_end);
1547 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1550 sel_row_start = tabular->row_of_cell(sel_cell_start);
1551 sel_row_end = tabular->row_of_cell(sel_cell_end);
1552 if (sel_row_start > sel_row_end) {
1553 //int tmp = sel_row_start;
1554 //sel_row_start = sel_row_end;
1555 //sel_row_end = tmp;
1556 swap(sel_row_start, sel_row_end);
1559 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1560 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1562 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1564 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1565 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1567 bv->text->cursor.par()->previous(),
1568 bv->text->cursor.par()->next()
1572 int row = tabular->row_of_cell(actcell);
1573 int column = tabular->column_of_cell(actcell);
1577 case LyXTabular::SET_PWIDTH:
1579 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1580 tabular->SetColumnPWidth(actcell,value);
1582 for (int i = 0; i < tabular->rows(); ++i) {
1583 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1586 UpdateLocal(bv, INIT, true);
1590 case LyXTabular::SET_MPWIDTH:
1592 bool const update = (tabular->GetPWidth(actcell) != value);
1593 tabular->SetMColumnPWidth(actcell,value);
1595 for (int i = 0; i < tabular->rows(); ++i) {
1596 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1599 UpdateLocal(bv, INIT, true);
1603 case LyXTabular::SET_SPECIAL_COLUMN:
1604 case LyXTabular::SET_SPECIAL_MULTI:
1605 tabular->SetAlignSpecial(actcell,value,feature);
1607 case LyXTabular::APPEND_ROW:
1608 // append the row into the tabular
1609 UnlockInsetInInset(bv, the_locking_inset);
1610 tabular->AppendRow(actcell);
1611 UpdateLocal(bv, INIT, true);
1613 case LyXTabular::APPEND_COLUMN:
1614 // append the column into the tabular
1615 UnlockInsetInInset(bv, the_locking_inset);
1616 tabular->AppendColumn(actcell);
1617 actcell = tabular->GetCellNumber(row, column);
1618 UpdateLocal(bv, INIT, true);
1620 case LyXTabular::DELETE_ROW:
1621 UnlockInsetInInset(bv, the_locking_inset);
1622 tabular->DeleteRow(tabular->row_of_cell(actcell));
1623 if ((row+1) > tabular->rows())
1625 actcell = tabular->GetCellNumber(row, column);
1627 UpdateLocal(bv, INIT, true);
1629 case LyXTabular::DELETE_COLUMN:
1630 UnlockInsetInInset(bv, the_locking_inset);
1631 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1632 if ((column+1) > tabular->columns())
1634 actcell = tabular->GetCellNumber(row, column);
1636 UpdateLocal(bv, INIT, true);
1638 case LyXTabular::M_TOGGLE_LINE_TOP:
1640 case LyXTabular::TOGGLE_LINE_TOP:
1642 bool lineSet = !tabular->TopLine(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->SetTopLine(
1646 tabular->GetCellNumber(i, j),
1648 UpdateLocal(bv, INIT, true);
1652 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1654 case LyXTabular::TOGGLE_LINE_BOTTOM:
1656 bool lineSet = !tabular->BottomLine(actcell, flag);
1657 for (int i = sel_row_start; i <= sel_row_end; ++i)
1658 for (int j = sel_col_start; j <= sel_col_end; ++j)
1659 tabular->SetBottomLine(
1660 tabular->GetCellNumber(i, j),
1663 UpdateLocal(bv, INIT, true);
1667 case LyXTabular::M_TOGGLE_LINE_LEFT:
1669 case LyXTabular::TOGGLE_LINE_LEFT:
1671 bool lineSet = !tabular->LeftLine(actcell, flag);
1672 for (int i = sel_row_start; i <= sel_row_end; ++i)
1673 for (int j = sel_col_start; j <= sel_col_end; ++j)
1674 tabular->SetLeftLine(
1675 tabular->GetCellNumber(i,j),
1678 UpdateLocal(bv, INIT, true);
1682 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1684 case LyXTabular::TOGGLE_LINE_RIGHT:
1686 bool lineSet = !tabular->RightLine(actcell, flag);
1687 for (int i = sel_row_start; i <= sel_row_end; ++i)
1688 for (int j = sel_col_start; j <= sel_col_end; ++j)
1689 tabular->SetRightLine(
1690 tabular->GetCellNumber(i,j),
1693 UpdateLocal(bv, INIT, true);
1697 case LyXTabular::M_ALIGN_LEFT:
1698 case LyXTabular::M_ALIGN_RIGHT:
1699 case LyXTabular::M_ALIGN_CENTER:
1701 case LyXTabular::ALIGN_LEFT:
1702 case LyXTabular::ALIGN_RIGHT:
1703 case LyXTabular::ALIGN_CENTER:
1704 for (int i = sel_row_start; i <= sel_row_end; ++i)
1705 for (int j = sel_col_start; j <= sel_col_end; ++j)
1706 tabular->SetAlignment(
1707 tabular->GetCellNumber(i, j),
1710 UpdateLocal(bv, INIT, true);
1712 case LyXTabular::M_VALIGN_TOP:
1713 case LyXTabular::M_VALIGN_BOTTOM:
1714 case LyXTabular::M_VALIGN_CENTER:
1716 case LyXTabular::VALIGN_TOP:
1717 case LyXTabular::VALIGN_BOTTOM:
1718 case LyXTabular::VALIGN_CENTER:
1719 for (int i = sel_row_start; i <= sel_row_end; ++i)
1720 for (int j = sel_col_start; j <= sel_col_end; ++j)
1721 tabular->SetVAlignment(
1722 tabular->GetCellNumber(i, j),
1724 UpdateLocal(bv, INIT, true);
1726 case LyXTabular::MULTICOLUMN:
1728 if (sel_row_start != sel_row_end) {
1729 WriteAlert(_("Impossible Operation!"),
1730 _("Multicolumns can only be horizontally."),
1734 // just multicol for one Single Cell
1735 if (!hasSelection()) {
1736 // check wether we are completly in a multicol
1737 if (tabular->IsMultiColumn(actcell)) {
1738 tabular->UnsetMultiColumn(actcell);
1739 UpdateLocal(bv, INIT, true);
1741 tabular->SetMultiColumn(actcell, 1);
1742 UpdateLocal(bv, CELL, true);
1746 // we have a selection so this means we just add all this
1747 // cells to form a multicolumn cell
1751 if (sel_cell_start > sel_cell_end) {
1752 s_start = sel_cell_end;
1753 s_end = sel_cell_start;
1755 s_start = sel_cell_start;
1756 s_end = sel_cell_end;
1758 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1760 sel_cell_end = sel_cell_start;
1761 UpdateLocal(bv, INIT, true);
1764 case LyXTabular::SET_ALL_LINES:
1766 case LyXTabular::UNSET_ALL_LINES:
1767 for (int i = sel_row_start; i <= sel_row_end; ++i)
1768 for (int j = sel_col_start; j <= sel_col_end; ++j)
1769 tabular->SetAllLines(
1770 tabular->GetCellNumber(i,j), setLines);
1771 UpdateLocal(bv, INIT, true);
1773 case LyXTabular::SET_LONGTABULAR:
1774 tabular->SetLongTabular(true);
1775 UpdateLocal(bv, INIT, true); // because this toggles displayed
1777 case LyXTabular::UNSET_LONGTABULAR:
1778 tabular->SetLongTabular(false);
1779 UpdateLocal(bv, INIT, true); // because this toggles displayed
1781 case LyXTabular::SET_ROTATE_TABULAR:
1782 tabular->SetRotateTabular(true);
1784 case LyXTabular::UNSET_ROTATE_TABULAR:
1785 tabular->SetRotateTabular(false);
1787 case LyXTabular::SET_ROTATE_CELL:
1788 for (int i = sel_row_start; i <= sel_row_end; ++i)
1789 for (int j = sel_col_start; j<=sel_col_end; ++j)
1790 tabular->SetRotateCell(
1791 tabular->GetCellNumber(i, j),
1794 case LyXTabular::UNSET_ROTATE_CELL:
1795 for (int i = sel_row_start; i <= sel_row_end; ++i)
1796 for (int j = sel_col_start; j <= sel_col_end; ++j)
1797 tabular->SetRotateCell(
1798 tabular->GetCellNumber(i, j), false);
1800 case LyXTabular::SET_USEBOX:
1802 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1803 if (val == tabular->GetUsebox(actcell))
1804 val = LyXTabular::BOX_NONE;
1805 for (int i = sel_row_start; i <= sel_row_end; ++i)
1806 for (int j = sel_col_start; j <= sel_col_end; ++j)
1808 tabular->GetCellNumber(i, j), val);
1811 case LyXTabular::SET_LTFIRSTHEAD:
1812 tabular->SetLTHead(actcell, true);
1814 case LyXTabular::SET_LTHEAD:
1815 tabular->SetLTHead(actcell, false);
1817 case LyXTabular::SET_LTFOOT:
1818 tabular->SetLTFoot(actcell, false);
1820 case LyXTabular::SET_LTLASTFOOT:
1821 tabular->SetLTFoot(actcell, true);
1823 case LyXTabular::SET_LTNEWPAGE:
1825 bool what = !tabular->GetLTNewPage(actcell);
1826 tabular->SetLTNewPage(actcell, what);
1829 // dummy stuff just to avoid warnings
1830 case LyXTabular::LAST_ACTION:
1836 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1839 UpdatableInset * inset =
1840 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1841 LyXFont font(LyXFont::ALL_SANE);
1843 x = inset->x() + inset->width(bv, font);
1844 y = inset->descent(bv, font);
1846 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1847 //inset_y = cursor.y();
1848 inset->Edit(bv, x, y, button);
1849 if (!the_locking_inset)
1851 UpdateLocal(bv, CELL, false);
1852 return (the_locking_inset != 0);
1856 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1859 inset_x = cursor.x()
1860 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1861 inset_y = cursor.y();
1862 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1866 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1869 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1873 // This returns paperWidth() if the cell-width is unlimited or the width
1874 // in pixels if we have a pwidth for this cell.
1875 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1877 string const s = tabular->GetPWidth(cell);
1881 return VSpace(s).inPixels(bv);
1885 int InsetTabular::getMaxWidth(BufferView * bv,
1886 UpdatableInset const * inset) const
1888 int const n = tabular->GetNumberOfCells();
1890 for (; cell < n; ++cell) {
1891 if (tabular->GetCellInset(cell) == inset)
1896 int w = GetMaxWidthOfCell(bv, cell);
1898 // because the inset then subtracts it's top_x and owner->x()
1899 w += (inset->x() - top_x);
1904 void InsetTabular::resizeLyXText(BufferView *) const
1910 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1911 bool const recursive) const
1913 if (the_locking_inset)
1914 return the_locking_inset->getLyXText(bv, recursive);
1915 return Inset::getLyXText(bv, recursive);
1919 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1921 if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1922 bv->owner()->getDialogs()
1923 ->showTabular(const_cast<InsetTabular *>(this));
1928 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1930 if (the_locking_inset) {
1931 InsetTabular * i = static_cast<InsetTabular *>
1932 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1934 i->OpenLayoutDialog(bv);
1938 bv->owner()->getDialogs()->showTabular(
1939 const_cast<InsetTabular *>(this));
1943 // functions returns:
1947 // 3 ... toggled off
1949 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1951 int action = LyXTabular::LAST_ACTION;
1952 LyXFunc::func_status status = LyXFunc::OK;
1955 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1956 string const tmp = tabularFeatures[i].feature;
1957 if (tmp == what.substr(0, tmp.length())) {
1958 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1959 // tabularFeatures[i].feature.length())) {
1960 action = tabularFeatures[i].action;
1964 if (action == LyXTabular::LAST_ACTION)
1965 return LyXFunc::Unknown;
1967 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1974 if (hasSelection()) {
1975 sel_row_start = tabular->row_of_cell(sel_cell_start);
1976 sel_row_end = tabular->row_of_cell(sel_cell_end);
1977 if (sel_row_start > sel_row_end) {
1978 //int tmp = sel_row_start;
1979 //sel_row_start = sel_row_end;
1980 //sel_row_end = tmp;
1981 swap(sel_row_start, sel_row_end);
1984 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1988 case LyXTabular::SET_PWIDTH:
1989 case LyXTabular::SET_MPWIDTH:
1990 case LyXTabular::SET_SPECIAL_COLUMN:
1991 case LyXTabular::SET_SPECIAL_MULTI:
1992 status |= LyXFunc::Disabled;
1995 case LyXTabular::APPEND_ROW:
1996 case LyXTabular::APPEND_COLUMN:
1997 case LyXTabular::DELETE_ROW:
1998 case LyXTabular::DELETE_COLUMN:
1999 case LyXTabular::SET_ALL_LINES:
2000 case LyXTabular::UNSET_ALL_LINES:
2001 status |= LyXFunc::OK;
2004 case LyXTabular::MULTICOLUMN:
2005 if (tabular->IsMultiColumn(actcell))
2006 status |= LyXFunc::ToggleOn;
2008 status |= LyXFunc::ToggleOff;
2010 case LyXTabular::M_TOGGLE_LINE_TOP:
2012 case LyXTabular::TOGGLE_LINE_TOP:
2013 if (tabular->TopLine(actcell, flag))
2014 status |= LyXFunc::ToggleOn;
2016 status |= LyXFunc::ToggleOff;
2018 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2020 case LyXTabular::TOGGLE_LINE_BOTTOM:
2021 if (tabular->BottomLine(actcell, flag))
2022 status |= LyXFunc::ToggleOn;
2024 status |= LyXFunc::ToggleOff;
2026 case LyXTabular::M_TOGGLE_LINE_LEFT:
2028 case LyXTabular::TOGGLE_LINE_LEFT:
2029 if (tabular->LeftLine(actcell, flag))
2030 status |= LyXFunc::ToggleOn;
2032 status |= LyXFunc::ToggleOff;
2034 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2036 case LyXTabular::TOGGLE_LINE_RIGHT:
2037 if (tabular->RightLine(actcell, flag))
2038 status |= LyXFunc::ToggleOn;
2040 status |= LyXFunc::ToggleOff;
2042 case LyXTabular::M_ALIGN_LEFT:
2044 case LyXTabular::ALIGN_LEFT:
2045 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2046 status |= LyXFunc::ToggleOn;
2048 status |= LyXFunc::ToggleOff;
2050 case LyXTabular::M_ALIGN_RIGHT:
2052 case LyXTabular::ALIGN_RIGHT:
2053 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2054 status |= LyXFunc::ToggleOn;
2056 status |= LyXFunc::ToggleOff;
2058 case LyXTabular::M_ALIGN_CENTER:
2060 case LyXTabular::ALIGN_CENTER:
2061 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2062 status |= LyXFunc::ToggleOn;
2064 status |= LyXFunc::ToggleOff;
2066 case LyXTabular::M_VALIGN_TOP:
2068 case LyXTabular::VALIGN_TOP:
2069 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2070 status |= LyXFunc::ToggleOn;
2072 status |= LyXFunc::ToggleOff;
2074 case LyXTabular::M_VALIGN_BOTTOM:
2076 case LyXTabular::VALIGN_BOTTOM:
2077 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2078 status |= LyXFunc::ToggleOn;
2080 status |= LyXFunc::ToggleOff;
2082 case LyXTabular::M_VALIGN_CENTER:
2084 case LyXTabular::VALIGN_CENTER:
2085 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2086 status |= LyXFunc::ToggleOn;
2088 status |= LyXFunc::ToggleOff;
2090 case LyXTabular::SET_LONGTABULAR:
2091 if (tabular->IsLongTabular())
2092 status |= LyXFunc::ToggleOn;
2094 status |= LyXFunc::ToggleOff;
2096 case LyXTabular::UNSET_LONGTABULAR:
2097 if (!tabular->IsLongTabular())
2098 status |= LyXFunc::ToggleOn;
2100 status |= LyXFunc::ToggleOff;
2102 case LyXTabular::SET_ROTATE_TABULAR:
2103 if (tabular->GetRotateTabular())
2104 status |= LyXFunc::ToggleOn;
2106 status |= LyXFunc::ToggleOff;
2108 case LyXTabular::UNSET_ROTATE_TABULAR:
2109 if (!tabular->GetRotateTabular())
2110 status |= LyXFunc::ToggleOn;
2112 status |= LyXFunc::ToggleOff;
2114 case LyXTabular::SET_ROTATE_CELL:
2115 if (tabular->GetRotateCell(actcell))
2116 status |= LyXFunc::ToggleOn;
2118 status |= LyXFunc::ToggleOff;
2120 case LyXTabular::UNSET_ROTATE_CELL:
2121 if (!tabular->GetRotateCell(actcell))
2122 status |= LyXFunc::ToggleOn;
2124 status |= LyXFunc::ToggleOff;
2126 case LyXTabular::SET_USEBOX:
2127 if (strToInt(argument) == tabular->GetUsebox(actcell))
2128 status |= LyXFunc::ToggleOn;
2130 status |= LyXFunc::ToggleOff;
2132 case LyXTabular::SET_LTFIRSTHEAD:
2133 if (tabular->GetRowOfLTHead(actcell, dummy))
2134 status |= LyXFunc::ToggleOn;
2136 status |= LyXFunc::ToggleOff;
2138 case LyXTabular::SET_LTHEAD:
2139 if (tabular->GetRowOfLTHead(actcell, dummy))
2140 status |= LyXFunc::ToggleOn;
2142 status |= LyXFunc::ToggleOff;
2144 case LyXTabular::SET_LTFOOT:
2145 if (tabular->GetRowOfLTFoot(actcell, dummy))
2146 status |= LyXFunc::ToggleOn;
2148 status |= LyXFunc::ToggleOff;
2150 case LyXTabular::SET_LTLASTFOOT:
2151 if (tabular->GetRowOfLTFoot(actcell, dummy))
2152 status |= LyXFunc::ToggleOn;
2154 status |= LyXFunc::ToggleOff;
2156 case LyXTabular::SET_LTNEWPAGE:
2157 if (tabular->GetLTNewPage(actcell))
2158 status |= LyXFunc::ToggleOn;
2160 status |= LyXFunc::ToggleOff;
2163 status = LyXFunc::Disabled;
2170 std::vector<string> const InsetTabular::getLabelList() const
2172 return tabular->getLabelList();
2176 bool InsetTabular::copySelection(BufferView * bv)
2178 if (!hasSelection())
2180 //delete paste_tabular;
2182 //int sel_col_start;
2184 //int sel_row_start;
2187 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2188 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2189 if (sel_col_start > sel_col_end) {
2190 sel_col_start = sel_col_end;
2191 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2193 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2195 int const columns = sel_col_end - sel_col_start + 1;
2197 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2198 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2199 if (sel_row_start > sel_row_end) {
2200 //int tmp tmp = sel_row_start;
2201 //sel_row_start = sel_row_end;
2202 //sel_row_end = tmp;
2203 swap(sel_row_start, sel_row_end);
2205 int const rows = sel_row_end - sel_row_start + 1;
2207 delete paste_tabular;
2208 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2210 for (int i = 0; i < sel_row_start; ++i)
2211 paste_tabular->DeleteRow(0);
2212 while(paste_tabular->rows() > rows)
2213 paste_tabular->DeleteRow(rows);
2214 paste_tabular->SetTopLine(0, true, true);
2215 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2217 for (int i = 0; i < sel_col_start; ++i)
2218 paste_tabular->DeleteColumn(0);
2219 while (paste_tabular->columns() > columns)
2220 paste_tabular->DeleteColumn(columns);
2221 paste_tabular->SetLeftLine(0, true, true);
2222 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2226 paste_tabular->Ascii(bv->buffer(), sstr);
2227 bv->stuffClipboard(sstr.str().c_str());
2232 bool InsetTabular::pasteSelection(BufferView * bv)
2237 for (int r1 = 0, r2 = actrow;
2238 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2240 for(int c1 = 0, c2 = actcol;
2241 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2243 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2244 tabular->IsPartOfMultiColumn(r2,c2))
2246 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2250 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2254 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2255 int const n2 = tabular->GetCellNumber(r2, c2);
2256 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2257 tabular->GetCellInset(n2)->setOwner(this);
2258 tabular->GetCellInset(n2)->deleteLyXText(bv);
2265 bool InsetTabular::cutSelection()
2267 if (!hasSelection())
2270 //int sel_col_start;
2272 //int sel_row_start;
2275 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2276 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2277 if (sel_col_start > sel_col_end) {
2278 sel_col_start = sel_col_end;
2279 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2281 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2283 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2284 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2285 if (sel_row_start > sel_row_end) {
2286 //int tmp = sel_row_start;
2287 //sel_row_start = sel_row_end;
2288 //sel_row_end = tmp;
2289 swap(sel_row_start, sel_row_end);
2291 if (sel_cell_start > sel_cell_end) {
2292 //int tmp = sel_cell_start;
2293 //sel_cell_start = sel_cell_end;
2294 //sel_cell_end = tmp;
2295 swap(sel_cell_start, sel_cell_end);
2297 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2298 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2299 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2306 bool InsetTabular::isRightToLeft(BufferView *bv )
2308 return bv->getParentLanguage(this)->RightToLeft();
2311 bool InsetTabular::nodraw() const
2313 if (the_locking_inset)
2314 return the_locking_inset->nodraw();
2315 return UpdatableInset::nodraw();
2319 int InsetTabular::scroll(bool recursive) const
2321 int sx = UpdatableInset::scroll(false);
2323 if (recursive && the_locking_inset)
2324 sx += the_locking_inset->scroll(recursive);
2330 bool InsetTabular::doClearArea() const
2332 return !locked || (need_update & (FULL|INIT));