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 no_selection = false;
767 UpdatableInset::RESULT result =
768 UpdatableInset::LocalDispatch(bv, action, arg);
769 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
774 if ((action < 0) && arg.empty())
777 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
778 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
780 if (the_locking_inset) {
781 result=the_locking_inset->LocalDispatch(bv, action, arg);
782 if (result == DISPATCHED_NOUPDATE)
784 else if (result == DISPATCHED) {
785 the_locking_inset->ToggleInsetCursor(bv);
786 UpdateLocal(bv, CELL, false);
787 the_locking_inset->ToggleInsetCursor(bv);
789 } else if (result == FINISHED) {
791 if ((action == LFUN_RIGHT) || (action == -1)) {
792 cursor.pos(inset_pos + 1);
794 } else if (action == LFUN_DOWN) {
795 if (moveDown(bv) == FINISHED) {
796 bv->unlockInset(this);
799 } else if (action == LFUN_UP) {
800 if (moveUp(bv) == FINISHED) {
801 bv->unlockInset(this);
805 sel_pos_start = sel_pos_end = cursor.pos();
806 sel_cell_start = sel_cell_end = actcell;
813 bool hs = hasSelection();
817 // --- Cursor Movements ---------------------------------------------
819 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
821 moveRight(bv, false);
823 if (!cellstart(cursor.pos())) {
824 sel_pos_end = cursor.pos();
825 if (tabular->right_column_of_cell(sel_cell_start) >
826 tabular->right_column_of_cell(actcell))
827 sel_cell_end = actcell+1;
829 sel_cell_end = actcell;
832 sel_pos_end = cursor.pos();
833 sel_cell_end = actcell;
835 UpdateLocal(bv, SELECTION, false);
838 result = moveRight(bv);
839 sel_pos_start = sel_pos_end = cursor.pos();
840 sel_cell_start = sel_cell_end = actcell;
842 UpdateLocal(bv, SELECTION, false);
845 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
849 if (cellstart(cursor.pos())) {
850 sel_pos_end = cursor.pos();
851 if (tabular->column_of_cell(sel_cell_start) >=
852 tabular->column_of_cell(actcell))
853 sel_cell_end = actcell;
855 sel_cell_end = actcell-1;
858 sel_pos_end = cursor.pos();
859 sel_cell_end = actcell;
861 UpdateLocal(bv, SELECTION, false);
864 result = moveLeft(bv);
865 sel_pos_start = sel_pos_end = cursor.pos();
866 sel_cell_start = sel_cell_end = actcell;
868 UpdateLocal(bv, SELECTION, false);
872 int const ocell = actcell;
874 sel_pos_end = cursor.pos();
875 if ((ocell == sel_cell_end) ||
876 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
877 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
879 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
880 UpdateLocal(bv, SELECTION, false);
884 result = moveDown(bv);
885 sel_pos_start = sel_pos_end = cursor.pos();
886 sel_cell_start = sel_cell_end = actcell;
888 UpdateLocal(bv, SELECTION, false);
892 int const ocell = actcell;
894 sel_pos_end = cursor.pos();
895 if ((ocell == sel_cell_end) ||
896 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
897 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
899 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
900 UpdateLocal(bv, SELECTION, false);
905 sel_pos_start = sel_pos_end = cursor.pos();
906 sel_cell_start = sel_cell_end = actcell;
908 UpdateLocal(bv, SELECTION, false);
922 if (the_locking_inset) {
923 UnlockInsetInInset(bv, the_locking_inset);
924 the_locking_inset = 0;
927 if (action == LFUN_TAB)
928 moveNextCell(bv, lock);
930 movePrevCell(bv, lock);
931 sel_pos_start = sel_pos_end = cursor.pos();
932 sel_cell_start = sel_cell_end = actcell;
934 UpdateLocal(bv, SELECTION, false);
937 case LFUN_LAYOUT_TABULAR:
939 bv->owner()->getDialogs()->showTabular(this);
942 case LFUN_TABULAR_FEATURE:
943 if (!TabularFeatures(bv, arg))
944 result = UNDISPATCHED;
947 if (!copySelection(bv))
949 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
951 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
952 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
954 bv->text->cursor.par()->previous,
955 bv->text->cursor.par()->next
959 UpdateLocal(bv, INIT, true);
964 bv->text->FinishUndo();
967 case LFUN_PASTESELECTION:
969 string clip(bv->workarea()->getClipboard());
973 if (clip.find('\t') != string::npos) {
977 unsigned int len = clip.length();
978 string::size_type p = 0;
981 ((p = clip.find_first_of("\t\n", p)) != string::npos))
990 maxCols = max(cols, maxCols);
996 maxCols = max(cols, maxCols);
997 delete paste_tabular;
998 paste_tabular = new LyXTabular(this, rows, maxCols);
999 string::size_type op = 0;
1001 int cells = paste_tabular->GetNumberOfCells();
1003 while((cell < cells) && (p < len) &&
1004 (p = clip.find_first_of("\t\n", p)) != string::npos)
1010 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1015 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, p-op));
1016 while(cols++ < maxCols)
1024 // check for the last cell if there is no trailing '\n'
1025 if ((cell < cells) && (op < len))
1026 paste_tabular->GetCellInset(cell)->SetText(clip.substr(op, len-op));
1028 // so that the clipboard is used and it goes on to default
1029 // and executes LFUN_PASTESELECTION in insettext!
1030 delete paste_tabular;
1035 if (hasPasteBuffer()) {
1036 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1038 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1039 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1041 bv->text->cursor.par()->previous,
1042 bv->text->cursor.par()->next
1046 UpdateLocal(bv, INIT, true);
1049 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1051 // we try to activate the actual inset and put this event down to
1052 // the insets dispatch function.
1053 result = UNDISPATCHED;
1054 if (the_locking_inset)
1056 if (ActivateCellInset(bv, 0, 0, 0, !cellstart(cursor.pos()))) {
1057 result=the_locking_inset->LocalDispatch(bv, action, arg);
1058 if (result == DISPATCHED_NOUPDATE)
1060 else if (result == DISPATCHED) {
1061 the_locking_inset->ToggleInsetCursor(bv);
1062 UpdateLocal(bv, CELL, false);
1063 the_locking_inset->ToggleInsetCursor(bv);
1066 result = UNDISPATCHED;
1071 if (result!=FINISHED) {
1072 if (!the_locking_inset) {
1073 ShowInsetCursor(bv);
1076 bv->unlockInset(this);
1081 int InsetTabular::Latex(Buffer const * buf, ostream & os,
1082 bool fragile, bool fp) const
1084 return tabular->Latex(buf, os, fragile, fp);
1088 int InsetTabular::Ascii(Buffer const * buf, ostream & os, int) const
1090 // This should be changed to a real ascii export
1091 return tabular->Ascii(buf, os);
1095 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
1101 int InsetTabular::DocBook(Buffer const * buf, ostream & os) const
1103 return tabular->DocBook(buf,os);
1107 void InsetTabular::Validate(LaTeXFeatures & features) const
1109 tabular->Validate(features);
1113 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
1114 LyXFont const & font,
1121 bool changed = false;
1123 // if we have a locking_inset we should have to check only this cell for
1124 // change so I'll try this to have a boost, but who knows ;)
1125 if ((need_update != INIT) &&
1126 (the_locking_inset == tabular->GetCellInset(actcell))) {
1127 for(int i = 0; i < tabular->columns(); ++i) {
1128 maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1130 maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1133 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1134 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1135 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1138 for (int i = 0; i < tabular->rows(); ++i) {
1141 for (int j= 0; j < tabular->columns(); ++j) {
1142 if (tabular->IsPartOfMultiColumn(i,j))
1145 inset = tabular->GetCellInset(cell);
1147 inset->update(bv, font, false);
1148 maxAsc = max(maxAsc, inset->ascent(bv, font));
1149 maxDesc = max(maxDesc, inset->descent(bv, font));
1150 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1152 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1153 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1159 void InsetTabular::GetCursorPos(BufferView *,
1160 int & x, int & y) const
1162 x = cursor.x() - top_x;
1167 void InsetTabular::ToggleInsetCursor(BufferView * bv)
1169 if (the_locking_inset) {
1170 the_locking_inset->ToggleInsetCursor(bv);
1174 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
1176 int const asc = lyxfont::maxAscent(font);
1177 int const desc = lyxfont::maxDescent(font);
1180 bv->hideLockedInsetCursor();
1182 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1183 cursor_visible = !cursor_visible;
1187 void InsetTabular::ShowInsetCursor(BufferView * bv, bool show)
1189 if (!cursor_visible) {
1190 LyXFont font; // = GetFont(par, cursor.pos);
1192 int const asc = lyxfont::maxAscent(font);
1193 int const desc = lyxfont::maxDescent(font);
1194 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1196 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
1197 cursor_visible = true;
1202 void InsetTabular::HideInsetCursor(BufferView * bv)
1204 if (cursor_visible) {
1205 bv->hideLockedInsetCursor();
1206 cursor_visible = false;
1208 // if (cursor_visible)
1209 // ToggleInsetCursor(bv);
1213 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1218 actcell = actrow = actcol = 0;
1219 int ly = tabular->GetDescentOfRow(actrow);
1221 // first search the right row
1222 while((ly < y) && (actrow < tabular->rows())) {
1223 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1224 tabular->GetAscentOfRow(actrow + 1) +
1225 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
1228 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
1230 actcell = tabular->GetCellNumber(actrow, actcol);
1232 // now search the right column
1233 int lx = tabular->GetWidthOfColumn(actcell) -
1234 tabular->GetAdditionalWidth(actcell);
1236 #warning Jürgen, can you rewrite this to _not_ use the sequencing operator. (Lgb)
1237 for (; !tabular->IsLastCellInRow(actcell) && (lx < x);
1238 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
1239 tabular->GetAdditionalWidth(actcell - 1));
1241 // Jürgen, you should check that this is correct. (Lgb)
1242 for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1243 lx += tabular->GetWidthOfColumn(actcell + 1)
1244 + tabular->GetAdditionalWidth(actcell);
1250 if ((lx - (tabular->GetWidthOfColumn(actcell) / 2)) < x) {
1251 cursor.x(lx + top_x - 2);
1254 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1260 int InsetTabular::getCellXPos(int cell) const
1264 for (; !tabular->IsFirstCellInRow(c); --c)
1266 int lx = tabular->GetWidthOfColumn(cell);
1267 for (; c < cell; ++c) {
1268 lx += tabular->GetWidthOfColumn(c);
1270 return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1274 void InsetTabular::resetPos(BufferView * bv) const
1278 actcol = tabular->column_of_cell(actcell);
1283 for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1284 if (tabular->IsLastCellInRow(cell)) {
1285 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1286 tabular->GetAscentOfRow(actrow + 1) +
1287 tabular->GetAdditionalHeight(cell + 1));
1291 static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1292 int new_x = getCellXPos(actcell);
1293 int old_x = cursor.x();
1296 // cursor.x(getCellXPos(actcell) + offset);
1297 if (((cursor.x() - offset) > 20) &&
1298 ((cursor.x()-offset+tabular->GetWidthOfColumn(actcell)) >
1299 (bv->workWidth()-20)))
1301 scroll(bv, -tabular->GetWidthOfColumn(actcell)-20);
1302 UpdateLocal(bv, FULL, false);
1303 } else if ((cursor.x() - offset) < 20) {
1304 scroll(bv, 20 - cursor.x() + offset);
1305 UpdateLocal(bv, FULL, false);
1306 } else if (!cellstart(cursor.pos())) {
1307 LyXFont font(LyXFont::ALL_SANE);
1308 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1309 tabular->GetBeginningOfTextInCell(actcell));
1310 } else if (scroll() && (top_x > 20) &&
1311 ((top_x+tabular->GetWidthOfTabular()) > (bv->workWidth()-20))) {
1312 scroll(bv, old_x - cursor.x());
1314 if ((!the_locking_inset ||
1315 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1316 (actcell != oldcell)) {
1317 InsetTabular * inset = const_cast<InsetTabular *>(this);
1318 bv->owner()->getDialogs()->updateTabular(inset);
1324 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1326 if (!cellstart(cursor.pos())) {
1327 bool moved = isRightToLeft(bv) ? movePrevCell(bv) : moveNextCell(bv);
1332 if (ActivateCellInset(bv))
1334 } else { // before the inset
1338 return DISPATCHED_NOUPDATE;
1342 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1344 if (cellstart(cursor.pos())) {
1345 bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1349 } else if (lock) { // behind the inset
1351 if (ActivateCellInset(bv, 0, 0, 0, true))
1357 return DISPATCHED_NOUPDATE;
1361 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1363 int const ocell = actcell;
1364 actcell = tabular->GetCellAbove(actcell);
1365 if (actcell == ocell) // we moved out of the inset
1368 return DISPATCHED_NOUPDATE;
1372 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1374 int const ocell = actcell;
1375 actcell = tabular->GetCellBelow(actcell);
1376 if (actcell == ocell) // we moved out of the inset
1379 return DISPATCHED_NOUPDATE;
1383 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1385 if (isRightToLeft(bv)) {
1386 if (tabular->IsFirstCellInRow(actcell)) {
1387 int row = tabular->row_of_cell(actcell);
1388 if (row == tabular->rows() - 1)
1390 actcell = tabular->GetLastCellInRow(row);
1391 actcell = tabular->GetCellBelow(actcell);
1398 if (tabular->IsLastCell(actcell))
1403 bool rtl = tabular->GetCellInset(actcell)->par->
1404 isRightToLeftPar(bv->buffer()->params);
1405 ActivateCellInset(bv, 0, 0, 0, !rtl);
1412 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1414 if (isRightToLeft(bv)) {
1415 if (tabular->IsLastCellInRow(actcell)) {
1416 int row = tabular->row_of_cell(actcell);
1419 actcell = tabular->GetFirstCellInRow(row);
1420 actcell = tabular->GetCellAbove(actcell);
1422 if (tabular->IsLastCell(actcell))
1427 if (!actcell) // first cell
1432 bool rtl = tabular->GetCellInset(actcell)->par->
1433 isRightToLeftPar(bv->buffer()->params);
1434 ActivateCellInset(bv, 0, 0, 0, !rtl);
1441 bool InsetTabular::Delete()
1447 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1449 if (the_locking_inset)
1450 the_locking_inset->SetFont(bv, font, tall);
1454 bool InsetTabular::TabularFeatures(BufferView * bv, string const & what)
1456 LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1459 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1460 string const tmp = tabularFeatures[i].feature;
1462 if (tmp == what.substr(0, tmp.length())) {
1463 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1464 //tabularFeatures[i].feature.length())) {
1465 action = tabularFeatures[i].action;
1469 if (action == LyXTabular::LAST_ACTION)
1473 frontStrip(what.substr(tabularFeatures[i].feature.length()));
1474 TabularFeatures(bv, action, val);
1479 void InsetTabular::TabularFeatures(BufferView * bv,
1480 LyXTabular::Feature feature,
1481 string const & value)
1490 LyXAlignment setAlign = LYX_ALIGN_LEFT;
1491 LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1496 case LyXTabular::M_ALIGN_LEFT:
1497 case LyXTabular::ALIGN_LEFT:
1498 setAlign=LYX_ALIGN_LEFT;
1500 case LyXTabular::M_ALIGN_RIGHT:
1501 case LyXTabular::ALIGN_RIGHT:
1502 setAlign=LYX_ALIGN_RIGHT;
1504 case LyXTabular::M_ALIGN_CENTER:
1505 case LyXTabular::ALIGN_CENTER:
1506 setAlign=LYX_ALIGN_CENTER;
1508 case LyXTabular::M_VALIGN_TOP:
1509 case LyXTabular::VALIGN_TOP:
1510 setVAlign=LyXTabular::LYX_VALIGN_TOP;
1512 case LyXTabular::M_VALIGN_BOTTOM:
1513 case LyXTabular::VALIGN_BOTTOM:
1514 setVAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1516 case LyXTabular::M_VALIGN_CENTER:
1517 case LyXTabular::VALIGN_CENTER:
1518 setVAlign=LyXTabular::LYX_VALIGN_CENTER;
1523 if (hasSelection()) {
1524 sel_col_start = tabular->column_of_cell(sel_cell_start);
1525 sel_col_end = tabular->column_of_cell(sel_cell_end);
1526 if (sel_col_start > sel_col_end) {
1527 sel_col_end = sel_col_start;
1528 sel_col_start = tabular->column_of_cell(sel_cell_end);
1530 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1533 sel_row_start = tabular->row_of_cell(sel_cell_start);
1534 sel_row_end = tabular->row_of_cell(sel_cell_end);
1535 if (sel_row_start > sel_row_end) {
1536 //int tmp = sel_row_start;
1537 //sel_row_start = sel_row_end;
1538 //sel_row_end = tmp;
1539 swap(sel_row_start, sel_row_end);
1542 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1543 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1545 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1547 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1548 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1550 bv->text->cursor.par()->previous,
1551 bv->text->cursor.par()->next
1555 int row = tabular->row_of_cell(actcell);
1556 int column = tabular->column_of_cell(actcell);
1560 case LyXTabular::SET_PWIDTH:
1562 bool const update = (tabular->GetColumnPWidth(actcell) != value);
1563 tabular->SetColumnPWidth(actcell,value);
1565 for (int i=0; i < tabular->rows(); ++i) {
1566 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1569 UpdateLocal(bv, INIT, true);
1573 case LyXTabular::SET_MPWIDTH:
1575 bool const update = (tabular->GetPWidth(actcell) != value);
1576 tabular->SetMColumnPWidth(actcell,value);
1578 for (int i=0; i < tabular->rows(); ++i) {
1579 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1582 UpdateLocal(bv, INIT, true);
1586 case LyXTabular::SET_SPECIAL_COLUMN:
1587 case LyXTabular::SET_SPECIAL_MULTI:
1588 tabular->SetAlignSpecial(actcell,value,feature);
1590 case LyXTabular::APPEND_ROW:
1591 // append the row into the tabular
1592 UnlockInsetInInset(bv, the_locking_inset);
1593 tabular->AppendRow(actcell);
1594 UpdateLocal(bv, INIT, true);
1596 case LyXTabular::APPEND_COLUMN:
1597 // append the column into the tabular
1598 UnlockInsetInInset(bv, the_locking_inset);
1599 tabular->AppendColumn(actcell);
1600 actcell = tabular->GetCellNumber(row, column);
1601 UpdateLocal(bv, INIT, true);
1603 case LyXTabular::DELETE_ROW:
1604 UnlockInsetInInset(bv, the_locking_inset);
1605 tabular->DeleteRow(tabular->row_of_cell(actcell));
1606 if ((row+1) > tabular->rows())
1608 actcell = tabular->GetCellNumber(row, column);
1610 UpdateLocal(bv, INIT, true);
1612 case LyXTabular::DELETE_COLUMN:
1613 UnlockInsetInInset(bv, the_locking_inset);
1614 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1615 if ((column+1) > tabular->columns())
1617 actcell = tabular->GetCellNumber(row, column);
1619 UpdateLocal(bv, INIT, true);
1621 case LyXTabular::M_TOGGLE_LINE_TOP:
1623 case LyXTabular::TOGGLE_LINE_TOP:
1624 lineSet = !tabular->TopLine(actcell, flag);
1625 for (i=sel_row_start; i<=sel_row_end; ++i)
1626 for (j=sel_col_start; j<=sel_col_end; ++j)
1627 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1628 UpdateLocal(bv, INIT, true);
1631 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1633 case LyXTabular::TOGGLE_LINE_BOTTOM:
1634 lineSet = !tabular->BottomLine(actcell, flag);
1635 for (i=sel_row_start; i<=sel_row_end; ++i)
1636 for (j=sel_col_start; j<=sel_col_end; ++j)
1637 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1639 UpdateLocal(bv, INIT, true);
1642 case LyXTabular::M_TOGGLE_LINE_LEFT:
1644 case LyXTabular::TOGGLE_LINE_LEFT:
1645 lineSet = !tabular->LeftLine(actcell, flag);
1646 for (i=sel_row_start; i<=sel_row_end; ++i)
1647 for (j=sel_col_start; j<=sel_col_end; ++j)
1648 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1650 UpdateLocal(bv, INIT, true);
1653 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1655 case LyXTabular::TOGGLE_LINE_RIGHT:
1656 lineSet = !tabular->RightLine(actcell, flag);
1657 for (i=sel_row_start; i<=sel_row_end; ++i)
1658 for (j=sel_col_start; j<=sel_col_end; ++j)
1659 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1661 UpdateLocal(bv, INIT, true);
1663 case LyXTabular::M_ALIGN_LEFT:
1664 case LyXTabular::M_ALIGN_RIGHT:
1665 case LyXTabular::M_ALIGN_CENTER:
1667 case LyXTabular::ALIGN_LEFT:
1668 case LyXTabular::ALIGN_RIGHT:
1669 case LyXTabular::ALIGN_CENTER:
1670 for (i = sel_row_start; i <= sel_row_end; ++i)
1671 for (j = sel_col_start; j <= sel_col_end; ++j)
1672 tabular->SetAlignment(tabular->GetCellNumber(i, j), setAlign,
1675 UpdateLocal(bv, INIT, true);
1677 UpdateLocal(bv, CELL, true);
1679 case LyXTabular::M_VALIGN_TOP:
1680 case LyXTabular::M_VALIGN_BOTTOM:
1681 case LyXTabular::M_VALIGN_CENTER:
1683 case LyXTabular::VALIGN_TOP:
1684 case LyXTabular::VALIGN_BOTTOM:
1685 case LyXTabular::VALIGN_CENTER:
1686 for (i = sel_row_start; i <= sel_row_end; ++i)
1687 for (j = sel_col_start; j <= sel_col_end; ++j)
1688 tabular->SetVAlignment(tabular->GetCellNumber(i, j),
1691 UpdateLocal(bv, INIT, true);
1693 UpdateLocal(bv, CELL, true);
1695 case LyXTabular::MULTICOLUMN:
1697 if (sel_row_start != sel_row_end) {
1698 WriteAlert(_("Impossible Operation!"),
1699 _("Multicolumns can only be horizontally."),
1703 // just multicol for one Single Cell
1704 if (!hasSelection()) {
1705 // check wether we are completly in a multicol
1706 if (tabular->IsMultiColumn(actcell)) {
1707 tabular->UnsetMultiColumn(actcell);
1708 UpdateLocal(bv, INIT, true);
1710 tabular->SetMultiColumn(actcell, 1);
1711 UpdateLocal(bv, CELL, true);
1715 // we have a selection so this means we just add all this
1716 // cells to form a multicolumn cell
1720 if (sel_cell_start > sel_cell_end) {
1721 s_start = sel_cell_end;
1722 s_end = sel_cell_start;
1724 s_start = sel_cell_start;
1725 s_end = sel_cell_end;
1727 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1730 sel_cell_end = sel_cell_start;
1731 sel_pos_end = sel_pos_start;
1732 UpdateLocal(bv, INIT, true);
1735 case LyXTabular::SET_ALL_LINES:
1737 case LyXTabular::UNSET_ALL_LINES:
1738 for (i=sel_row_start; i<=sel_row_end; ++i)
1739 for (j=sel_col_start; j<=sel_col_end; ++j)
1740 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1741 UpdateLocal(bv, INIT, true);
1743 case LyXTabular::SET_LONGTABULAR:
1744 tabular->SetLongTabular(true);
1745 UpdateLocal(bv, INIT, true); // because this toggles displayed
1747 case LyXTabular::UNSET_LONGTABULAR:
1748 tabular->SetLongTabular(false);
1749 UpdateLocal(bv, INIT, true); // because this toggles displayed
1751 case LyXTabular::SET_ROTATE_TABULAR:
1752 tabular->SetRotateTabular(true);
1754 case LyXTabular::UNSET_ROTATE_TABULAR:
1755 tabular->SetRotateTabular(false);
1757 case LyXTabular::SET_ROTATE_CELL:
1758 for (i=sel_row_start; i<=sel_row_end; ++i)
1759 for (j=sel_col_start; j<=sel_col_end; ++j)
1760 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1762 case LyXTabular::UNSET_ROTATE_CELL:
1763 for (i = sel_row_start; i <= sel_row_end; ++i)
1764 for (j = sel_col_start; j <= sel_col_end; ++j)
1765 tabular->SetRotateCell(tabular->GetCellNumber(i, j), false);
1767 case LyXTabular::SET_USEBOX:
1769 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
1770 if (val == tabular->GetUsebox(actcell))
1771 val = LyXTabular::BOX_NONE;
1772 for (i = sel_row_start; i <= sel_row_end; ++i)
1773 for (j = sel_col_start; j <= sel_col_end; ++j)
1774 tabular->SetUsebox(tabular->GetCellNumber(i, j), val);
1777 case LyXTabular::SET_LTFIRSTHEAD:
1778 tabular->SetLTHead(actcell, true);
1780 case LyXTabular::SET_LTHEAD:
1781 tabular->SetLTHead(actcell, false);
1783 case LyXTabular::SET_LTFOOT:
1784 tabular->SetLTFoot(actcell, false);
1786 case LyXTabular::SET_LTLASTFOOT:
1787 tabular->SetLTFoot(actcell, true);
1789 case LyXTabular::SET_LTNEWPAGE:
1790 what = !tabular->GetLTNewPage(actcell);
1791 tabular->SetLTNewPage(actcell, what);
1793 // dummy stuff just to avoid warnings
1794 case LyXTabular::LAST_ACTION:
1800 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1803 // the cursor.pos has to be before the inset so if it isn't now just
1804 // reset the curor pos first!
1805 if (!cellstart(cursor.pos())) {
1809 UpdatableInset * inset =
1810 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1811 LyXFont font(LyXFont::ALL_SANE);
1813 x = inset->x() + inset->width(bv, font);
1814 y = inset->descent(bv, font);
1816 //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1817 //inset_y = cursor.y();
1818 inset->Edit(bv, x, y, button);
1819 if (!the_locking_inset)
1821 UpdateLocal(bv, CELL, false);
1822 return (the_locking_inset != 0);
1826 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1828 InsetText * inset = tabular->GetCellInset(actcell);
1829 int const x1 = x + top_x;
1831 if (!cellstart(cursor.pos())) {
1832 return ((x + top_x) < cursor.x() &&
1833 (x + top_x) > (cursor.x() - inset->width(bv,
1834 LyXFont(LyXFont::ALL_SANE))));
1836 int const x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1838 x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE))));
1843 // This returns paperWidth() if the cell-width is unlimited or the width
1844 // in pixels if we have a pwidth for this cell.
1845 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1847 string const s = tabular->GetPWidth(cell);
1851 return VSpace(s).inPixels(0, 0);
1855 int InsetTabular::getMaxWidth(Painter & pain,
1856 UpdatableInset const * inset) const
1858 int const n = tabular->GetNumberOfCells();
1860 for (; cell < n; ++cell) {
1861 if (tabular->GetCellInset(cell) == inset)
1866 int w = GetMaxWidthOfCell(pain, cell);
1868 // because the inset then subtracts it's top_x and owner->x()
1869 w += (inset->x() - top_x);
1874 void InsetTabular::resizeLyXText(BufferView *) const
1880 LyXText * InsetTabular::getLyXText(BufferView const * bv) const
1882 if (the_locking_inset)
1883 return the_locking_inset->getLyXText(bv);
1884 return Inset::getLyXText(bv);
1888 void InsetTabular::OpenLayoutDialog(BufferView * bv) const
1890 if (the_locking_inset) {
1891 InsetTabular * i = static_cast<InsetTabular *>
1892 (the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE));
1894 i->OpenLayoutDialog(bv);
1898 bv->owner()->getDialogs()->showTabular(const_cast<InsetTabular *>(this));
1902 // functions returns:
1906 // 3 ... toggled off
1908 LyXFunc::func_status InsetTabular::getStatus(string const & what) const
1910 int action = LyXTabular::LAST_ACTION;
1911 LyXFunc::func_status status = LyXFunc::OK;
1914 for (; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1915 string const tmp = tabularFeatures[i].feature;
1916 if (tmp == what.substr(0, tmp.length())) {
1917 //if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1918 // tabularFeatures[i].feature.length())) {
1919 action = tabularFeatures[i].action;
1923 if (action == LyXTabular::LAST_ACTION)
1924 return LyXFunc::Unknown;
1926 string const argument = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1928 int sel_row_start, sel_row_end;
1932 if (hasSelection()) {
1933 sel_row_start = tabular->row_of_cell(sel_cell_start);
1934 sel_row_end = tabular->row_of_cell(sel_cell_end);
1935 if (sel_row_start > sel_row_end) {
1936 //int tmp = sel_row_start;
1937 //sel_row_start = sel_row_end;
1938 //sel_row_end = tmp;
1939 swap(sel_row_start, sel_row_end);
1942 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1946 case LyXTabular::SET_PWIDTH:
1947 case LyXTabular::SET_MPWIDTH:
1948 case LyXTabular::SET_SPECIAL_COLUMN:
1949 case LyXTabular::SET_SPECIAL_MULTI:
1950 status |= LyXFunc::Disabled;
1953 case LyXTabular::APPEND_ROW:
1954 case LyXTabular::APPEND_COLUMN:
1955 case LyXTabular::DELETE_ROW:
1956 case LyXTabular::DELETE_COLUMN:
1957 case LyXTabular::SET_ALL_LINES:
1958 case LyXTabular::UNSET_ALL_LINES:
1959 status |= LyXFunc::OK;
1962 case LyXTabular::MULTICOLUMN:
1963 if (tabular->IsMultiColumn(actcell))
1964 status |= LyXFunc::ToggleOn;
1966 status |= LyXFunc::ToggleOff;
1968 case LyXTabular::M_TOGGLE_LINE_TOP:
1970 case LyXTabular::TOGGLE_LINE_TOP:
1971 if (tabular->TopLine(actcell, flag))
1972 status |= LyXFunc::ToggleOn;
1974 status |= LyXFunc::ToggleOff;
1976 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1978 case LyXTabular::TOGGLE_LINE_BOTTOM:
1979 if (tabular->BottomLine(actcell, flag))
1980 status |= LyXFunc::ToggleOn;
1982 status |= LyXFunc::ToggleOff;
1984 case LyXTabular::M_TOGGLE_LINE_LEFT:
1986 case LyXTabular::TOGGLE_LINE_LEFT:
1987 if (tabular->LeftLine(actcell, flag))
1988 status |= LyXFunc::ToggleOn;
1990 status |= LyXFunc::ToggleOff;
1992 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1994 case LyXTabular::TOGGLE_LINE_RIGHT:
1995 if (tabular->RightLine(actcell, flag))
1996 status |= LyXFunc::ToggleOn;
1998 status |= LyXFunc::ToggleOff;
2000 case LyXTabular::M_ALIGN_LEFT:
2002 case LyXTabular::ALIGN_LEFT:
2003 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT)
2004 status |= LyXFunc::ToggleOn;
2006 status |= LyXFunc::ToggleOff;
2008 case LyXTabular::M_ALIGN_RIGHT:
2010 case LyXTabular::ALIGN_RIGHT:
2011 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT)
2012 status |= LyXFunc::ToggleOn;
2014 status |= LyXFunc::ToggleOff;
2016 case LyXTabular::M_ALIGN_CENTER:
2018 case LyXTabular::ALIGN_CENTER:
2019 if (tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER)
2020 status |= LyXFunc::ToggleOn;
2022 status |= LyXFunc::ToggleOff;
2024 case LyXTabular::M_VALIGN_TOP:
2026 case LyXTabular::VALIGN_TOP:
2027 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP)
2028 status |= LyXFunc::ToggleOn;
2030 status |= LyXFunc::ToggleOff;
2032 case LyXTabular::M_VALIGN_BOTTOM:
2034 case LyXTabular::VALIGN_BOTTOM:
2035 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM)
2036 status |= LyXFunc::ToggleOn;
2038 status |= LyXFunc::ToggleOff;
2040 case LyXTabular::M_VALIGN_CENTER:
2042 case LyXTabular::VALIGN_CENTER:
2043 if (tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER)
2044 status |= LyXFunc::ToggleOn;
2046 status |= LyXFunc::ToggleOff;
2048 case LyXTabular::SET_LONGTABULAR:
2049 if (tabular->IsLongTabular())
2050 status |= LyXFunc::ToggleOn;
2052 status |= LyXFunc::ToggleOff;
2054 case LyXTabular::UNSET_LONGTABULAR:
2055 if (!tabular->IsLongTabular())
2056 status |= LyXFunc::ToggleOn;
2058 status |= LyXFunc::ToggleOff;
2060 case LyXTabular::SET_ROTATE_TABULAR:
2061 if (tabular->GetRotateTabular())
2062 status |= LyXFunc::ToggleOn;
2064 status |= LyXFunc::ToggleOff;
2066 case LyXTabular::UNSET_ROTATE_TABULAR:
2067 if (!tabular->GetRotateTabular())
2068 status |= LyXFunc::ToggleOn;
2070 status |= LyXFunc::ToggleOff;
2072 case LyXTabular::SET_ROTATE_CELL:
2073 if (tabular->GetRotateCell(actcell))
2074 status |= LyXFunc::ToggleOn;
2076 status |= LyXFunc::ToggleOff;
2078 case LyXTabular::UNSET_ROTATE_CELL:
2079 if (!tabular->GetRotateCell(actcell))
2080 status |= LyXFunc::ToggleOn;
2082 status |= LyXFunc::ToggleOff;
2084 case LyXTabular::SET_USEBOX:
2085 if (strToInt(argument) == tabular->GetUsebox(actcell))
2086 status |= LyXFunc::ToggleOn;
2088 status |= LyXFunc::ToggleOff;
2090 case LyXTabular::SET_LTFIRSTHEAD:
2091 if (tabular->GetRowOfLTHead(actcell, dummy))
2092 status |= LyXFunc::ToggleOn;
2094 status |= LyXFunc::ToggleOff;
2096 case LyXTabular::SET_LTHEAD:
2097 if (tabular->GetRowOfLTHead(actcell, dummy))
2098 status |= LyXFunc::ToggleOn;
2100 status |= LyXFunc::ToggleOff;
2102 case LyXTabular::SET_LTFOOT:
2103 if (tabular->GetRowOfLTFoot(actcell, dummy))
2104 status |= LyXFunc::ToggleOn;
2106 status |= LyXFunc::ToggleOff;
2108 case LyXTabular::SET_LTLASTFOOT:
2109 if (tabular->GetRowOfLTFoot(actcell, dummy))
2110 status |= LyXFunc::ToggleOn;
2112 status |= LyXFunc::ToggleOff;
2114 case LyXTabular::SET_LTNEWPAGE:
2115 if (tabular->GetLTNewPage(actcell))
2116 status |= LyXFunc::ToggleOn;
2118 status |= LyXFunc::ToggleOff;
2121 status = LyXFunc::Disabled;
2128 bool InsetTabular::copySelection(BufferView * bv)
2130 if (!hasSelection())
2132 delete paste_tabular;
2134 int sel_col_start, sel_col_end;
2135 int sel_row_start, sel_row_end;
2137 sel_col_start = tabular->column_of_cell(sel_cell_start);
2138 sel_col_end = tabular->column_of_cell(sel_cell_end);
2139 if (sel_col_start > sel_col_end) {
2140 sel_col_start = sel_col_end;
2141 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2143 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2145 int columns = sel_col_end - sel_col_start + 1;
2147 sel_row_start = tabular->row_of_cell(sel_cell_start);
2148 sel_row_end = tabular->row_of_cell(sel_cell_end);
2149 if (sel_row_start > sel_row_end) {
2150 //int tmp tmp = sel_row_start;
2151 //sel_row_start = sel_row_end;
2152 //sel_row_end = tmp;
2153 swap(sel_row_start, sel_row_end);
2155 int rows = sel_row_end - sel_row_start + 1;
2157 paste_tabular = new LyXTabular(this, *tabular); // rows, columns);
2159 for (i=0; i < sel_row_start; ++i)
2160 paste_tabular->DeleteRow(0);
2161 while(paste_tabular->rows() > rows)
2162 paste_tabular->DeleteRow(rows);
2163 paste_tabular->SetTopLine(0, true, true);
2164 paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows-1),
2166 for (i=0; i < sel_col_start; ++i)
2167 paste_tabular->DeleteColumn(0);
2168 while(paste_tabular->columns() > columns)
2169 paste_tabular->DeleteColumn(columns);
2170 paste_tabular->SetLeftLine(0, true, true);
2171 paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),true, true);
2174 paste_tabular->Ascii(bv->buffer(), sstr);
2175 bv->stuffClipboard(sstr.str().c_str());
2180 bool InsetTabular::pasteSelection(BufferView * bv)
2185 for (int r1 = 0, r2 = actrow;
2186 (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2189 for(int c1 = 0, c2 = actcol;
2190 (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2193 if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2194 tabular->IsPartOfMultiColumn(r2,c2))
2196 if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2200 if (tabular->IsPartOfMultiColumn(r2,c2)) {
2204 int n1 = paste_tabular->GetCellNumber(r1, c1);
2205 int n2 = tabular->GetCellNumber(r2, c2);
2206 *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2207 tabular->GetCellInset(n2)->setOwner(this);
2208 tabular->GetCellInset(n2)->deleteLyXText(bv);
2215 bool InsetTabular::cutSelection()
2217 if (!hasSelection())
2220 int sel_col_start, sel_col_end;
2221 int sel_row_start, sel_row_end;
2223 sel_col_start = tabular->column_of_cell(sel_cell_start);
2224 sel_col_end = tabular->column_of_cell(sel_cell_end);
2225 if (sel_col_start > sel_col_end) {
2226 sel_col_start = sel_col_end;
2227 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2229 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2231 sel_row_start = tabular->row_of_cell(sel_cell_start);
2232 sel_row_end = tabular->row_of_cell(sel_cell_end);
2233 if (sel_row_start > sel_row_end) {
2234 //int tmp = sel_row_start;
2235 //sel_row_start = sel_row_end;
2236 //sel_row_end = tmp;
2237 swap(sel_row_start, sel_row_end);
2239 if (sel_cell_start > sel_cell_end) {
2240 //int tmp = sel_cell_start;
2241 //sel_cell_start = sel_cell_end;
2242 //sel_cell_end = tmp;
2243 swap(sel_cell_start, sel_cell_end);
2245 for (int i = sel_row_start; i <= sel_row_end; ++i) {
2246 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2247 tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear();
2253 bool InsetTabular::isRightToLeft(BufferView *bv )
2255 return bv->getParentLanguage(this)->RightToLeft();