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 string::size_type p = 0;
978 while((p < clip.length()) &&
979 (p = clip.find_first_of("\t\n", p)) != string::npos)
987 maxCols = max(cols+1, maxCols);
993 delete paste_tabular;
994 paste_tabular = new LyXTabular(this, rows+1, maxCols);
995 string::size_type op = 0;
997 unsigned int len = clip.length();
998 int cells = paste_tabular->GetNumberOfCells();
1000 while((cell < cells) && (p < len) &&
1001 (p = clip.find_first_of("\t\n", p)) != string::npos)
1005 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1010 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1011 while(cols++ < maxCols)
1019 // check for the last cell if there is no trailing '\n'
1020 if ((cell < cells) && ((op-1) < len))
1021 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1023 // so that the clipboard is used and it goes on to default
1024 // and executes LFUN_PASTESELECTION in insettext!
1025 delete paste_tabular;
1030 if (hasPasteBuffer()) {
1031 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1033 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1034 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1036 bv->text->cursor.par()->previous,
1037 bv->text->cursor.par()->next
1041 UpdateLocal(bv, INIT, true);
1044 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1046 // we try to activate the actual inset and put this event down to
1047 // the insets dispatch function.
1048 result = UNDISPATCHED;
1049 if (the_locking_inset)
1051 if (ActivateCellInset(bv, 0, 0, 0, !cellstart(cursor.pos()))) {
1052 result=the_locking_inset->LocalDispatch(bv, action, arg);
1053 if (result == DISPATCHED_NOUPDATE)
1055 else if (result == DISPATCHED) {
1056 the_locking_inset->ToggleInsetCursor(bv);
1057 UpdateLocal(bv, CELL, false);
1058 the_locking_inset->ToggleInsetCursor(bv);
1061 result = UNDISPATCHED;
1066 if (result!=FINISHED) {
1067 if (!the_locking_inset) {
1068 ShowInsetCursor(bv);
1071 bv->unlockInset(this);
1076 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1077 bool fragile, bool fp) const
1079 return tabular->Latex(buf, os, fragile, fp);
1083 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1085 // This should be changed to a real ascii export
1086 return tabular->Ascii(buf, os);
1090 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1096 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1098 return tabular->DocBook(buf,os);
1102 void InsetTabular::Validate(LaTeXFeatures & features) const
1104 tabular->Validate(features);
1108 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1109 LyXFont const & font,
1116 bool changed = false;
1118 // if we have a locking_inset we should have to check only this cell for
1119 // change so I'll try this to have a boost, but who knows ;)
1120 if ((need_update != INIT) &&
1121 (the_locking_inset == tabular->GetCellInset(actcell))) {
1122 for(int i = 0; i < tabular->columns(); ++i) {
1123 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1125 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1128 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1129 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1130 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1133 for (int i = 0; i < tabular->rows(); ++i) {
1134 for (int j= 0; j < tabular->columns(); ++j) {
1135 if (tabular->IsPartOfMultiColumn(i,j))
1138 inset = tabular->GetCellInset(cell);
1140 inset->update(bv, font, false);
1141 maxAsc = max(maxAsc, inset->ascent(bv, font));
1142 maxDesc = max(maxDesc, inset->descent(bv, font));
1143 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1145 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1146 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1152 void InsetTabular::GetCursorPos(BufferView *,
1153 int & x, int & y) const
1155 x = cursor.x() - top_x;
1160 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1162 if (the_locking_inset) {
1163 the_locking_inset->ToggleInsetCursor(bv);
1167 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1169 int const asc = lyxfont::maxAscent(font);
1170 int const desc = lyxfont::maxDescent(font);
1173 bv->hideLockedInsetCursor();
1175 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1176 cursor_visible = !cursor_visible;
1180 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1182 if (!cursor_visible) {
1183 LyXFont font; // = GetFont(par, cursor.pos);
1185 int const asc = lyxfont::maxAscent(font);
1186 int const desc = lyxfont::maxDescent(font);
1187 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1189 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1190 cursor_visible = true;
1195 void InsetTabular::HideInsetCursor(BufferView * bv)
1197 if (cursor_visible) {
1198 bv->hideLockedInsetCursor();
1199 cursor_visible = false;
1201 // if (cursor_visible)
1202 // ToggleInsetCursor(bv);
1206 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1211 actcell = actrow = actcol = 0;
1212 int ly = tabular->GetDescentOfRow(actrow);
1214 // first search the right row
1215 while((ly < y) && (actrow < tabular->rows())) {
1216 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1217 tabular->GetAscentOfRow(actrow + 1) +
1218 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1221 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1223 actcell = tabular->GetCellNumber(actrow, actcol);
1225 // now search the right column
1226 int lx = tabular->GetWidthOfColumn(actcell) -
1227 tabular->GetAdditionalWidth(actcell);
1229 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1230 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1231 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1232 tabular->GetAdditionalWidth(actcell - 1));
1234 // Jürgen, you should check that this is correct. (Lgb)
1235 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1236 lx += tabular->GetWidthOfColumn(actcell + 1)
1237 + tabular->GetAdditionalWidth(actcell);
1243 if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1244 cursor.x(lx + top_x - 2);
1247 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1253 int InsetTabular::getCellXPos(int cell) const
1257 for (; !tabular->IsFirstCellInRow(c); --c)
1259 int lx = tabular->GetWidthOfColumn(cell);
1260 for (; c < cell; ++c) {
1261 lx += tabular->GetWidthOfColumn(c);
1263 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1267 void InsetTabular::resetPos(BufferView * bv) const
1271 actcol = tabular->column_of_cell(actcell);
1276 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1277 if (tabular->IsLastCellInRow(cell)) {
1278 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1279 tabular->GetAscentOfRow(actrow + 1) +
1280 tabular->GetAdditionalHeight(cell + 1));
1284 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1285 int new_x = getCellXPos(actcell);
1288 // cursor.x(getCellXPos(actcell) + offset);
1289 if (((cursor.x() - offset) > 20) &&
1290 ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1291 (bv->workWidth()-20)))
1293 scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1294 UpdateLocal(bv, FULL, false);
1295 } else if ((cursor.x() - offset) < 20) {
1296 scroll(bv, 20 - cursor.x() + offset);
1297 UpdateLocal(bv, FULL, false);
1298 } else if (!cellstart(cursor.pos())) {
1299 LyXFont font(LyXFont::ALL_SANE);
1300 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1301 tabular->GetBeginningOfTextInCell(actcell));
1303 if ((!the_locking_inset ||
1304 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1305 (actcell != oldcell)) {
1306 InsetTabular * inset = const_cast<InsetTabular *>(this);
1307 bv->owner()->getDialogs()->updateTabular(inset);
1313 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1315 if (!cellstart(cursor.pos())) {
1316 if (tabular->IsLastCell(actcell))
1321 if (ActivateCellInset(bv))
1323 } else { // before the inset
1327 return DISPATCHED_NOUPDATE;
1331 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1333 if (cellstart(cursor.pos()) && !actcell)
1335 if (cellstart(cursor.pos())) {
1338 } else if (lock) { // behind the inset
1340 if (ActivateCellInset(bv, 0, 0, 0, true))
1346 return DISPATCHED_NOUPDATE;
1350 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1352 int const ocell = actcell;
1353 actcell = tabular->GetCellAbove(actcell);
1354 if (actcell == ocell) // we moved out of the inset
1357 return DISPATCHED_NOUPDATE;
1361 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1363 int const ocell = actcell;
1364 actcell = tabular->GetCellBelow(actcell);
1365 if (actcell == ocell) // we moved out of the inset
1368 return DISPATCHED_NOUPDATE;
1372 bool InsetTabular::moveNextCell(BufferView * bv)
1374 if (tabular->IsLastCell(actcell))
1382 bool InsetTabular::movePrevCell(BufferView * bv)
1384 if (!actcell) // first cell
1392 bool InsetTabular::Delete()
1398 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1400 if (the_locking_inset)
1401 the_locking_inset->SetFont(bv, font, tall);
1405 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1407 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1410 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1411 string const tmp = tabularFeatures[i].feature;
1413 if (tmp == what.substr(0, tmp.length())) {
1414 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1415 //tabularFeatures[i].feature.length())) {
1416 action = tabularFeatures[i].action;
1420 if (action == LyXTabular::LAST_ACTION)
1424 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1425 TabularFeatures(bv, action, val);
1430 void InsetTabular::TabularFeatures(BufferView * bv,
1431 LyXTabular::Feature feature,
1432 string const & value)
1441 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1442 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1447 case LyXTabular::M_ALIGN_LEFT:
1448 case LyXTabular::ALIGN_LEFT:
1449 setAlign=LYX_ALIGN_LEFT;
1451 case LyXTabular::M_ALIGN_RIGHT:
1452 case LyXTabular::ALIGN_RIGHT:
1453 setAlign=LYX_ALIGN_RIGHT;
1455 case LyXTabular::M_ALIGN_CENTER:
1456 case LyXTabular::ALIGN_CENTER:
1457 setAlign=LYX_ALIGN_CENTER;
1459 case LyXTabular::M_VALIGN_TOP:
1460 case LyXTabular::VALIGN_TOP:
1461 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1463 case LyXTabular::M_VALIGN_BOTTOM:
1464 case LyXTabular::VALIGN_BOTTOM:
1465 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1467 case LyXTabular::M_VALIGN_CENTER:
1468 case LyXTabular::VALIGN_CENTER:
1469 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1474 if (hasSelection()) {
1475 sel_col_start = tabular->column_of_cell(sel_cell_start);
1476 sel_col_end = tabular->column_of_cell(sel_cell_end);
1477 if (sel_col_start > sel_col_end) {
1478 sel_col_end = sel_col_start;
1479 sel_col_start = tabular->column_of_cell(sel_cell_end);
1481 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1484 sel_row_start = tabular->row_of_cell(sel_cell_start);
1485 sel_row_end = tabular->row_of_cell(sel_cell_end);
1486 if (sel_row_start > sel_row_end) {
1487 //int tmp = sel_row_start;
1488 //sel_row_start = sel_row_end;
1489 //sel_row_end = tmp;
1490 swap(sel_row_start, sel_row_end);
1493 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1494 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1496 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1498 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1499 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1501 bv->text->cursor.par()->previous,
1502 bv->text->cursor.par()->next
1506 int row = tabular->row_of_cell(actcell);
1507 int column = tabular->column_of_cell(actcell);
1511 case LyXTabular::SET_PWIDTH:
1513 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1514 tabular->SetColumnPWidth(actcell,value);
1516 for (int i=0; i < tabular->rows(); ++i) {
1517 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1520 UpdateLocal(bv, INIT, true);
1524 case LyXTabular::SET_MPWIDTH:
1526 bool const update = (tabular->GetPWidth(actcell) != value);
1527 tabular->SetMColumnPWidth(actcell,value);
1529 for (int i=0; i < tabular->rows(); ++i) {
1530 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1533 UpdateLocal(bv, INIT, true);
1537 case LyXTabular::SET_SPECIAL_COLUMN:
1538 case LyXTabular::SET_SPECIAL_MULTI:
1539 tabular->SetAlignSpecial(actcell,value,feature);
1541 case LyXTabular::APPEND_ROW:
1542 // append the row into the tabular
1543 UnlockInsetInInset(bv, the_locking_inset);
1544 tabular->AppendRow(actcell);
1545 UpdateLocal(bv, INIT, true);
1547 case LyXTabular::APPEND_COLUMN:
1548 // append the column into the tabular
1549 tabular->AppendColumn(actcell);
1550 actcell = tabular->GetCellNumber(row, column);
1551 UpdateLocal(bv, INIT, true);
1553 case LyXTabular::DELETE_ROW:
1554 tabular->DeleteRow(tabular->row_of_cell(actcell));
1555 if ((row+1) > tabular->rows())
1557 actcell = tabular->GetCellNumber(row, column);
1559 UpdateLocal(bv, INIT, true);
1561 case LyXTabular::DELETE_COLUMN:
1562 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1563 if ((column+1) > tabular->columns())
1565 actcell = tabular->GetCellNumber(row, column);
1567 UpdateLocal(bv, INIT, true);
1569 case LyXTabular::M_TOGGLE_LINE_TOP:
1571 case LyXTabular::TOGGLE_LINE_TOP:
1572 lineSet = !tabular->TopLine(actcell, flag);
1573 for (i=sel_row_start; i<=sel_row_end; ++i)
1574 for (j=sel_col_start; j<=sel_col_end; ++j)
1575 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1576 UpdateLocal(bv, INIT, true);
1579 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1581 case LyXTabular::TOGGLE_LINE_BOTTOM:
1582 lineSet = !tabular->BottomLine(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->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1587 UpdateLocal(bv, INIT, true);
1590 case LyXTabular::M_TOGGLE_LINE_LEFT:
1592 case LyXTabular::TOGGLE_LINE_LEFT:
1593 lineSet = !tabular->LeftLine(actcell, flag);
1594 for (i=sel_row_start; i<=sel_row_end; ++i)
1595 for (j=sel_col_start; j<=sel_col_end; ++j)
1596 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1598 UpdateLocal(bv, INIT, true);
1601 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1603 case LyXTabular::TOGGLE_LINE_RIGHT:
1604 lineSet = !tabular->RightLine(actcell, flag);
1605 for (i=sel_row_start; i<=sel_row_end; ++i)
1606 for (j=sel_col_start; j<=sel_col_end; ++j)
1607 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1609 UpdateLocal(bv, INIT, true);
1611 case LyXTabular::M_ALIGN_LEFT:
1612 case LyXTabular::M_ALIGN_RIGHT:
1613 case LyXTabular::M_ALIGN_CENTER:
1615 case LyXTabular::ALIGN_LEFT:
1616 case LyXTabular::ALIGN_RIGHT:
1617 case LyXTabular::ALIGN_CENTER:
1618 for (i = sel_row_start; i <= sel_row_end; ++i)
1619 for (j = sel_col_start; j <= sel_col_end; ++j)
1620 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1623 UpdateLocal(bv, INIT, true);
1625 UpdateLocal(bv, CELL, true);
1627 case LyXTabular::M_VALIGN_TOP:
1628 case LyXTabular::M_VALIGN_BOTTOM:
1629 case LyXTabular::M_VALIGN_CENTER:
1631 case LyXTabular::VALIGN_TOP:
1632 case LyXTabular::VALIGN_BOTTOM:
1633 case LyXTabular::VALIGN_CENTER:
1634 for (i = sel_row_start; i <= sel_row_end; ++i)
1635 for (j = sel_col_start; j <= sel_col_end; ++j)
1636 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1639 UpdateLocal(bv, INIT, true);
1641 UpdateLocal(bv, CELL, true);
1643 case LyXTabular::MULTICOLUMN:
1645 if (sel_row_start != sel_row_end) {
1646 WriteAlert(_("Impossible Operation!"),
1647 _("Multicolumns can only be horizontally."),
1651 // just multicol for one Single Cell
1652 if (!hasSelection()) {
1653 // check wether we are completly in a multicol
1654 if (tabular->IsMultiColumn(actcell)) {
1655 tabular->UnsetMultiColumn(actcell);
1656 UpdateLocal(bv, INIT, true);
1658 tabular->SetMultiColumn(actcell, 1);
1659 UpdateLocal(bv, CELL, true);
1663 // we have a selection so this means we just add all this
1664 // cells to form a multicolumn cell
1668 if (sel_cell_start > sel_cell_end) {
1669 s_start = sel_cell_end;
1670 s_end = sel_cell_start;
1672 s_start = sel_cell_start;
1673 s_end = sel_cell_end;
1675 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1678 sel_cell_end = sel_cell_start;
1679 sel_pos_end = sel_pos_start;
1680 UpdateLocal(bv, INIT, true);
1683 case LyXTabular::SET_ALL_LINES:
1685 case LyXTabular::UNSET_ALL_LINES:
1686 for (i=sel_row_start; i<=sel_row_end; ++i)
1687 for (j=sel_col_start; j<=sel_col_end; ++j)
1688 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1689 UpdateLocal(bv, INIT, true);
1691 case LyXTabular::SET_LONGTABULAR:
1692 tabular->SetLongTabular(true);
1693 UpdateLocal(bv, INIT, true); // because this toggles displayed
1695 case LyXTabular::UNSET_LONGTABULAR:
1696 tabular->SetLongTabular(false);
1697 UpdateLocal(bv, INIT, true); // because this toggles displayed
1699 case LyXTabular::SET_ROTATE_TABULAR:
1700 tabular->SetRotateTabular(true);
1702 case LyXTabular::UNSET_ROTATE_TABULAR:
1703 tabular->SetRotateTabular(false);
1705 case LyXTabular::SET_ROTATE_CELL:
1706 for (i=sel_row_start; i<=sel_row_end; ++i)
1707 for (j=sel_col_start; j<=sel_col_end; ++j)
1708 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1710 case LyXTabular::UNSET_ROTATE_CELL:
1711 for (i = sel_row_start; i <= sel_row_end; ++i)
1712 for (j = sel_col_start; j <= sel_col_end; ++j)
1713 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1715 case LyXTabular::SET_USEBOX:
1717 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1718 if (val == tabular->GetUsebox(actcell))
1719 val = LyXTabular::BOX_NONE;
1720 for (i = sel_row_start; i <= sel_row_end; ++i)
1721 for (j = sel_col_start; j <= sel_col_end; ++j)
1722 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1725 case LyXTabular::SET_LTFIRSTHEAD:
1726 tabular->SetLTHead(actcell, true);
1728 case LyXTabular::SET_LTHEAD:
1729 tabular->SetLTHead(actcell, false);
1731 case LyXTabular::SET_LTFOOT:
1732 tabular->SetLTFoot(actcell, false);
1734 case LyXTabular::SET_LTLASTFOOT:
1735 tabular->SetLTFoot(actcell, true);
1737 case LyXTabular::SET_LTNEWPAGE:
1738 what = !tabular->GetLTNewPage(actcell);
1739 tabular->SetLTNewPage(actcell, what);
1741 // dummy stuff just to avoid warnings
1742 case LyXTabular::LAST_ACTION:
1748 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1751 // the cursor.pos has to be before the inset so if it isn't now just
1752 // reset the curor pos first!
1753 if (!cellstart(cursor.pos())) {
1757 UpdatableInset * inset =
1758 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1759 LyXFont font(LyXFont::ALL_SANE);
1761 x = inset->x() + inset->width(bv, font);
1762 y = inset->descent(bv, font);
1764 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1765 inset_y = cursor.y();
1766 inset->Edit(bv, x - inset_x, y - inset_y, button);
1767 if (!the_locking_inset)
1769 UpdateLocal(bv, CELL, false);
1770 return (the_locking_inset != 0);
1774 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1776 InsetText * inset = tabular->GetCellInset(actcell);
1777 int const x1 = x + top_x;
1779 if (!cellstart(cursor.pos())) {
1780 return ((x + top_x) < cursor.x() &&
1781 (x + top_x) > (cursor.x() - inset->width(bv,
1782 LyXFont(LyXFont::ALL_SANE))));
1784 int const x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1786 x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE))));
1791 // This returns paperWidth() if the cell-width is unlimited or the width
1792 // in pixels if we have a pwidth for this cell.
1793 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1795 string const s = tabular->GetPWidth(cell);
1799 return VSpace(s).inPixels(0, 0);
1803 int InsetTabular::getMaxWidth(Painter & pain,
1804 UpdatableInset const * inset) const
1806 int const n = tabular->GetNumberOfCells();
1808 for (; cell < n; ++cell) {
1809 if (tabular->GetCellInset(cell) == inset)
1814 int w = GetMaxWidthOfCell(pain, cell);
1816 // because the inset then subtracts it's top_x and owner->x()
1817 w += (inset->x() - top_x);
1822 void InsetTabular::resizeLyXText(BufferView *) const
1828 LyXText * InsetTabular::getLyXText(BufferView const * bv) const
1830 if (the_locking_inset)
1831 return the_locking_inset->getLyXText(bv);
1832 return Inset::getLyXText(bv);
1836 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1838 if (the_locking_inset) {
1839 InsetTabular * i = static_cast<InsetTabular *>
1840 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1842 i->OpenLayoutDialog(bv);
1846 bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1850 // functions returns:
1854 // 3 ... toggled off
1856 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1858 int action = LyXTabular::LAST_ACTION;
1859 LyXFunc::func_status status = LyXFunc::OK;
1862 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1863 string const tmp = tabularFeatures[i].feature;
1864 if (tmp == what.substr(0, tmp.length())) {
1865 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1866 // tabularFeatures[i].feature.length())) {
1867 action = tabularFeatures[i].action;
1871 if (action == LyXTabular::LAST_ACTION)
1872 return LyXFunc::Unknown;
1874 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1876 int sel_row_start, sel_row_end;
1880 if (hasSelection()) {
1881 sel_row_start = tabular->row_of_cell(sel_cell_start);
1882 sel_row_end = tabular->row_of_cell(sel_cell_end);
1883 if (sel_row_start > sel_row_end) {
1884 //int tmp = sel_row_start;
1885 //sel_row_start = sel_row_end;
1886 //sel_row_end = tmp;
1887 swap(sel_row_start, sel_row_end);
1890 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1894 case LyXTabular::SET_PWIDTH:
1895 case LyXTabular::SET_MPWIDTH:
1896 case LyXTabular::SET_SPECIAL_COLUMN:
1897 case LyXTabular::SET_SPECIAL_MULTI:
1898 status |= LyXFunc::Disabled;
1901 case LyXTabular::APPEND_ROW:
1902 case LyXTabular::APPEND_COLUMN:
1903 case LyXTabular::DELETE_ROW:
1904 case LyXTabular::DELETE_COLUMN:
1905 case LyXTabular::SET_ALL_LINES:
1906 case LyXTabular::UNSET_ALL_LINES:
1907 status |= LyXFunc::OK;
1910 case LyXTabular::MULTICOLUMN:
1911 if (tabular->IsMultiColumn(actcell))
1912 status |= LyXFunc::ToggleOn;
1914 status |= LyXFunc::ToggleOff;
1916 case LyXTabular::M_TOGGLE_LINE_TOP:
1918 case LyXTabular::TOGGLE_LINE_TOP:
1919 if (tabular->TopLine(actcell, flag))
1920 status |= LyXFunc::ToggleOn;
1922 status |= LyXFunc::ToggleOff;
1924 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1926 case LyXTabular::TOGGLE_LINE_BOTTOM:
1927 if (tabular->BottomLine(actcell, flag))
1928 status |= LyXFunc::ToggleOn;
1930 status |= LyXFunc::ToggleOff;
1932 case LyXTabular::M_TOGGLE_LINE_LEFT:
1934 case LyXTabular::TOGGLE_LINE_LEFT:
1935 if (tabular->LeftLine(actcell, flag))
1936 status |= LyXFunc::ToggleOn;
1938 status |= LyXFunc::ToggleOff;
1940 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1942 case LyXTabular::TOGGLE_LINE_RIGHT:
1943 if (tabular->RightLine(actcell, flag))
1944 status |= LyXFunc::ToggleOn;
1946 status |= LyXFunc::ToggleOff;
1948 case LyXTabular::M_ALIGN_LEFT:
1950 case LyXTabular::ALIGN_LEFT:
1951 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1952 status |= LyXFunc::ToggleOn;
1954 status |= LyXFunc::ToggleOff;
1956 case LyXTabular::M_ALIGN_RIGHT:
1958 case LyXTabular::ALIGN_RIGHT:
1959 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1960 status |= LyXFunc::ToggleOn;
1962 status |= LyXFunc::ToggleOff;
1964 case LyXTabular::M_ALIGN_CENTER:
1966 case LyXTabular::ALIGN_CENTER:
1967 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1968 status |= LyXFunc::ToggleOn;
1970 status |= LyXFunc::ToggleOff;
1972 case LyXTabular::M_VALIGN_TOP:
1974 case LyXTabular::VALIGN_TOP:
1975 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1976 status |= LyXFunc::ToggleOn;
1978 status |= LyXFunc::ToggleOff;
1980 case LyXTabular::M_VALIGN_BOTTOM:
1982 case LyXTabular::VALIGN_BOTTOM:
1983 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1984 status |= LyXFunc::ToggleOn;
1986 status |= LyXFunc::ToggleOff;
1988 case LyXTabular::M_VALIGN_CENTER:
1990 case LyXTabular::VALIGN_CENTER:
1991 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1992 status |= LyXFunc::ToggleOn;
1994 status |= LyXFunc::ToggleOff;
1996 case LyXTabular::SET_LONGTABULAR:
1997 if (tabular->IsLongTabular())
1998 status |= LyXFunc::ToggleOn;
2000 status |= LyXFunc::ToggleOff;
2002 case LyXTabular::UNSET_LONGTABULAR:
2003 if (!tabular->IsLongTabular())
2004 status |= LyXFunc::ToggleOn;
2006 status |= LyXFunc::ToggleOff;
2008 case LyXTabular::SET_ROTATE_TABULAR:
2009 if (tabular->GetRotateTabular())
2010 status |= LyXFunc::ToggleOn;
2012 status |= LyXFunc::ToggleOff;
2014 case LyXTabular::UNSET_ROTATE_TABULAR:
2015 if (!tabular->GetRotateTabular())
2016 status |= LyXFunc::ToggleOn;
2018 status |= LyXFunc::ToggleOff;
2020 case LyXTabular::SET_ROTATE_CELL:
2021 if (tabular->GetRotateCell(actcell))
2022 status |= LyXFunc::ToggleOn;
2024 status |= LyXFunc::ToggleOff;
2026 case LyXTabular::UNSET_ROTATE_CELL:
2027 if (!tabular->GetRotateCell(actcell))
2028 status |= LyXFunc::ToggleOn;
2030 status |= LyXFunc::ToggleOff;
2032 case LyXTabular::SET_USEBOX:
2033 if (strToInt(argument) == tabular->GetUsebox(actcell))
2034 status |= LyXFunc::ToggleOn;
2036 status |= LyXFunc::ToggleOff;
2038 case LyXTabular::SET_LTFIRSTHEAD:
2039 if (tabular->GetRowOfLTHead(actcell, dummy))
2040 status |= LyXFunc::ToggleOn;
2042 status |= LyXFunc::ToggleOff;
2044 case LyXTabular::SET_LTHEAD:
2045 if (tabular->GetRowOfLTHead(actcell, dummy))
2046 status |= LyXFunc::ToggleOn;
2048 status |= LyXFunc::ToggleOff;
2050 case LyXTabular::SET_LTFOOT:
2051 if (tabular->GetRowOfLTFoot(actcell, dummy))
2052 status |= LyXFunc::ToggleOn;
2054 status |= LyXFunc::ToggleOff;
2056 case LyXTabular::SET_LTLASTFOOT:
2057 if (tabular->GetRowOfLTFoot(actcell, dummy))
2058 status |= LyXFunc::ToggleOn;
2060 status |= LyXFunc::ToggleOff;
2062 case LyXTabular::SET_LTNEWPAGE:
2063 if (tabular->GetLTNewPage(actcell))
2064 status |= LyXFunc::ToggleOn;
2066 status |= LyXFunc::ToggleOff;
2069 status = LyXFunc::Disabled;
2076 bool InsetTabular::copySelection(BufferView * bv)
2078 if (!hasSelection())
2080 delete paste_tabular;
2082 int sel_col_start, sel_col_end;
2083 int sel_row_start, sel_row_end;
2085 sel_col_start = tabular->column_of_cell(sel_cell_start);
2086 sel_col_end = tabular->column_of_cell(sel_cell_end);
2087 if (sel_col_start > sel_col_end) {
2088 sel_col_start = sel_col_end;
2089 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2091 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2093 int columns = sel_col_end - sel_col_start + 1;
2095 sel_row_start = tabular->row_of_cell(sel_cell_start);
2096 sel_row_end = tabular->row_of_cell(sel_cell_end);
2097 if (sel_row_start > sel_row_end) {
2098 //int tmp tmp = sel_row_start;
2099 //sel_row_start = sel_row_end;
2100 //sel_row_end = tmp;
2101 swap(sel_row_start, sel_row_end);
2103 int rows = sel_row_end - sel_row_start + 1;
2105 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2107 for (i=0; i < sel_row_start; ++i)
2108 paste_tabular->DeleteRow(0);
2109 while(paste_tabular->rows() > rows)
2110 paste_tabular->DeleteRow(rows);
2111 paste_tabular->SetTopLine(0, true, true);
2112 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2114 for (i=0; i < sel_col_start; ++i)
2115 paste_tabular->DeleteColumn(0);
2116 while(paste_tabular->columns() > columns)
2117 paste_tabular->DeleteColumn(columns);
2118 paste_tabular->SetLeftLine(0, true, true);
2119 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2122 paste_tabular->Ascii(bv->buffer(), sstr);
2123 bv->stuffClipboard(sstr.str().c_str());
2128 bool InsetTabular::pasteSelection(BufferView * bv)
2133 for (int r1 = 0, r2 = actrow;
2134 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2137 for(int c1 = 0, c2 = actcol;
2138 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2141 int n1 = paste_tabular->GetCellNumber(r1, c1);
2142 int n2 = tabular->GetCellNumber(r2, c2);
2143 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2144 tabular->GetCellInset(n2)->setOwner(this);
2145 tabular->GetCellInset(n2)->deleteLyXText(bv);
2152 bool InsetTabular::cutSelection()
2154 if (!hasSelection())
2157 int sel_col_start, sel_col_end;
2158 int sel_row_start, sel_row_end;
2160 sel_col_start = tabular->column_of_cell(sel_cell_start);
2161 sel_col_end = tabular->column_of_cell(sel_cell_end);
2162 if (sel_col_start > sel_col_end) {
2163 sel_col_start = sel_col_end;
2164 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2166 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2168 sel_row_start = tabular->row_of_cell(sel_cell_start);
2169 sel_row_end = tabular->row_of_cell(sel_cell_end);
2170 if (sel_row_start > sel_row_end) {
2171 //int tmp = sel_row_start;
2172 //sel_row_start = sel_row_end;
2173 //sel_row_end = tmp;
2174 swap(sel_row_start, sel_row_end);
2176 if (sel_cell_start > sel_cell_end) {
2177 //int tmp = sel_cell_start;
2178 //sel_cell_start = sel_cell_end;
2179 //sel_cell_end = tmp;
2180 swap(sel_cell_start, sel_cell_end);
2182 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2183 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2184 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();