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, "" }
114 bool cellstart(LyXParagraph::size_type p)
116 return ((p % 2) == 0);
120 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
127 tabular = new LyXTabular(this, rows,columns);
128 // for now make it always display as display() inset
130 the_locking_inset = 0;
131 locked = no_selection = cursor_visible = false;
134 actrow = actcell = 0;
141 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
144 tabular = new LyXTabular(this, *(tab.tabular));
145 the_locking_inset = 0;
146 locked = no_selection = cursor_visible = false;
149 actrow = actcell = 0;
151 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
156 InsetTabular::~InsetTabular()
163 Inset * InsetTabular::Clone(Buffer const & buf) const
165 InsetTabular * t = new InsetTabular(*this, buf);
167 t->tabular = tabular->Clone(t);
172 void InsetTabular::Write(Buffer const * buf, ostream & os) const
174 os << " Tabular" << endl;
175 tabular->Write(buf, os);
179 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
181 bool old_format = (lex.GetString() == "\\LyXTable");
186 tabular = new LyXTabular(buf, this, lex);
194 token = lex.GetString();
195 while (lex.IsOK() && (token != "\\end_inset")) {
197 token = lex.GetString();
199 if (token != "\\end_inset") {
200 lex.printError("Missing \\end_inset at this point. "
206 int InsetTabular::ascent(BufferView *, LyXFont const &) const
208 return tabular->GetAscentOfRow(0);
212 int InsetTabular::descent(BufferView *, LyXFont const &) const
214 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
218 int InsetTabular::width(BufferView *, LyXFont const &) const
220 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
224 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
225 float & x, bool cleared) const
227 Painter & pain = bv->painter();
231 UpdatableInset::draw(bv, font, baseline, x, cleared);
232 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
233 (top_x != int(x)) || (top_baseline != baseline))) {
234 int h = ascent(bv, font) + descent(bv, font);
235 int tx = display() || !owner() ? 0 : top_x;
236 int w = tx ? width(bv, font) : pain.paperWidth();
237 int ty = baseline - ascent(bv, font);
241 if ((ty + h) > pain.paperHeight())
242 h = pain.paperHeight();
243 if ((top_x + w) > pain.paperWidth())
244 w = pain.paperWidth();
245 pain.fillRectangle(tx, ty, w, h);
250 top_baseline = baseline;
251 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
254 x += ADD_TO_TABULAR_WIDTH;
258 for (i = 0; i < tabular->rows(); ++i) {
260 dodraw = ((baseline + tabular->GetDescentOfRow(i)) > 0) &&
261 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight();
262 for (j = 0; j < tabular->columns(); ++j) {
263 if (tabular->IsPartOfMultiColumn(i, j))
265 cx = nx + tabular->GetBeginningOfTextInCell(cell);
268 DrawCellSelection(pain, nx, baseline, i, j, cell);
269 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
271 DrawCellLines(pain, nx, baseline, i, cell);
273 nx += tabular->GetWidthOfColumn(cell);
276 baseline += tabular->GetDescentOfRow(i) +
277 tabular->GetAscentOfRow(i + 1) +
278 tabular->GetAdditionalHeight(cell);
280 } else if (need_update == CELL) {
283 if (the_locking_inset) {
284 Inset * inset = tabular->GetCellInset(cell);
285 for (i = 0; (inset != the_locking_inset) && (i < tabular->rows());
288 for (j = 0; (inset != the_locking_inset) && (j < tabular->columns()); ++j)
290 if (tabular->IsPartOfMultiColumn(i, j))
292 nx += tabular->GetWidthOfColumn(cell);
294 inset = tabular->GetCellInset(cell);
296 if (tabular->row_of_cell(cell) > i) {
298 baseline += tabular->GetDescentOfRow(i) +
299 tabular->GetAscentOfRow(i + 1) +
300 tabular->GetAdditionalHeight(cell);
304 for (i = 0; (cell < actcell) && (i < tabular->rows()); ++i) {
305 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
306 if (tabular->IsPartOfMultiColumn(i, j))
308 nx += tabular->GetWidthOfColumn(cell);
311 if (tabular->row_of_cell(cell) > i) {
313 baseline += tabular->GetDescentOfRow(i) +
314 tabular->GetAscentOfRow(i + 1) +
315 tabular->GetAdditionalHeight(cell);
319 i = tabular->row_of_cell(cell);
320 if (the_locking_inset != tabular->GetCellInset(cell)) {
321 lyxerr[Debug::INSETS] << "ERROR this shouldn't happen\n";
324 // LyXText::text_status st = bv->text->status;
327 float cx = nx + tabular->GetBeginningOfTextInCell(cell);
328 bv->text->status = st;
329 if (need_update == CELL) {
330 // clear before the inset
333 baseline - tabular->GetAscentOfRow(i) + 1,
335 tabular->GetAscentOfRow(i) +
336 tabular->GetDescentOfRow(i) - 1);
337 // clear behind the inset
339 int(cx + the_locking_inset->width(bv,font) + 1),
340 baseline - tabular->GetAscentOfRow(i) + 1,
341 tabular->GetWidthOfColumn(cell) -
342 tabular->GetBeginningOfTextInCell(cell) -
343 the_locking_inset->width(bv,font) -
344 tabular->GetAdditionalWidth(cell) - 1,
345 tabular->GetAscentOfRow(i) +
346 tabular->GetDescentOfRow(i) - 1);
348 tabular->GetCellInset(cell)->draw(bv,font,baseline, cx, false);
349 } while(bv->text->status == LyXText::CHANGED_IN_DRAW);
353 cx = dx = nx + tabular->GetBeginningOfTextInCell(cell);
354 tabular->GetCellInset(cell)->draw(bv,font,baseline, dx, false);
355 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
357 // clear only if we didn't have a change
358 if (need_update == CELL) {
359 // clear before the inset
362 baseline - tabular->GetAscentOfRow(i) + 1,
364 tabular->GetAscentOfRow(i) +
365 tabular->GetDescentOfRow(i) - 1);
366 // clear behind the inset
368 int(cx + the_locking_inset->width(bv,font) + 1),
369 baseline - tabular->GetAscentOfRow(i) + 1,
370 tabular->GetWidthOfColumn(cell) -
371 tabular->GetBeginningOfTextInCell(cell) -
372 the_locking_inset->width(bv,font) -
373 tabular->GetAdditionalWidth(cell) - 1,
374 tabular->GetAscentOfRow(i) +
375 tabular->GetDescentOfRow(i) - 1);
379 x -= ADD_TO_TABULAR_WIDTH;
380 x += width(bv, font);
381 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
388 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
389 int row, int cell) const
391 int x2 = x + tabular->GetWidthOfColumn(cell);
394 if (!tabular->TopAlreadyDrawed(cell)) {
395 on_off = !tabular->TopLine(cell);
396 pain.line(x, baseline - tabular->GetAscentOfRow(row),
397 x2, baseline - tabular->GetAscentOfRow(row),
398 on_off ? LColor::tabularonoffline : LColor::tabularline,
399 on_off ? Painter::line_onoffdash : Painter::line_solid);
401 on_off = !tabular->BottomLine(cell);
402 pain.line(x,baseline + tabular->GetDescentOfRow(row),
403 x2, baseline + tabular->GetDescentOfRow(row),
404 on_off ? LColor::tabularonoffline : LColor::tabularline,
405 on_off ? Painter::line_onoffdash : Painter::line_solid);
406 if (!tabular->LeftAlreadyDrawed(cell)) {
407 on_off = !tabular->LeftLine(cell);
408 pain.line(x, baseline - tabular->GetAscentOfRow(row),
409 x, baseline + tabular->GetDescentOfRow(row),
410 on_off ? LColor::tabularonoffline : LColor::tabularline,
411 on_off ? Painter::line_onoffdash : Painter::line_solid);
413 on_off = !tabular->RightLine(cell);
414 pain.line(x2 - tabular->GetAdditionalWidth(cell),
415 baseline - tabular->GetAscentOfRow(row),
416 x2 - tabular->GetAdditionalWidth(cell),
417 baseline + tabular->GetDescentOfRow(row),
418 on_off ? LColor::tabularonoffline : LColor::tabularline,
419 on_off ? Painter::line_onoffdash : Painter::line_solid);
423 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
424 int row, int column, int cell) const
426 int cs = tabular->column_of_cell(sel_cell_start);
427 int ce = tabular->column_of_cell(sel_cell_end);
430 cs = tabular->column_of_cell(sel_cell_end);
432 ce = tabular->right_column_of_cell(sel_cell_end);
435 int rs = tabular->row_of_cell(sel_cell_start);
436 int re = tabular->row_of_cell(sel_cell_end);
437 if (rs > re) swap(rs, re);
439 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
440 int w = tabular->GetWidthOfColumn(cell);
441 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
442 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
443 w, h, LColor::selection);
448 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
452 calculate_dimensions_of_cells(bv, font, true);
454 owner()->update(bv, font, true);
457 if (the_locking_inset) {
458 the_locking_inset->update(bv, font, reinit);
460 // inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
461 // inset_y = cursor.y();
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;
499 sel_pos_start = sel_pos_end = cursor.pos();
500 sel_cell_start = sel_cell_end = actcell;
501 bv->text->FinishUndo();
502 if (InsetHit(bv, x, y) && (button != 3)) {
503 ActivateCellInset(bv, x, y, button);
505 // UpdateLocal(bv, NONE, false);
506 // bv->getOwner()->getPopups().updateFormTabular();
510 void InsetTabular::InsetUnlock(BufferView * bv)
512 if (the_locking_inset) {
513 the_locking_inset->InsetUnlock(bv);
514 the_locking_inset = 0;
517 no_selection = false;
520 if (scroll() || hasSelection()) {
524 sel_pos_start = sel_pos_end = 0;
525 sel_cell_start = sel_cell_end = 0;
527 UpdateLocal(bv, FULL, false);
532 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
533 bool mark_dirty) const
535 if (need_update < what) // only set this if it has greater update
537 if ((what == INIT) && hasSelection())
540 if (need_update != NONE) {
541 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
542 if (locked) // && (what != NONE))
548 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
550 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
554 if (inset == tabular->GetCellInset(actcell)) {
555 lyxerr[Debug::INSETS] << "OK" << endl;
556 the_locking_inset = tabular->GetCellInset(actcell);
558 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
559 inset_y = cursor.y();
560 inset_pos = cursor.pos();
562 } else if (the_locking_inset && (the_locking_inset == inset)) {
563 if (cursor.pos() == inset_pos) {
564 lyxerr[Debug::INSETS] << "OK" << endl;
566 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
567 inset_y = cursor.y();
569 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
571 } else if (the_locking_inset) {
572 lyxerr[Debug::INSETS] << "MAYBE" << endl;
573 return the_locking_inset->LockInsetInInset(bv, inset);
575 lyxerr[Debug::INSETS] << "NOT OK" << endl;
580 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
583 if (!the_locking_inset)
585 if (the_locking_inset == inset) {
586 the_locking_inset->InsetUnlock(bv);
587 the_locking_inset = 0;
589 moveRight(bv, false);
590 ShowInsetCursor(bv, false);
591 UpdateLocal(bv, CELL, false);
594 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
595 if (inset->LyxCode() == TABULAR_CODE &&
596 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
598 bv->owner()->getDialogs()->updateTabular(this);
607 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
609 if (!the_locking_inset)
611 if (the_locking_inset != inset) {
612 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
615 UpdateLocal(bv, CELL, false);
620 unsigned int InsetTabular::InsetInInsetY()
622 if (!the_locking_inset)
625 return (inset_y + the_locking_inset->InsetInInsetY());
629 UpdatableInset * InsetTabular::GetLockingInset()
631 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
635 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
639 if (the_locking_inset)
640 return the_locking_inset->GetFirstLockingInsetOfType(c);
645 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
647 if (the_locking_inset)
648 return the_locking_inset->InsertInset(bv, inset);
653 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
655 if (hasSelection() && (button == 3))
658 if (hasSelection()) {
660 UpdateLocal(bv, SELECTION, false);
663 no_selection = false;
665 int const ocell = actcell;
666 int const orow = actrow;
671 UpdateLocal(bv, NONE, false);
672 sel_pos_start = sel_pos_end = cursor.pos();
673 sel_cell_start = sel_cell_end = actcell;
675 if ((ocell != actcell) && the_locking_inset) {
676 the_locking_inset->InsetUnlock(bv);
677 the_locking_inset = 0;
683 bool const inset_hit = InsetHit(bv, x, y);
685 if ((ocell == actcell) && the_locking_inset && inset_hit) {
686 cursor.pos(0); // always before the inset!
688 the_locking_inset->InsetButtonPress(bv,
689 x - inset_x, y - inset_y, button);
691 } else if (the_locking_inset) {
692 the_locking_inset->InsetUnlock(bv);
694 the_locking_inset = 0;
696 LocalDispatch(bv, LFUN_PASTESELECTION, "paragraph");
699 if (inset_hit && bv->theLockingInset()) {
700 if (ActivateCellInset(bv, x, y, button))
701 the_locking_inset->InsetButtonPress(bv, x - inset_x,
702 y - inset_y, button);
709 void InsetTabular::InsetButtonRelease(BufferView * bv,
710 int x, int y, int button)
713 if (the_locking_inset) {
715 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
716 i->InsetButtonRelease(bv, x, y, button);
720 bv->owner()->getDialogs()->showTabular(this);
723 if (the_locking_inset) {
724 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
727 no_selection = false;
731 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
733 if (the_locking_inset) {
734 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
735 y - inset_y, button);
740 LyXParagraph::size_type const old_pos = sel_pos_end;
741 int const old_cell = actcell;
744 sel_pos_end = cursor.pos();
745 sel_cell_end = actcell;
746 if ((sel_cell_end != old_cell) || (old_pos != sel_pos_end))
747 UpdateLocal(bv, SELECTION, false);
750 no_selection = false;
754 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
756 if (the_locking_inset) {
757 the_locking_inset->InsetKeyPress(xke);
763 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
766 UpdatableInset::RESULT
769 no_selection = false;
770 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
771 || (result == DISPATCHED_NOUPDATE)) {
777 if ((action < 0) && arg.empty())
780 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
781 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
783 if (the_locking_inset) {
784 result=the_locking_inset->LocalDispatch(bv, action, arg);
785 if (result == DISPATCHED_NOUPDATE)
787 else if (result == DISPATCHED) {
788 the_locking_inset->ToggleInsetCursor(bv);
789 UpdateLocal(bv, CELL, false);
790 the_locking_inset->ToggleInsetCursor(bv);
792 } else if (result == FINISHED) {
794 if ((action == LFUN_RIGHT) || (action == -1)) {
795 cursor.pos(inset_pos + 1);
797 } else if (action == LFUN_DOWN) {
798 if (moveDown(bv) == FINISHED) {
799 bv->unlockInset(this);
802 } else if (action == LFUN_UP) {
803 if (moveUp(bv) == FINISHED) {
804 bv->unlockInset(this);
808 sel_pos_start = sel_pos_end = cursor.pos();
809 sel_cell_start = sel_cell_end = actcell;
816 bool hs = hasSelection();
820 // --- Cursor Movements ---------------------------------------------
822 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
824 moveRight(bv, false);
826 if (!cellstart(cursor.pos())) {
827 sel_pos_end = cursor.pos();
828 if (tabular->right_column_of_cell(sel_cell_start) >
829 tabular->right_column_of_cell(actcell))
830 sel_cell_end = actcell+1;
832 sel_cell_end = actcell;
835 sel_pos_end = cursor.pos();
836 sel_cell_end = actcell;
838 UpdateLocal(bv, SELECTION, false);
841 result = moveRight(bv);
842 sel_pos_start = sel_pos_end = cursor.pos();
843 sel_cell_start = sel_cell_end = actcell;
845 UpdateLocal(bv, SELECTION, false);
848 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
852 if (cellstart(cursor.pos())) {
853 sel_pos_end = cursor.pos();
854 if (tabular->column_of_cell(sel_cell_start) >=
855 tabular->column_of_cell(actcell))
856 sel_cell_end = actcell;
858 sel_cell_end = actcell-1;
861 sel_pos_end = cursor.pos();
862 sel_cell_end = actcell;
864 UpdateLocal(bv, SELECTION, false);
867 result = moveLeft(bv);
868 sel_pos_start = sel_pos_end = cursor.pos();
869 sel_cell_start = sel_cell_end = actcell;
871 UpdateLocal(bv, SELECTION, false);
875 int const ocell = actcell;
877 sel_pos_end = cursor.pos();
878 if ((ocell == sel_cell_end) ||
879 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
880 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
882 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
883 UpdateLocal(bv, SELECTION, false);
887 result = moveDown(bv);
888 sel_pos_start = sel_pos_end = cursor.pos();
889 sel_cell_start = sel_cell_end = actcell;
891 UpdateLocal(bv, SELECTION, false);
895 int const ocell = actcell;
897 sel_pos_end = cursor.pos();
898 if ((ocell == sel_cell_end) ||
899 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
900 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
902 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
903 UpdateLocal(bv, SELECTION, false);
908 sel_pos_start = sel_pos_end = cursor.pos();
909 sel_cell_start = sel_cell_end = actcell;
911 UpdateLocal(bv, SELECTION, false);
923 if (the_locking_inset) {
924 UnlockInsetInInset(bv, the_locking_inset);
925 the_locking_inset = 0;
927 if (action == LFUN_TAB)
931 sel_pos_start = sel_pos_end = cursor.pos();
932 sel_cell_start = sel_cell_end = actcell;
934 UpdateLocal(bv, SELECTION, false);
936 case LFUN_LAYOUT_TABULAR:
938 bv->owner()->getDialogs()->showTabular(this);
941 case LFUN_TABULAR_FEATURE:
942 if (!TabularFeatures(bv, arg))
943 result = UNDISPATCHED;
946 if (!copySelection(bv))
948 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
950 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
951 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
953 bv->text->cursor.par()->previous,
954 bv->text->cursor.par()->next
958 UpdateLocal(bv, INIT, true);
963 bv->text->FinishUndo();
966 case LFUN_PASTESELECTION:
968 string clip(bv->workarea()->getClipboard());
972 if (clip.find('\t') != string::npos) {
976 unsigned int len = clip.length();
977 string::size_type p = 0;
980 ((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)
1009 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1014 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1015 while(cols++ < maxCols)
1023 // check for the last cell if there is no trailing '\n'
1024 if ((cell < cells) && (op < len))
1025 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1027 // so that the clipboard is used and it goes on to default
1028 // and executes LFUN_PASTESELECTION in insettext!
1029 delete paste_tabular;
1034 if (hasPasteBuffer()) {
1035 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1037 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1038 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1040 bv->text->cursor.par()->previous,
1041 bv->text->cursor.par()->next
1045 UpdateLocal(bv, INIT, true);
1048 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1050 // we try to activate the actual inset and put this event down to
1051 // the insets dispatch function.
1052 result = UNDISPATCHED;
1053 if (the_locking_inset)
1055 if (ActivateCellInset(bv, 0, 0, 0, !cellstart(cursor.pos()))) {
1056 result=the_locking_inset->LocalDispatch(bv, action, arg);
1057 if (result == DISPATCHED_NOUPDATE)
1059 else if (result == DISPATCHED) {
1060 the_locking_inset->ToggleInsetCursor(bv);
1061 UpdateLocal(bv, CELL, false);
1062 the_locking_inset->ToggleInsetCursor(bv);
1065 result = UNDISPATCHED;
1070 if (result!=FINISHED) {
1071 if (!the_locking_inset) {
1072 ShowInsetCursor(bv);
1075 bv->unlockInset(this);
1080 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1081 bool fragile, bool fp) const
1083 return tabular->Latex(buf, os, fragile, fp);
1087 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1089 // This should be changed to a real ascii export
1090 return tabular->Ascii(buf, os);
1094 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1100 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1102 return tabular->DocBook(buf,os);
1106 void InsetTabular::Validate(LaTeXFeatures & features) const
1108 tabular->Validate(features);
1112 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1113 LyXFont const & font,
1120 bool changed = false;
1122 // if we have a locking_inset we should have to check only this cell for
1123 // change so I'll try this to have a boost, but who knows ;)
1124 if ((need_update != INIT) &&
1125 (the_locking_inset == tabular->GetCellInset(actcell))) {
1126 for(int i = 0; i < tabular->columns(); ++i) {
1127 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1129 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1132 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1133 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1134 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1137 for (int i = 0; i < tabular->rows(); ++i) {
1140 for (int j= 0; j < tabular->columns(); ++j) {
1141 if (tabular->IsPartOfMultiColumn(i,j))
1144 inset = tabular->GetCellInset(cell);
1146 inset->update(bv, font, false);
1147 maxAsc = max(maxAsc, inset->ascent(bv, font));
1148 maxDesc = max(maxDesc, inset->descent(bv, font));
1149 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1151 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1152 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1158 void InsetTabular::GetCursorPos(BufferView *,
1159 int & x, int & y) const
1161 x = cursor.x() - top_x;
1166 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1168 if (the_locking_inset) {
1169 the_locking_inset->ToggleInsetCursor(bv);
1173 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1175 int const asc = lyxfont::maxAscent(font);
1176 int const desc = lyxfont::maxDescent(font);
1179 bv->hideLockedInsetCursor();
1181 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1182 cursor_visible = !cursor_visible;
1186 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1188 if (!cursor_visible) {
1189 LyXFont font; // = GetFont(par, cursor.pos);
1191 int const asc = lyxfont::maxAscent(font);
1192 int const desc = lyxfont::maxDescent(font);
1193 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1195 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1196 cursor_visible = true;
1201 void InsetTabular::HideInsetCursor(BufferView * bv)
1203 if (cursor_visible) {
1204 bv->hideLockedInsetCursor();
1205 cursor_visible = false;
1207 // if (cursor_visible)
1208 // ToggleInsetCursor(bv);
1212 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1217 actcell = actrow = actcol = 0;
1218 int ly = tabular->GetDescentOfRow(actrow);
1220 // first search the right row
1221 while((ly < y) && (actrow < tabular->rows())) {
1222 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1223 tabular->GetAscentOfRow(actrow + 1) +
1224 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1227 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1229 actcell = tabular->GetCellNumber(actrow, actcol);
1231 // now search the right column
1232 int lx = tabular->GetWidthOfColumn(actcell) -
1233 tabular->GetAdditionalWidth(actcell);
1235 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1236 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1237 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1238 tabular->GetAdditionalWidth(actcell - 1));
1240 // Jürgen, you should check that this is correct. (Lgb)
1241 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1242 lx += tabular->GetWidthOfColumn(actcell + 1)
1243 + tabular->GetAdditionalWidth(actcell);
1249 if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1250 cursor.x(lx + top_x - 2);
1253 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1259 int InsetTabular::getCellXPos(int cell) const
1263 for (; !tabular->IsFirstCellInRow(c); --c)
1265 int lx = tabular->GetWidthOfColumn(cell);
1266 for (; c < cell; ++c) {
1267 lx += tabular->GetWidthOfColumn(c);
1269 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1273 void InsetTabular::resetPos(BufferView * bv) const
1277 actcol = tabular->column_of_cell(actcell);
1282 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1283 if (tabular->IsLastCellInRow(cell)) {
1284 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1285 tabular->GetAscentOfRow(actrow + 1) +
1286 tabular->GetAdditionalHeight(cell + 1));
1290 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1291 int new_x = getCellXPos(actcell);
1292 int old_x = cursor.x();
1295 // cursor.x(getCellXPos(actcell) + offset);
1296 if (((cursor.x() - offset) > 20) &&
1297 ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1298 (bv->workWidth()-20)))
1300 scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1301 UpdateLocal(bv, FULL, false);
1302 } else if ((cursor.x() - offset) < 20) {
1303 scroll(bv, 20 - cursor.x() + offset);
1304 UpdateLocal(bv, FULL, false);
1305 } else if (!cellstart(cursor.pos())) {
1306 LyXFont font(LyXFont::ALL_SANE);
1307 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1308 tabular->GetBeginningOfTextInCell(actcell));
1309 } else if (scroll() && (top_x > 20) &&
1310 ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1311 scroll(bv, old_x - cursor.x());
1313 if ((!the_locking_inset ||
1314 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1315 (actcell != oldcell)) {
1316 InsetTabular * inset = const_cast<InsetTabular *>(this);
1317 bv->owner()->getDialogs()->updateTabular(inset);
1323 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1325 if (!cellstart(cursor.pos())) {
1326 if (tabular->IsLastCell(actcell))
1331 if (ActivateCellInset(bv))
1333 } else { // before the inset
1337 return DISPATCHED_NOUPDATE;
1341 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1343 if (cellstart(cursor.pos()) && !actcell)
1345 if (cellstart(cursor.pos())) {
1348 } else if (lock) { // behind the inset
1350 if (ActivateCellInset(bv, 0, 0, 0, true))
1356 return DISPATCHED_NOUPDATE;
1360 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1362 int const ocell = actcell;
1363 actcell = tabular->GetCellAbove(actcell);
1364 if (actcell == ocell) // we moved out of the inset
1367 return DISPATCHED_NOUPDATE;
1371 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1373 int const ocell = actcell;
1374 actcell = tabular->GetCellBelow(actcell);
1375 if (actcell == ocell) // we moved out of the inset
1378 return DISPATCHED_NOUPDATE;
1382 bool InsetTabular::moveNextCell(BufferView * bv)
1384 if (tabular->IsLastCell(actcell))
1392 bool InsetTabular::movePrevCell(BufferView * bv)
1394 if (!actcell) // first cell
1402 bool InsetTabular::Delete()
1408 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1410 if (the_locking_inset)
1411 the_locking_inset->SetFont(bv, font, tall);
1415 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1417 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1420 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1421 string const tmp = tabularFeatures[i].feature;
1423 if (tmp == what.substr(0, tmp.length())) {
1424 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1425 //tabularFeatures[i].feature.length())) {
1426 action = tabularFeatures[i].action;
1430 if (action == LyXTabular::LAST_ACTION)
1434 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1435 TabularFeatures(bv, action, val);
1440 void InsetTabular::TabularFeatures(BufferView * bv,
1441 LyXTabular::Feature feature,
1442 string const & value)
1451 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1452 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1457 case LyXTabular::M_ALIGN_LEFT:
1458 case LyXTabular::ALIGN_LEFT:
1459 setAlign=LYX_ALIGN_LEFT;
1461 case LyXTabular::M_ALIGN_RIGHT:
1462 case LyXTabular::ALIGN_RIGHT:
1463 setAlign=LYX_ALIGN_RIGHT;
1465 case LyXTabular::M_ALIGN_CENTER:
1466 case LyXTabular::ALIGN_CENTER:
1467 setAlign=LYX_ALIGN_CENTER;
1469 case LyXTabular::M_VALIGN_TOP:
1470 case LyXTabular::VALIGN_TOP:
1471 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1473 case LyXTabular::M_VALIGN_BOTTOM:
1474 case LyXTabular::VALIGN_BOTTOM:
1475 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1477 case LyXTabular::M_VALIGN_CENTER:
1478 case LyXTabular::VALIGN_CENTER:
1479 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1484 if (hasSelection()) {
1485 sel_col_start = tabular->column_of_cell(sel_cell_start);
1486 sel_col_end = tabular->column_of_cell(sel_cell_end);
1487 if (sel_col_start > sel_col_end) {
1488 sel_col_end = sel_col_start;
1489 sel_col_start = tabular->column_of_cell(sel_cell_end);
1491 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1494 sel_row_start = tabular->row_of_cell(sel_cell_start);
1495 sel_row_end = tabular->row_of_cell(sel_cell_end);
1496 if (sel_row_start > sel_row_end) {
1497 //int tmp = sel_row_start;
1498 //sel_row_start = sel_row_end;
1499 //sel_row_end = tmp;
1500 swap(sel_row_start, sel_row_end);
1503 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1504 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1506 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1508 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1509 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1511 bv->text->cursor.par()->previous,
1512 bv->text->cursor.par()->next
1516 int row = tabular->row_of_cell(actcell);
1517 int column = tabular->column_of_cell(actcell);
1521 case LyXTabular::SET_PWIDTH:
1523 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1524 tabular->SetColumnPWidth(actcell,value);
1526 for (int i=0; i < tabular->rows(); ++i) {
1527 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1530 UpdateLocal(bv, INIT, true);
1534 case LyXTabular::SET_MPWIDTH:
1536 bool const update = (tabular->GetPWidth(actcell) != value);
1537 tabular->SetMColumnPWidth(actcell,value);
1539 for (int i=0; i < tabular->rows(); ++i) {
1540 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1543 UpdateLocal(bv, INIT, true);
1547 case LyXTabular::SET_SPECIAL_COLUMN:
1548 case LyXTabular::SET_SPECIAL_MULTI:
1549 tabular->SetAlignSpecial(actcell,value,feature);
1551 case LyXTabular::APPEND_ROW:
1552 // append the row into the tabular
1553 UnlockInsetInInset(bv, the_locking_inset);
1554 tabular->AppendRow(actcell);
1555 UpdateLocal(bv, INIT, true);
1557 case LyXTabular::APPEND_COLUMN:
1558 // append the column into the tabular
1559 tabular->AppendColumn(actcell);
1560 actcell = tabular->GetCellNumber(row, column);
1561 UpdateLocal(bv, INIT, true);
1563 case LyXTabular::DELETE_ROW:
1564 tabular->DeleteRow(tabular->row_of_cell(actcell));
1565 if ((row+1) > tabular->rows())
1567 actcell = tabular->GetCellNumber(row, column);
1569 UpdateLocal(bv, INIT, true);
1571 case LyXTabular::DELETE_COLUMN:
1572 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1573 if ((column+1) > tabular->columns())
1575 actcell = tabular->GetCellNumber(row, column);
1577 UpdateLocal(bv, INIT, true);
1579 case LyXTabular::M_TOGGLE_LINE_TOP:
1581 case LyXTabular::TOGGLE_LINE_TOP:
1582 lineSet = !tabular->TopLine(actcell, flag);
1583 for (i=sel_row_start; i<=sel_row_end; ++i)
1584 for (j=sel_col_start; j<=sel_col_end; ++j)
1585 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1586 UpdateLocal(bv, INIT, true);
1589 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1591 case LyXTabular::TOGGLE_LINE_BOTTOM:
1592 lineSet = !tabular->BottomLine(actcell, flag);
1593 for (i=sel_row_start; i<=sel_row_end; ++i)
1594 for (j=sel_col_start; j<=sel_col_end; ++j)
1595 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1597 UpdateLocal(bv, INIT, true);
1600 case LyXTabular::M_TOGGLE_LINE_LEFT:
1602 case LyXTabular::TOGGLE_LINE_LEFT:
1603 lineSet = !tabular->LeftLine(actcell, flag);
1604 for (i=sel_row_start; i<=sel_row_end; ++i)
1605 for (j=sel_col_start; j<=sel_col_end; ++j)
1606 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1608 UpdateLocal(bv, INIT, true);
1611 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1613 case LyXTabular::TOGGLE_LINE_RIGHT:
1614 lineSet = !tabular->RightLine(actcell, flag);
1615 for (i=sel_row_start; i<=sel_row_end; ++i)
1616 for (j=sel_col_start; j<=sel_col_end; ++j)
1617 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1619 UpdateLocal(bv, INIT, true);
1621 case LyXTabular::M_ALIGN_LEFT:
1622 case LyXTabular::M_ALIGN_RIGHT:
1623 case LyXTabular::M_ALIGN_CENTER:
1625 case LyXTabular::ALIGN_LEFT:
1626 case LyXTabular::ALIGN_RIGHT:
1627 case LyXTabular::ALIGN_CENTER:
1628 for (i = sel_row_start; i <= sel_row_end; ++i)
1629 for (j = sel_col_start; j <= sel_col_end; ++j)
1630 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1633 UpdateLocal(bv, INIT, true);
1635 UpdateLocal(bv, CELL, true);
1637 case LyXTabular::M_VALIGN_TOP:
1638 case LyXTabular::M_VALIGN_BOTTOM:
1639 case LyXTabular::M_VALIGN_CENTER:
1641 case LyXTabular::VALIGN_TOP:
1642 case LyXTabular::VALIGN_BOTTOM:
1643 case LyXTabular::VALIGN_CENTER:
1644 for (i = sel_row_start; i <= sel_row_end; ++i)
1645 for (j = sel_col_start; j <= sel_col_end; ++j)
1646 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1649 UpdateLocal(bv, INIT, true);
1651 UpdateLocal(bv, CELL, true);
1653 case LyXTabular::MULTICOLUMN:
1655 if (sel_row_start != sel_row_end) {
1656 WriteAlert(_("Impossible Operation!"),
1657 _("Multicolumns can only be horizontally."),
1661 // just multicol for one Single Cell
1662 if (!hasSelection()) {
1663 // check wether we are completly in a multicol
1664 if (tabular->IsMultiColumn(actcell)) {
1665 tabular->UnsetMultiColumn(actcell);
1666 UpdateLocal(bv, INIT, true);
1668 tabular->SetMultiColumn(actcell, 1);
1669 UpdateLocal(bv, CELL, true);
1673 // we have a selection so this means we just add all this
1674 // cells to form a multicolumn cell
1678 if (sel_cell_start > sel_cell_end) {
1679 s_start = sel_cell_end;
1680 s_end = sel_cell_start;
1682 s_start = sel_cell_start;
1683 s_end = sel_cell_end;
1685 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1688 sel_cell_end = sel_cell_start;
1689 sel_pos_end = sel_pos_start;
1690 UpdateLocal(bv, INIT, true);
1693 case LyXTabular::SET_ALL_LINES:
1695 case LyXTabular::UNSET_ALL_LINES:
1696 for (i=sel_row_start; i<=sel_row_end; ++i)
1697 for (j=sel_col_start; j<=sel_col_end; ++j)
1698 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1699 UpdateLocal(bv, INIT, true);
1701 case LyXTabular::SET_LONGTABULAR:
1702 tabular->SetLongTabular(true);
1703 UpdateLocal(bv, INIT, true); // because this toggles displayed
1705 case LyXTabular::UNSET_LONGTABULAR:
1706 tabular->SetLongTabular(false);
1707 UpdateLocal(bv, INIT, true); // because this toggles displayed
1709 case LyXTabular::SET_ROTATE_TABULAR:
1710 tabular->SetRotateTabular(true);
1712 case LyXTabular::UNSET_ROTATE_TABULAR:
1713 tabular->SetRotateTabular(false);
1715 case LyXTabular::SET_ROTATE_CELL:
1716 for (i=sel_row_start; i<=sel_row_end; ++i)
1717 for (j=sel_col_start; j<=sel_col_end; ++j)
1718 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1720 case LyXTabular::UNSET_ROTATE_CELL:
1721 for (i = sel_row_start; i <= sel_row_end; ++i)
1722 for (j = sel_col_start; j <= sel_col_end; ++j)
1723 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1725 case LyXTabular::SET_USEBOX:
1727 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1728 if (val == tabular->GetUsebox(actcell))
1729 val = LyXTabular::BOX_NONE;
1730 for (i = sel_row_start; i <= sel_row_end; ++i)
1731 for (j = sel_col_start; j <= sel_col_end; ++j)
1732 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1735 case LyXTabular::SET_LTFIRSTHEAD:
1736 tabular->SetLTHead(actcell, true);
1738 case LyXTabular::SET_LTHEAD:
1739 tabular->SetLTHead(actcell, false);
1741 case LyXTabular::SET_LTFOOT:
1742 tabular->SetLTFoot(actcell, false);
1744 case LyXTabular::SET_LTLASTFOOT:
1745 tabular->SetLTFoot(actcell, true);
1747 case LyXTabular::SET_LTNEWPAGE:
1748 what = !tabular->GetLTNewPage(actcell);
1749 tabular->SetLTNewPage(actcell, what);
1751 // dummy stuff just to avoid warnings
1752 case LyXTabular::LAST_ACTION:
1758 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1761 // the cursor.pos has to be before the inset so if it isn't now just
1762 // reset the curor pos first!
1763 if (!cellstart(cursor.pos())) {
1767 UpdatableInset * inset =
1768 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1769 LyXFont font(LyXFont::ALL_SANE);
1771 x = inset->x() + inset->width(bv, font);
1772 y = inset->descent(bv, font);
1774 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1775 inset_y = cursor.y();
1776 inset->Edit(bv, x - inset_x, y - inset_y, button);
1777 if (!the_locking_inset)
1779 UpdateLocal(bv, CELL, false);
1780 return (the_locking_inset != 0);
1784 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1786 InsetText * inset = tabular->GetCellInset(actcell);
1787 int const x1 = x + top_x;
1789 if (!cellstart(cursor.pos())) {
1790 return ((x + top_x) < cursor.x() &&
1791 (x + top_x) > (cursor.x() - inset->width(bv,
1792 LyXFont(LyXFont::ALL_SANE))));
1794 int const x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1796 x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE))));
1801 // This returns paperWidth() if the cell-width is unlimited or the width
1802 // in pixels if we have a pwidth for this cell.
1803 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1805 string const s = tabular->GetPWidth(cell);
1809 return VSpace(s).inPixels(0, 0);
1813 int InsetTabular::getMaxWidth(Painter & pain,
1814 UpdatableInset const * inset) const
1816 int const n = tabular->GetNumberOfCells();
1818 for (; cell < n; ++cell) {
1819 if (tabular->GetCellInset(cell) == inset)
1824 int w = GetMaxWidthOfCell(pain, cell);
1826 // because the inset then subtracts it's top_x and owner->x()
1827 w += (inset->x() - top_x);
1832 void InsetTabular::resizeLyXText(BufferView *) const
1838 LyXText * InsetTabular::getLyXText(BufferView const * bv) const
1840 if (the_locking_inset)
1841 return the_locking_inset->getLyXText(bv);
1842 return Inset::getLyXText(bv);
1846 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1848 if (the_locking_inset) {
1849 InsetTabular * i = static_cast<InsetTabular *>
1850 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1852 i->OpenLayoutDialog(bv);
1856 bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1860 // functions returns:
1864 // 3 ... toggled off
1866 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1868 int action = LyXTabular::LAST_ACTION;
1869 LyXFunc::func_status status = LyXFunc::OK;
1872 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1873 string const tmp = tabularFeatures[i].feature;
1874 if (tmp == what.substr(0, tmp.length())) {
1875 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1876 // tabularFeatures[i].feature.length())) {
1877 action = tabularFeatures[i].action;
1881 if (action == LyXTabular::LAST_ACTION)
1882 return LyXFunc::Unknown;
1884 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1886 int sel_row_start, sel_row_end;
1890 if (hasSelection()) {
1891 sel_row_start = tabular->row_of_cell(sel_cell_start);
1892 sel_row_end = tabular->row_of_cell(sel_cell_end);
1893 if (sel_row_start > sel_row_end) {
1894 //int tmp = sel_row_start;
1895 //sel_row_start = sel_row_end;
1896 //sel_row_end = tmp;
1897 swap(sel_row_start, sel_row_end);
1900 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1904 case LyXTabular::SET_PWIDTH:
1905 case LyXTabular::SET_MPWIDTH:
1906 case LyXTabular::SET_SPECIAL_COLUMN:
1907 case LyXTabular::SET_SPECIAL_MULTI:
1908 status |= LyXFunc::Disabled;
1911 case LyXTabular::APPEND_ROW:
1912 case LyXTabular::APPEND_COLUMN:
1913 case LyXTabular::DELETE_ROW:
1914 case LyXTabular::DELETE_COLUMN:
1915 case LyXTabular::SET_ALL_LINES:
1916 case LyXTabular::UNSET_ALL_LINES:
1917 status |= LyXFunc::OK;
1920 case LyXTabular::MULTICOLUMN:
1921 if (tabular->IsMultiColumn(actcell))
1922 status |= LyXFunc::ToggleOn;
1924 status |= LyXFunc::ToggleOff;
1926 case LyXTabular::M_TOGGLE_LINE_TOP:
1928 case LyXTabular::TOGGLE_LINE_TOP:
1929 if (tabular->TopLine(actcell, flag))
1930 status |= LyXFunc::ToggleOn;
1932 status |= LyXFunc::ToggleOff;
1934 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1936 case LyXTabular::TOGGLE_LINE_BOTTOM:
1937 if (tabular->BottomLine(actcell, flag))
1938 status |= LyXFunc::ToggleOn;
1940 status |= LyXFunc::ToggleOff;
1942 case LyXTabular::M_TOGGLE_LINE_LEFT:
1944 case LyXTabular::TOGGLE_LINE_LEFT:
1945 if (tabular->LeftLine(actcell, flag))
1946 status |= LyXFunc::ToggleOn;
1948 status |= LyXFunc::ToggleOff;
1950 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1952 case LyXTabular::TOGGLE_LINE_RIGHT:
1953 if (tabular->RightLine(actcell, flag))
1954 status |= LyXFunc::ToggleOn;
1956 status |= LyXFunc::ToggleOff;
1958 case LyXTabular::M_ALIGN_LEFT:
1960 case LyXTabular::ALIGN_LEFT:
1961 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1962 status |= LyXFunc::ToggleOn;
1964 status |= LyXFunc::ToggleOff;
1966 case LyXTabular::M_ALIGN_RIGHT:
1968 case LyXTabular::ALIGN_RIGHT:
1969 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1970 status |= LyXFunc::ToggleOn;
1972 status |= LyXFunc::ToggleOff;
1974 case LyXTabular::M_ALIGN_CENTER:
1976 case LyXTabular::ALIGN_CENTER:
1977 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1978 status |= LyXFunc::ToggleOn;
1980 status |= LyXFunc::ToggleOff;
1982 case LyXTabular::M_VALIGN_TOP:
1984 case LyXTabular::VALIGN_TOP:
1985 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1986 status |= LyXFunc::ToggleOn;
1988 status |= LyXFunc::ToggleOff;
1990 case LyXTabular::M_VALIGN_BOTTOM:
1992 case LyXTabular::VALIGN_BOTTOM:
1993 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1994 status |= LyXFunc::ToggleOn;
1996 status |= LyXFunc::ToggleOff;
1998 case LyXTabular::M_VALIGN_CENTER:
2000 case LyXTabular::VALIGN_CENTER:
2001 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2002 status |= LyXFunc::ToggleOn;
2004 status |= LyXFunc::ToggleOff;
2006 case LyXTabular::SET_LONGTABULAR:
2007 if (tabular->IsLongTabular())
2008 status |= LyXFunc::ToggleOn;
2010 status |= LyXFunc::ToggleOff;
2012 case LyXTabular::UNSET_LONGTABULAR:
2013 if (!tabular->IsLongTabular())
2014 status |= LyXFunc::ToggleOn;
2016 status |= LyXFunc::ToggleOff;
2018 case LyXTabular::SET_ROTATE_TABULAR:
2019 if (tabular->GetRotateTabular())
2020 status |= LyXFunc::ToggleOn;
2022 status |= LyXFunc::ToggleOff;
2024 case LyXTabular::UNSET_ROTATE_TABULAR:
2025 if (!tabular->GetRotateTabular())
2026 status |= LyXFunc::ToggleOn;
2028 status |= LyXFunc::ToggleOff;
2030 case LyXTabular::SET_ROTATE_CELL:
2031 if (tabular->GetRotateCell(actcell))
2032 status |= LyXFunc::ToggleOn;
2034 status |= LyXFunc::ToggleOff;
2036 case LyXTabular::UNSET_ROTATE_CELL:
2037 if (!tabular->GetRotateCell(actcell))
2038 status |= LyXFunc::ToggleOn;
2040 status |= LyXFunc::ToggleOff;
2042 case LyXTabular::SET_USEBOX:
2043 if (strToInt(argument) == tabular->GetUsebox(actcell))
2044 status |= LyXFunc::ToggleOn;
2046 status |= LyXFunc::ToggleOff;
2048 case LyXTabular::SET_LTFIRSTHEAD:
2049 if (tabular->GetRowOfLTHead(actcell, dummy))
2050 status |= LyXFunc::ToggleOn;
2052 status |= LyXFunc::ToggleOff;
2054 case LyXTabular::SET_LTHEAD:
2055 if (tabular->GetRowOfLTHead(actcell, dummy))
2056 status |= LyXFunc::ToggleOn;
2058 status |= LyXFunc::ToggleOff;
2060 case LyXTabular::SET_LTFOOT:
2061 if (tabular->GetRowOfLTFoot(actcell, dummy))
2062 status |= LyXFunc::ToggleOn;
2064 status |= LyXFunc::ToggleOff;
2066 case LyXTabular::SET_LTLASTFOOT:
2067 if (tabular->GetRowOfLTFoot(actcell, dummy))
2068 status |= LyXFunc::ToggleOn;
2070 status |= LyXFunc::ToggleOff;
2072 case LyXTabular::SET_LTNEWPAGE:
2073 if (tabular->GetLTNewPage(actcell))
2074 status |= LyXFunc::ToggleOn;
2076 status |= LyXFunc::ToggleOff;
2079 status = LyXFunc::Disabled;
2086 bool InsetTabular::copySelection(BufferView * bv)
2088 if (!hasSelection())
2090 delete paste_tabular;
2092 int sel_col_start, sel_col_end;
2093 int sel_row_start, sel_row_end;
2095 sel_col_start = tabular->column_of_cell(sel_cell_start);
2096 sel_col_end = tabular->column_of_cell(sel_cell_end);
2097 if (sel_col_start > sel_col_end) {
2098 sel_col_start = sel_col_end;
2099 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2101 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2103 int columns = sel_col_end - sel_col_start + 1;
2105 sel_row_start = tabular->row_of_cell(sel_cell_start);
2106 sel_row_end = tabular->row_of_cell(sel_cell_end);
2107 if (sel_row_start > sel_row_end) {
2108 //int tmp tmp = sel_row_start;
2109 //sel_row_start = sel_row_end;
2110 //sel_row_end = tmp;
2111 swap(sel_row_start, sel_row_end);
2113 int rows = sel_row_end - sel_row_start + 1;
2115 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2117 for (i=0; i < sel_row_start; ++i)
2118 paste_tabular->DeleteRow(0);
2119 while(paste_tabular->rows() > rows)
2120 paste_tabular->DeleteRow(rows);
2121 paste_tabular->SetTopLine(0, true, true);
2122 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2124 for (i=0; i < sel_col_start; ++i)
2125 paste_tabular->DeleteColumn(0);
2126 while(paste_tabular->columns() > columns)
2127 paste_tabular->DeleteColumn(columns);
2128 paste_tabular->SetLeftLine(0, true, true);
2129 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2132 paste_tabular->Ascii(bv->buffer(), sstr);
2133 bv->stuffClipboard(sstr.str().c_str());
2138 bool InsetTabular::pasteSelection(BufferView * bv)
2143 for (int r1 = 0, r2 = actrow;
2144 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2147 for(int c1 = 0, c2 = actcol;
2148 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2151 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2152 tabular->IsPartOfMultiColumn(r2,c2))
2154 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2158 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2162 int n1 = paste_tabular->GetCellNumber(r1, c1);
2163 int n2 = tabular->GetCellNumber(r2, c2);
2164 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2165 tabular->GetCellInset(n2)->setOwner(this);
2166 tabular->GetCellInset(n2)->deleteLyXText(bv);
2173 bool InsetTabular::cutSelection()
2175 if (!hasSelection())
2178 int sel_col_start, sel_col_end;
2179 int sel_row_start, sel_row_end;
2181 sel_col_start = tabular->column_of_cell(sel_cell_start);
2182 sel_col_end = tabular->column_of_cell(sel_cell_end);
2183 if (sel_col_start > sel_col_end) {
2184 sel_col_start = sel_col_end;
2185 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2187 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2189 sel_row_start = tabular->row_of_cell(sel_cell_start);
2190 sel_row_end = tabular->row_of_cell(sel_cell_end);
2191 if (sel_row_start > sel_row_end) {
2192 //int tmp = sel_row_start;
2193 //sel_row_start = sel_row_end;
2194 //sel_row_end = tmp;
2195 swap(sel_row_start, sel_row_end);
2197 if (sel_cell_start > sel_cell_end) {
2198 //int tmp = sel_cell_start;
2199 //sel_cell_start = sel_cell_end;
2200 //sel_cell_end = tmp;
2201 swap(sel_cell_start, sel_cell_end);
2203 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2204 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2205 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();