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
227 if (need_update == INIT) {
228 if (calculate_dimensions_of_cells(bv, font, true))
229 bv->text->status = LyXText::CHANGED_IN_DRAW;
234 Painter & pain = bv->painter();
240 UpdatableInset::draw(bv, font, baseline, x, cleared);
243 x += static_cast<float>(scroll());
245 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
246 (top_x != int(x)) || (top_baseline != baseline))) {
247 int h = ascent(bv, font) + descent(bv, font);
248 int const tx = display() || !owner() ? 0 : top_x;
249 int w = tx ? width(bv, font) : pain.paperWidth();
250 int ty = baseline - ascent(bv, font);
254 if ((ty + h) > pain.paperHeight())
255 h = pain.paperHeight();
256 if ((top_x + w) > pain.paperWidth())
257 w = pain.paperWidth();
258 pain.fillRectangle(tx, ty, w, h);
263 top_baseline = baseline;
264 x += ADD_TO_TABULAR_WIDTH;
268 first_visible_cell = -1;
269 for (i = 0; i < tabular->rows(); ++i) {
271 cell = tabular->GetCellNumber(i, 0);
272 if (!((baseline + tabular->GetDescentOfRow(i)) > 0) &&
273 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight())
275 baseline += tabular->GetDescentOfRow(i) +
276 tabular->GetAscentOfRow(i + 1) +
277 tabular->GetAdditionalHeight(i + 1);
280 for (j = 0; j < tabular->columns(); ++j) {
281 if (nx > bv->workWidth())
283 if (tabular->IsPartOfMultiColumn(i, j))
285 cx = nx + tabular->GetBeginningOfTextInCell(cell);
286 if (first_visible_cell < 0)
287 first_visible_cell = cell;
289 DrawCellSelection(pain, nx, baseline, i, j, cell);
290 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
292 DrawCellLines(pain, nx, baseline, i, cell);
293 nx += tabular->GetWidthOfColumn(cell);
296 baseline += tabular->GetDescentOfRow(i) +
297 tabular->GetAscentOfRow(i + 1) +
298 tabular->GetAdditionalHeight(i + 1);
300 } else if (need_update == CELL) {
303 if (the_locking_inset &&
304 tabular->GetCellInset(actcell) != the_locking_inset)
306 Inset * inset = tabular->GetCellInset(cell);
308 inset != the_locking_inset && i < tabular->rows();
311 inset != the_locking_inset
312 && j < tabular->columns();
314 if (tabular->IsPartOfMultiColumn(i, j))
316 nx += tabular->GetWidthOfColumn(cell);
318 inset = tabular->GetCellInset(cell);
320 if (tabular->row_of_cell(cell) > i) {
322 baseline += tabular->GetDescentOfRow(i) +
323 tabular->GetAscentOfRow(i + 1) +
324 tabular->GetAdditionalHeight(i + 1);
328 // copute baseline for actual row
329 for (i = 0; i < actrow; ++i) {
330 baseline += tabular->GetDescentOfRow(i) +
331 tabular->GetAscentOfRow(i + 1) +
332 tabular->GetAdditionalHeight(i + 1);
334 // now compute the right x position
335 cell = tabular->GetCellNumber(actrow, 0);
336 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
337 if (tabular->IsPartOfMultiColumn(actrow, j))
339 nx += tabular->GetWidthOfColumn(cell);
343 i = tabular->row_of_cell(cell);
344 if (the_locking_inset != tabular->GetCellInset(cell)) {
345 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
348 float dx = nx + tabular->GetBeginningOfTextInCell(cell);
350 //cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
351 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
353 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
356 // clear only if we didn't have a change
357 if (need_update == CELL) {
358 // clear before the inset
361 baseline - tabular->GetAscentOfRow(i) + 1,
363 tabular->GetAscentOfRow(i) +
364 tabular->GetDescentOfRow(i) - 1);
365 // clear behind the inset
367 int(cx + the_locking_inset->width(bv,font) + 1),
368 baseline - tabular->GetAscentOfRow(i) + 1,
369 tabular->GetWidthOfColumn(cell) -
370 tabular->GetBeginningOfTextInCell(cell) -
371 the_locking_inset->width(bv,font) -
372 tabular->GetAdditionalWidth(cell) - 1,
373 tabular->GetAscentOfRow(i) +
374 tabular->GetDescentOfRow(i) - 1);
377 x -= ADD_TO_TABULAR_WIDTH;
378 x += width(bv, font);
379 if (bv->text->status == LyXText::CHANGED_IN_DRAW) {
381 for(Inset * inset=owner();inset;++i)
382 inset = inset->owner();
383 if (calculate_dimensions_of_cells(bv, font, false))
391 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
392 int row, int cell) const
394 int x2 = x + tabular->GetWidthOfColumn(cell);
397 if (!tabular->TopAlreadyDrawed(cell)) {
398 on_off = !tabular->TopLine(cell);
399 pain.line(x, baseline - tabular->GetAscentOfRow(row),
400 x2, baseline - tabular->GetAscentOfRow(row),
401 on_off ? LColor::tabularonoffline : LColor::tabularline,
402 on_off ? Painter::line_onoffdash : Painter::line_solid);
404 on_off = !tabular->BottomLine(cell);
405 pain.line(x,baseline + tabular->GetDescentOfRow(row),
406 x2, baseline + tabular->GetDescentOfRow(row),
407 on_off ? LColor::tabularonoffline : LColor::tabularline,
408 on_off ? Painter::line_onoffdash : Painter::line_solid);
409 if (!tabular->LeftAlreadyDrawed(cell)) {
410 on_off = !tabular->LeftLine(cell);
411 pain.line(x, baseline - tabular->GetAscentOfRow(row),
412 x, baseline + tabular->GetDescentOfRow(row),
413 on_off ? LColor::tabularonoffline : LColor::tabularline,
414 on_off ? Painter::line_onoffdash : Painter::line_solid);
416 on_off = !tabular->RightLine(cell);
417 pain.line(x2 - tabular->GetAdditionalWidth(cell),
418 baseline - tabular->GetAscentOfRow(row),
419 x2 - tabular->GetAdditionalWidth(cell),
420 baseline + tabular->GetDescentOfRow(row),
421 on_off ? LColor::tabularonoffline : LColor::tabularline,
422 on_off ? Painter::line_onoffdash : Painter::line_solid);
426 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
427 int row, int column, int cell) const
429 int cs = tabular->column_of_cell(sel_cell_start);
430 int ce = tabular->column_of_cell(sel_cell_end);
433 cs = tabular->column_of_cell(sel_cell_end);
435 ce = tabular->right_column_of_cell(sel_cell_end);
438 int rs = tabular->row_of_cell(sel_cell_start);
439 int re = tabular->row_of_cell(sel_cell_end);
440 if (rs > re) swap(rs, re);
442 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
443 int w = tabular->GetWidthOfColumn(cell);
444 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
445 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
446 w, h, LColor::selection);
451 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
455 calculate_dimensions_of_cells(bv, font, true);
457 owner()->update(bv, font, true);
460 if (the_locking_inset)
461 the_locking_inset->update(bv, font, reinit);
463 switch (need_update) {
467 if (calculate_dimensions_of_cells(bv, font, false))
479 string const InsetTabular::EditMessage() const
481 return _("Opened Tabular Inset");
485 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
487 UpdatableInset::Edit(bv, x, y, button);
489 if (!bv->lockInset(this)) {
490 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
494 the_locking_inset = 0;
498 sel_cell_start = sel_cell_end = actcell;
499 bv->text->FinishUndo();
500 if (InsetHit(bv, x, y) && (button != 3)) {
501 ActivateCellInsetAbs(bv, x, y, button);
503 // UpdateLocal(bv, NONE, false);
504 // bv->getOwner()->getPopups().updateFormTabular();
508 void InsetTabular::InsetUnlock(BufferView * bv)
510 if (the_locking_inset) {
511 the_locking_inset->InsetUnlock(bv);
512 the_locking_inset = 0;
515 no_selection = false;
518 if (scroll(false) || hasSelection()) {
519 sel_cell_start = sel_cell_end = 0;
523 UpdateLocal(bv, FULL, false);
528 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
529 bool mark_dirty) const
531 if (need_update < what) // only set this if it has greater update
533 if ((what == INIT) && hasSelection())
536 if (need_update != NONE) {
537 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
538 if (locked) // && (what != NONE))
544 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
546 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset("
551 if (inset == tabular->GetCellInset(actcell)) {
552 lyxerr[Debug::INSETS] << "OK" << endl;
553 the_locking_inset = tabular->GetCellInset(actcell);
555 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
556 inset_y = cursor.y();
558 } else if (the_locking_inset && (the_locking_inset == inset)) {
559 lyxerr[Debug::INSETS] << "OK" << endl;
561 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
562 inset_y = cursor.y();
563 } else if (the_locking_inset) {
564 lyxerr[Debug::INSETS] << "MAYBE" << endl;
565 return the_locking_inset->LockInsetInInset(bv, inset);
567 lyxerr[Debug::INSETS] << "NOT OK" << endl;
572 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
575 if (!the_locking_inset)
577 if (the_locking_inset == inset) {
578 the_locking_inset->InsetUnlock(bv);
579 the_locking_inset = 0;
583 UpdateLocal(bv, CELL, false);
584 ShowInsetCursor(bv, false);
587 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
588 if (inset->LyxCode() == TABULAR_CODE &&
589 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) {
590 bv->owner()->getDialogs()->updateTabular(this);
599 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
601 if (!the_locking_inset)
603 if (the_locking_inset != inset) {
604 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
607 UpdateLocal(bv, CELL, false);
612 unsigned int InsetTabular::InsetInInsetY()
614 if (!the_locking_inset)
617 return inset_y + the_locking_inset->InsetInInsetY();
621 UpdatableInset * InsetTabular::GetLockingInset()
623 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
627 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
631 if (the_locking_inset)
632 return the_locking_inset->GetFirstLockingInsetOfType(c);
637 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
639 if (the_locking_inset)
640 return the_locking_inset->InsertInset(bv, inset);
645 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
647 if (hasSelection() && (button == 3))
650 if (hasSelection()) {
652 UpdateLocal(bv, SELECTION, false);
655 no_selection = false;
657 int const ocell = actcell;
658 int const orow = actrow;
663 UpdateLocal(bv, NONE, false);
664 sel_cell_start = sel_cell_end = actcell;
666 if ((ocell != actcell) && the_locking_inset) {
667 the_locking_inset->InsetUnlock(bv);
668 the_locking_inset = 0;
674 bool const inset_hit = InsetHit(bv, x, y);
676 if ((ocell == actcell) && the_locking_inset && inset_hit) {
678 the_locking_inset->InsetButtonPress(bv,
679 x - inset_x, y - inset_y,
682 } else if (the_locking_inset) {
683 the_locking_inset->InsetUnlock(bv);
685 the_locking_inset = 0;
687 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
690 if (inset_hit && bv->theLockingInset()) {
691 if (ActivateCellInsetAbs(bv, x, y, button))
692 the_locking_inset->InsetButtonPress(bv,
702 void InsetTabular::InsetButtonRelease(BufferView * bv,
703 int x, int y, int button)
706 if (the_locking_inset) {
708 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
709 i->InsetButtonRelease(bv, x, y, button);
713 bv->owner()->getDialogs()->showTabular(this);
716 if (the_locking_inset) {
717 the_locking_inset->InsetButtonRelease(bv,
718 x - inset_x, y - inset_y,
722 no_selection = false;
726 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
728 if (the_locking_inset) {
729 the_locking_inset->InsetMotionNotify(bv,
737 int const old_cell = actcell;
740 sel_cell_end = actcell;
741 if (sel_cell_end != old_cell)
742 UpdateLocal(bv, SELECTION, false);
745 no_selection = false;
749 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
751 if (the_locking_inset) {
752 the_locking_inset->InsetKeyPress(xke);
758 UpdatableInset::RESULT
759 InsetTabular::LocalDispatch(BufferView * bv,
763 // We need to save the value of the_locking_inset as the call to
764 // the_locking_inset->LocalDispatch might unlock it.
765 old_locking_inset = the_locking_inset;
766 no_selection = false;
767 UpdatableInset::RESULT result =
768 UpdatableInset::LocalDispatch(bv, action, arg);
769 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
774 if ((action < 0) && arg.empty())
777 bool hs = hasSelection();
780 // this one have priority over the locked InsetText!
785 if (GetFirstLockingInsetOfType(Inset::TABULAR_CODE))
788 if (the_locking_inset) {
789 UnlockInsetInInset(bv, the_locking_inset);
790 the_locking_inset = 0;
792 if (action == LFUN_TAB)
793 moveNextCell(bv, old_locking_inset != 0);
795 movePrevCell(bv, old_locking_inset != 0);
796 sel_cell_start = sel_cell_end = actcell;
798 UpdateLocal(bv, SELECTION, false);
799 if (!the_locking_inset) {
801 return DISPATCHED_NOUPDATE;
805 // this to avoid compiler warnings.
810 if (the_locking_inset) {
811 result=the_locking_inset->LocalDispatch(bv, action, arg);
812 if (result == DISPATCHED_NOUPDATE) {
815 if (sc != scroll()) { // inset has been scrolled
816 the_locking_inset->ToggleInsetCursor(bv);
817 UpdateLocal(bv, FULL, false);
818 the_locking_inset->ToggleInsetCursor(bv);
821 } else if (result == DISPATCHED) {
822 the_locking_inset->ToggleInsetCursor(bv);
823 UpdateLocal(bv, CELL, false);
824 the_locking_inset->ToggleInsetCursor(bv);
826 } else if (result == FINISHED) {
833 // --- Cursor Movements ----------------------------------
835 if (tabular->IsLastCellInRow(actcell))
837 moveRight(bv, false);
838 sel_cell_end = actcell;
839 UpdateLocal(bv, SELECTION, false);
842 result = moveRight(bv);
843 sel_cell_start = sel_cell_end = actcell;
845 UpdateLocal(bv, SELECTION, false);
848 if (tabular->IsFirstCellInRow(actcell))
851 sel_cell_end = actcell;
852 UpdateLocal(bv, SELECTION, false);
855 result = moveLeft(bv);
856 sel_cell_start = sel_cell_end = actcell;
858 UpdateLocal(bv, SELECTION, false);
862 int const ocell = actcell;
864 if ((ocell == sel_cell_end) ||
865 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
866 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
868 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
869 UpdateLocal(bv, SELECTION, false);
873 result = moveDown(bv, old_locking_inset != 0);
874 sel_cell_start = sel_cell_end = actcell;
876 UpdateLocal(bv, SELECTION, false);
880 int const ocell = actcell;
882 if ((ocell == sel_cell_end) ||
883 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
884 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
886 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
887 UpdateLocal(bv, SELECTION, false);
891 result = moveUp(bv, old_locking_inset != 0);
892 sel_cell_start = sel_cell_end = actcell;
894 UpdateLocal(bv, SELECTION, false);
898 if (the_locking_inset) {
899 UnlockInsetInInset(bv, the_locking_inset);
900 the_locking_inset = 0;
902 if (bv->text->first + bv->painter().paperHeight() <
903 (top_baseline + tabular->GetHeightOfTabular()))
905 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
906 UpdateLocal(bv, FULL, false);
907 actcell = tabular->GetCellBelow(first_visible_cell) + column;
909 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
912 UpdateLocal(bv, CURSOR, false);
917 if (the_locking_inset) {
918 UnlockInsetInInset(bv, the_locking_inset);
919 the_locking_inset = 0;
921 if (top_baseline < 0) {
922 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
923 UpdateLocal(bv, FULL, false);
924 if (top_baseline > 0)
927 actcell = tabular->GetCellBelow(first_visible_cell) + column;
932 UpdateLocal(bv, CURSOR, false);
943 case LFUN_LAYOUT_TABULAR:
945 bv->owner()->getDialogs()->showTabular(this);
948 case LFUN_TABULAR_FEATURE:
949 if (!TabularFeatures(bv, arg))
950 result = UNDISPATCHED;
953 if (!copySelection(bv))
955 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
956 bv->text->cursor.par()->previous(),
957 bv->text->cursor.par()->next());
959 UpdateLocal(bv, INIT, true);
964 bv->text->FinishUndo();
967 case LFUN_PASTESELECTION:
969 string clip(bv->getClipboard());
973 if (clip.find('\t') != string::npos) {
977 unsigned int len = clip.length();
978 string::size_type p = 0;
981 ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
989 maxCols = max(cols, maxCols);
995 maxCols = max(cols, maxCols);
996 delete paste_tabular;
997 paste_tabular = new LyXTabular(this, rows, maxCols);
998 string::size_type op = 0;
1000 int cells = paste_tabular->GetNumberOfCells();
1002 while((cell < cells) && (p < len) &&
1003 (p = clip.find_first_of("\t\n", p)) != string::npos) {
1008 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1013 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1014 while(cols++ < maxCols)
1022 // check for the last cell if there is no trailing '\n'
1023 if ((cell < cells) && (op < len))
1024 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1026 // so that the clipboard is used and it goes on
1028 // and executes LFUN_PASTESELECTION in insettext!
1029 delete paste_tabular;
1034 if (hasPasteBuffer()) {
1035 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1036 bv->text->cursor.par()->previous(),
1037 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));
1293 UpdateLocal(bv, FULL, false);
1294 } else if (xx < 20) {
1300 UpdateLocal(bv, FULL, false);
1302 } else if ((cursor.x() - offset) > 20 &&
1303 (cursor.x() - offset + tabular->GetWidthOfColumn(actcell))
1304 > (bv->workWidth() - 20)) {
1305 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1306 UpdateLocal(bv, FULL, false);
1307 } else if ((cursor.x() - offset) < 20) {
1308 scroll(bv, 20 - cursor.x() + offset);
1309 UpdateLocal(bv, FULL, false);
1310 } else if (scroll(false) && top_x > 20 &&
1311 (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1312 scroll(bv, old_x - cursor.x());
1313 UpdateLocal(bv, FULL, false);
1315 if ((!the_locking_inset ||
1316 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1317 actcell != oldcell) {
1318 InsetTabular * inset = const_cast<InsetTabular *>(this);
1319 bv->owner()->getDialogs()->updateTabular(inset);
1325 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1327 if (lock && !old_locking_inset) {
1328 if (ActivateCellInset(bv))
1331 bool moved = isRightToLeft(bv)
1332 ? movePrevCell(bv) : moveNextCell(bv);
1335 if (lock && ActivateCellInset(bv))
1339 return DISPATCHED_NOUPDATE;
1343 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1345 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1348 if (lock) { // behind the inset
1349 if (ActivateCellInset(bv, 0, 0, 0, true))
1353 return DISPATCHED_NOUPDATE;
1357 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1359 int const ocell = actcell;
1360 actcell = tabular->GetCellAbove(actcell);
1361 if (actcell == ocell) // we moved out of the inset
1367 if (old_locking_inset) {
1368 old_locking_inset->GetCursorPos(bv, x, y);
1369 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1371 if (ActivateCellInset(bv, x, 0))
1374 return DISPATCHED_NOUPDATE;
1378 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1380 int const ocell = actcell;
1381 actcell = tabular->GetCellBelow(actcell);
1382 if (actcell == ocell) // we moved out of the inset
1388 if (old_locking_inset) {
1389 old_locking_inset->GetCursorPos(bv, x, y);
1390 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1392 if (ActivateCellInset(bv, x, 0))
1395 return DISPATCHED_NOUPDATE;
1399 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1401 if (isRightToLeft(bv)) {
1402 if (tabular->IsFirstCellInRow(actcell)) {
1403 int row = tabular->row_of_cell(actcell);
1404 if (row == tabular->rows() - 1)
1406 actcell = tabular->GetLastCellInRow(row);
1407 actcell = tabular->GetCellBelow(actcell);
1414 if (tabular->IsLastCell(actcell))
1419 bool rtl = tabular->GetCellInset(actcell)->par->
1420 isRightToLeftPar(bv->buffer()->params);
1421 ActivateCellInset(bv, 0, 0, 0, !rtl);
1428 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1430 if (isRightToLeft(bv)) {
1431 if (tabular->IsLastCellInRow(actcell)) {
1432 int row = tabular->row_of_cell(actcell);
1435 actcell = tabular->GetFirstCellInRow(row);
1436 actcell = tabular->GetCellAbove(actcell);
1438 if (tabular->IsLastCell(actcell))
1443 if (!actcell) // first cell
1448 bool rtl = tabular->GetCellInset(actcell)->par->
1449 isRightToLeftPar(bv->buffer()->params);
1450 ActivateCellInset(bv, 0, 0, 0, !rtl);
1457 bool InsetTabular::Delete()
1463 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall,
1468 sel_cell_end = tabular->GetNumberOfCells() - 1;
1470 if (hasSelection()) {
1472 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1473 bv->text->cursor.par()->previous(),
1474 bv->text->cursor.par()->next());
1475 frozen = bv->text->undo_frozen;
1477 bv->text->FreezeUndo();
1478 // apply the fontchange on the whole selection
1483 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1484 for(int i=sel_row_start; i <= sel_row_end; ++i) {
1485 for(int j=sel_col_start; j <= sel_col_end; ++j) {
1486 tabular->GetCellInset(i, j)->SetFont(bv, font, tall, true);
1490 bv->text->UnFreezeUndo();
1491 UpdateLocal(bv, INIT, true);
1493 if (the_locking_inset)
1494 the_locking_inset->SetFont(bv, font, tall);
1498 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1500 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1503 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1504 string const tmp = tabularFeatures[i].feature;
1506 if (tmp == what.substr(0, tmp.length())) {
1507 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1508 //tabularFeatures[i].feature.length())) {
1509 action = tabularFeatures[i].action;
1513 if (action == LyXTabular::LAST_ACTION)
1517 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1518 TabularFeatures(bv, action, val);
1523 void InsetTabular::TabularFeatures(BufferView * bv,
1524 LyXTabular::Feature feature,
1525 string const & value)
1533 bool setLines = false;
1534 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1535 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1540 case LyXTabular::M_ALIGN_LEFT:
1541 case LyXTabular::ALIGN_LEFT:
1542 setAlign=LYX_ALIGN_LEFT;
1544 case LyXTabular::M_ALIGN_RIGHT:
1545 case LyXTabular::ALIGN_RIGHT:
1546 setAlign=LYX_ALIGN_RIGHT;
1548 case LyXTabular::M_ALIGN_CENTER:
1549 case LyXTabular::ALIGN_CENTER:
1550 setAlign=LYX_ALIGN_CENTER;
1552 case LyXTabular::M_VALIGN_TOP:
1553 case LyXTabular::VALIGN_TOP:
1554 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1556 case LyXTabular::M_VALIGN_BOTTOM:
1557 case LyXTabular::VALIGN_BOTTOM:
1558 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1560 case LyXTabular::M_VALIGN_CENTER:
1561 case LyXTabular::VALIGN_CENTER:
1562 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1567 if (hasSelection()) {
1568 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1570 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1571 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1573 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1574 bv->text->cursor.par()->previous(),
1575 bv->text->cursor.par()->next());
1577 int row = tabular->row_of_cell(actcell);
1578 int column = tabular->column_of_cell(actcell);
1582 case LyXTabular::SET_PWIDTH:
1584 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1585 tabular->SetColumnPWidth(actcell,value);
1587 for (int i = 0; i < tabular->rows(); ++i) {
1588 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1591 UpdateLocal(bv, INIT, true);
1595 case LyXTabular::SET_MPWIDTH:
1597 bool const update = (tabular->GetPWidth(actcell) != value);
1598 tabular->SetMColumnPWidth(actcell,value);
1600 for (int i = 0; i < tabular->rows(); ++i) {
1601 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1604 UpdateLocal(bv, INIT, true);
1608 case LyXTabular::SET_SPECIAL_COLUMN:
1609 case LyXTabular::SET_SPECIAL_MULTI:
1610 tabular->SetAlignSpecial(actcell,value,feature);
1612 case LyXTabular::APPEND_ROW:
1613 // append the row into the tabular
1614 UnlockInsetInInset(bv, the_locking_inset);
1615 tabular->AppendRow(actcell);
1616 UpdateLocal(bv, INIT, true);
1618 case LyXTabular::APPEND_COLUMN:
1619 // append the column into the tabular
1620 UnlockInsetInInset(bv, the_locking_inset);
1621 tabular->AppendColumn(actcell);
1622 actcell = tabular->GetCellNumber(row, column);
1623 UpdateLocal(bv, INIT, true);
1625 case LyXTabular::DELETE_ROW:
1626 UnlockInsetInInset(bv, the_locking_inset);
1627 tabular->DeleteRow(tabular->row_of_cell(actcell));
1628 if ((row+1) > tabular->rows())
1630 actcell = tabular->GetCellNumber(row, column);
1632 UpdateLocal(bv, INIT, true);
1634 case LyXTabular::DELETE_COLUMN:
1635 UnlockInsetInInset(bv, the_locking_inset);
1636 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1637 if ((column+1) > tabular->columns())
1639 actcell = tabular->GetCellNumber(row, column);
1641 UpdateLocal(bv, INIT, true);
1643 case LyXTabular::M_TOGGLE_LINE_TOP:
1645 case LyXTabular::TOGGLE_LINE_TOP:
1647 bool lineSet = !tabular->TopLine(actcell, flag);
1648 for (int i = sel_row_start; i <= sel_row_end; ++i)
1649 for (int j = sel_col_start; j <= sel_col_end; ++j)
1650 tabular->SetTopLine(
1651 tabular->GetCellNumber(i, j),
1653 UpdateLocal(bv, INIT, true);
1657 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1659 case LyXTabular::TOGGLE_LINE_BOTTOM:
1661 bool lineSet = !tabular->BottomLine(actcell, flag);
1662 for (int i = sel_row_start; i <= sel_row_end; ++i)
1663 for (int j = sel_col_start; j <= sel_col_end; ++j)
1664 tabular->SetBottomLine(
1665 tabular->GetCellNumber(i, j),
1668 UpdateLocal(bv, INIT, true);
1672 case LyXTabular::M_TOGGLE_LINE_LEFT:
1674 case LyXTabular::TOGGLE_LINE_LEFT:
1676 bool lineSet = !tabular->LeftLine(actcell, flag);
1677 for (int i = sel_row_start; i <= sel_row_end; ++i)
1678 for (int j = sel_col_start; j <= sel_col_end; ++j)
1679 tabular->SetLeftLine(
1680 tabular->GetCellNumber(i,j),
1683 UpdateLocal(bv, INIT, true);
1687 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1689 case LyXTabular::TOGGLE_LINE_RIGHT:
1691 bool lineSet = !tabular->RightLine(actcell, flag);
1692 for (int i = sel_row_start; i <= sel_row_end; ++i)
1693 for (int j = sel_col_start; j <= sel_col_end; ++j)
1694 tabular->SetRightLine(
1695 tabular->GetCellNumber(i,j),
1698 UpdateLocal(bv, INIT, true);
1702 case LyXTabular::M_ALIGN_LEFT:
1703 case LyXTabular::M_ALIGN_RIGHT:
1704 case LyXTabular::M_ALIGN_CENTER:
1706 case LyXTabular::ALIGN_LEFT:
1707 case LyXTabular::ALIGN_RIGHT:
1708 case LyXTabular::ALIGN_CENTER:
1709 for (int i = sel_row_start; i <= sel_row_end; ++i)
1710 for (int j = sel_col_start; j <= sel_col_end; ++j)
1711 tabular->SetAlignment(
1712 tabular->GetCellNumber(i, j),
1715 UpdateLocal(bv, INIT, true);
1717 case LyXTabular::M_VALIGN_TOP:
1718 case LyXTabular::M_VALIGN_BOTTOM:
1719 case LyXTabular::M_VALIGN_CENTER:
1721 case LyXTabular::VALIGN_TOP:
1722 case LyXTabular::VALIGN_BOTTOM:
1723 case LyXTabular::VALIGN_CENTER:
1724 for (int i = sel_row_start; i <= sel_row_end; ++i)
1725 for (int j = sel_col_start; j <= sel_col_end; ++j)
1726 tabular->SetVAlignment(
1727 tabular->GetCellNumber(i, j),
1729 UpdateLocal(bv, INIT, true);
1731 case LyXTabular::MULTICOLUMN:
1733 if (sel_row_start != sel_row_end) {
1734 WriteAlert(_("Impossible Operation!"),
1735 _("Multicolumns can only be horizontally."),
1739 // just multicol for one Single Cell
1740 if (!hasSelection()) {
1741 // check wether we are completly in a multicol
1742 if (tabular->IsMultiColumn(actcell)) {
1743 tabular->UnsetMultiColumn(actcell);
1744 UpdateLocal(bv, INIT, true);
1746 tabular->SetMultiColumn(actcell, 1);
1747 UpdateLocal(bv, CELL, true);
1751 // we have a selection so this means we just add all this
1752 // cells to form a multicolumn cell
1756 if (sel_cell_start > sel_cell_end) {
1757 s_start = sel_cell_end;
1758 s_end = sel_cell_start;
1760 s_start = sel_cell_start;
1761 s_end = sel_cell_end;
1763 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1765 sel_cell_end = sel_cell_start;
1766 UpdateLocal(bv, INIT, true);
1769 case LyXTabular::SET_ALL_LINES:
1771 case LyXTabular::UNSET_ALL_LINES:
1772 for (int i = sel_row_start; i <= sel_row_end; ++i)
1773 for (int j = sel_col_start; j <= sel_col_end; ++j)
1774 tabular->SetAllLines(
1775 tabular->GetCellNumber(i,j), setLines);
1776 UpdateLocal(bv, INIT, true);
1778 case LyXTabular::SET_LONGTABULAR:
1779 tabular->SetLongTabular(true);
1780 UpdateLocal(bv, INIT, true); // because this toggles displayed
1782 case LyXTabular::UNSET_LONGTABULAR:
1783 tabular->SetLongTabular(false);
1784 UpdateLocal(bv, INIT, true); // because this toggles displayed
1786 case LyXTabular::SET_ROTATE_TABULAR:
1787 tabular->SetRotateTabular(true);
1789 case LyXTabular::UNSET_ROTATE_TABULAR:
1790 tabular->SetRotateTabular(false);
1792 case LyXTabular::SET_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),
1799 case LyXTabular::UNSET_ROTATE_CELL:
1800 for (int i = sel_row_start; i <= sel_row_end; ++i)
1801 for (int j = sel_col_start; j <= sel_col_end; ++j)
1802 tabular->SetRotateCell(
1803 tabular->GetCellNumber(i, j), false);
1805 case LyXTabular::SET_USEBOX:
1807 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1808 if (val == tabular->GetUsebox(actcell))
1809 val = LyXTabular::BOX_NONE;
1810 for (int i = sel_row_start; i <= sel_row_end; ++i)
1811 for (int j = sel_col_start; j <= sel_col_end; ++j)
1813 tabular->GetCellNumber(i, j), val);
1816 case LyXTabular::SET_LTFIRSTHEAD:
1817 tabular->SetLTHead(actcell, true);
1819 case LyXTabular::SET_LTHEAD:
1820 tabular->SetLTHead(actcell, false);
1822 case LyXTabular::SET_LTFOOT:
1823 tabular->SetLTFoot(actcell, false);
1825 case LyXTabular::SET_LTLASTFOOT:
1826 tabular->SetLTFoot(actcell, true);
1828 case LyXTabular::SET_LTNEWPAGE:
1830 bool what = !tabular->GetLTNewPage(actcell);
1831 tabular->SetLTNewPage(actcell, what);
1834 // dummy stuff just to avoid warnings
1835 case LyXTabular::LAST_ACTION:
1841 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1844 UpdatableInset * inset =
1845 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1846 LyXFont font(LyXFont::ALL_SANE);
1848 x = inset->x() + inset->width(bv, font);
1849 y = inset->descent(bv, font);
1851 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1852 //inset_y = cursor.y();
1853 inset->Edit(bv, x, y, button);
1854 if (!the_locking_inset)
1856 UpdateLocal(bv, CELL, false);
1857 return (the_locking_inset != 0);
1861 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1864 inset_x = cursor.x()
1865 - top_x + tabular->GetBeginningOfTextInCell(actcell);
1866 inset_y = cursor.y();
1867 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1871 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1874 > (cursor.x() + tabular->GetBeginningOfTextInCell(actcell));
1878 // This returns paperWidth() if the cell-width is unlimited or the width
1879 // in pixels if we have a pwidth for this cell.
1880 int InsetTabular::GetMaxWidthOfCell(BufferView * bv, int cell) const
1882 string const s = tabular->GetPWidth(cell);
1886 return VSpace(s).inPixels(bv);
1890 int InsetTabular::getMaxWidth(BufferView * bv,
1891 UpdatableInset const * inset) const
1893 int const n = tabular->GetNumberOfCells();
1895 for (; cell < n; ++cell) {
1896 if (tabular->GetCellInset(cell) == inset)
1901 int w = GetMaxWidthOfCell(bv, cell);
1903 // because the inset then subtracts it's top_x and owner->x()
1904 w += (inset->x() - top_x);
1909 void InsetTabular::resizeLyXText(BufferView *) const
1915 LyXText * InsetTabular::getLyXText(BufferView const * bv,
1916 bool const recursive) const
1918 if (the_locking_inset)
1919 return the_locking_inset->getLyXText(bv, recursive);
1920 return Inset::getLyXText(bv, recursive);
1924 bool InsetTabular::ShowInsetDialog(BufferView * bv) const
1926 if (!the_locking_inset || !the_locking_inset->ShowInsetDialog(bv))
1927 bv->owner()->getDialogs()
1928 ->showTabular(const_cast<InsetTabular *>(this));
1933 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1935 if (the_locking_inset) {
1936 InsetTabular * i = static_cast<InsetTabular *>
1937 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1939 i->OpenLayoutDialog(bv);
1943 bv->owner()->getDialogs()->showTabular(
1944 const_cast<InsetTabular *>(this));
1948 // functions returns:
1952 // 3 ... toggled off
1954 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1956 int action = LyXTabular::LAST_ACTION;
1957 LyXFunc::func_status status = LyXFunc::OK;
1960 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1961 string const tmp = tabularFeatures[i].feature;
1962 if (tmp == what.substr(0, tmp.length())) {
1963 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1964 // tabularFeatures[i].feature.length())) {
1965 action = tabularFeatures[i].action;
1969 if (action == LyXTabular::LAST_ACTION)
1970 return LyXFunc::Unknown;
1972 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1979 if (hasSelection()) {
1980 getSelection(sel_row_start, sel_row_end, dummy, dummy);
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));
2331 void InsetTabular::getSelection(int & srow, int & erow, int & scol, int & ecol) const
2333 srow = tabular->row_of_cell(sel_cell_start);
2334 erow = tabular->row_of_cell(sel_cell_end);
2338 scol = tabular->column_of_cell(sel_cell_start);
2339 ecol = tabular->column_of_cell(sel_cell_end);
2343 ecol = tabular->right_column_of_cell(sel_cell_end);