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"
39 const int ADD_TO_HEIGHT = 2;
40 const int ADD_TO_TABULAR_WIDTH = 2;
42 static LyXTabular * paste_tabular = 0;
43 bool InsetTabular::hasPasteBuffer() const
45 return (paste_tabular != 0);
55 struct tabular_features {
56 LyXTabular::Feature action;
60 //static tabular_features * tabularFeatures = 0;
62 static tabular_features tabularFeatures[] =
64 { LyXTabular::APPEND_ROW, "append-row" },
65 { LyXTabular::APPEND_COLUMN, "append-column" },
66 { LyXTabular::DELETE_ROW, "delete-row" },
67 { LyXTabular::DELETE_COLUMN, "delete-column" },
68 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
69 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
70 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
71 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
72 { LyXTabular::ALIGN_LEFT, "align-left" },
73 { LyXTabular::ALIGN_RIGHT, "align-right" },
74 { LyXTabular::ALIGN_CENTER, "align-center" },
75 { LyXTabular::VALIGN_TOP, "valign-top" },
76 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
77 { LyXTabular::VALIGN_CENTER, "valign-center" },
78 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
79 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
80 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
81 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
82 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
83 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
84 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
85 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
86 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
87 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
88 { LyXTabular::MULTICOLUMN, "multicolumn" },
89 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
90 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
91 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
92 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
93 { LyXTabular::SET_PWIDTH, "set-pwidth" },
94 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
95 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
96 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
97 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
98 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
99 { LyXTabular::SET_USEBOX, "set-usebox" },
100 { LyXTabular::SET_LTHEAD, "set-lthead" },
101 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
102 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
103 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
104 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
105 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
106 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
107 { LyXTabular::LAST_ACTION, "" }
112 bool cellstart(LyXParagraph::size_type p)
114 return ((p % 2) == 0);
118 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
125 tabular = new LyXTabular(this, rows,columns);
126 // for now make it always display as display() inset
128 the_locking_inset = 0;
129 locked = no_selection = cursor_visible = false;
132 actrow = actcell = 0;
139 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer const & buf)
142 tabular = new LyXTabular(this, *(tab.tabular));
143 the_locking_inset = 0;
144 locked = no_selection = cursor_visible = false;
147 actrow = actcell = 0;
149 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
154 InsetTabular::~InsetTabular()
161 Inset * InsetTabular::Clone(Buffer const & buf) const
163 InsetTabular * t = new InsetTabular(*this, buf);
165 t->tabular = tabular->Clone(t);
170 void InsetTabular::Write(Buffer const * buf, ostream & os) const
172 os << " Tabular" << endl;
173 tabular->Write(buf, os);
177 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
179 bool old_format = (lex.GetString() == "\\LyXTable");
184 tabular = new LyXTabular(buf, this, lex);
192 token = lex.GetString();
193 while (lex.IsOK() && (token != "\\end_inset")) {
195 token = lex.GetString();
197 if (token != "\\end_inset") {
198 lex.printError("Missing \\end_inset at this point. "
204 int InsetTabular::ascent(BufferView *, LyXFont const &) const
206 return tabular->GetAscentOfRow(0);
210 int InsetTabular::descent(BufferView *, LyXFont const &) const
212 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
216 int InsetTabular::width(BufferView *, LyXFont const &) const
218 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
222 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
223 float & x, bool cleared) const
225 Painter & pain = bv->painter();
230 UpdatableInset::draw(bv, font, baseline, x, cleared);
231 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
232 (top_x != int(x)) || (top_baseline != baseline))) {
233 int h = ascent(bv, font) + descent(bv, font);
234 int tx = display() || !owner() ? 0 : top_x;
235 int w = tx ? width(bv, font) : pain.paperWidth();
236 int ty = baseline - ascent(bv, font);
240 if ((ty + h) > pain.paperHeight())
241 h = pain.paperHeight();
242 if ((top_x + w) > pain.paperWidth())
243 w = pain.paperWidth();
244 pain.fillRectangle(tx, ty, w, h);
249 top_baseline = baseline;
250 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
253 x += ADD_TO_TABULAR_WIDTH;
255 for (i = 0; i < tabular->rows(); ++i) {
257 dodraw = ((baseline + tabular->GetDescentOfRow(i)) > 0) &&
258 (baseline - tabular->GetAscentOfRow(i))<pain.paperHeight();
259 for (j = 0; j < tabular->columns(); ++j) {
260 if (tabular->IsPartOfMultiColumn(i, j))
262 cx = nx + tabular->GetBeginningOfTextInCell(cell);
265 DrawCellSelection(pain, nx, baseline, i, j, cell);
266 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
268 DrawCellLines(pain, nx, baseline, i, cell);
270 nx += tabular->GetWidthOfColumn(cell);
273 baseline += tabular->GetDescentOfRow(i) +
274 tabular->GetAscentOfRow(i + 1) +
275 tabular->GetAdditionalHeight(cell);
277 } else if (need_update == CELL) {
279 for (i = 0; (cell < actcell) && (i < tabular->rows()); ++i) {
280 for (j = 0; (cell < actcell) && (j < tabular->columns()); ++j) {
281 if (tabular->IsPartOfMultiColumn(i, j))
283 nx += tabular->GetWidthOfColumn(cell);
286 if (tabular->row_of_cell(cell) > i) {
288 baseline += tabular->GetDescentOfRow(i) +
289 tabular->GetAscentOfRow(i + 1) +
290 tabular->GetAdditionalHeight(cell);
293 i = tabular->row_of_cell(actcell);
294 if (the_locking_inset == tabular->GetCellInset(cell)) {
296 LyXText::text_status st = bv->text->status;
298 cx = nx + tabular->GetBeginningOfTextInCell(cell);
299 bv->text->status = st;
300 if (need_update == CELL) {
301 // clear before the inset
304 baseline - tabular->GetAscentOfRow(i) + 1,
306 tabular->GetAscentOfRow(i) +
307 tabular->GetDescentOfRow(i) - 1);
308 // clear behind the inset
310 int(cx + the_locking_inset->width(bv,font) + 1),
311 baseline - tabular->GetAscentOfRow(i) + 1,
312 tabular->GetWidthOfColumn(cell) -
313 tabular->GetBeginningOfTextInCell(cell) -
314 the_locking_inset->width(bv,font) -
315 tabular->GetAdditionalWidth(cell) - 1,
316 tabular->GetAscentOfRow(i) +
317 tabular->GetDescentOfRow(i) - 1);
319 tabular->GetCellInset(cell)->draw(bv,font,baseline, cx, false);
320 } while(bv->text->status == LyXText::CHANGED_IN_DRAW);
322 cx = nx + tabular->GetBeginningOfTextInCell(cell);
323 if (need_update == CELL) {
324 // clear before the inset
327 baseline - tabular->GetAscentOfRow(i) + 1,
329 tabular->GetAscentOfRow(i) +
330 tabular->GetDescentOfRow(i) - 1);
331 // clear behind the inset
333 int(cx + the_locking_inset->width(bv,font) + 1),
334 baseline - tabular->GetAscentOfRow(i) + 1,
335 tabular->GetWidthOfColumn(cell) -
336 tabular->GetBeginningOfTextInCell(cell) -
337 the_locking_inset->width(bv,font) -
338 tabular->GetAdditionalWidth(cell) - 1,
339 tabular->GetAscentOfRow(i) +
340 tabular->GetDescentOfRow(i) - 1);
342 tabular->GetCellInset(cell)->draw(bv,font,baseline, cx, false);
343 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
348 x -= ADD_TO_TABULAR_WIDTH;
349 x += width(bv, font);
350 if (bv->text->status == LyXText::CHANGED_IN_DRAW)
357 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
358 int row, int cell) const
360 int x2 = x + tabular->GetWidthOfColumn(cell);
363 if (!tabular->TopAlreadyDrawed(cell)) {
364 on_off = !tabular->TopLine(cell);
365 pain.line(x, baseline - tabular->GetAscentOfRow(row),
366 x2, baseline - tabular->GetAscentOfRow(row),
367 on_off ? LColor::tabularonoffline : LColor::tabularline,
368 on_off ? Painter::line_onoffdash : Painter::line_solid);
370 on_off = !tabular->BottomLine(cell);
371 pain.line(x,baseline + tabular->GetDescentOfRow(row),
372 x2, baseline + tabular->GetDescentOfRow(row),
373 on_off ? LColor::tabularonoffline : LColor::tabularline,
374 on_off ? Painter::line_onoffdash : Painter::line_solid);
375 if (!tabular->LeftAlreadyDrawed(cell)) {
376 on_off = !tabular->LeftLine(cell);
377 pain.line(x, baseline - tabular->GetAscentOfRow(row),
378 x, baseline + tabular->GetDescentOfRow(row),
379 on_off ? LColor::tabularonoffline : LColor::tabularline,
380 on_off ? Painter::line_onoffdash : Painter::line_solid);
382 on_off = !tabular->RightLine(cell);
383 pain.line(x2 - tabular->GetAdditionalWidth(cell),
384 baseline - tabular->GetAscentOfRow(row),
385 x2 - tabular->GetAdditionalWidth(cell),
386 baseline + tabular->GetDescentOfRow(row),
387 on_off ? LColor::tabularonoffline : LColor::tabularline,
388 on_off ? Painter::line_onoffdash : Painter::line_solid);
392 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
393 int row, int column, int cell) const
395 int cs = tabular->column_of_cell(sel_cell_start);
396 int ce = tabular->column_of_cell(sel_cell_end);
399 cs = tabular->column_of_cell(sel_cell_end);
401 ce = tabular->right_column_of_cell(sel_cell_end);
404 int rs = tabular->row_of_cell(sel_cell_start);
405 int re = tabular->row_of_cell(sel_cell_end);
406 if (rs > re) swap(rs, re);
408 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
409 int w = tabular->GetWidthOfColumn(cell);
410 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
411 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
412 w, h, LColor::selection);
417 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
421 calculate_dimensions_of_cells(bv, font, true);
423 owner()->update(bv, font, true);
426 if (the_locking_inset) {
427 the_locking_inset->update(bv, font, reinit);
429 // inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
430 // inset_y = cursor.y();
432 switch (need_update) {
436 if (calculate_dimensions_of_cells(bv, font, false))
448 string const InsetTabular::EditMessage() const
450 return _("Opened Tabular Inset");
454 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
456 UpdatableInset::Edit(bv, x, y, button);
458 if (!bv->lockInset(this)) {
459 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
463 the_locking_inset = 0;
468 sel_pos_start = sel_pos_end = cursor.pos();
469 sel_cell_start = sel_cell_end = actcell;
470 bv->text->FinishUndo();
471 if (InsetHit(bv, x, y)) {
472 ActivateCellInset(bv, x, y, button);
474 // UpdateLocal(bv, NONE, false);
475 // bv->getOwner()->getPopups().updateFormTabular();
479 void InsetTabular::InsetUnlock(BufferView * bv)
481 if (the_locking_inset) {
482 the_locking_inset->InsetUnlock(bv);
483 the_locking_inset = 0;
486 no_selection = false;
489 if (scroll() || hasSelection()) {
493 sel_pos_start = sel_pos_end = 0;
494 sel_cell_start = sel_cell_end = 0;
496 UpdateLocal(bv, FULL, false);
501 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
502 bool mark_dirty) const
504 if (need_update < what) // only set this if it has greater update
507 if (need_update != NONE) {
508 bv->updateInset(const_cast<InsetTabular *>(this), mark_dirty);
509 if (locked) // && (what != NONE))
515 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
517 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
521 if (inset == tabular->GetCellInset(actcell)) {
522 lyxerr[Debug::INSETS] << "OK" << endl;
523 the_locking_inset = tabular->GetCellInset(actcell);
525 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
526 inset_y = cursor.y();
527 inset_pos = cursor.pos();
529 } else if (the_locking_inset && (the_locking_inset == inset)) {
530 if (cursor.pos() == inset_pos) {
531 lyxerr[Debug::INSETS] << "OK" << endl;
533 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
534 inset_y = cursor.y();
536 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
538 } else if (the_locking_inset) {
539 lyxerr[Debug::INSETS] << "MAYBE" << endl;
540 return the_locking_inset->LockInsetInInset(bv, inset);
542 lyxerr[Debug::INSETS] << "NOT OK" << endl;
547 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
550 if (!the_locking_inset)
552 if (the_locking_inset == inset) {
553 the_locking_inset->InsetUnlock(bv);
554 the_locking_inset = 0;
556 moveRight(bv, false);
557 ShowInsetCursor(bv, false);
558 UpdateLocal(bv, CELL, false);
561 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
562 if (inset->LyxCode() == TABULAR_CODE &&
563 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
565 bv->owner()->getDialogs()->updateTabular(this);
574 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
576 if (!the_locking_inset)
578 if (the_locking_inset != inset) {
579 if (!the_locking_inset->UpdateInsetInInset(bv, inset))
582 UpdateLocal(bv, CELL, false);
587 unsigned int InsetTabular::InsetInInsetY()
589 if (!the_locking_inset)
592 return (inset_y + the_locking_inset->InsetInInsetY());
596 UpdatableInset * InsetTabular::GetLockingInset()
598 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
602 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
606 if (the_locking_inset)
607 return the_locking_inset->GetFirstLockingInsetOfType(c);
612 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
614 if (the_locking_inset)
615 return the_locking_inset->InsertInset(bv, inset);
620 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
622 if (hasSelection() && (button != 3)) {
624 UpdateLocal(bv, SELECTION, false);
626 no_selection = false;
628 int const ocell = actcell;
629 int const orow = actrow;
634 UpdateLocal(bv, NONE, false);
635 sel_pos_start = sel_pos_end = cursor.pos();
636 sel_cell_start = sel_cell_end = actcell;
638 bool const inset_hit = InsetHit(bv, x, y);
640 if ((ocell == actcell) && the_locking_inset && inset_hit) {
641 cursor.pos(0); // always before the inset!
643 the_locking_inset->InsetButtonPress(bv,
644 x - inset_x, y - inset_y, button);
646 } else if (the_locking_inset) {
647 the_locking_inset->InsetUnlock(bv);
649 the_locking_inset = 0;
650 if (inset_hit && bv->theLockingInset()) {
651 if (ActivateCellInset(bv, x, y, button))
652 the_locking_inset->InsetButtonPress(bv, x - inset_x,
653 y - inset_y, button);
660 void InsetTabular::InsetButtonRelease(BufferView * bv,
661 int x, int y, int button)
664 if (the_locking_inset) {
666 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
667 i->InsetButtonRelease(bv, x, y, button);
671 bv->owner()->getDialogs()->showTabular(this);
674 if (the_locking_inset) {
675 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
678 no_selection = false;
682 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
684 if (the_locking_inset) {
685 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
686 y - inset_y, button);
691 LyXParagraph::size_type const old_pos = sel_pos_end;
692 int const old_cell = actcell;
695 sel_pos_end = cursor.pos();
696 sel_cell_end = actcell;
697 if ((sel_cell_end != old_cell) || (old_pos != sel_pos_end))
698 UpdateLocal(bv, SELECTION, false);
701 no_selection = false;
705 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
707 if (the_locking_inset) {
708 the_locking_inset->InsetKeyPress(xke);
714 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
717 UpdatableInset::RESULT
720 no_selection = false;
721 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
722 || (result == DISPATCHED_NOUPDATE)) {
728 if ((action < 0) && arg.empty())
731 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
732 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
734 if (the_locking_inset) {
735 result=the_locking_inset->LocalDispatch(bv, action, arg);
736 if (result == DISPATCHED_NOUPDATE)
738 else if (result == DISPATCHED) {
739 the_locking_inset->ToggleInsetCursor(bv);
740 UpdateLocal(bv, CELL, false);
741 the_locking_inset->ToggleInsetCursor(bv);
743 } else if (result == FINISHED) {
744 if ((action == LFUN_RIGHT) || (action == -1)) {
745 cursor.pos(inset_pos + 1);
748 sel_pos_start = sel_pos_end = cursor.pos();
749 sel_cell_start = sel_cell_end = actcell;
757 bool hs = hasSelection();
761 // --- Cursor Movements ---------------------------------------------
763 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
765 moveRight(bv, false);
767 if (!cellstart(cursor.pos())) {
768 sel_pos_end = cursor.pos();
769 if (tabular->right_column_of_cell(sel_cell_start) >
770 tabular->right_column_of_cell(actcell))
771 sel_cell_end = actcell+1;
773 sel_cell_end = actcell;
776 sel_pos_end = cursor.pos();
777 sel_cell_end = actcell;
779 UpdateLocal(bv, SELECTION, false);
782 result = moveRight(bv);
783 sel_pos_start = sel_pos_end = cursor.pos();
784 sel_cell_start = sel_cell_end = actcell;
786 UpdateLocal(bv, SELECTION, false);
789 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
793 if (cellstart(cursor.pos())) {
794 sel_pos_end = cursor.pos();
795 if (tabular->column_of_cell(sel_cell_start) >=
796 tabular->column_of_cell(actcell))
797 sel_cell_end = actcell;
799 sel_cell_end = actcell-1;
802 sel_pos_end = cursor.pos();
803 sel_cell_end = actcell;
805 UpdateLocal(bv, SELECTION, false);
808 result = moveLeft(bv);
809 sel_pos_start = sel_pos_end = cursor.pos();
810 sel_cell_start = sel_cell_end = actcell;
812 UpdateLocal(bv, SELECTION, false);
816 int const ocell = actcell;
818 sel_pos_end = cursor.pos();
819 if ((ocell == sel_cell_end) ||
820 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
821 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
823 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
824 UpdateLocal(bv, SELECTION, false);
828 result = moveDown(bv);
829 sel_pos_start = sel_pos_end = cursor.pos();
830 sel_cell_start = sel_cell_end = actcell;
832 UpdateLocal(bv, SELECTION, false);
836 int const ocell = actcell;
838 sel_pos_end = cursor.pos();
839 if ((ocell == sel_cell_end) ||
840 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
841 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
843 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
844 UpdateLocal(bv, SELECTION, false);
849 sel_pos_start = sel_pos_end = cursor.pos();
850 sel_cell_start = sel_cell_end = actcell;
852 UpdateLocal(bv, SELECTION, false);
864 if (the_locking_inset) {
865 UnlockInsetInInset(bv, the_locking_inset);
866 the_locking_inset = 0;
868 if (action == LFUN_TAB)
872 sel_pos_start = sel_pos_end = cursor.pos();
873 sel_cell_start = sel_cell_end = actcell;
875 UpdateLocal(bv, SELECTION, false);
877 case LFUN_LAYOUT_TABULAR:
879 bv->owner()->getDialogs()->showTabular(this);
882 case LFUN_TABULAR_FEATURE:
883 if (!TabularFeatures(bv, arg))
884 result = UNDISPATCHED;
887 if (!copySelection(bv))
889 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
891 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
892 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
894 bv->text->cursor.par()->previous,
895 bv->text->cursor.par()->next
899 UpdateLocal(bv, INIT, true);
904 bv->text->FinishUndo();
908 if (!hasPasteBuffer())
910 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
912 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
913 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
915 bv->text->cursor.par()->previous,
916 bv->text->cursor.par()->next
920 UpdateLocal(bv, INIT, true);
923 // we try to activate the actual inset and put this event down to
924 // the insets dispatch function.
925 result = UNDISPATCHED;
926 if (the_locking_inset)
928 if (ActivateCellInset(bv, 0, 0, 0, !cellstart(cursor.pos()))) {
929 result=the_locking_inset->LocalDispatch(bv, action, arg);
930 if (result == DISPATCHED_NOUPDATE)
932 else if (result == DISPATCHED) {
933 the_locking_inset->ToggleInsetCursor(bv);
934 UpdateLocal(bv, CELL, false);
935 the_locking_inset->ToggleInsetCursor(bv);
938 result = UNDISPATCHED;
943 if (result!=FINISHED) {
944 if (!the_locking_inset) {
948 bv->unlockInset(this);
953 int InsetTabular::Latex(Buffer const * buf, ostream & os,
954 bool fragile, bool fp) const
956 return tabular->Latex(buf, os, fragile, fp);
960 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
962 // This should be changed to a real ascii export
963 return tabular->Ascii(buf, os);
967 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
973 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
975 return tabular->DocBook(buf,os);
979 void InsetTabular::Validate(LaTeXFeatures & features) const
981 tabular->Validate(features);
985 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
986 LyXFont const & font,
992 bool changed = false;
994 for (int i = 0; i < tabular->rows(); ++i) {
995 maxAsc = maxDesc = 0;
996 for (int j= 0; j < tabular->columns(); ++j) {
997 if (tabular->IsPartOfMultiColumn(i,j))
1000 inset = tabular->GetCellInset(cell);
1002 inset->update(bv, font, false);
1003 maxAsc = max(maxAsc, inset->ascent(bv, font));
1004 maxDesc = max(maxDesc, inset->descent(bv, font));
1005 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1007 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1008 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1014 void InsetTabular::GetCursorPos(BufferView *,
1015 int & x, int & y) const
1017 x = cursor.x() - top_x;
1022 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1024 if (the_locking_inset) {
1025 the_locking_inset->ToggleInsetCursor(bv);
1029 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1031 int const asc = lyxfont::maxAscent(font);
1032 int const desc = lyxfont::maxDescent(font);
1035 bv->hideLockedInsetCursor();
1037 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1038 cursor_visible = !cursor_visible;
1042 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1044 if (!cursor_visible) {
1045 LyXFont font; // = GetFont(par, cursor.pos);
1047 int const asc = lyxfont::maxAscent(font);
1048 int const desc = lyxfont::maxDescent(font);
1049 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1051 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1052 cursor_visible = true;
1057 void InsetTabular::HideInsetCursor(BufferView * bv)
1059 if (cursor_visible) {
1060 bv->hideLockedInsetCursor();
1061 cursor_visible = false;
1063 // if (cursor_visible)
1064 // ToggleInsetCursor(bv);
1068 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1073 actcell = actrow = actcol = 0;
1074 int ly = tabular->GetDescentOfRow(actrow);
1076 // first search the right row
1077 while((ly < y) && (actrow < tabular->rows())) {
1078 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1079 tabular->GetAscentOfRow(actrow + 1) +
1080 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1083 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1085 actcell = tabular->GetCellNumber(actrow, actcol);
1087 // now search the right column
1088 int lx = tabular->GetWidthOfColumn(actcell) -
1089 tabular->GetAdditionalWidth(actcell);
1090 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1092 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1093 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1094 tabular->GetAdditionalWidth(actcell - 1));
1096 // Jürgen, you should check that this is correct. (Lgb)
1097 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1098 lx += tabular->GetWidthOfColumn(actcell + 1)
1099 + tabular->GetAdditionalWidth(actcell);
1105 if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1106 cursor.x(lx + top_x - 2);
1109 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1115 int InsetTabular::getCellXPos(int cell) const
1119 for (; !tabular->IsFirstCellInRow(c); --c)
1121 int lx = tabular->GetWidthOfColumn(cell);
1122 for (; c < cell; ++c) {
1123 lx += tabular->GetWidthOfColumn(c);
1125 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1129 void InsetTabular::resetPos(BufferView * bv) const
1133 actcol = tabular->column_of_cell(actcell);
1138 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1139 if (tabular->IsLastCellInRow(cell)) {
1140 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1141 tabular->GetAscentOfRow(actrow + 1) +
1142 tabular->GetAdditionalHeight(cell + 1));
1146 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1147 int new_x = getCellXPos(actcell);
1150 // cursor.x(getCellXPos(actcell) + offset);
1151 if (((cursor.x() - offset) > 20) &&
1152 ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1153 (bv->workWidth()-20)))
1155 scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1156 UpdateLocal(bv, FULL, false);
1157 } else if ((cursor.x() - offset) < 20) {
1158 scroll(bv, 20 - cursor.x() + offset);
1159 UpdateLocal(bv, FULL, false);
1160 } else if (!cellstart(cursor.pos())) {
1161 LyXFont font(LyXFont::ALL_SANE);
1162 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1163 tabular->GetBeginningOfTextInCell(actcell));
1165 if ((!the_locking_inset ||
1166 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1167 (actcell != oldcell)) {
1168 InsetTabular * inset = const_cast<InsetTabular *>(this);
1169 bv->owner()->getDialogs()->updateTabular(inset);
1175 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1177 if (!cellstart(cursor.pos())) {
1178 if (tabular->IsLastCell(actcell))
1183 if (ActivateCellInset(bv))
1185 } else { // before the inset
1189 return DISPATCHED_NOUPDATE;
1193 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1195 if (cellstart(cursor.pos()) && !actcell)
1197 if (cellstart(cursor.pos())) {
1200 } else if (lock) { // behind the inset
1202 if (ActivateCellInset(bv, 0, 0, 0, true))
1208 return DISPATCHED_NOUPDATE;
1212 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1214 int const ocell = actcell;
1215 actcell = tabular->GetCellAbove(actcell);
1216 if (actcell == ocell) // we moved out of the inset
1219 return DISPATCHED_NOUPDATE;
1223 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1225 int const ocell = actcell;
1226 actcell = tabular->GetCellBelow(actcell);
1227 if (actcell == ocell) // we moved out of the inset
1230 return DISPATCHED_NOUPDATE;
1234 bool InsetTabular::moveNextCell(BufferView * bv)
1236 if (tabular->IsLastCell(actcell))
1244 bool InsetTabular::movePrevCell(BufferView * bv)
1246 if (!actcell) // first cell
1254 bool InsetTabular::Delete()
1260 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1262 if (the_locking_inset)
1263 the_locking_inset->SetFont(bv, font, tall);
1267 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1269 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1272 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1273 string const tmp = tabularFeatures[i].feature;
1275 if (tmp == what.substr(0, tmp.length())) {
1276 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1277 //tabularFeatures[i].feature.length())) {
1278 action = tabularFeatures[i].action;
1282 if (action == LyXTabular::LAST_ACTION)
1286 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1287 TabularFeatures(bv, action, val);
1292 void InsetTabular::TabularFeatures(BufferView * bv,
1293 LyXTabular::Feature feature,
1294 string const & value)
1303 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1304 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1309 case LyXTabular::M_ALIGN_LEFT:
1310 case LyXTabular::ALIGN_LEFT:
1311 setAlign=LYX_ALIGN_LEFT;
1313 case LyXTabular::M_ALIGN_RIGHT:
1314 case LyXTabular::ALIGN_RIGHT:
1315 setAlign=LYX_ALIGN_RIGHT;
1317 case LyXTabular::M_ALIGN_CENTER:
1318 case LyXTabular::ALIGN_CENTER:
1319 setAlign=LYX_ALIGN_CENTER;
1321 case LyXTabular::M_VALIGN_TOP:
1322 case LyXTabular::VALIGN_TOP:
1323 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1325 case LyXTabular::M_VALIGN_BOTTOM:
1326 case LyXTabular::VALIGN_BOTTOM:
1327 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1329 case LyXTabular::M_VALIGN_CENTER:
1330 case LyXTabular::VALIGN_CENTER:
1331 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1336 if (hasSelection()) {
1337 sel_col_start = tabular->column_of_cell(sel_cell_start);
1338 sel_col_end = tabular->column_of_cell(sel_cell_end);
1339 if (sel_col_start > sel_col_end) {
1340 sel_col_end = sel_col_start;
1341 sel_col_start = tabular->column_of_cell(sel_cell_end);
1343 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1346 sel_row_start = tabular->row_of_cell(sel_cell_start);
1347 sel_row_end = tabular->row_of_cell(sel_cell_end);
1348 if (sel_row_start > sel_row_end) {
1349 //int tmp = sel_row_start;
1350 //sel_row_start = sel_row_end;
1351 //sel_row_end = tmp;
1352 swap(sel_row_start, sel_row_end);
1355 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1356 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1358 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1360 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1361 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1363 bv->text->cursor.par()->previous,
1364 bv->text->cursor.par()->next
1368 int row = tabular->row_of_cell(actcell);
1369 int column = tabular->column_of_cell(actcell);
1373 case LyXTabular::SET_PWIDTH:
1375 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1376 tabular->SetColumnPWidth(actcell,value);
1378 for (int i=0; i < tabular->rows(); ++i) {
1379 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1382 UpdateLocal(bv, INIT, true);
1386 case LyXTabular::SET_MPWIDTH:
1388 bool const update = (tabular->GetPWidth(actcell) != value);
1389 tabular->SetMColumnPWidth(actcell,value);
1391 for (int i=0; i < tabular->rows(); ++i) {
1392 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1395 UpdateLocal(bv, INIT, true);
1399 case LyXTabular::SET_SPECIAL_COLUMN:
1400 case LyXTabular::SET_SPECIAL_MULTI:
1401 tabular->SetAlignSpecial(actcell,value,feature);
1403 case LyXTabular::APPEND_ROW:
1404 // append the row into the tabular
1405 UnlockInsetInInset(bv, the_locking_inset);
1406 tabular->AppendRow(actcell);
1407 UpdateLocal(bv, INIT, true);
1409 case LyXTabular::APPEND_COLUMN:
1410 // append the column into the tabular
1411 tabular->AppendColumn(actcell);
1412 actcell = tabular->GetCellNumber(row, column);
1413 UpdateLocal(bv, INIT, true);
1415 case LyXTabular::DELETE_ROW:
1416 tabular->DeleteRow(tabular->row_of_cell(actcell));
1417 if ((row+1) > tabular->rows())
1419 actcell = tabular->GetCellNumber(row, column);
1421 UpdateLocal(bv, INIT, true);
1423 case LyXTabular::DELETE_COLUMN:
1424 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1425 if ((column+1) > tabular->columns())
1427 actcell = tabular->GetCellNumber(row, column);
1429 UpdateLocal(bv, INIT, true);
1431 case LyXTabular::M_TOGGLE_LINE_TOP:
1433 case LyXTabular::TOGGLE_LINE_TOP:
1434 lineSet = !tabular->TopLine(actcell, flag);
1435 for (i=sel_row_start; i<=sel_row_end; ++i)
1436 for (j=sel_col_start; j<=sel_col_end; ++j)
1437 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1438 UpdateLocal(bv, INIT, true);
1441 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1443 case LyXTabular::TOGGLE_LINE_BOTTOM:
1444 lineSet = !tabular->BottomLine(actcell, flag);
1445 for (i=sel_row_start; i<=sel_row_end; ++i)
1446 for (j=sel_col_start; j<=sel_col_end; ++j)
1447 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1449 UpdateLocal(bv, INIT, true);
1452 case LyXTabular::M_TOGGLE_LINE_LEFT:
1454 case LyXTabular::TOGGLE_LINE_LEFT:
1455 lineSet = !tabular->LeftLine(actcell, flag);
1456 for (i=sel_row_start; i<=sel_row_end; ++i)
1457 for (j=sel_col_start; j<=sel_col_end; ++j)
1458 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1460 UpdateLocal(bv, INIT, true);
1463 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1465 case LyXTabular::TOGGLE_LINE_RIGHT:
1466 lineSet = !tabular->RightLine(actcell, flag);
1467 for (i=sel_row_start; i<=sel_row_end; ++i)
1468 for (j=sel_col_start; j<=sel_col_end; ++j)
1469 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1471 UpdateLocal(bv, INIT, true);
1473 case LyXTabular::M_ALIGN_LEFT:
1474 case LyXTabular::M_ALIGN_RIGHT:
1475 case LyXTabular::M_ALIGN_CENTER:
1477 case LyXTabular::ALIGN_LEFT:
1478 case LyXTabular::ALIGN_RIGHT:
1479 case LyXTabular::ALIGN_CENTER:
1480 for (i = sel_row_start; i <= sel_row_end; ++i)
1481 for (j = sel_col_start; j <= sel_col_end; ++j)
1482 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1485 UpdateLocal(bv, INIT, true);
1487 UpdateLocal(bv, CELL, true);
1489 case LyXTabular::M_VALIGN_TOP:
1490 case LyXTabular::M_VALIGN_BOTTOM:
1491 case LyXTabular::M_VALIGN_CENTER:
1493 case LyXTabular::VALIGN_TOP:
1494 case LyXTabular::VALIGN_BOTTOM:
1495 case LyXTabular::VALIGN_CENTER:
1496 for (i = sel_row_start; i <= sel_row_end; ++i)
1497 for (j = sel_col_start; j <= sel_col_end; ++j)
1498 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1501 UpdateLocal(bv, INIT, true);
1503 UpdateLocal(bv, CELL, true);
1505 case LyXTabular::MULTICOLUMN:
1507 if (sel_row_start != sel_row_end) {
1508 WriteAlert(_("Impossible Operation!"),
1509 _("Multicolumns can only be horizontally."),
1513 // just multicol for one Single Cell
1514 if (!hasSelection()) {
1515 // check wether we are completly in a multicol
1516 if (tabular->IsMultiColumn(actcell)) {
1517 tabular->UnsetMultiColumn(actcell);
1518 UpdateLocal(bv, INIT, true);
1520 tabular->SetMultiColumn(actcell, 1);
1521 UpdateLocal(bv, CELL, true);
1525 // we have a selection so this means we just add all this
1526 // cells to form a multicolumn cell
1530 if (sel_cell_start > sel_cell_end) {
1531 s_start = sel_cell_end;
1532 s_end = sel_cell_start;
1534 s_start = sel_cell_start;
1535 s_end = sel_cell_end;
1537 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1540 sel_cell_end = sel_cell_start;
1541 sel_pos_end = sel_pos_start;
1542 UpdateLocal(bv, INIT, true);
1545 case LyXTabular::SET_ALL_LINES:
1547 case LyXTabular::UNSET_ALL_LINES:
1548 for (i=sel_row_start; i<=sel_row_end; ++i)
1549 for (j=sel_col_start; j<=sel_col_end; ++j)
1550 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1551 UpdateLocal(bv, INIT, true);
1553 case LyXTabular::SET_LONGTABULAR:
1554 tabular->SetLongTabular(true);
1555 UpdateLocal(bv, INIT, true); // because this toggles displayed
1557 case LyXTabular::UNSET_LONGTABULAR:
1558 tabular->SetLongTabular(false);
1559 UpdateLocal(bv, INIT, true); // because this toggles displayed
1561 case LyXTabular::SET_ROTATE_TABULAR:
1562 tabular->SetRotateTabular(true);
1564 case LyXTabular::UNSET_ROTATE_TABULAR:
1565 tabular->SetRotateTabular(false);
1567 case LyXTabular::SET_ROTATE_CELL:
1568 for (i=sel_row_start; i<=sel_row_end; ++i)
1569 for (j=sel_col_start; j<=sel_col_end; ++j)
1570 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1572 case LyXTabular::UNSET_ROTATE_CELL:
1573 for (i = sel_row_start; i <= sel_row_end; ++i)
1574 for (j = sel_col_start; j <= sel_col_end; ++j)
1575 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1577 case LyXTabular::SET_USEBOX:
1579 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1580 if (val == tabular->GetUsebox(actcell))
1581 val = LyXTabular::BOX_NONE;
1582 for (i = sel_row_start; i <= sel_row_end; ++i)
1583 for (j = sel_col_start; j <= sel_col_end; ++j)
1584 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1587 case LyXTabular::SET_LTFIRSTHEAD:
1588 tabular->SetLTHead(actcell, true);
1590 case LyXTabular::SET_LTHEAD:
1591 tabular->SetLTHead(actcell, false);
1593 case LyXTabular::SET_LTFOOT:
1594 tabular->SetLTFoot(actcell, false);
1596 case LyXTabular::SET_LTLASTFOOT:
1597 tabular->SetLTFoot(actcell, true);
1599 case LyXTabular::SET_LTNEWPAGE:
1600 what = !tabular->GetLTNewPage(actcell);
1601 tabular->SetLTNewPage(actcell, what);
1603 // dummy stuff just to avoid warnings
1604 case LyXTabular::LAST_ACTION:
1610 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1613 // the cursor.pos has to be before the inset so if it isn't now just
1614 // reset the curor pos first!
1615 if (!cellstart(cursor.pos())) {
1619 UpdatableInset * inset =
1620 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1621 LyXFont font(LyXFont::ALL_SANE);
1623 x = inset->x() + inset->width(bv, font);
1624 y = inset->descent(bv, font);
1626 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1627 inset_y = cursor.y();
1628 inset->Edit(bv, x - inset_x, y - inset_y, button);
1629 if (!the_locking_inset)
1631 UpdateLocal(bv, CELL, false);
1632 return (the_locking_inset != 0);
1636 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1638 InsetText * inset = tabular->GetCellInset(actcell);
1639 int const x1 = x + top_x;
1641 if (!cellstart(cursor.pos())) {
1642 return ((x + top_x) < cursor.x() &&
1643 (x + top_x) > (cursor.x() - inset->width(bv,
1644 LyXFont(LyXFont::ALL_SANE))));
1646 int const x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1648 x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE))));
1653 // This returns paperWidth() if the cell-width is unlimited or the width
1654 // in pixels if we have a pwidth for this cell.
1655 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1657 string const s = tabular->GetPWidth(cell);
1661 return VSpace(s).inPixels(0, 0);
1665 int InsetTabular::getMaxWidth(Painter & pain,
1666 UpdatableInset const * inset) const
1668 int const n = tabular->GetNumberOfCells();
1670 for (; cell < n; ++cell) {
1671 if (tabular->GetCellInset(cell) == inset)
1676 int w = GetMaxWidthOfCell(pain, cell);
1678 // because the inset then subtracts it's top_x and owner->x()
1679 w += (inset->x() - top_x);
1684 void InsetTabular::resizeLyXText(BufferView *) const
1690 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1692 if (the_locking_inset)
1693 return the_locking_inset->getLyXText(bv);
1694 return Inset::getLyXText(bv);
1698 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1700 if (the_locking_inset) {
1701 InsetTabular * i = static_cast<InsetTabular *>
1702 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1704 i->OpenLayoutDialog(bv);
1708 bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1712 // functions returns:
1716 // 3 ... toggled off
1718 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1720 int action = LyXTabular::LAST_ACTION;
1721 LyXFunc::func_status status = LyXFunc::OK;
1724 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1725 string const tmp = tabularFeatures[i].feature;
1726 if (tmp == what.substr(0, tmp.length())) {
1727 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1728 // tabularFeatures[i].feature.length())) {
1729 action = tabularFeatures[i].action;
1733 if (action == LyXTabular::LAST_ACTION)
1734 return LyXFunc::Unknown;
1736 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1738 int sel_row_start, sel_row_end;
1742 if (hasSelection()) {
1743 sel_row_start = tabular->row_of_cell(sel_cell_start);
1744 sel_row_end = tabular->row_of_cell(sel_cell_end);
1745 if (sel_row_start > sel_row_end) {
1746 //int tmp = sel_row_start;
1747 //sel_row_start = sel_row_end;
1748 //sel_row_end = tmp;
1749 swap(sel_row_start, sel_row_end);
1752 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1756 case LyXTabular::SET_PWIDTH:
1757 case LyXTabular::SET_MPWIDTH:
1758 case LyXTabular::SET_SPECIAL_COLUMN:
1759 case LyXTabular::SET_SPECIAL_MULTI:
1760 status |= LyXFunc::Disabled;
1763 case LyXTabular::APPEND_ROW:
1764 case LyXTabular::APPEND_COLUMN:
1765 case LyXTabular::DELETE_ROW:
1766 case LyXTabular::DELETE_COLUMN:
1767 case LyXTabular::SET_ALL_LINES:
1768 case LyXTabular::UNSET_ALL_LINES:
1769 status |= LyXFunc::OK;
1772 case LyXTabular::MULTICOLUMN:
1773 if (tabular->IsMultiColumn(actcell))
1774 status |= LyXFunc::ToggleOn;
1776 status |= LyXFunc::ToggleOff;
1778 case LyXTabular::M_TOGGLE_LINE_TOP:
1780 case LyXTabular::TOGGLE_LINE_TOP:
1781 if (tabular->TopLine(actcell, flag))
1782 status |= LyXFunc::ToggleOn;
1784 status |= LyXFunc::ToggleOff;
1786 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1788 case LyXTabular::TOGGLE_LINE_BOTTOM:
1789 if (tabular->BottomLine(actcell, flag))
1790 status |= LyXFunc::ToggleOn;
1792 status |= LyXFunc::ToggleOff;
1794 case LyXTabular::M_TOGGLE_LINE_LEFT:
1796 case LyXTabular::TOGGLE_LINE_LEFT:
1797 if (tabular->LeftLine(actcell, flag))
1798 status |= LyXFunc::ToggleOn;
1800 status |= LyXFunc::ToggleOff;
1802 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1804 case LyXTabular::TOGGLE_LINE_RIGHT:
1805 if (tabular->RightLine(actcell, flag))
1806 status |= LyXFunc::ToggleOn;
1808 status |= LyXFunc::ToggleOff;
1810 case LyXTabular::M_ALIGN_LEFT:
1812 case LyXTabular::ALIGN_LEFT:
1813 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
1814 status |= LyXFunc::ToggleOn;
1816 status |= LyXFunc::ToggleOff;
1818 case LyXTabular::M_ALIGN_RIGHT:
1820 case LyXTabular::ALIGN_RIGHT:
1821 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
1822 status |= LyXFunc::ToggleOn;
1824 status |= LyXFunc::ToggleOff;
1826 case LyXTabular::M_ALIGN_CENTER:
1828 case LyXTabular::ALIGN_CENTER:
1829 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
1830 status |= LyXFunc::ToggleOn;
1832 status |= LyXFunc::ToggleOff;
1834 case LyXTabular::M_VALIGN_TOP:
1836 case LyXTabular::VALIGN_TOP:
1837 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
1838 status |= LyXFunc::ToggleOn;
1840 status |= LyXFunc::ToggleOff;
1842 case LyXTabular::M_VALIGN_BOTTOM:
1844 case LyXTabular::VALIGN_BOTTOM:
1845 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
1846 status |= LyXFunc::ToggleOn;
1848 status |= LyXFunc::ToggleOff;
1850 case LyXTabular::M_VALIGN_CENTER:
1852 case LyXTabular::VALIGN_CENTER:
1853 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
1854 status |= LyXFunc::ToggleOn;
1856 status |= LyXFunc::ToggleOff;
1858 case LyXTabular::SET_LONGTABULAR:
1859 if (tabular->IsLongTabular())
1860 status |= LyXFunc::ToggleOn;
1862 status |= LyXFunc::ToggleOff;
1864 case LyXTabular::UNSET_LONGTABULAR:
1865 if (!tabular->IsLongTabular())
1866 status |= LyXFunc::ToggleOn;
1868 status |= LyXFunc::ToggleOff;
1870 case LyXTabular::SET_ROTATE_TABULAR:
1871 if (tabular->GetRotateTabular())
1872 status |= LyXFunc::ToggleOn;
1874 status |= LyXFunc::ToggleOff;
1876 case LyXTabular::UNSET_ROTATE_TABULAR:
1877 if (!tabular->GetRotateTabular())
1878 status |= LyXFunc::ToggleOn;
1880 status |= LyXFunc::ToggleOff;
1882 case LyXTabular::SET_ROTATE_CELL:
1883 if (tabular->GetRotateCell(actcell))
1884 status |= LyXFunc::ToggleOn;
1886 status |= LyXFunc::ToggleOff;
1888 case LyXTabular::UNSET_ROTATE_CELL:
1889 if (!tabular->GetRotateCell(actcell))
1890 status |= LyXFunc::ToggleOn;
1892 status |= LyXFunc::ToggleOff;
1894 case LyXTabular::SET_USEBOX:
1895 if (strToInt(argument) == tabular->GetUsebox(actcell))
1896 status |= LyXFunc::ToggleOn;
1898 status |= LyXFunc::ToggleOff;
1900 case LyXTabular::SET_LTFIRSTHEAD:
1901 if (tabular->GetRowOfLTHead(actcell, dummy))
1902 status |= LyXFunc::ToggleOn;
1904 status |= LyXFunc::ToggleOff;
1906 case LyXTabular::SET_LTHEAD:
1907 if (tabular->GetRowOfLTHead(actcell, dummy))
1908 status |= LyXFunc::ToggleOn;
1910 status |= LyXFunc::ToggleOff;
1912 case LyXTabular::SET_LTFOOT:
1913 if (tabular->GetRowOfLTFoot(actcell, dummy))
1914 status |= LyXFunc::ToggleOn;
1916 status |= LyXFunc::ToggleOff;
1918 case LyXTabular::SET_LTLASTFOOT:
1919 if (tabular->GetRowOfLTFoot(actcell, dummy))
1920 status |= LyXFunc::ToggleOn;
1922 status |= LyXFunc::ToggleOff;
1924 case LyXTabular::SET_LTNEWPAGE:
1925 if (tabular->GetLTNewPage(actcell))
1926 status |= LyXFunc::ToggleOn;
1928 status |= LyXFunc::ToggleOff;
1931 status = LyXFunc::Disabled;
1938 bool InsetTabular::copySelection(BufferView * bv)
1940 if (!hasSelection())
1942 delete paste_tabular;
1944 int sel_col_start, sel_col_end;
1945 int sel_row_start, sel_row_end;
1947 sel_col_start = tabular->column_of_cell(sel_cell_start);
1948 sel_col_end = tabular->column_of_cell(sel_cell_end);
1949 if (sel_col_start > sel_col_end) {
1950 sel_col_start = sel_col_end;
1951 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
1953 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1955 int columns = sel_col_end - sel_col_start + 1;
1957 sel_row_start = tabular->row_of_cell(sel_cell_start);
1958 sel_row_end = tabular->row_of_cell(sel_cell_end);
1959 if (sel_row_start > sel_row_end) {
1960 //int tmp tmp = sel_row_start;
1961 //sel_row_start = sel_row_end;
1962 //sel_row_end = tmp;
1963 swap(sel_row_start, sel_row_end);
1965 int rows = sel_row_end - sel_row_start + 1;
1967 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
1969 for (i=0; i < sel_row_start; ++i)
1970 paste_tabular->DeleteRow(0);
1971 while(paste_tabular->rows() > rows)
1972 paste_tabular->DeleteRow(rows);
1973 paste_tabular->SetTopLine(0, true, true);
1974 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
1976 for (i=0; i < sel_col_start; ++i)
1977 paste_tabular->DeleteColumn(0);
1978 while(paste_tabular->columns() > columns)
1979 paste_tabular->DeleteColumn(columns);
1980 paste_tabular->SetLeftLine(0, true, true);
1981 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
1984 paste_tabular->Ascii(bv->buffer(), sstr);
1985 bv->stuffClipboard(sstr.str().c_str());
1990 bool InsetTabular::pasteSelection(BufferView * bv)
1994 for (int j=0, i=actcell; j<paste_tabular->GetNumberOfCells(); ++j,++i) {
1995 while (paste_tabular->row_of_cell(j) > tabular->row_of_cell(i)-actrow)
1997 if (tabular->GetNumberOfCells() <= i)
1999 while (paste_tabular->row_of_cell(j) < tabular->row_of_cell(i)-actrow)
2001 if (paste_tabular->GetNumberOfCells() <= j)
2003 *(tabular->GetCellInset(i)) = *(paste_tabular->GetCellInset(j));
2004 tabular->GetCellInset(i)->setOwner(this);
2005 tabular->GetCellInset(i)->deleteLyXText(bv);
2011 bool InsetTabular::cutSelection()
2013 if (!hasSelection())
2016 int sel_col_start, sel_col_end;
2017 int sel_row_start, sel_row_end;
2019 sel_col_start = tabular->column_of_cell(sel_cell_start);
2020 sel_col_end = tabular->column_of_cell(sel_cell_end);
2021 if (sel_col_start > sel_col_end) {
2022 sel_col_start = sel_col_end;
2023 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2025 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2027 sel_row_start = tabular->row_of_cell(sel_cell_start);
2028 sel_row_end = tabular->row_of_cell(sel_cell_end);
2029 if (sel_row_start > sel_row_end) {
2030 //int tmp = sel_row_start;
2031 //sel_row_start = sel_row_end;
2032 //sel_row_end = tmp;
2033 swap(sel_row_start, sel_row_end);
2035 if (sel_cell_start > sel_cell_end) {
2036 //int tmp = sel_cell_start;
2037 //sel_cell_start = sel_cell_end;
2038 //sel_cell_end = tmp;
2039 swap(sel_cell_start, sel_cell_end);
2041 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2042 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2043 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();