1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettabular.h"
25 #include "commandtags.h"
27 #include "LaTeXFeatures.h"
31 #include "lyx_gui_misc.h"
34 #include "insets/insettext.h"
35 #include "frontends/Dialogs.h"
40 const int ADD_TO_HEIGHT = 2;
41 const int ADD_TO_TABULAR_WIDTH = 2;
43 static LyXTabular * paste_tabular = 0;
44 bool InsetTabular::hasPasteBuffer() const
46 return (paste_tabular != 0);
57 struct tabular_features {
58 LyXTabular::Feature action;
62 //static tabular_features * tabularFeatures = 0;
64 static tabular_features tabularFeatures[] =
66 { LyXTabular::APPEND_ROW, "append-row" },
67 { LyXTabular::APPEND_COLUMN, "append-column" },
68 { LyXTabular::DELETE_ROW, "delete-row" },
69 { LyXTabular::DELETE_COLUMN, "delete-column" },
70 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
71 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
72 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
73 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
74 { LyXTabular::ALIGN_LEFT, "align-left" },
75 { LyXTabular::ALIGN_RIGHT, "align-right" },
76 { LyXTabular::ALIGN_CENTER, "align-center" },
77 { LyXTabular::VALIGN_TOP, "valign-top" },
78 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
79 { LyXTabular::VALIGN_CENTER, "valign-center" },
80 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
81 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
82 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
83 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
84 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
85 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
86 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
87 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
88 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
89 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
90 { LyXTabular::MULTICOLUMN, "multicolumn" },
91 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
92 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
93 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
94 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
95 { LyXTabular::SET_PWIDTH, "set-pwidth" },
96 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
97 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
98 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
99 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
100 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
101 { LyXTabular::SET_USEBOX, "set-usebox" },
102 { LyXTabular::SET_LTHEAD, "set-lthead" },
103 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
104 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
105 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
106 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
107 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
108 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
109 { LyXTabular::LAST_ACTION, "" }
112 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
119 tabular = new LyXTabular(this, rows,columns);
120 // for now make it always display as display() inset
122 the_locking_inset = 0;
123 locked = no_selection = cursor_visible = false;
125 actrow = actcell = 0;
132 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
135 tabular = new LyXTabular(this, *(tab.tabular));
136 the_locking_inset = 0;
137 locked = no_selection = cursor_visible = false;
139 actrow = actcell = 0;
140 sel_cell_start = sel_cell_end = 0;
146 InsetTabular::~InsetTabular()
153 Inset * InsetTabular::Clone(Buffer const & buf) const
155 InsetTabular * t = new InsetTabular(*this, buf);
157 t->tabular = tabular->Clone(t);
162 void InsetTabular::Write(Buffer const * buf, ostream & os) const
164 os << " Tabular" << endl;
165 tabular->Write(buf, os);
169 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
171 bool old_format = (lex.GetString() == "\\LyXTable");
176 tabular = new LyXTabular(buf, this, lex);
184 token = lex.GetString();
185 while (lex.IsOK() && (token != "\\end_inset")) {
187 token = lex.GetString();
189 if (token != "\\end_inset") {
190 lex.printError("Missing \\end_inset at this point. "
196 int InsetTabular::ascent(BufferView *, LyXFont const &) const
198 return tabular->GetAscentOfRow(0);
202 int InsetTabular::descent(BufferView *, LyXFont const &) const
204 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
208 int InsetTabular::width(BufferView *, LyXFont const &) const
210 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
214 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
215 float & x, bool cleared) const
220 Painter & pain = bv->painter();
225 UpdatableInset::draw(bv, font, baseline, x, cleared);
226 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
227 (top_x != int(x)) || (top_baseline != baseline))) {
228 int h = ascent(bv, font) + descent(bv, font);
229 int tx = display() || !owner() ? 0 : top_x;
230 int w = tx ? width(bv, font) : pain.paperWidth();
231 int ty = baseline - ascent(bv, font);
235 if ((ty + h) > pain.paperHeight())
236 h = pain.paperHeight();
237 if ((top_x + w) > pain.paperWidth())
238 w = pain.paperWidth();
239 pain.fillRectangle(tx, ty, w, h);
244 top_baseline = baseline;
245 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
248 x += ADD_TO_TABULAR_WIDTH;
252 first_visible_cell = -1;
253 for (i = 0; i < tabular->rows(); ++i) {
255 dodraw = ((baseline + tabular->GetDescentOfRow(i)) > 0) &&
256 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight();
257 for (j = 0; j < tabular->columns(); ++j) {
258 if (tabular->IsPartOfMultiColumn(i, j))
260 cx = nx + tabular->GetBeginningOfTextInCell(cell);
262 if (first_visible_cell < 0)
263 first_visible_cell = cell;
265 DrawCellSelection(pain, nx, baseline, i, j, cell);
266 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
268 DrawCellLines(pain, nx, baseline, i, cell);
270 nx += tabular->GetWidthOfColumn(cell);
273 baseline += tabular->GetDescentOfRow(i) +
274 tabular->GetAscentOfRow(i + 1) +
275 tabular->GetAdditionalHeight(cell);
277 } else if (need_update == CELL) {
280 if (the_locking_inset) {
281 Inset * inset = tabular->GetCellInset(cell);
282 for (i = 0; (inset != the_locking_inset) && (i < tabular->rows());
285 for (j = 0; (inset != the_locking_inset) && (j < tabular->columns()); ++j)
287 if (tabular->IsPartOfMultiColumn(i, j))
289 nx += tabular->GetWidthOfColumn(cell);
291 inset = tabular->GetCellInset(cell);
293 if (tabular->row_of_cell(cell) > i) {
295 baseline += tabular->GetDescentOfRow(i) +
296 tabular->GetAscentOfRow(i + 1) +
297 tabular->GetAdditionalHeight(cell);
301 for (i = 0; (cell < actcell) && (i < tabular->rows()); ++i) {
302 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
303 if (tabular->IsPartOfMultiColumn(i, j))
305 nx += tabular->GetWidthOfColumn(cell);
308 if (tabular->row_of_cell(cell) > i) {
310 baseline += tabular->GetDescentOfRow(i) +
311 tabular->GetAscentOfRow(i + 1) +
312 tabular->GetAdditionalHeight(cell);
316 i = tabular->row_of_cell(cell);
317 if (the_locking_inset != tabular->GetCellInset(cell)) {
318 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
323 cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
324 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
325 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
327 // clear only if we didn't have a change
328 if (need_update == CELL) {
329 // clear before the inset
332 baseline - tabular->GetAscentOfRow(i) + 1,
334 tabular->GetAscentOfRow(i) +
335 tabular->GetDescentOfRow(i) - 1);
336 // clear behind the inset
338 int(cx + the_locking_inset->width(bv,font) + 1),
339 baseline - tabular->GetAscentOfRow(i) + 1,
340 tabular->GetWidthOfColumn(cell) -
341 tabular->GetBeginningOfTextInCell(cell) -
342 the_locking_inset->width(bv,font) -
343 tabular->GetAdditionalWidth(cell) - 1,
344 tabular->GetAscentOfRow(i) +
345 tabular->GetDescentOfRow(i) - 1);
348 x -= ADD_TO_TABULAR_WIDTH;
349 x += width(bv, font);
350 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
357 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
358 int row, int cell) const
360 int x2 = x + tabular->GetWidthOfColumn(cell);
363 if (!tabular->TopAlreadyDrawed(cell)) {
364 on_off = !tabular->TopLine(cell);
365 pain.line(x, baseline - tabular->GetAscentOfRow(row),
366 x2, baseline - tabular->GetAscentOfRow(row),
367 on_off ? LColor::tabularonoffline : LColor::tabularline,
368 on_off ? Painter::line_onoffdash : Painter::line_solid);
370 on_off = !tabular->BottomLine(cell);
371 pain.line(x,baseline + tabular->GetDescentOfRow(row),
372 x2, baseline + tabular->GetDescentOfRow(row),
373 on_off ? LColor::tabularonoffline : LColor::tabularline,
374 on_off ? Painter::line_onoffdash : Painter::line_solid);
375 if (!tabular->LeftAlreadyDrawed(cell)) {
376 on_off = !tabular->LeftLine(cell);
377 pain.line(x, baseline - tabular->GetAscentOfRow(row),
378 x, baseline + tabular->GetDescentOfRow(row),
379 on_off ? LColor::tabularonoffline : LColor::tabularline,
380 on_off ? Painter::line_onoffdash : Painter::line_solid);
382 on_off = !tabular->RightLine(cell);
383 pain.line(x2 - tabular->GetAdditionalWidth(cell),
384 baseline - tabular->GetAscentOfRow(row),
385 x2 - tabular->GetAdditionalWidth(cell),
386 baseline + tabular->GetDescentOfRow(row),
387 on_off ? LColor::tabularonoffline : LColor::tabularline,
388 on_off ? Painter::line_onoffdash : Painter::line_solid);
392 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
393 int row, int column, int cell) const
395 int cs = tabular->column_of_cell(sel_cell_start);
396 int ce = tabular->column_of_cell(sel_cell_end);
399 cs = tabular->column_of_cell(sel_cell_end);
401 ce = tabular->right_column_of_cell(sel_cell_end);
404 int rs = tabular->row_of_cell(sel_cell_start);
405 int re = tabular->row_of_cell(sel_cell_end);
406 if (rs > re) swap(rs, re);
408 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
409 int w = tabular->GetWidthOfColumn(cell);
410 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
411 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
412 w, h, LColor::selection);
417 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
421 calculate_dimensions_of_cells(bv, font, true);
423 owner()->update(bv, font, true);
426 if (the_locking_inset) {
427 the_locking_inset->update(bv, font, reinit);
429 // inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
430 // inset_y = cursor.y();
432 switch (need_update) {
436 if (calculate_dimensions_of_cells(bv, font, false))
448 string const InsetTabular::EditMessage() const
450 return _("Opened Tabular Inset");
454 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
456 UpdatableInset::Edit(bv, x, y, button);
458 if (!bv->lockInset(this)) {
459 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
463 the_locking_inset = 0;
467 sel_cell_start = sel_cell_end = actcell;
468 bv->text->FinishUndo();
469 if (InsetHit(bv, x, y) && (button != 3)) {
470 ActivateCellInsetAbs(bv, x, y, button);
472 // UpdateLocal(bv, NONE, false);
473 // bv->getOwner()->getPopups().updateFormTabular();
477 void InsetTabular::InsetUnlock(BufferView * bv)
479 if (the_locking_inset) {
480 the_locking_inset->InsetUnlock(bv);
481 the_locking_inset = 0;
484 no_selection = false;
487 if (scroll() || hasSelection()) {
488 sel_cell_start = sel_cell_end = 0;
492 UpdateLocal(bv, FULL, false);
497 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
498 bool mark_dirty) const
500 if (need_update < what) // only set this if it has greater update
502 if ((what == INIT) && hasSelection())
505 if (need_update != NONE) {
506 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
507 if (locked) // && (what != NONE))
513 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
515 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
519 if (inset == tabular->GetCellInset(actcell)) {
520 lyxerr[Debug::INSETS] << "OK" << endl;
521 the_locking_inset = tabular->GetCellInset(actcell);
523 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
524 inset_y = cursor.y();
526 } else if (the_locking_inset && (the_locking_inset == inset)) {
527 lyxerr[Debug::INSETS] << "OK" << endl;
529 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
530 inset_y = cursor.y();
531 } else if (the_locking_inset) {
532 lyxerr[Debug::INSETS] << "MAYBE" << endl;
533 return the_locking_inset->LockInsetInInset(bv, inset);
535 lyxerr[Debug::INSETS] << "NOT OK" << endl;
540 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
543 if (!the_locking_inset)
545 if (the_locking_inset == inset) {
546 the_locking_inset->InsetUnlock(bv);
547 the_locking_inset = 0;
548 UpdateLocal(bv, CELL, false);
549 ShowInsetCursor(bv, false);
552 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
553 if (inset->LyxCode() == TABULAR_CODE &&
554 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
556 bv->owner()->getDialogs()->updateTabular(this);
565 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
567 if (!the_locking_inset)
569 if (the_locking_inset != inset) {
570 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
573 UpdateLocal(bv, CELL, false);
578 unsigned int InsetTabular::InsetInInsetY()
580 if (!the_locking_inset)
583 return (inset_y + the_locking_inset->InsetInInsetY());
587 UpdatableInset * InsetTabular::GetLockingInset()
589 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
593 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
597 if (the_locking_inset)
598 return the_locking_inset->GetFirstLockingInsetOfType(c);
603 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
605 if (the_locking_inset)
606 return the_locking_inset->InsertInset(bv, inset);
611 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
613 if (hasSelection() && (button == 3))
616 if (hasSelection()) {
618 UpdateLocal(bv, SELECTION, false);
621 no_selection = false;
623 int const ocell = actcell;
624 int const orow = actrow;
629 UpdateLocal(bv, NONE, false);
630 sel_cell_start = sel_cell_end = actcell;
632 if ((ocell != actcell) && the_locking_inset) {
633 the_locking_inset->InsetUnlock(bv);
634 the_locking_inset = 0;
640 bool const inset_hit = InsetHit(bv, x, y);
642 if ((ocell == actcell) && the_locking_inset && inset_hit) {
644 the_locking_inset->InsetButtonPress(bv,
645 x - inset_x, y - inset_y, button);
647 } else if (the_locking_inset) {
648 the_locking_inset->InsetUnlock(bv);
650 the_locking_inset = 0;
652 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
655 if (inset_hit && bv->theLockingInset()) {
656 if (ActivateCellInsetAbs(bv, x, y, button))
657 the_locking_inset->InsetButtonPress(bv, x - inset_x,
658 y - inset_y, button);
665 void InsetTabular::InsetButtonRelease(BufferView * bv,
666 int x, int y, int button)
669 if (the_locking_inset) {
671 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
672 i->InsetButtonRelease(bv, x, y, button);
676 bv->owner()->getDialogs()->showTabular(this);
679 if (the_locking_inset) {
680 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
683 no_selection = false;
687 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
689 if (the_locking_inset) {
690 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
691 y - inset_y, button);
696 int const old_cell = actcell;
699 sel_cell_end = actcell;
700 if (sel_cell_end != old_cell)
701 UpdateLocal(bv, SELECTION, false);
704 no_selection = false;
708 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
710 if (the_locking_inset) {
711 the_locking_inset->InsetKeyPress(xke);
717 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
720 // We need to save the value of the_locking_inset as the call to
721 // the_locking_inset->LocalDispatch might unlock it.
722 old_locking_inset = the_locking_inset;
723 no_selection = false;
724 UpdatableInset::RESULT result =
725 UpdatableInset::LocalDispatch(bv, action, arg);
726 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
731 if ((action < 0) && arg.empty())
734 if (the_locking_inset) {
735 result=the_locking_inset->LocalDispatch(bv, action, arg);
736 if (result == DISPATCHED_NOUPDATE) {
739 if (sc != scroll()) { // inset has been scrolled
740 the_locking_inset->ToggleInsetCursor(bv);
741 UpdateLocal(bv, FULL, false);
742 the_locking_inset->ToggleInsetCursor(bv);
745 } else if (result == DISPATCHED) {
746 the_locking_inset->ToggleInsetCursor(bv);
747 UpdateLocal(bv, CELL, false);
748 the_locking_inset->ToggleInsetCursor(bv);
750 } else if (result == FINISHED) {
754 bool hs = hasSelection();
758 // --- Cursor Movements ---------------------------------------------
760 if (tabular->IsLastCellInRow(actcell))
762 moveRight(bv, false);
763 sel_cell_end = actcell;
764 UpdateLocal(bv, SELECTION, false);
767 result = moveRight(bv);
768 sel_cell_start = sel_cell_end = actcell;
770 UpdateLocal(bv, SELECTION, false);
773 if (tabular->IsFirstCellInRow(actcell))
776 sel_cell_end = actcell;
777 UpdateLocal(bv, SELECTION, false);
780 result = moveLeft(bv);
781 sel_cell_start = sel_cell_end = actcell;
783 UpdateLocal(bv, SELECTION, false);
787 int const ocell = actcell;
789 if ((ocell == sel_cell_end) ||
790 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
791 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
793 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
794 UpdateLocal(bv, SELECTION, false);
798 result = moveDown(bv, old_locking_inset != 0);
799 sel_cell_start = sel_cell_end = actcell;
801 UpdateLocal(bv, SELECTION, false);
805 int const ocell = actcell;
807 if ((ocell == sel_cell_end) ||
808 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
809 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
811 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
812 UpdateLocal(bv, SELECTION, false);
816 result = moveUp(bv, old_locking_inset != 0);
817 sel_cell_start = sel_cell_end = actcell;
819 UpdateLocal(bv, SELECTION, false);
823 if (the_locking_inset) {
824 UnlockInsetInInset(bv, the_locking_inset);
825 the_locking_inset = 0;
827 if (bv->text->first + bv->painter().paperHeight() <
828 (top_baseline + tabular->GetHeightOfTabular()))
830 bv->scrollCB(bv->text->first + bv->painter().paperHeight());
831 UpdateLocal(bv, FULL, false);
832 actcell = tabular->GetCellBelow(first_visible_cell) + column;
834 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
837 UpdateLocal(bv, CURSOR, false);
842 if (the_locking_inset) {
843 UnlockInsetInInset(bv, the_locking_inset);
844 the_locking_inset = 0;
846 if (top_baseline < 0) {
847 bv->scrollCB(bv->text->first - bv->painter().paperHeight());
848 UpdateLocal(bv, FULL, false);
849 if (top_baseline > 0)
852 actcell = tabular->GetCellBelow(first_visible_cell) + column;
857 UpdateLocal(bv, CURSOR, false);
871 if (the_locking_inset) {
872 UnlockInsetInInset(bv, the_locking_inset);
873 the_locking_inset = 0;
875 if (action == LFUN_TAB)
876 moveNextCell(bv, old_locking_inset != 0);
878 movePrevCell(bv, old_locking_inset != 0);
879 sel_cell_start = sel_cell_end = actcell;
881 UpdateLocal(bv, SELECTION, false);
884 case LFUN_LAYOUT_TABULAR:
886 bv->owner()->getDialogs()->showTabular(this);
889 case LFUN_TABULAR_FEATURE:
890 if (!TabularFeatures(bv, arg))
891 result = UNDISPATCHED;
894 if (!copySelection(bv))
896 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
898 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
899 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
901 bv->text->cursor.par()->previous,
902 bv->text->cursor.par()->next
906 UpdateLocal(bv, INIT, true);
911 bv->text->FinishUndo();
914 case LFUN_PASTESELECTION:
916 string clip(bv->workarea()->getClipboard());
920 if (clip.find('\t') != string::npos) {
924 unsigned int len = clip.length();
925 string::size_type p = 0;
928 ((p = clip.find_first_of("\t\n", p)) != string::npos))
937 maxCols = max(cols, maxCols);
943 maxCols = max(cols, maxCols);
944 delete paste_tabular;
945 paste_tabular = new LyXTabular(this, rows, maxCols);
946 string::size_type op = 0;
948 int cells = paste_tabular->GetNumberOfCells();
950 while((cell < cells) && (p < len) &&
951 (p = clip.find_first_of("\t\n", p)) != string::npos)
957 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
962 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
963 while(cols++ < maxCols)
971 // check for the last cell if there is no trailing '\n'
972 if ((cell < cells) && (op < len))
973 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
975 // so that the clipboard is used and it goes on to default
976 // and executes LFUN_PASTESELECTION in insettext!
977 delete paste_tabular;
982 if (hasPasteBuffer()) {
983 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
985 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
986 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
988 bv->text->cursor.par()->previous,
989 bv->text->cursor.par()->next
993 UpdateLocal(bv, INIT, true);
996 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
998 // we try to activate the actual inset and put this event down to
999 // the insets dispatch function.
1000 result = UNDISPATCHED;
1001 if (the_locking_inset)
1004 if (ActivateCellInset(bv)) {
1005 result=the_locking_inset->LocalDispatch(bv, action, arg);
1006 if ((result == UNDISPATCHED) || (result == FINISHED)) {
1007 UnlockInsetInInset(bv, the_locking_inset);
1009 the_locking_inset = 0;
1010 return UNDISPATCHED;
1013 the_locking_inset->ToggleInsetCursor(bv);
1014 UpdateLocal(bv, CELL, false);
1015 the_locking_inset->ToggleInsetCursor(bv);
1020 if (result!=FINISHED) {
1021 if (!the_locking_inset) {
1022 ShowInsetCursor(bv);
1025 bv->unlockInset(this);
1030 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1031 bool fragile, bool fp) const
1033 return tabular->Latex(buf, os, fragile, fp);
1037 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1039 // This should be changed to a real ascii export
1040 return tabular->Ascii(buf, os);
1044 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1050 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1052 return tabular->DocBook(buf,os);
1056 void InsetTabular::Validate(LaTeXFeatures & features) const
1058 tabular->Validate(features);
1062 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1063 LyXFont const & font,
1070 bool changed = false;
1072 // if we have a locking_inset we should have to check only this cell for
1073 // change so I'll try this to have a boost, but who knows ;)
1074 if ((need_update != INIT) &&
1075 (the_locking_inset == tabular->GetCellInset(actcell))) {
1076 for(int i = 0; i < tabular->columns(); ++i) {
1077 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1079 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1082 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1083 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1084 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1087 for (int i = 0; i < tabular->rows(); ++i) {
1090 for (int j= 0; j < tabular->columns(); ++j) {
1091 if (tabular->IsPartOfMultiColumn(i,j))
1094 inset = tabular->GetCellInset(cell);
1096 inset->update(bv, font, false);
1097 maxAsc = max(maxAsc, inset->ascent(bv, font));
1098 maxDesc = max(maxDesc, inset->descent(bv, font));
1099 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1101 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1102 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1108 void InsetTabular::GetCursorPos(BufferView *,
1109 int & x, int & y) const
1111 x = cursor.x() - top_x;
1116 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1118 if (the_locking_inset) {
1119 the_locking_inset->ToggleInsetCursor(bv);
1123 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1125 int const asc = lyxfont::maxAscent(font);
1126 int const desc = lyxfont::maxDescent(font);
1129 bv->hideLockedInsetCursor();
1131 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1132 cursor_visible = !cursor_visible;
1136 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1138 if (!cursor_visible) {
1139 LyXFont font; // = GetFont(par, cursor.pos);
1141 int const asc = lyxfont::maxAscent(font);
1142 int const desc = lyxfont::maxDescent(font);
1143 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1145 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1146 cursor_visible = true;
1151 void InsetTabular::HideInsetCursor(BufferView * bv)
1153 if (cursor_visible) {
1154 bv->hideLockedInsetCursor();
1155 cursor_visible = false;
1157 // if (cursor_visible)
1158 // ToggleInsetCursor(bv);
1162 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1166 actcell = actrow = actcol = 0;
1167 int ly = tabular->GetDescentOfRow(actrow);
1169 // first search the right row
1170 while((ly < y) && (actrow < tabular->rows())) {
1171 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1172 tabular->GetAscentOfRow(actrow + 1) +
1173 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1176 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1178 actcell = tabular->GetCellNumber(actrow, actcol);
1180 // now search the right column
1181 int lx = tabular->GetWidthOfColumn(actcell) -
1182 tabular->GetAdditionalWidth(actcell);
1184 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1185 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1186 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1187 tabular->GetAdditionalWidth(actcell - 1));
1189 // Jürgen, you should check that this is correct. (Lgb)
1190 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1191 lx += tabular->GetWidthOfColumn(actcell + 1)
1192 + tabular->GetAdditionalWidth(actcell);
1196 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1201 int InsetTabular::getCellXPos(int cell) const
1205 for (; !tabular->IsFirstCellInRow(c); --c)
1207 int lx = tabular->GetWidthOfColumn(cell);
1208 for (; c < cell; ++c) {
1209 lx += tabular->GetWidthOfColumn(c);
1211 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1215 void InsetTabular::resetPos(BufferView * bv) const
1219 actcol = tabular->column_of_cell(actcell);
1224 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1225 if (tabular->IsLastCellInRow(cell)) {
1226 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1227 tabular->GetAscentOfRow(actrow + 1) +
1228 tabular->GetAdditionalHeight(cell + 1));
1232 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1233 int new_x = getCellXPos(actcell);
1234 int old_x = cursor.x();
1237 // cursor.x(getCellXPos(actcell) + offset);
1238 if (scroll() && (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1240 else if (the_locking_inset &&
1241 (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1243 int xx = cursor.x() - offset + bv->text->GetRealCursorX(bv);
1245 if (xx > (bv->workWidth()-20))
1246 scroll(bv, -(xx - bv->workWidth() + 60));
1254 } else if (((cursor.x() - offset) > 20) &&
1255 ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1256 (bv->workWidth()-20)))
1258 scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1259 UpdateLocal(bv, FULL, false);
1260 } else if ((cursor.x() - offset) < 20) {
1261 scroll(bv, 20 - cursor.x() + offset);
1262 UpdateLocal(bv, FULL, false);
1263 } else if (scroll() && (top_x > 20) &&
1264 ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1265 scroll(bv, old_x - cursor.x());
1267 if ((!the_locking_inset ||
1268 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1269 (actcell != oldcell)) {
1270 InsetTabular * inset = const_cast<InsetTabular *>(this);
1271 bv->owner()->getDialogs()->updateTabular(inset);
1277 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1279 if (lock && !old_locking_inset) {
1280 if (ActivateCellInset(bv))
1283 bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1286 if (lock && ActivateCellInset(bv))
1290 return DISPATCHED_NOUPDATE;
1294 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1296 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1299 if (lock) { // behind the inset
1300 if (ActivateCellInset(bv, 0, 0, 0, true))
1304 return DISPATCHED_NOUPDATE;
1308 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1310 int const ocell = actcell;
1311 actcell = tabular->GetCellAbove(actcell);
1312 if (actcell == ocell) // we moved out of the inset
1318 if (old_locking_inset) {
1319 old_locking_inset->GetCursorPos(bv, x, y);
1320 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1322 if (ActivateCellInset(bv, x, 0))
1325 return DISPATCHED_NOUPDATE;
1329 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1331 int const ocell = actcell;
1332 actcell = tabular->GetCellBelow(actcell);
1333 if (actcell == ocell) // we moved out of the inset
1339 if (old_locking_inset) {
1340 old_locking_inset->GetCursorPos(bv, x, y);
1341 x -= cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1343 if (ActivateCellInset(bv, x, 0))
1346 return DISPATCHED_NOUPDATE;
1350 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1352 if (isRightToLeft(bv)) {
1353 if (tabular->IsFirstCellInRow(actcell)) {
1354 int row = tabular->row_of_cell(actcell);
1355 if (row == tabular->rows() - 1)
1357 actcell = tabular->GetLastCellInRow(row);
1358 actcell = tabular->GetCellBelow(actcell);
1365 if (tabular->IsLastCell(actcell))
1370 bool rtl = tabular->GetCellInset(actcell)->par->
1371 isRightToLeftPar(bv->buffer()->params);
1372 ActivateCellInset(bv, 0, 0, 0, !rtl);
1379 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1381 if (isRightToLeft(bv)) {
1382 if (tabular->IsLastCellInRow(actcell)) {
1383 int row = tabular->row_of_cell(actcell);
1386 actcell = tabular->GetFirstCellInRow(row);
1387 actcell = tabular->GetCellAbove(actcell);
1389 if (tabular->IsLastCell(actcell))
1394 if (!actcell) // first cell
1399 bool rtl = tabular->GetCellInset(actcell)->par->
1400 isRightToLeftPar(bv->buffer()->params);
1401 ActivateCellInset(bv, 0, 0, 0, !rtl);
1408 bool InsetTabular::Delete()
1414 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1416 if (the_locking_inset)
1417 the_locking_inset->SetFont(bv, font, tall);
1421 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1423 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1426 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1427 string const tmp = tabularFeatures[i].feature;
1429 if (tmp == what.substr(0, tmp.length())) {
1430 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1431 //tabularFeatures[i].feature.length())) {
1432 action = tabularFeatures[i].action;
1436 if (action == LyXTabular::LAST_ACTION)
1440 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1441 TabularFeatures(bv, action, val);
1446 void InsetTabular::TabularFeatures(BufferView * bv,
1447 LyXTabular::Feature feature,
1448 string const & value)
1457 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1458 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1463 case LyXTabular::M_ALIGN_LEFT:
1464 case LyXTabular::ALIGN_LEFT:
1465 setAlign=LYX_ALIGN_LEFT;
1467 case LyXTabular::M_ALIGN_RIGHT:
1468 case LyXTabular::ALIGN_RIGHT:
1469 setAlign=LYX_ALIGN_RIGHT;
1471 case LyXTabular::M_ALIGN_CENTER:
1472 case LyXTabular::ALIGN_CENTER:
1473 setAlign=LYX_ALIGN_CENTER;
1475 case LyXTabular::M_VALIGN_TOP:
1476 case LyXTabular::VALIGN_TOP:
1477 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1479 case LyXTabular::M_VALIGN_BOTTOM:
1480 case LyXTabular::VALIGN_BOTTOM:
1481 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1483 case LyXTabular::M_VALIGN_CENTER:
1484 case LyXTabular::VALIGN_CENTER:
1485 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1490 if (hasSelection()) {
1491 sel_col_start = tabular->column_of_cell(sel_cell_start);
1492 sel_col_end = tabular->column_of_cell(sel_cell_end);
1493 if (sel_col_start > sel_col_end) {
1494 sel_col_end = sel_col_start;
1495 sel_col_start = tabular->column_of_cell(sel_cell_end);
1497 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1500 sel_row_start = tabular->row_of_cell(sel_cell_start);
1501 sel_row_end = tabular->row_of_cell(sel_cell_end);
1502 if (sel_row_start > sel_row_end) {
1503 //int tmp = sel_row_start;
1504 //sel_row_start = sel_row_end;
1505 //sel_row_end = tmp;
1506 swap(sel_row_start, sel_row_end);
1509 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1510 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1512 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1514 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1515 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1517 bv->text->cursor.par()->previous,
1518 bv->text->cursor.par()->next
1522 int row = tabular->row_of_cell(actcell);
1523 int column = tabular->column_of_cell(actcell);
1527 case LyXTabular::SET_PWIDTH:
1529 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1530 tabular->SetColumnPWidth(actcell,value);
1532 for (int i=0; i < tabular->rows(); ++i) {
1533 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1536 UpdateLocal(bv, INIT, true);
1540 case LyXTabular::SET_MPWIDTH:
1542 bool const update = (tabular->GetPWidth(actcell) != value);
1543 tabular->SetMColumnPWidth(actcell,value);
1545 for (int i=0; i < tabular->rows(); ++i) {
1546 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1549 UpdateLocal(bv, INIT, true);
1553 case LyXTabular::SET_SPECIAL_COLUMN:
1554 case LyXTabular::SET_SPECIAL_MULTI:
1555 tabular->SetAlignSpecial(actcell,value,feature);
1557 case LyXTabular::APPEND_ROW:
1558 // append the row into the tabular
1559 UnlockInsetInInset(bv, the_locking_inset);
1560 tabular->AppendRow(actcell);
1561 UpdateLocal(bv, INIT, true);
1563 case LyXTabular::APPEND_COLUMN:
1564 // append the column into the tabular
1565 UnlockInsetInInset(bv, the_locking_inset);
1566 tabular->AppendColumn(actcell);
1567 actcell = tabular->GetCellNumber(row, column);
1568 UpdateLocal(bv, INIT, true);
1570 case LyXTabular::DELETE_ROW:
1571 UnlockInsetInInset(bv, the_locking_inset);
1572 tabular->DeleteRow(tabular->row_of_cell(actcell));
1573 if ((row+1) > tabular->rows())
1575 actcell = tabular->GetCellNumber(row, column);
1577 UpdateLocal(bv, INIT, true);
1579 case LyXTabular::DELETE_COLUMN:
1580 UnlockInsetInInset(bv, the_locking_inset);
1581 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1582 if ((column+1) > tabular->columns())
1584 actcell = tabular->GetCellNumber(row, column);
1586 UpdateLocal(bv, INIT, true);
1588 case LyXTabular::M_TOGGLE_LINE_TOP:
1590 case LyXTabular::TOGGLE_LINE_TOP:
1591 lineSet = !tabular->TopLine(actcell, flag);
1592 for (i=sel_row_start; i<=sel_row_end; ++i)
1593 for (j=sel_col_start; j<=sel_col_end; ++j)
1594 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1595 UpdateLocal(bv, INIT, true);
1598 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1600 case LyXTabular::TOGGLE_LINE_BOTTOM:
1601 lineSet = !tabular->BottomLine(actcell, flag);
1602 for (i=sel_row_start; i<=sel_row_end; ++i)
1603 for (j=sel_col_start; j<=sel_col_end; ++j)
1604 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1606 UpdateLocal(bv, INIT, true);
1609 case LyXTabular::M_TOGGLE_LINE_LEFT:
1611 case LyXTabular::TOGGLE_LINE_LEFT:
1612 lineSet = !tabular->LeftLine(actcell, flag);
1613 for (i=sel_row_start; i<=sel_row_end; ++i)
1614 for (j=sel_col_start; j<=sel_col_end; ++j)
1615 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1617 UpdateLocal(bv, INIT, true);
1620 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1622 case LyXTabular::TOGGLE_LINE_RIGHT:
1623 lineSet = !tabular->RightLine(actcell, flag);
1624 for (i=sel_row_start; i<=sel_row_end; ++i)
1625 for (j=sel_col_start; j<=sel_col_end; ++j)
1626 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1628 UpdateLocal(bv, INIT, true);
1630 case LyXTabular::M_ALIGN_LEFT:
1631 case LyXTabular::M_ALIGN_RIGHT:
1632 case LyXTabular::M_ALIGN_CENTER:
1634 case LyXTabular::ALIGN_LEFT:
1635 case LyXTabular::ALIGN_RIGHT:
1636 case LyXTabular::ALIGN_CENTER:
1637 for (i = sel_row_start; i <= sel_row_end; ++i)
1638 for (j = sel_col_start; j <= sel_col_end; ++j)
1639 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1641 UpdateLocal(bv, INIT, true);
1643 case LyXTabular::M_VALIGN_TOP:
1644 case LyXTabular::M_VALIGN_BOTTOM:
1645 case LyXTabular::M_VALIGN_CENTER:
1647 case LyXTabular::VALIGN_TOP:
1648 case LyXTabular::VALIGN_BOTTOM:
1649 case LyXTabular::VALIGN_CENTER:
1650 for (i = sel_row_start; i <= sel_row_end; ++i)
1651 for (j = sel_col_start; j <= sel_col_end; ++j)
1652 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1654 UpdateLocal(bv, INIT, true);
1656 case LyXTabular::MULTICOLUMN:
1658 if (sel_row_start != sel_row_end) {
1659 WriteAlert(_("Impossible Operation!"),
1660 _("Multicolumns can only be horizontally."),
1664 // just multicol for one Single Cell
1665 if (!hasSelection()) {
1666 // check wether we are completly in a multicol
1667 if (tabular->IsMultiColumn(actcell)) {
1668 tabular->UnsetMultiColumn(actcell);
1669 UpdateLocal(bv, INIT, true);
1671 tabular->SetMultiColumn(actcell, 1);
1672 UpdateLocal(bv, CELL, true);
1676 // we have a selection so this means we just add all this
1677 // cells to form a multicolumn cell
1681 if (sel_cell_start > sel_cell_end) {
1682 s_start = sel_cell_end;
1683 s_end = sel_cell_start;
1685 s_start = sel_cell_start;
1686 s_end = sel_cell_end;
1688 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1690 sel_cell_end = sel_cell_start;
1691 UpdateLocal(bv, INIT, true);
1694 case LyXTabular::SET_ALL_LINES:
1696 case LyXTabular::UNSET_ALL_LINES:
1697 for (i=sel_row_start; i<=sel_row_end; ++i)
1698 for (j=sel_col_start; j<=sel_col_end; ++j)
1699 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1700 UpdateLocal(bv, INIT, true);
1702 case LyXTabular::SET_LONGTABULAR:
1703 tabular->SetLongTabular(true);
1704 UpdateLocal(bv, INIT, true); // because this toggles displayed
1706 case LyXTabular::UNSET_LONGTABULAR:
1707 tabular->SetLongTabular(false);
1708 UpdateLocal(bv, INIT, true); // because this toggles displayed
1710 case LyXTabular::SET_ROTATE_TABULAR:
1711 tabular->SetRotateTabular(true);
1713 case LyXTabular::UNSET_ROTATE_TABULAR:
1714 tabular->SetRotateTabular(false);
1716 case LyXTabular::SET_ROTATE_CELL:
1717 for (i=sel_row_start; i<=sel_row_end; ++i)
1718 for (j=sel_col_start; j<=sel_col_end; ++j)
1719 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1721 case LyXTabular::UNSET_ROTATE_CELL:
1722 for (i = sel_row_start; i <= sel_row_end; ++i)
1723 for (j = sel_col_start; j <= sel_col_end; ++j)
1724 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1726 case LyXTabular::SET_USEBOX:
1728 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1729 if (val == tabular->GetUsebox(actcell))
1730 val = LyXTabular::BOX_NONE;
1731 for (i = sel_row_start; i <= sel_row_end; ++i)
1732 for (j = sel_col_start; j <= sel_col_end; ++j)
1733 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1736 case LyXTabular::SET_LTFIRSTHEAD:
1737 tabular->SetLTHead(actcell, true);
1739 case LyXTabular::SET_LTHEAD:
1740 tabular->SetLTHead(actcell, false);
1742 case LyXTabular::SET_LTFOOT:
1743 tabular->SetLTFoot(actcell, false);
1745 case LyXTabular::SET_LTLASTFOOT:
1746 tabular->SetLTFoot(actcell, true);
1748 case LyXTabular::SET_LTNEWPAGE:
1749 what = !tabular->GetLTNewPage(actcell);
1750 tabular->SetLTNewPage(actcell, what);
1752 // dummy stuff just to avoid warnings
1753 case LyXTabular::LAST_ACTION:
1759 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1762 UpdatableInset * inset =
1763 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1764 LyXFont font(LyXFont::ALL_SANE);
1766 x = inset->x() + inset->width(bv, font);
1767 y = inset->descent(bv, font);
1769 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1770 //inset_y = cursor.y();
1771 inset->Edit(bv, x, y, button);
1772 if (!the_locking_inset)
1774 UpdateLocal(bv, CELL, false);
1775 return (the_locking_inset != 0);
1779 bool InsetTabular::ActivateCellInsetAbs(BufferView * bv, int x, int y,
1782 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1783 inset_y = cursor.y();
1784 return ActivateCellInset(bv, x - inset_x, y - inset_y, button);
1788 bool InsetTabular::InsetHit(BufferView *, int x, int) const
1790 return x + top_x > cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1794 // This returns paperWidth() if the cell-width is unlimited or the width
1795 // in pixels if we have a pwidth for this cell.
1796 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1798 string const s = tabular->GetPWidth(cell);
1802 return VSpace(s).inPixels(0, 0);
1806 int InsetTabular::getMaxWidth(Painter & pain,
1807 UpdatableInset const * inset) const
1809 int const n = tabular->GetNumberOfCells();
1811 for (; cell < n; ++cell) {
1812 if (tabular->GetCellInset(cell) == inset)
1817 int w = GetMaxWidthOfCell(pain, cell);
1819 // because the inset then subtracts it's top_x and owner->x()
1820 w += (inset->x() - top_x);
1825 void InsetTabular::resizeLyXText(BufferView *) const
1831 LyXText * InsetTabular::getLyXText(BufferView const * bv) const
1833 if (the_locking_inset)
1834 return the_locking_inset->getLyXText(bv);
1835 return Inset::getLyXText(bv);
1839 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1841 if (the_locking_inset) {
1842 InsetTabular * i = static_cast<InsetTabular *>
1843 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1845 i->OpenLayoutDialog(bv);
1849 bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1853 // functions returns:
1857 // 3 ... toggled off
1859 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1861 int action = LyXTabular::LAST_ACTION;
1862 LyXFunc::func_status status = LyXFunc::OK;
1865 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1866 string const tmp = tabularFeatures[i].feature;
1867 if (tmp == what.substr(0, tmp.length())) {
1868 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1869 // tabularFeatures[i].feature.length())) {
1870 action = tabularFeatures[i].action;
1874 if (action == LyXTabular::LAST_ACTION)
1875 return LyXFunc::Unknown;
1877 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1884 if (hasSelection()) {
1885 sel_row_start = tabular->row_of_cell(sel_cell_start);
1886 sel_row_end = tabular->row_of_cell(sel_cell_end);
1887 if (sel_row_start > sel_row_end) {
1888 //int tmp = sel_row_start;
1889 //sel_row_start = sel_row_end;
1890 //sel_row_end = tmp;
1891 swap(sel_row_start, sel_row_end);
1894 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1898 case LyXTabular::SET_PWIDTH:
1899 case LyXTabular::SET_MPWIDTH:
1900 case LyXTabular::SET_SPECIAL_COLUMN:
1901 case LyXTabular::SET_SPECIAL_MULTI:
1902 status |= LyXFunc::Disabled;
1905 case LyXTabular::APPEND_ROW:
1906 case LyXTabular::APPEND_COLUMN:
1907 case LyXTabular::DELETE_ROW:
1908 case LyXTabular::DELETE_COLUMN:
1909 case LyXTabular::SET_ALL_LINES:
1910 case LyXTabular::UNSET_ALL_LINES:
1911 status |= LyXFunc::OK;
1914 case LyXTabular::MULTICOLUMN:
1915 if (tabular->IsMultiColumn(actcell))
1916 status |= LyXFunc::ToggleOn;
1918 status |= LyXFunc::ToggleOff;
1920 case LyXTabular::M_TOGGLE_LINE_TOP:
1922 case LyXTabular::TOGGLE_LINE_TOP:
1923 if (tabular->TopLine(actcell, flag))
1924 status |= LyXFunc::ToggleOn;
1926 status |= LyXFunc::ToggleOff;
1928 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1930 case LyXTabular::TOGGLE_LINE_BOTTOM:
1931 if (tabular->BottomLine(actcell, flag))
1932 status |= LyXFunc::ToggleOn;
1934 status |= LyXFunc::ToggleOff;
1936 case LyXTabular::M_TOGGLE_LINE_LEFT:
1938 case LyXTabular::TOGGLE_LINE_LEFT:
1939 if (tabular->LeftLine(actcell, flag))
1940 status |= LyXFunc::ToggleOn;
1942 status |= LyXFunc::ToggleOff;
1944 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1946 case LyXTabular::TOGGLE_LINE_RIGHT:
1947 if (tabular->RightLine(actcell, flag))
1948 status |= LyXFunc::ToggleOn;
1950 status |= LyXFunc::ToggleOff;
1952 case LyXTabular::M_ALIGN_LEFT:
1954 case LyXTabular::ALIGN_LEFT:
1955 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1956 status |= LyXFunc::ToggleOn;
1958 status |= LyXFunc::ToggleOff;
1960 case LyXTabular::M_ALIGN_RIGHT:
1962 case LyXTabular::ALIGN_RIGHT:
1963 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1964 status |= LyXFunc::ToggleOn;
1966 status |= LyXFunc::ToggleOff;
1968 case LyXTabular::M_ALIGN_CENTER:
1970 case LyXTabular::ALIGN_CENTER:
1971 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1972 status |= LyXFunc::ToggleOn;
1974 status |= LyXFunc::ToggleOff;
1976 case LyXTabular::M_VALIGN_TOP:
1978 case LyXTabular::VALIGN_TOP:
1979 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1980 status |= LyXFunc::ToggleOn;
1982 status |= LyXFunc::ToggleOff;
1984 case LyXTabular::M_VALIGN_BOTTOM:
1986 case LyXTabular::VALIGN_BOTTOM:
1987 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1988 status |= LyXFunc::ToggleOn;
1990 status |= LyXFunc::ToggleOff;
1992 case LyXTabular::M_VALIGN_CENTER:
1994 case LyXTabular::VALIGN_CENTER:
1995 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1996 status |= LyXFunc::ToggleOn;
1998 status |= LyXFunc::ToggleOff;
2000 case LyXTabular::SET_LONGTABULAR:
2001 if (tabular->IsLongTabular())
2002 status |= LyXFunc::ToggleOn;
2004 status |= LyXFunc::ToggleOff;
2006 case LyXTabular::UNSET_LONGTABULAR:
2007 if (!tabular->IsLongTabular())
2008 status |= LyXFunc::ToggleOn;
2010 status |= LyXFunc::ToggleOff;
2012 case LyXTabular::SET_ROTATE_TABULAR:
2013 if (tabular->GetRotateTabular())
2014 status |= LyXFunc::ToggleOn;
2016 status |= LyXFunc::ToggleOff;
2018 case LyXTabular::UNSET_ROTATE_TABULAR:
2019 if (!tabular->GetRotateTabular())
2020 status |= LyXFunc::ToggleOn;
2022 status |= LyXFunc::ToggleOff;
2024 case LyXTabular::SET_ROTATE_CELL:
2025 if (tabular->GetRotateCell(actcell))
2026 status |= LyXFunc::ToggleOn;
2028 status |= LyXFunc::ToggleOff;
2030 case LyXTabular::UNSET_ROTATE_CELL:
2031 if (!tabular->GetRotateCell(actcell))
2032 status |= LyXFunc::ToggleOn;
2034 status |= LyXFunc::ToggleOff;
2036 case LyXTabular::SET_USEBOX:
2037 if (strToInt(argument) == tabular->GetUsebox(actcell))
2038 status |= LyXFunc::ToggleOn;
2040 status |= LyXFunc::ToggleOff;
2042 case LyXTabular::SET_LTFIRSTHEAD:
2043 if (tabular->GetRowOfLTHead(actcell, dummy))
2044 status |= LyXFunc::ToggleOn;
2046 status |= LyXFunc::ToggleOff;
2048 case LyXTabular::SET_LTHEAD:
2049 if (tabular->GetRowOfLTHead(actcell, dummy))
2050 status |= LyXFunc::ToggleOn;
2052 status |= LyXFunc::ToggleOff;
2054 case LyXTabular::SET_LTFOOT:
2055 if (tabular->GetRowOfLTFoot(actcell, dummy))
2056 status |= LyXFunc::ToggleOn;
2058 status |= LyXFunc::ToggleOff;
2060 case LyXTabular::SET_LTLASTFOOT:
2061 if (tabular->GetRowOfLTFoot(actcell, dummy))
2062 status |= LyXFunc::ToggleOn;
2064 status |= LyXFunc::ToggleOff;
2066 case LyXTabular::SET_LTNEWPAGE:
2067 if (tabular->GetLTNewPage(actcell))
2068 status |= LyXFunc::ToggleOn;
2070 status |= LyXFunc::ToggleOff;
2073 status = LyXFunc::Disabled;
2080 bool InsetTabular::copySelection(BufferView * bv)
2082 if (!hasSelection())
2084 delete paste_tabular;
2091 sel_col_start = tabular->column_of_cell(sel_cell_start);
2092 sel_col_end = tabular->column_of_cell(sel_cell_end);
2093 if (sel_col_start > sel_col_end) {
2094 sel_col_start = sel_col_end;
2095 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2097 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2099 int columns = sel_col_end - sel_col_start + 1;
2101 sel_row_start = tabular->row_of_cell(sel_cell_start);
2102 sel_row_end = tabular->row_of_cell(sel_cell_end);
2103 if (sel_row_start > sel_row_end) {
2104 //int tmp tmp = sel_row_start;
2105 //sel_row_start = sel_row_end;
2106 //sel_row_end = tmp;
2107 swap(sel_row_start, sel_row_end);
2109 int rows = sel_row_end - sel_row_start + 1;
2111 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2113 for (i=0; i < sel_row_start; ++i)
2114 paste_tabular->DeleteRow(0);
2115 while(paste_tabular->rows() > rows)
2116 paste_tabular->DeleteRow(rows);
2117 paste_tabular->SetTopLine(0, true, true);
2118 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2120 for (i=0; i < sel_col_start; ++i)
2121 paste_tabular->DeleteColumn(0);
2122 while(paste_tabular->columns() > columns)
2123 paste_tabular->DeleteColumn(columns);
2124 paste_tabular->SetLeftLine(0, true, true);
2125 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2128 paste_tabular->Ascii(bv->buffer(), sstr);
2129 bv->stuffClipboard(sstr.str().c_str());
2134 bool InsetTabular::pasteSelection(BufferView * bv)
2139 for (int r1 = 0, r2 = actrow;
2140 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2143 for(int c1 = 0, c2 = actcol;
2144 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2147 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2148 tabular->IsPartOfMultiColumn(r2,c2))
2150 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2154 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2158 int n1 = paste_tabular->GetCellNumber(r1, c1);
2159 int n2 = tabular->GetCellNumber(r2, c2);
2160 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2161 tabular->GetCellInset(n2)->setOwner(this);
2162 tabular->GetCellInset(n2)->deleteLyXText(bv);
2169 bool InsetTabular::cutSelection()
2171 if (!hasSelection())
2179 sel_col_start = tabular->column_of_cell(sel_cell_start);
2180 sel_col_end = tabular->column_of_cell(sel_cell_end);
2181 if (sel_col_start > sel_col_end) {
2182 sel_col_start = sel_col_end;
2183 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2185 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2187 sel_row_start = tabular->row_of_cell(sel_cell_start);
2188 sel_row_end = tabular->row_of_cell(sel_cell_end);
2189 if (sel_row_start > sel_row_end) {
2190 //int tmp = sel_row_start;
2191 //sel_row_start = sel_row_end;
2192 //sel_row_end = tmp;
2193 swap(sel_row_start, sel_row_end);
2195 if (sel_cell_start > sel_cell_end) {
2196 //int tmp = sel_cell_start;
2197 //sel_cell_start = sel_cell_end;
2198 //sel_cell_end = tmp;
2199 swap(sel_cell_start, sel_cell_end);
2201 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2202 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2203 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2209 bool InsetTabular::isRightToLeft(BufferView *bv )
2211 return bv->getParentLanguage(this)->RightToLeft();