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;
139 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
142 tabular.reset(new LyXTabular(this, *(tab.tabular)));
143 the_locking_inset = 0;
144 locked = no_selection = false;
146 actrow = actcell = 0;
147 sel_cell_start = sel_cell_end = 0;
152 InsetTabular::~InsetTabular()
159 Inset * InsetTabular::Clone(Buffer const & buf) const
161 InsetTabular * t = new InsetTabular(*this, buf);
163 //t->tabular = tabular->Clone(t);
164 t->tabular.reset(tabular->Clone(t));
169 void InsetTabular::Write(Buffer const * buf, ostream & os) const
171 os << " Tabular" << endl;
172 tabular->Write(buf, os);
176 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
178 bool const old_format = (lex.GetString() == "\\LyXTable");
182 //tabular = new LyXTabular(buf, this, lex);
183 tabular.reset(new LyXTabular(buf, this, lex));
191 string token = lex.GetString();
192 while (lex.IsOK() && (token != "\\end_inset")) {
194 token = lex.GetString();
196 if (token != "\\end_inset") {
197 lex.printError("Missing \\end_inset at this point. "
203 int InsetTabular::ascent(BufferView *, LyXFont const &) const
205 return tabular->GetAscentOfRow(0);
209 int InsetTabular::descent(BufferView *, LyXFont const &) const
211 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
215 int InsetTabular::width(BufferView *, LyXFont const &) const
217 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
221 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
222 float & x, bool cleared) const
226 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
229 // lyxerr << "InsetTabular::draw(" << need_update << ")\n";
231 Painter & pain = bv->painter();
237 UpdatableInset::draw(bv, font, baseline, x, cleared);
240 x += static_cast<float>(scroll());
242 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
243 (top_x != int(x)) || (top_baseline != baseline))) {
244 int h = ascent(bv, font) + descent(bv, font);
245 int const tx = display() || !owner() ? 0 : top_x;
246 int w = tx ? width(bv, font) : pain.paperWidth();
247 int ty = baseline - ascent(bv, font);
251 if ((ty + h) > pain.paperHeight())
252 h = pain.paperHeight();
253 if ((top_x + w) > pain.paperWidth())
254 w = pain.paperWidth();
255 pain.fillRectangle(tx, ty, w, h);
260 top_baseline = baseline;
261 x += ADD_TO_TABULAR_WIDTH;
265 first_visible_cell = -1;
266 for (i = 0; i < tabular->rows(); ++i) {
268 cell = tabular->GetCellNumber(i, 0);
269 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
270 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
272 baseline += tabular->GetDescentOfRow(i) +
273 tabular->GetAscentOfRow(i + 1) +
274 tabular->GetAdditionalHeight(i + 1);
277 for (j = 0; j < tabular->columns(); ++j) {
278 if (nx > bv->workWidth())
280 if (tabular->IsPartOfMultiColumn(i, j))
282 cx = nx + tabular->GetBeginningOfTextInCell(cell);
283 if (first_visible_cell < 0)
284 first_visible_cell = cell;
286 DrawCellSelection(pain, nx, baseline, i, j, cell);
287 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
289 DrawCellLines(pain, nx, baseline, i, cell);
290 nx += tabular->GetWidthOfColumn(cell);
293 baseline += tabular->GetDescentOfRow(i) +
294 tabular->GetAscentOfRow(i + 1) +
295 tabular->GetAdditionalHeight(i + 1);
297 } else if (need_update == CELL) {
300 if (the_locking_inset &&
301 tabular->GetCellInset(actcell) != the_locking_inset)
303 Inset * inset = tabular->GetCellInset(cell);
305 inset != the_locking_inset && i < tabular->rows();
308 inset != the_locking_inset
309 && j < tabular->columns();
311 if (tabular->IsPartOfMultiColumn(i, j))
313 nx += tabular->GetWidthOfColumn(cell);
315 inset = tabular->GetCellInset(cell);
317 if (tabular->row_of_cell(cell) > i) {
319 baseline += tabular->GetDescentOfRow(i) +
320 tabular->GetAscentOfRow(i + 1) +
321 tabular->GetAdditionalHeight(i + 1);
325 // copute baseline for actual row
326 for (i = 0; i < actrow; ++i) {
327 baseline += tabular->GetDescentOfRow(i) +
328 tabular->GetAscentOfRow(i + 1) +
329 tabular->GetAdditionalHeight(i + 1);
331 // now compute the right x position
332 cell = tabular->GetCellNumber(actrow, 0);
333 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
334 if (tabular->IsPartOfMultiColumn(actrow, j))
336 nx += tabular->GetWidthOfColumn(cell);
340 i = tabular->row_of_cell(cell);
341 if (the_locking_inset != tabular->GetCellInset(cell)) {
342 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
345 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
347 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
348 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
349 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
351 // clear only if we didn't have a change
352 if (need_update == CELL) {
353 // clear before the inset
356 baseline - tabular->GetAscentOfRow(i) + 1,
358 tabular->GetAscentOfRow(i) +
359 tabular->GetDescentOfRow(i) - 1);
360 // clear behind the inset
362 int(cx + the_locking_inset->width(bv,font) + 1),
363 baseline - tabular->GetAscentOfRow(i) + 1,
364 tabular->GetWidthOfColumn(cell) -
365 tabular->GetBeginningOfTextInCell(cell) -
366 the_locking_inset->width(bv,font) -
367 tabular->GetAdditionalWidth(cell) - 1,
368 tabular->GetAscentOfRow(i) +
369 tabular->GetDescentOfRow(i) - 1);
372 x -= ADD_TO_TABULAR_WIDTH;
373 x += width(bv, font);
374 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
381 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
382 int row, int cell) const
384 int x2 = x + tabular->GetWidthOfColumn(cell);
387 if (!tabular->TopAlreadyDrawed(cell)) {
388 on_off = !tabular->TopLine(cell);
389 pain.line(x, baseline - tabular->GetAscentOfRow(row),
390 x2, baseline - tabular->GetAscentOfRow(row),
391 on_off ? LColor::tabularonoffline : LColor::tabularline,
392 on_off ? Painter::line_onoffdash : Painter::line_solid);
394 on_off = !tabular->BottomLine(cell);
395 pain.line(x,baseline + tabular->GetDescentOfRow(row),
396 x2, baseline + tabular->GetDescentOfRow(row),
397 on_off ? LColor::tabularonoffline : LColor::tabularline,
398 on_off ? Painter::line_onoffdash : Painter::line_solid);
399 if (!tabular->LeftAlreadyDrawed(cell)) {
400 on_off = !tabular->LeftLine(cell);
401 pain.line(x, baseline - tabular->GetAscentOfRow(row),
402 x, baseline + tabular->GetDescentOfRow(row),
403 on_off ? LColor::tabularonoffline : LColor::tabularline,
404 on_off ? Painter::line_onoffdash : Painter::line_solid);
406 on_off = !tabular->RightLine(cell);
407 pain.line(x2 - tabular->GetAdditionalWidth(cell),
408 baseline - tabular->GetAscentOfRow(row),
409 x2 - tabular->GetAdditionalWidth(cell),
410 baseline + tabular->GetDescentOfRow(row),
411 on_off ? LColor::tabularonoffline : LColor::tabularline,
412 on_off ? Painter::line_onoffdash : Painter::line_solid);
416 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
417 int row, int column, int cell) const
419 int cs = tabular->column_of_cell(sel_cell_start);
420 int ce = tabular->column_of_cell(sel_cell_end);
423 cs = tabular->column_of_cell(sel_cell_end);
425 ce = tabular->right_column_of_cell(sel_cell_end);
428 int rs = tabular->row_of_cell(sel_cell_start);
429 int re = tabular->row_of_cell(sel_cell_end);
430 if (rs > re) swap(rs, re);
432 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
433 int w = tabular->GetWidthOfColumn(cell);
434 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
435 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
436 w, h, LColor::selection);
441 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
445 calculate_dimensions_of_cells(bv, font, true);
447 owner()->update(bv, font, true);
450 if (the_locking_inset)
451 the_locking_inset->update(bv, font, reinit);
453 switch (need_update) {
457 if (calculate_dimensions_of_cells(bv, font, false))
469 string const InsetTabular::EditMessage() const
471 return _("Opened Tabular Inset");
475 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
477 UpdatableInset::Edit(bv, x, y, button);
479 if (!bv->lockInset(this)) {
480 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
484 the_locking_inset = 0;
488 sel_cell_start = sel_cell_end = actcell;
489 bv->text->FinishUndo();
490 if (InsetHit(bv, x, y) && (button != 3)) {
491 ActivateCellInsetAbs(bv, x, y, button);
493 // UpdateLocal(bv, NONE, false);
494 // bv->getOwner()->getPopups().updateFormTabular();
498 void InsetTabular::InsetUnlock(BufferView * bv)
500 if (the_locking_inset) {
501 the_locking_inset->InsetUnlock(bv);
502 the_locking_inset = 0;
505 no_selection = false;
508 if (scroll(false) || hasSelection()) {
509 sel_cell_start = sel_cell_end = 0;
513 UpdateLocal(bv, FULL, false);
518 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
519 bool mark_dirty) const
521 if (need_update < what) // only set this if it has greater update
523 if ((what == INIT) && hasSelection())
526 if (need_update != NONE) {
527 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
528 if (locked) // && (what != NONE))
534 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
536 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
541 if (inset == tabular->GetCellInset(actcell)) {
542 lyxerr[Debug::INSETS] << "OK" << endl;
543 the_locking_inset = tabular->GetCellInset(actcell);
545 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
546 inset_y = cursor.y();
548 } else if (the_locking_inset && (the_locking_inset == inset)) {
549 lyxerr[Debug::INSETS] << "OK" << endl;
551 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
552 inset_y = cursor.y();
553 } else if (the_locking_inset) {
554 lyxerr[Debug::INSETS] << "MAYBE" << endl;
555 return the_locking_inset->LockInsetInInset(bv, inset);
557 lyxerr[Debug::INSETS] << "NOT OK" << endl;
562 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
565 if (!the_locking_inset)
567 if (the_locking_inset == inset) {
568 the_locking_inset->InsetUnlock(bv);
569 the_locking_inset = 0;
573 UpdateLocal(bv, CELL, false);
574 ShowInsetCursor(bv, false);
577 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
578 if (inset->LyxCode() == TABULAR_CODE &&
579 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
580 bv->owner()->getDialogs()->updateTabular(this);
589 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
591 if (!the_locking_inset)
593 if (the_locking_inset != inset) {
594 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
597 UpdateLocal(bv, CELL, false);
602 unsigned int InsetTabular::InsetInInsetY()
604 if (!the_locking_inset)
607 return inset_y + the_locking_inset->InsetInInsetY();
611 UpdatableInset * InsetTabular::GetLockingInset()
613 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
617 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
621 if (the_locking_inset)
622 return the_locking_inset->GetFirstLockingInsetOfType(c);
627 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
629 if (the_locking_inset)
630 return the_locking_inset->InsertInset(bv, inset);
635 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
637 if (hasSelection() && (button == 3))
640 if (hasSelection()) {
642 UpdateLocal(bv, SELECTION, false);
645 no_selection = false;
647 int const ocell = actcell;
648 int const orow = actrow;
653 UpdateLocal(bv, NONE, false);
654 sel_cell_start = sel_cell_end = actcell;
656 if ((ocell != actcell) && the_locking_inset) {
657 the_locking_inset->InsetUnlock(bv);
658 the_locking_inset = 0;
664 bool const inset_hit = InsetHit(bv, x, y);
666 if ((ocell == actcell) && the_locking_inset && inset_hit) {
668 the_locking_inset->InsetButtonPress(bv,
669 x - inset_x, y - inset_y,
672 } else if (the_locking_inset) {
673 the_locking_inset->InsetUnlock(bv);
675 the_locking_inset = 0;
677 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
680 if (inset_hit && bv->theLockingInset()) {
681 if (ActivateCellInsetAbs(bv, x, y, button))
682 the_locking_inset->InsetButtonPress(bv,
692 void InsetTabular::InsetButtonRelease(BufferView * bv,
693 int x, int y, int button)
696 if (the_locking_inset) {
698 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
699 i->InsetButtonRelease(bv, x, y, button);
703 bv->owner()->getDialogs()->showTabular(this);
706 if (the_locking_inset) {
707 the_locking_inset->InsetButtonRelease(bv,
708 x - inset_x, y - inset_y,
712 no_selection = false;
716 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
718 if (the_locking_inset) {
719 the_locking_inset->InsetMotionNotify(bv,
727 int const old_cell = actcell;
730 sel_cell_end = actcell;
731 if (sel_cell_end != old_cell)
732 UpdateLocal(bv, SELECTION, false);
735 no_selection = false;
739 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
741 if (the_locking_inset) {
742 the_locking_inset->InsetKeyPress(xke);
748 UpdatableInset::RESULT
749 InsetTabular::LocalDispatch(BufferView * bv,
753 // We need to save the value of the_locking_inset as the call to
754 // the_locking_inset->LocalDispatch might unlock it.
755 old_locking_inset = the_locking_inset;
756 no_selection = false;
757 UpdatableInset::RESULT result =
758 UpdatableInset::LocalDispatch(bv, action, arg);
759 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
764 if ((action < 0) && arg.empty())
767 bool hs = hasSelection();
770 // this one have priority over the locked InsetText!
776 if (the_locking_inset) {
777 UnlockInsetInInset(bv, the_locking_inset);
778 the_locking_inset = 0;
780 if (action == LFUN_TAB)
781 moveNextCell(bv, old_locking_inset != 0);
783 movePrevCell(bv, old_locking_inset != 0);
784 sel_cell_start = sel_cell_end = actcell;
786 UpdateLocal(bv, SELECTION, false);
787 if (!the_locking_inset) {
789 return DISPATCHED_NOUPDATE;
793 // this to avoid compiler warnings.
798 if (the_locking_inset) {
799 result=the_locking_inset->LocalDispatch(bv, action, arg);
800 if (result == DISPATCHED_NOUPDATE) {
803 if (sc != scroll()) { // inset has been scrolled
804 the_locking_inset->ToggleInsetCursor(bv);
805 UpdateLocal(bv, FULL, false);
806 the_locking_inset->ToggleInsetCursor(bv);
809 } else if (result == DISPATCHED) {
810 the_locking_inset->ToggleInsetCursor(bv);
811 UpdateLocal(bv, CELL, false);
812 the_locking_inset->ToggleInsetCursor(bv);
814 } else if (result == FINISHED) {
821 // --- Cursor Movements ----------------------------------
823 if (tabular->IsLastCellInRow(actcell))
825 moveRight(bv, false);
826 sel_cell_end = actcell;
827 UpdateLocal(bv, SELECTION, false);
830 result = moveRight(bv);
831 sel_cell_start = sel_cell_end = actcell;
833 UpdateLocal(bv, SELECTION, false);
836 if (tabular->IsFirstCellInRow(actcell))
839 sel_cell_end = actcell;
840 UpdateLocal(bv, SELECTION, false);
843 result = moveLeft(bv);
844 sel_cell_start = sel_cell_end = actcell;
846 UpdateLocal(bv, SELECTION, false);
850 int const ocell = actcell;
852 if ((ocell == sel_cell_end) ||
853 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
854 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
856 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
857 UpdateLocal(bv, SELECTION, false);
861 result = moveDown(bv, old_locking_inset != 0);
862 sel_cell_start = sel_cell_end = actcell;
864 UpdateLocal(bv, SELECTION, false);
868 int const ocell = actcell;
870 if ((ocell == sel_cell_end) ||
871 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
872 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
874 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
875 UpdateLocal(bv, SELECTION, false);
879 result = moveUp(bv, old_locking_inset != 0);
880 sel_cell_start = sel_cell_end = actcell;
882 UpdateLocal(bv, SELECTION, false);
886 if (the_locking_inset) {
887 UnlockInsetInInset(bv, the_locking_inset);
888 the_locking_inset = 0;
890 if (bv->text->first + bv->painter().paperHeight() <
891 (top_baseline + tabular->GetHeightOfTabular()))
893 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
894 UpdateLocal(bv, FULL, false);
895 actcell = tabular->GetCellBelow(first_visible_cell) + column;
897 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
900 UpdateLocal(bv, CURSOR, false);
905 if (the_locking_inset) {
906 UnlockInsetInInset(bv, the_locking_inset);
907 the_locking_inset = 0;
909 if (top_baseline < 0) {
910 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
911 UpdateLocal(bv, FULL, false);
912 if (top_baseline > 0)
915 actcell = tabular->GetCellBelow(first_visible_cell) + column;
920 UpdateLocal(bv, CURSOR, false);
931 case LFUN_LAYOUT_TABULAR:
933 bv->owner()->getDialogs()->showTabular(this);
936 case LFUN_TABULAR_FEATURE:
937 if (!TabularFeatures(bv, arg))
938 result = UNDISPATCHED;
941 if (!copySelection(bv))
943 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
945 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
946 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
948 bv->text->cursor.par()->previous(),
949 bv->text->cursor.par()->next()
953 UpdateLocal(bv, INIT, true);
958 bv->text->FinishUndo();
961 case LFUN_PASTESELECTION:
963 string clip(bv->workarea()->getClipboard());
967 if (clip.find('\t') != string::npos) {
971 unsigned int len = clip.length();
972 string::size_type p = 0;
975 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
983 maxCols = max(cols, maxCols);
989 maxCols = max(cols, maxCols);
990 delete paste_tabular;
991 paste_tabular = new LyXTabular(this, rows, maxCols);
992 string::size_type op = 0;
994 int cells = paste_tabular->GetNumberOfCells();
996 while((cell < cells) && (p < len) &&
997 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1002 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1007 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1008 while(cols++ < maxCols)
1016 // check for the last cell if there is no trailing '\n'
1017 if ((cell < cells) && (op < len))
1018 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1020 // so that the clipboard is used and it goes on
1022 // and executes LFUN_PASTESELECTION in insettext!
1023 delete paste_tabular;
1028 if (hasPasteBuffer()) {
1029 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1031 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1032 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1034 bv->text->cursor.par()->previous(),
1035 bv->text->cursor.par()->next()
1039 UpdateLocal(bv, INIT, true);
1042 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1044 // we try to activate the actual inset and put this event down to
1045 // the insets dispatch function.
1046 result = UNDISPATCHED;
1047 if (the_locking_inset)
1050 if (ActivateCellInset(bv)) {
1051 result = the_locking_inset->LocalDispatch(bv, action, arg);
1052 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1053 UnlockInsetInInset(bv, the_locking_inset);
1055 the_locking_inset = 0;
1056 return UNDISPATCHED;
1059 // the_locking_inset->ToggleInsetCursor(bv);
1060 UpdateLocal(bv, CELL, false);
1061 // the_locking_inset->ToggleInsetCursor(bv);
1066 if (result!=FINISHED) {
1067 if (!the_locking_inset) {
1068 ShowInsetCursor(bv);
1071 bv->unlockInset(this);
1076 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1077 bool fragile, bool fp) const
1079 return tabular->Latex(buf, os, fragile, fp);
1083 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1085 // This should be changed to a real ascii export
1086 return tabular->Ascii(buf, os);
1090 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1096 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1098 return tabular->DocBook(buf,os);
1102 void InsetTabular::Validate(LaTeXFeatures & features) const
1104 tabular->Validate(features);
1108 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1109 LyXFont const & font,
1116 bool changed = false;
1118 // if we have a locking_inset we should have to check only this cell for
1119 // change so I'll try this to have a boost, but who knows ;)
1120 if ((need_update != INIT) &&
1121 (the_locking_inset == tabular->GetCellInset(actcell))) {
1122 for(int i = 0; i < tabular->columns(); ++i) {
1123 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1125 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1128 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1129 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1130 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1133 for (int i = 0; i < tabular->rows(); ++i) {
1136 for (int j= 0; j < tabular->columns(); ++j) {
1137 if (tabular->IsPartOfMultiColumn(i,j))
1140 inset = tabular->GetCellInset(cell);
1142 inset->update(bv, font, false);
1143 maxAsc = max(maxAsc, inset->ascent(bv, font));
1144 maxDesc = max(maxDesc, inset->descent(bv, font));
1145 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1147 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1148 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1154 void InsetTabular::GetCursorPos(BufferView *,
1155 int & x, int & y) const
1157 x = cursor.x() - top_x;
1162 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1164 if (the_locking_inset) {
1165 the_locking_inset->ToggleInsetCursor(bv);
1169 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1171 int const asc = lyxfont::maxAscent(font);
1172 int const desc = lyxfont::maxDescent(font);
1174 if (isCursorVisible())
1175 bv->hideLockedInsetCursor();
1177 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1178 toggleCursorVisible();
1182 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1184 if (!isCursorVisible()) {
1185 LyXFont font; // = GetFont(par, cursor.pos);
1187 int const asc = lyxfont::maxAscent(font);
1188 int const desc = lyxfont::maxDescent(font);
1189 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1191 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1192 setCursorVisible(true);
1197 void InsetTabular::HideInsetCursor(BufferView * bv)
1199 if (isCursorVisible()) {
1200 bv->hideLockedInsetCursor();
1201 setCursorVisible(false);
1203 // if (cursor_visible)
1204 // ToggleInsetCursor(bv);
1208 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1212 actcell = actrow = actcol = 0;
1213 int ly = tabular->GetDescentOfRow(actrow);
1215 // first search the right row
1216 while((ly < y) && (actrow < tabular->rows())) {
1217 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1218 tabular->GetAscentOfRow(actrow + 1) +
1219 tabular->GetAdditionalHeight(actrow + 1));
1221 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1223 actcell = tabular->GetCellNumber(actrow, actcol);
1225 // now search the right column
1226 int lx = tabular->GetWidthOfColumn(actcell) -
1227 tabular->GetAdditionalWidth(actcell);
1229 #ifdef WITH_WARNINGS
1230 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1232 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1233 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1234 tabular->GetAdditionalWidth(actcell - 1));
1236 // Jürgen, you should check that this is correct. (Lgb)
1237 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1238 lx += tabular->GetWidthOfColumn(actcell + 1)
1239 + tabular->GetAdditionalWidth(actcell);
1243 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1248 int InsetTabular::getCellXPos(int cell) const
1252 for (; !tabular->IsFirstCellInRow(c); --c)
1254 int lx = tabular->GetWidthOfColumn(cell);
1255 for (; c < cell; ++c) {
1256 lx += tabular->GetWidthOfColumn(c);
1258 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1262 void InsetTabular::resetPos(BufferView * bv) const
1266 actcol = tabular->column_of_cell(actcell);
1271 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1272 if (tabular->IsLastCellInRow(cell)) {
1273 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1274 tabular->GetAscentOfRow(actrow + 1) +
1275 tabular->GetAdditionalHeight(actrow + 1));
1279 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1280 int new_x = getCellXPos(actcell);
1281 int old_x = cursor.x();
1284 // cursor.x(getCellXPos(actcell) + offset);
1285 if (scroll(false) && (tabular->GetWidthOfTabular() < bv->workWidth()-20)) {
1287 UpdateLocal(bv, FULL, false);
1288 } else if (the_locking_inset &&
1289 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20)) {
1290 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1291 if (xx > (bv->workWidth()-20))
1292 scroll(bv, -(xx - bv->workWidth() + 60));
1300 UpdateLocal(bv, FULL, false);
1301 } else if ((cursor.x() - offset) > 20 &&
1302 (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1303 > (bv->workWidth() - 20)) {
1304 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1305 UpdateLocal(bv, FULL, false);
1306 } else if ((cursor.x() - offset) < 20) {
1307 scroll(bv, 20 - cursor.x() + offset);
1308 UpdateLocal(bv, FULL, false);
1309 } else if (scroll(false) && top_x > 20 &&
1310 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1311 scroll(bv, old_x - cursor.x());
1312 UpdateLocal(bv, FULL, false);
1314 if ((!the_locking_inset ||
1315 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1316 actcell != oldcell) {
1317 InsetTabular * inset = const_cast<InsetTabular *>(this);
1318 bv->owner()->getDialogs()->updateTabular(inset);
1324 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1326 if (lock && !old_locking_inset) {
1327 if (ActivateCellInset(bv))
1330 bool moved = isRightToLeft(bv)
1331 ? movePrevCell(bv) : moveNextCell(bv);
1334 if (lock && ActivateCellInset(bv))
1338 return DISPATCHED_NOUPDATE;
1342 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1344 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1347 if (lock) { // behind the inset
1348 if (ActivateCellInset(bv, 0, 0, 0, true))
1352 return DISPATCHED_NOUPDATE;
1356 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1358 int const ocell = actcell;
1359 actcell = tabular->GetCellAbove(actcell);
1360 if (actcell == ocell) // we moved out of the inset
1366 if (old_locking_inset) {
1367 old_locking_inset->GetCursorPos(bv, x, y);
1368 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1370 if (ActivateCellInset(bv, x, 0))
1373 return DISPATCHED_NOUPDATE;
1377 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1379 int const ocell = actcell;
1380 actcell = tabular->GetCellBelow(actcell);
1381 if (actcell == ocell) // we moved out of the inset
1387 if (old_locking_inset) {
1388 old_locking_inset->GetCursorPos(bv, x, y);
1389 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1391 if (ActivateCellInset(bv, x, 0))
1394 return DISPATCHED_NOUPDATE;
1398 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1400 if (isRightToLeft(bv)) {
1401 if (tabular->IsFirstCellInRow(actcell)) {
1402 int row = tabular->row_of_cell(actcell);
1403 if (row == tabular->rows() - 1)
1405 actcell = tabular->GetLastCellInRow(row);
1406 actcell = tabular->GetCellBelow(actcell);
1413 if (tabular->IsLastCell(actcell))
1418 bool rtl = tabular->GetCellInset(actcell)->par->
1419 isRightToLeftPar(bv->buffer()->params);
1420 ActivateCellInset(bv, 0, 0, 0, !rtl);
1427 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1429 if (isRightToLeft(bv)) {
1430 if (tabular->IsLastCellInRow(actcell)) {
1431 int row = tabular->row_of_cell(actcell);
1434 actcell = tabular->GetFirstCellInRow(row);
1435 actcell = tabular->GetCellAbove(actcell);
1437 if (tabular->IsLastCell(actcell))
1442 if (!actcell) // first cell
1447 bool rtl = tabular->GetCellInset(actcell)->par->
1448 isRightToLeftPar(bv->buffer()->params);
1449 ActivateCellInset(bv, 0, 0, 0, !rtl);
1456 bool InsetTabular::Delete()
1462 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1464 if (the_locking_inset)
1465 the_locking_inset->SetFont(bv, font, tall);
1469 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1471 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1474 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1475 string const tmp = tabularFeatures[i].feature;
1477 if (tmp == what.substr(0, tmp.length())) {
1478 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1479 //tabularFeatures[i].feature.length())) {
1480 action = tabularFeatures[i].action;
1484 if (action == LyXTabular::LAST_ACTION)
1488 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1489 TabularFeatures(bv, action, val);
1494 void InsetTabular::TabularFeatures(BufferView * bv,
1495 LyXTabular::Feature feature,
1496 string const & value)
1504 bool setLines = false;
1505 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1506 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1511 case LyXTabular::M_ALIGN_LEFT:
1512 case LyXTabular::ALIGN_LEFT:
1513 setAlign=LYX_ALIGN_LEFT;
1515 case LyXTabular::M_ALIGN_RIGHT:
1516 case LyXTabular::ALIGN_RIGHT:
1517 setAlign=LYX_ALIGN_RIGHT;
1519 case LyXTabular::M_ALIGN_CENTER:
1520 case LyXTabular::ALIGN_CENTER:
1521 setAlign=LYX_ALIGN_CENTER;
1523 case LyXTabular::M_VALIGN_TOP:
1524 case LyXTabular::VALIGN_TOP:
1525 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1527 case LyXTabular::M_VALIGN_BOTTOM:
1528 case LyXTabular::VALIGN_BOTTOM:
1529 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1531 case LyXTabular::M_VALIGN_CENTER:
1532 case LyXTabular::VALIGN_CENTER:
1533 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1538 if (hasSelection()) {
1539 sel_col_start = tabular->column_of_cell(sel_cell_start);
1540 sel_col_end = tabular->column_of_cell(sel_cell_end);
1541 if (sel_col_start > sel_col_end) {
1542 sel_col_end = sel_col_start;
1543 sel_col_start = tabular->column_of_cell(sel_cell_end);
1545 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1548 sel_row_start = tabular->row_of_cell(sel_cell_start);
1549 sel_row_end = tabular->row_of_cell(sel_cell_end);
1550 if (sel_row_start > sel_row_end) {
1551 //int tmp = sel_row_start;
1552 //sel_row_start = sel_row_end;
1553 //sel_row_end = tmp;
1554 swap(sel_row_start, sel_row_end);
1557 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1558 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1560 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1562 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1563 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1565 bv->text->cursor.par()->previous(),
1566 bv->text->cursor.par()->next()
1570 int row = tabular->row_of_cell(actcell);
1571 int column = tabular->column_of_cell(actcell);
1575 case LyXTabular::SET_PWIDTH:
1577 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1578 tabular->SetColumnPWidth(actcell,value);
1580 for (int i = 0; i < tabular->rows(); ++i) {
1581 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1584 UpdateLocal(bv, INIT, true);
1588 case LyXTabular::SET_MPWIDTH:
1590 bool const update = (tabular->GetPWidth(actcell) != value);
1591 tabular->SetMColumnPWidth(actcell,value);
1593 for (int i = 0; i < tabular->rows(); ++i) {
1594 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1597 UpdateLocal(bv, INIT, true);
1601 case LyXTabular::SET_SPECIAL_COLUMN:
1602 case LyXTabular::SET_SPECIAL_MULTI:
1603 tabular->SetAlignSpecial(actcell,value,feature);
1605 case LyXTabular::APPEND_ROW:
1606 // append the row into the tabular
1607 UnlockInsetInInset(bv, the_locking_inset);
1608 tabular->AppendRow(actcell);
1609 UpdateLocal(bv, INIT, true);
1611 case LyXTabular::APPEND_COLUMN:
1612 // append the column into the tabular
1613 UnlockInsetInInset(bv, the_locking_inset);
1614 tabular->AppendColumn(actcell);
1615 actcell = tabular->GetCellNumber(row, column);
1616 UpdateLocal(bv, INIT, true);
1618 case LyXTabular::DELETE_ROW:
1619 UnlockInsetInInset(bv, the_locking_inset);
1620 tabular->DeleteRow(tabular->row_of_cell(actcell));
1621 if ((row+1) > tabular->rows())
1623 actcell = tabular->GetCellNumber(row, column);
1625 UpdateLocal(bv, INIT, true);
1627 case LyXTabular::DELETE_COLUMN:
1628 UnlockInsetInInset(bv, the_locking_inset);
1629 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1630 if ((column+1) > tabular->columns())
1632 actcell = tabular->GetCellNumber(row, column);
1634 UpdateLocal(bv, INIT, true);
1636 case LyXTabular::M_TOGGLE_LINE_TOP:
1638 case LyXTabular::TOGGLE_LINE_TOP:
1640 bool lineSet = !tabular->TopLine(actcell, flag);
1641 for (int i = sel_row_start; i <= sel_row_end; ++i)
1642 for (int j = sel_col_start; j <= sel_col_end; ++j)
1643 tabular->SetTopLine(
1644 tabular->GetCellNumber(i, j),
1646 UpdateLocal(bv, INIT, true);
1650 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1652 case LyXTabular::TOGGLE_LINE_BOTTOM:
1654 bool lineSet = !tabular->BottomLine(actcell, flag);
1655 for (int i = sel_row_start; i <= sel_row_end; ++i)
1656 for (int j = sel_col_start; j <= sel_col_end; ++j)
1657 tabular->SetBottomLine(
1658 tabular->GetCellNumber(i, j),
1661 UpdateLocal(bv, INIT, true);
1665 case LyXTabular::M_TOGGLE_LINE_LEFT:
1667 case LyXTabular::TOGGLE_LINE_LEFT:
1669 bool lineSet = !tabular->LeftLine(actcell, flag);
1670 for (int i = sel_row_start; i <= sel_row_end; ++i)
1671 for (int j = sel_col_start; j <= sel_col_end; ++j)
1672 tabular->SetLeftLine(
1673 tabular->GetCellNumber(i,j),
1676 UpdateLocal(bv, INIT, true);
1680 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1682 case LyXTabular::TOGGLE_LINE_RIGHT:
1684 bool lineSet = !tabular->RightLine(actcell, flag);
1685 for (int i = sel_row_start; i <= sel_row_end; ++i)
1686 for (int j = sel_col_start; j <= sel_col_end; ++j)
1687 tabular->SetRightLine(
1688 tabular->GetCellNumber(i,j),
1691 UpdateLocal(bv, INIT, true);
1695 case LyXTabular::M_ALIGN_LEFT:
1696 case LyXTabular::M_ALIGN_RIGHT:
1697 case LyXTabular::M_ALIGN_CENTER:
1699 case LyXTabular::ALIGN_LEFT:
1700 case LyXTabular::ALIGN_RIGHT:
1701 case LyXTabular::ALIGN_CENTER:
1702 for (int i = sel_row_start; i <= sel_row_end; ++i)
1703 for (int j = sel_col_start; j <= sel_col_end; ++j)
1704 tabular->SetAlignment(
1705 tabular->GetCellNumber(i, j),
1708 UpdateLocal(bv, INIT, true);
1710 case LyXTabular::M_VALIGN_TOP:
1711 case LyXTabular::M_VALIGN_BOTTOM:
1712 case LyXTabular::M_VALIGN_CENTER:
1714 case LyXTabular::VALIGN_TOP:
1715 case LyXTabular::VALIGN_BOTTOM:
1716 case LyXTabular::VALIGN_CENTER:
1717 for (int i = sel_row_start; i <= sel_row_end; ++i)
1718 for (int j = sel_col_start; j <= sel_col_end; ++j)
1719 tabular->SetVAlignment(
1720 tabular->GetCellNumber(i, j),
1722 UpdateLocal(bv, INIT, true);
1724 case LyXTabular::MULTICOLUMN:
1726 if (sel_row_start != sel_row_end) {
1727 WriteAlert(_("Impossible Operation!"),
1728 _("Multicolumns can only be horizontally."),
1732 // just multicol for one Single Cell
1733 if (!hasSelection()) {
1734 // check wether we are completly in a multicol
1735 if (tabular->IsMultiColumn(actcell)) {
1736 tabular->UnsetMultiColumn(actcell);
1737 UpdateLocal(bv, INIT, true);
1739 tabular->SetMultiColumn(actcell, 1);
1740 UpdateLocal(bv, CELL, true);
1744 // we have a selection so this means we just add all this
1745 // cells to form a multicolumn cell
1749 if (sel_cell_start > sel_cell_end) {
1750 s_start = sel_cell_end;
1751 s_end = sel_cell_start;
1753 s_start = sel_cell_start;
1754 s_end = sel_cell_end;
1756 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1758 sel_cell_end = sel_cell_start;
1759 UpdateLocal(bv, INIT, true);
1762 case LyXTabular::SET_ALL_LINES:
1764 case LyXTabular::UNSET_ALL_LINES:
1765 for (int i = sel_row_start; i <= sel_row_end; ++i)
1766 for (int j = sel_col_start; j <= sel_col_end; ++j)
1767 tabular->SetAllLines(
1768 tabular->GetCellNumber(i,j), setLines);
1769 UpdateLocal(bv, INIT, true);
1771 case LyXTabular::SET_LONGTABULAR:
1772 tabular->SetLongTabular(true);
1773 UpdateLocal(bv, INIT, true); // because this toggles displayed
1775 case LyXTabular::UNSET_LONGTABULAR:
1776 tabular->SetLongTabular(false);
1777 UpdateLocal(bv, INIT, true); // because this toggles displayed
1779 case LyXTabular::SET_ROTATE_TABULAR:
1780 tabular->SetRotateTabular(true);
1782 case LyXTabular::UNSET_ROTATE_TABULAR:
1783 tabular->SetRotateTabular(false);
1785 case LyXTabular::SET_ROTATE_CELL:
1786 for (int i = sel_row_start; i <= sel_row_end; ++i)
1787 for (int j = sel_col_start; j<=sel_col_end; ++j)
1788 tabular->SetRotateCell(
1789 tabular->GetCellNumber(i, j),
1792 case LyXTabular::UNSET_ROTATE_CELL:
1793 for (int i = sel_row_start; i <= sel_row_end; ++i)
1794 for (int j = sel_col_start; j <= sel_col_end; ++j)
1795 tabular->SetRotateCell(
1796 tabular->GetCellNumber(i, j), false);
1798 case LyXTabular::SET_USEBOX:
1800 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1801 if (val == tabular->GetUsebox(actcell))
1802 val = LyXTabular::BOX_NONE;
1803 for (int i = sel_row_start; i <= sel_row_end; ++i)
1804 for (int j = sel_col_start; j <= sel_col_end; ++j)
1806 tabular->GetCellNumber(i, j), val);
1809 case LyXTabular::SET_LTFIRSTHEAD:
1810 tabular->SetLTHead(actcell, true);
1812 case LyXTabular::SET_LTHEAD:
1813 tabular->SetLTHead(actcell, false);
1815 case LyXTabular::SET_LTFOOT:
1816 tabular->SetLTFoot(actcell, false);
1818 case LyXTabular::SET_LTLASTFOOT:
1819 tabular->SetLTFoot(actcell, true);
1821 case LyXTabular::SET_LTNEWPAGE:
1823 bool what = !tabular->GetLTNewPage(actcell);
1824 tabular->SetLTNewPage(actcell, what);
1827 // dummy stuff just to avoid warnings
1828 case LyXTabular::LAST_ACTION:
1834 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1837 UpdatableInset * inset =
1838 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1839 LyXFont font(LyXFont::ALL_SANE);
1841 x = inset->x() + inset->width(bv, font);
1842 y = inset->descent(bv, font);
1844 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1845 //inset_y = cursor.y();
1846 inset->Edit(bv, x, y, button);
1847 if (!the_locking_inset)
1849 UpdateLocal(bv, CELL, false);
1850 return (the_locking_inset != 0);
1854 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1857 inset_x = cursor.x()
1858 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1859 inset_y = cursor.y();
1860 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1864 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1867 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1871 // This returns paperWidth() if the cell-width is unlimited or the width
1872 // in pixels if we have a pwidth for this cell.
1873 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1875 string const s = tabular->GetPWidth(cell);
1879 return VSpace(s).inPixels(bv);
1883 int InsetTabular::getMaxWidth(BufferView * bv,
1884 UpdatableInset const * inset) const
1886 int const n = tabular->GetNumberOfCells();
1888 for (; cell < n; ++cell) {
1889 if (tabular->GetCellInset(cell) == inset)
1894 int w = GetMaxWidthOfCell(bv, cell);
1896 // because the inset then subtracts it's top_x and owner->x()
1897 w += (inset->x() - top_x);
1902 void InsetTabular::resizeLyXText(BufferView *) const
1908 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1909 bool const recursive) const
1911 if (the_locking_inset)
1912 return the_locking_inset->getLyXText(bv, recursive);
1913 return Inset::getLyXText(bv, recursive);
1917 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1919 if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1920 bv->owner()->getDialogs()
1921 ->showTabular(const_cast<InsetTabular *>(this));
1926 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1928 if (the_locking_inset) {
1929 InsetTabular * i = static_cast<InsetTabular *>
1930 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1932 i->OpenLayoutDialog(bv);
1936 bv->owner()->getDialogs()->showTabular(
1937 const_cast<InsetTabular *>(this));
1941 // functions returns:
1945 // 3 ... toggled off
1947 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1949 int action = LyXTabular::LAST_ACTION;
1950 LyXFunc::func_status status = LyXFunc::OK;
1953 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1954 string const tmp = tabularFeatures[i].feature;
1955 if (tmp == what.substr(0, tmp.length())) {
1956 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1957 // tabularFeatures[i].feature.length())) {
1958 action = tabularFeatures[i].action;
1962 if (action == LyXTabular::LAST_ACTION)
1963 return LyXFunc::Unknown;
1965 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1972 if (hasSelection()) {
1973 sel_row_start = tabular->row_of_cell(sel_cell_start);
1974 sel_row_end = tabular->row_of_cell(sel_cell_end);
1975 if (sel_row_start > sel_row_end) {
1976 //int tmp = sel_row_start;
1977 //sel_row_start = sel_row_end;
1978 //sel_row_end = tmp;
1979 swap(sel_row_start, sel_row_end);
1982 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1986 case LyXTabular::SET_PWIDTH:
1987 case LyXTabular::SET_MPWIDTH:
1988 case LyXTabular::SET_SPECIAL_COLUMN:
1989 case LyXTabular::SET_SPECIAL_MULTI:
1990 status |= LyXFunc::Disabled;
1993 case LyXTabular::APPEND_ROW:
1994 case LyXTabular::APPEND_COLUMN:
1995 case LyXTabular::DELETE_ROW:
1996 case LyXTabular::DELETE_COLUMN:
1997 case LyXTabular::SET_ALL_LINES:
1998 case LyXTabular::UNSET_ALL_LINES:
1999 status |= LyXFunc::OK;
2002 case LyXTabular::MULTICOLUMN:
2003 if (tabular->IsMultiColumn(actcell))
2004 status |= LyXFunc::ToggleOn;
2006 status |= LyXFunc::ToggleOff;
2008 case LyXTabular::M_TOGGLE_LINE_TOP:
2010 case LyXTabular::TOGGLE_LINE_TOP:
2011 if (tabular->TopLine(actcell, flag))
2012 status |= LyXFunc::ToggleOn;
2014 status |= LyXFunc::ToggleOff;
2016 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2018 case LyXTabular::TOGGLE_LINE_BOTTOM:
2019 if (tabular->BottomLine(actcell, flag))
2020 status |= LyXFunc::ToggleOn;
2022 status |= LyXFunc::ToggleOff;
2024 case LyXTabular::M_TOGGLE_LINE_LEFT:
2026 case LyXTabular::TOGGLE_LINE_LEFT:
2027 if (tabular->LeftLine(actcell, flag))
2028 status |= LyXFunc::ToggleOn;
2030 status |= LyXFunc::ToggleOff;
2032 case LyXTabular::M_TOGGLE_LINE_RIGHT:
2034 case LyXTabular::TOGGLE_LINE_RIGHT:
2035 if (tabular->RightLine(actcell, flag))
2036 status |= LyXFunc::ToggleOn;
2038 status |= LyXFunc::ToggleOff;
2040 case LyXTabular::M_ALIGN_LEFT:
2042 case LyXTabular::ALIGN_LEFT:
2043 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2044 status |= LyXFunc::ToggleOn;
2046 status |= LyXFunc::ToggleOff;
2048 case LyXTabular::M_ALIGN_RIGHT:
2050 case LyXTabular::ALIGN_RIGHT:
2051 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2052 status |= LyXFunc::ToggleOn;
2054 status |= LyXFunc::ToggleOff;
2056 case LyXTabular::M_ALIGN_CENTER:
2058 case LyXTabular::ALIGN_CENTER:
2059 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2060 status |= LyXFunc::ToggleOn;
2062 status |= LyXFunc::ToggleOff;
2064 case LyXTabular::M_VALIGN_TOP:
2066 case LyXTabular::VALIGN_TOP:
2067 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2068 status |= LyXFunc::ToggleOn;
2070 status |= LyXFunc::ToggleOff;
2072 case LyXTabular::M_VALIGN_BOTTOM:
2074 case LyXTabular::VALIGN_BOTTOM:
2075 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2076 status |= LyXFunc::ToggleOn;
2078 status |= LyXFunc::ToggleOff;
2080 case LyXTabular::M_VALIGN_CENTER:
2082 case LyXTabular::VALIGN_CENTER:
2083 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2084 status |= LyXFunc::ToggleOn;
2086 status |= LyXFunc::ToggleOff;
2088 case LyXTabular::SET_LONGTABULAR:
2089 if (tabular->IsLongTabular())
2090 status |= LyXFunc::ToggleOn;
2092 status |= LyXFunc::ToggleOff;
2094 case LyXTabular::UNSET_LONGTABULAR:
2095 if (!tabular->IsLongTabular())
2096 status |= LyXFunc::ToggleOn;
2098 status |= LyXFunc::ToggleOff;
2100 case LyXTabular::SET_ROTATE_TABULAR:
2101 if (tabular->GetRotateTabular())
2102 status |= LyXFunc::ToggleOn;
2104 status |= LyXFunc::ToggleOff;
2106 case LyXTabular::UNSET_ROTATE_TABULAR:
2107 if (!tabular->GetRotateTabular())
2108 status |= LyXFunc::ToggleOn;
2110 status |= LyXFunc::ToggleOff;
2112 case LyXTabular::SET_ROTATE_CELL:
2113 if (tabular->GetRotateCell(actcell))
2114 status |= LyXFunc::ToggleOn;
2116 status |= LyXFunc::ToggleOff;
2118 case LyXTabular::UNSET_ROTATE_CELL:
2119 if (!tabular->GetRotateCell(actcell))
2120 status |= LyXFunc::ToggleOn;
2122 status |= LyXFunc::ToggleOff;
2124 case LyXTabular::SET_USEBOX:
2125 if (strToInt(argument) == tabular->GetUsebox(actcell))
2126 status |= LyXFunc::ToggleOn;
2128 status |= LyXFunc::ToggleOff;
2130 case LyXTabular::SET_LTFIRSTHEAD:
2131 if (tabular->GetRowOfLTHead(actcell, dummy))
2132 status |= LyXFunc::ToggleOn;
2134 status |= LyXFunc::ToggleOff;
2136 case LyXTabular::SET_LTHEAD:
2137 if (tabular->GetRowOfLTHead(actcell, dummy))
2138 status |= LyXFunc::ToggleOn;
2140 status |= LyXFunc::ToggleOff;
2142 case LyXTabular::SET_LTFOOT:
2143 if (tabular->GetRowOfLTFoot(actcell, dummy))
2144 status |= LyXFunc::ToggleOn;
2146 status |= LyXFunc::ToggleOff;
2148 case LyXTabular::SET_LTLASTFOOT:
2149 if (tabular->GetRowOfLTFoot(actcell, dummy))
2150 status |= LyXFunc::ToggleOn;
2152 status |= LyXFunc::ToggleOff;
2154 case LyXTabular::SET_LTNEWPAGE:
2155 if (tabular->GetLTNewPage(actcell))
2156 status |= LyXFunc::ToggleOn;
2158 status |= LyXFunc::ToggleOff;
2161 status = LyXFunc::Disabled;
2168 std::vector<string> const InsetTabular::getLabelList() const
2170 return tabular->getLabelList();
2174 bool InsetTabular::copySelection(BufferView * bv)
2176 if (!hasSelection())
2178 //delete paste_tabular;
2180 //int sel_col_start;
2182 //int sel_row_start;
2185 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2186 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2187 if (sel_col_start > sel_col_end) {
2188 sel_col_start = sel_col_end;
2189 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2191 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2193 int const columns = sel_col_end - sel_col_start + 1;
2195 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2196 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2197 if (sel_row_start > sel_row_end) {
2198 //int tmp tmp = sel_row_start;
2199 //sel_row_start = sel_row_end;
2200 //sel_row_end = tmp;
2201 swap(sel_row_start, sel_row_end);
2203 int const rows = sel_row_end - sel_row_start + 1;
2205 delete paste_tabular;
2206 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2208 for (int i = 0; i < sel_row_start; ++i)
2209 paste_tabular->DeleteRow(0);
2210 while(paste_tabular->rows() > rows)
2211 paste_tabular->DeleteRow(rows);
2212 paste_tabular->SetTopLine(0, true, true);
2213 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2215 for (int i = 0; i < sel_col_start; ++i)
2216 paste_tabular->DeleteColumn(0);
2217 while (paste_tabular->columns() > columns)
2218 paste_tabular->DeleteColumn(columns);
2219 paste_tabular->SetLeftLine(0, true, true);
2220 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2224 paste_tabular->Ascii(bv->buffer(), sstr);
2225 bv->stuffClipboard(sstr.str().c_str());
2230 bool InsetTabular::pasteSelection(BufferView * bv)
2235 for (int r1 = 0, r2 = actrow;
2236 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2238 for(int c1 = 0, c2 = actcol;
2239 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2241 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2242 tabular->IsPartOfMultiColumn(r2,c2))
2244 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2248 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2252 int const n1 = paste_tabular->GetCellNumber(r1, c1);
2253 int const n2 = tabular->GetCellNumber(r2, c2);
2254 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2255 tabular->GetCellInset(n2)->setOwner(this);
2256 tabular->GetCellInset(n2)->deleteLyXText(bv);
2263 bool InsetTabular::cutSelection()
2265 if (!hasSelection())
2268 //int sel_col_start;
2270 //int sel_row_start;
2273 int sel_col_start = tabular->column_of_cell(sel_cell_start);
2274 int sel_col_end = tabular->column_of_cell(sel_cell_end);
2275 if (sel_col_start > sel_col_end) {
2276 sel_col_start = sel_col_end;
2277 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2279 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2281 int sel_row_start = tabular->row_of_cell(sel_cell_start);
2282 int sel_row_end = tabular->row_of_cell(sel_cell_end);
2283 if (sel_row_start > sel_row_end) {
2284 //int tmp = sel_row_start;
2285 //sel_row_start = sel_row_end;
2286 //sel_row_end = tmp;
2287 swap(sel_row_start, sel_row_end);
2289 if (sel_cell_start > sel_cell_end) {
2290 //int tmp = sel_cell_start;
2291 //sel_cell_start = sel_cell_end;
2292 //sel_cell_end = tmp;
2293 swap(sel_cell_start, sel_cell_end);
2295 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2296 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2297 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2304 bool InsetTabular::isRightToLeft(BufferView *bv )
2306 return bv->getParentLanguage(this)->RightToLeft();
2309 bool InsetTabular::nodraw() const
2311 if (the_locking_inset)
2312 return the_locking_inset->nodraw();
2313 return UpdatableInset::nodraw();
2316 int InsetTabular::scroll(bool recursive) const
2318 int sx = UpdatableInset::scroll(false);
2320 if (recursive && the_locking_inset)
2321 sx += the_locking_inset->scroll(recursive);
2326 bool InsetTabular::doClearArea() const
2328 return !locked || (need_update & (FULL|INIT));