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"
38 const int ADD_TO_HEIGHT = 2;
39 const int ADD_TO_TABULAR_WIDTH = 2;
48 struct tabular_features {
53 //static tabular_features * tabularFeatures = 0;
55 static tabular_features tabularFeatures[] =
57 { LyXTabular::APPEND_ROW, "append-row" },
58 { LyXTabular::APPEND_COLUMN, "append-column" },
59 { LyXTabular::DELETE_ROW, "delete-row" },
60 { LyXTabular::DELETE_COLUMN, "delete-column" },
61 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
62 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
63 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
64 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
65 { LyXTabular::ALIGN_LEFT, "align-left" },
66 { LyXTabular::ALIGN_RIGHT, "align-right" },
67 { LyXTabular::ALIGN_CENTER, "align-center" },
68 { LyXTabular::VALIGN_TOP, "valign-top" },
69 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
70 { LyXTabular::VALIGN_CENTER, "valign-center" },
71 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
72 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
73 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
74 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
75 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
76 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
77 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
78 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
79 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
80 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
81 { LyXTabular::DELETE_TABULAR, "delete-tabular" },
82 { LyXTabular::MULTICOLUMN, "multicolumn" },
83 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
84 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
85 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
86 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
87 { LyXTabular::SET_PWIDTH, "set-pwidth" },
88 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
89 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
90 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
91 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
92 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
93 { LyXTabular::SET_LINEBREAKS, "set-linebreaks" },
94 { LyXTabular::SET_LTHEAD, "set-lthead" },
95 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
96 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
97 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
98 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
99 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
100 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
101 { LyXTabular::LAST_ACTION, "" }
104 //#define cellstart(p) ((p % 2) == 0)
106 bool cellstart(LyXParagraph::size_type p)
108 return ((p % 2) == 0);
112 InsetTabular::InsetTabular(Buffer * buf, int rows, int columns)
119 tabular = new LyXTabular(this, rows,columns);
120 // for now make it always display as display() inset
122 the_locking_inset = 0;
123 locked = no_selection = cursor_visible = false;
128 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
135 InsetTabular::InsetTabular(InsetTabular const & tab, Buffer * buf)
138 tabular = new LyXTabular(this, *(tab.tabular));
139 the_locking_inset = 0;
140 locked = no_selection = cursor_visible = false;
145 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
151 void InsetTabular::initFeatures()
156 tabular_features tf[] = {
157 { LyXTabular::APPEND_ROW, "append-row" },
158 { LyXTabular::APPEND_COLUMN, "append-column" },
159 { LyXTabular::DELETE_ROW, "delete-row" },
160 { LyXTabular::DELETE_COLUMN, "delete-column" },
161 { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
162 { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
163 { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
164 { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
165 { LyXTabular::ALIGN_LEFT, "align-left" },
166 { LyXTabular::ALIGN_RIGHT, "align-right" },
167 { LyXTabular::ALIGN_CENTER, "align-center" },
168 { LyXTabular::VALIGN_TOP, "valign-top" },
169 { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
170 { LyXTabular::VALIGN_CENTER, "valign-center" },
171 { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
172 { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
173 { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
174 { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
175 { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
176 { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
177 { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
178 { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
179 { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
180 { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
181 { LyXTabular::DELETE_TABULAR, "delete-tabular" },
182 { LyXTabular::MULTICOLUMN, "multicolumn" },
183 { LyXTabular::SET_ALL_LINES, "set-all-lines" },
184 { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
185 { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
186 { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
187 { LyXTabular::SET_PWIDTH, "set-pwidth" },
188 { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
189 { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
190 { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
191 { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
192 { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
193 { LyXTabular::SET_LINEBREAKS, "set-linebreaks" },
194 { LyXTabular::SET_LTHEAD, "set-lthead" },
195 { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
196 { LyXTabular::SET_LTFOOT, "set-ltfoot" },
197 { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
198 { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
199 { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
200 { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
201 { LyXTabular::LAST_ACTION, "" }
206 InsetTabular::~InsetTabular()
210 dialogs_->hideTabular(this);
214 Inset * InsetTabular::Clone() const
216 InsetTabular * t = new InsetTabular(*this, buffer);
218 t->tabular = tabular->Clone(t);
223 void InsetTabular::Write(Buffer const * buf, ostream & os) const
225 os << " Tabular" << endl;
226 tabular->Write(buf, os);
230 void InsetTabular::Read(Buffer const * buf, LyXLex & lex)
232 bool old_format = (lex.GetString() == "\\LyXTable");
237 tabular = new LyXTabular(buf, this, lex);
245 token = lex.GetString();
246 while (lex.IsOK() && (token != "\\end_inset")) {
248 token = lex.GetString();
250 if (token != "\\end_inset") {
251 lex.printError("Missing \\end_inset at this point. "
257 int InsetTabular::ascent(BufferView *, LyXFont const &) const
259 return tabular->GetAscentOfRow(0);
263 int InsetTabular::descent(BufferView *, LyXFont const &) const
265 return tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
269 int InsetTabular::width(BufferView *, LyXFont const &) const
271 return tabular->GetWidthOfTabular() + (2 * ADD_TO_TABULAR_WIDTH);
275 void InsetTabular::draw(BufferView * bv, LyXFont const & font, int baseline,
276 float & x, bool cleared) const
278 Painter & pain = bv->painter();
283 UpdatableInset::draw(bv,font,baseline,x,cleared);
284 if (!cleared && ((need_update == INIT) || (need_update == FULL) ||
285 (top_x != int(x)) || (top_baseline != baseline))) {
286 int h = ascent(bv, font) + descent(bv, font);
287 int tx = display()||!owner()? 0:top_x;
288 int w = tx? width(bv, font):pain.paperWidth();
289 int ty = baseline - ascent(bv, font);
293 if ((ty + h) > pain.paperHeight())
294 h = pain.paperHeight();
295 if ((top_x + w) > pain.paperWidth())
296 w = pain.paperWidth();
297 pain.fillRectangle(tx, ty, w, h);
302 top_baseline = baseline;
304 x += ADD_TO_TABULAR_WIDTH;
305 if (cleared || (need_update == FULL) || (need_update == CELL)) {
306 for(i=0;i<tabular->rows();++i) {
308 dodraw = ((baseline+tabular->GetDescentOfRow(i)) > 0) &&
309 (baseline-tabular->GetAscentOfRow(i)) < pain.paperHeight();
310 for(j=0;j<tabular->columns();++j) {
311 if (tabular->IsPartOfMultiColumn(i,j))
313 cx = nx + tabular->GetBeginningOfTextInCell(cell);
315 DrawCellSelection(pain, nx, baseline, i, j, cell);
316 if (dodraw && !cleared && locked && the_locking_inset) {
317 if (the_locking_inset == tabular->GetCellInset(cell))
318 tabular->GetCellInset(cell)->draw(bv, font,
322 tabular->GetCellInset(cell)->draw(bv, font, baseline, cx,
324 DrawCellLines(pain, nx, baseline, i, cell);
326 nx += tabular->GetWidthOfColumn(cell);
329 baseline += tabular->GetDescentOfRow(i) +
330 tabular->GetAscentOfRow(i+1)+
331 tabular->GetAdditionalHeight(cell+1);
334 x += width(bv, font);
339 void InsetTabular::DrawCellLines(Painter & pain, int x, int baseline,
340 int row, int cell) const
342 int x2 = x + tabular->GetWidthOfColumn(cell);
345 if (!tabular->TopAlreadyDrawed(cell)) {
346 on_off = !tabular->TopLine(cell);
347 pain.line(x, baseline - tabular->GetAscentOfRow(row),
348 x2, baseline - tabular->GetAscentOfRow(row),
349 on_off ? LColor::tabularonoffline:LColor::tabularline,
350 on_off ? Painter::line_onoffdash:Painter::line_solid);
352 on_off = !tabular->BottomLine(cell);
353 pain.line(x,baseline + tabular->GetDescentOfRow(row),
354 x2, baseline + tabular->GetDescentOfRow(row),
355 on_off ? LColor::tabularonoffline:LColor::tabularline,
356 on_off ? Painter::line_onoffdash:Painter::line_solid);
357 if (!tabular->LeftAlreadyDrawed(cell)) {
358 on_off = !tabular->LeftLine(cell);
359 pain.line(x, baseline - tabular->GetAscentOfRow(row),
360 x, baseline + tabular->GetDescentOfRow(row),
361 on_off ? LColor::tabularonoffline:LColor::tabularline,
362 on_off ? Painter::line_onoffdash:Painter::line_solid);
364 on_off = !tabular->RightLine(cell);
365 pain.line(x2 - tabular->GetAdditionalWidth(cell),
366 baseline - tabular->GetAscentOfRow(row),
367 x2 - tabular->GetAdditionalWidth(cell),
368 baseline + tabular->GetDescentOfRow(row),
369 on_off ? LColor::tabularonoffline:LColor::tabularline,
370 on_off ? Painter::line_onoffdash:Painter::line_solid);
374 void InsetTabular::DrawCellSelection(Painter & pain, int x, int baseline,
375 int row, int column, int cell) const
377 int cs = tabular->column_of_cell(sel_cell_start);
378 int ce = tabular->column_of_cell(sel_cell_end);
381 cs = tabular->column_of_cell(sel_cell_end);
383 ce = tabular->right_column_of_cell(sel_cell_end);
386 int rs = tabular->row_of_cell(sel_cell_start);
387 int re = tabular->row_of_cell(sel_cell_end);
388 if (rs > re) swap(rs, re);
390 if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
391 int w = tabular->GetWidthOfColumn(cell);
392 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row);
393 pain.fillRectangle(x, baseline - tabular->GetAscentOfRow(row),
394 w, h, LColor::selection);
399 void InsetTabular::update(BufferView * bv, LyXFont const & font, bool reinit)
403 calculate_dimensions_of_cells(bv, font, true);
405 owner()->update(bv, font, true);
408 if (the_locking_inset)
409 the_locking_inset->update(bv, font, reinit);
410 switch(need_update) {
414 if (calculate_dimensions_of_cells(bv, font, false))
426 char const * InsetTabular::EditMessage() const
428 return _("Opened Tabular Inset");
432 void InsetTabular::Edit(BufferView * bv, int x, int y, unsigned int button)
434 UpdatableInset::Edit(bv, x, y, button);
436 if (!bv->lockInset(this)) {
437 lyxerr[Debug::INSETS] << "InsetTabular::Cannot lock inset" << endl;
441 the_locking_inset = 0;
442 inset_pos = inset_x = inset_y = 0;
444 sel_pos_start = sel_pos_end = cursor.pos();
445 sel_cell_start = sel_cell_end = actcell;
446 bv->text->FinishUndo();
447 if (InsetHit(bv, x, y)) {
448 ActivateCellInset(bv, x, y, button);
450 UpdateLocal(bv, NONE, false);
451 // bv->getOwner()->getPopups().updateFormTabular();
455 void InsetTabular::InsetUnlock(BufferView * bv)
457 if (the_locking_inset) {
458 the_locking_inset->InsetUnlock(bv);
459 the_locking_inset = 0;
462 if (hasSelection()) {
463 sel_pos_start = sel_pos_end = 0;
464 sel_cell_start = sel_cell_end = 0;
465 UpdateLocal(bv, FULL, false);
467 no_selection = false;
473 void InsetTabular::UpdateLocal(BufferView * bv, UpdateCodes what,
477 bv->updateInset(this, mark_dirty);
483 bool InsetTabular::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
485 lyxerr[Debug::INSETS] << "InsetTabular::LockInsetInInset(" <<inset<< "): ";
489 if (inset == tabular->GetCellInset(actcell)) {
490 lyxerr[Debug::INSETS] << "OK" << endl;
491 the_locking_inset = tabular->GetCellInset(actcell);
493 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
494 inset_y = cursor.y();
495 inset_pos = cursor.pos();
497 } else if (the_locking_inset && (the_locking_inset == inset)) {
498 if (cursor.pos() == inset_pos) {
499 lyxerr[Debug::INSETS] << "OK" << endl;
501 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
502 inset_y = cursor.y();
504 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
506 } else if (the_locking_inset) {
507 lyxerr[Debug::INSETS] << "MAYBE" << endl;
508 return the_locking_inset->LockInsetInInset(bv, inset);
510 lyxerr[Debug::INSETS] << "NOT OK" << endl;
515 bool InsetTabular::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
518 if (!the_locking_inset)
520 if (the_locking_inset == inset) {
521 the_locking_inset->InsetUnlock(bv);
522 the_locking_inset = 0;
524 moveRight(bv, false);
525 UpdateLocal(bv, CELL, false);
528 if (the_locking_inset->UnlockInsetInInset(bv, inset, lr)) {
529 if ((inset->LyxCode() == TABULAR_CODE) &&
530 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))
532 dialogs_ = bv->owner()->getDialogs();
533 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
542 bool InsetTabular::UpdateInsetInInset(BufferView * bv, Inset * inset)
544 if (!the_locking_inset)
546 if (the_locking_inset != inset)
547 return the_locking_inset->UpdateInsetInInset(bv, inset);
548 UpdateLocal(bv, CELL, false);
553 int InsetTabular::InsetInInsetY()
555 if (!the_locking_inset)
558 return (inset_y + the_locking_inset->InsetInInsetY());
562 UpdatableInset * InsetTabular::GetLockingInset()
564 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
568 UpdatableInset * InsetTabular::GetFirstLockingInsetOfType(Inset::Code c)
572 if (the_locking_inset)
573 return the_locking_inset->GetFirstLockingInsetOfType(c);
578 bool InsetTabular::InsertInset(BufferView * bv, Inset * inset)
580 if (the_locking_inset)
581 return the_locking_inset->InsertInset(bv, inset);
586 void InsetTabular::InsetButtonPress(BufferView * bv, int x, int y, int button)
588 if (hasSelection()) {
589 sel_pos_start = sel_pos_end = sel_cell_start = sel_cell_end = 0;
590 UpdateLocal(bv, SELECTION, false);
592 no_selection = false;
597 sel_pos_start = sel_pos_end = cursor.pos();
598 sel_cell_start = sel_cell_end = actcell;
600 bool inset_hit = InsetHit(bv, x, y);
602 if ((ocell == actcell) && the_locking_inset && inset_hit) {
603 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
605 } else if (the_locking_inset) {
606 the_locking_inset->InsetUnlock(bv);
608 the_locking_inset = 0;
609 if (inset_hit && bv->the_locking_inset) {
610 ActivateCellInset(bv, x, y, button);
611 the_locking_inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
616 void InsetTabular::InsetButtonRelease(BufferView * bv,
617 int x, int y, int button)
620 if (the_locking_inset) {
622 if ((i=the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE))) {
623 i->InsetButtonRelease(bv, x, y, button);
627 dialogs_ = bv->owner()->getDialogs();
628 dialogs_->showTabular(this);
630 else if (ocell != actcell)
631 bview->getOwner()->getPopups().updateTabular();
635 if (the_locking_inset) {
636 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
639 no_selection = false;
643 void InsetTabular::InsetMotionNotify(BufferView * bv, int x, int y, int button)
645 if (the_locking_inset) {
646 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
647 y - inset_y, button);
651 // int ocell = actcell,
652 int old = sel_pos_end;
655 sel_pos_end = cursor.pos();
656 sel_cell_end = actcell;
657 if (old != sel_pos_end)
658 UpdateLocal(bv, SELECTION, false);
660 if (ocell != actcell)
661 bview->getOwner()->getPopups().updateFormTabular();
664 no_selection = false;
668 void InsetTabular::InsetKeyPress(XKeyEvent * xke)
670 if (the_locking_inset) {
671 the_locking_inset->InsetKeyPress(xke);
677 UpdatableInset::RESULT InsetTabular::LocalDispatch(BufferView * bv, int action,
680 UpdatableInset::RESULT
683 no_selection = false;
684 if (((result=UpdatableInset::LocalDispatch(bv, action, arg)) == DISPATCHED)
685 || (result == DISPATCHED_NOUPDATE)) {
691 if ((action < 0) && arg.empty())
694 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
695 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
697 if (the_locking_inset) {
698 result=the_locking_inset->LocalDispatch(bv, action, arg);
699 if (result == DISPATCHED_NOUPDATE)
701 else if (result == DISPATCHED) {
702 the_locking_inset->ToggleInsetCursor(bv);
703 UpdateLocal(bv, CELL, false);
704 the_locking_inset->ToggleInsetCursor(bv);
706 } else if (result == FINISHED) {
707 if ((action == LFUN_RIGHT) || (action == -1)) {
708 cursor.pos(inset_pos + 1);
711 sel_pos_start = sel_pos_end = cursor.pos();
712 sel_cell_start = sel_cell_end = actcell;
719 bool hs = hasSelection();
723 // Normal chars not handled here
726 // --- Cursor Movements ---------------------------------------------
728 if (tabular->IsLastCellInRow(actcell) && !cellstart(cursor.pos()))
730 moveRight(bv, false);
731 sel_pos_end = cursor.pos();
732 if (!cellstart(cursor.pos())) {
733 if (tabular->right_column_of_cell(sel_cell_start) >
734 tabular->right_column_of_cell(actcell))
735 sel_cell_end = actcell+1;
737 sel_cell_end = actcell;
739 UpdateLocal(bv, SELECTION, false);
742 result = moveRight(bv);
743 sel_pos_start = sel_pos_end = cursor.pos();
744 sel_cell_start = sel_cell_end = actcell;
746 UpdateLocal(bv, SELECTION, false);
749 if (tabular->IsFirstCellInRow(actcell) && cellstart(cursor.pos()))
752 sel_pos_end = cursor.pos();
753 if (cellstart(cursor.pos())) {
754 if (tabular->column_of_cell(sel_cell_start) >=
755 tabular->column_of_cell(actcell))
756 sel_cell_end = actcell;
758 sel_cell_end = actcell-1;
760 UpdateLocal(bv, SELECTION, false);
763 result = moveLeft(bv);
764 sel_pos_start = sel_pos_end = cursor.pos();
765 sel_cell_start = sel_cell_end = actcell;
767 UpdateLocal(bv, SELECTION, false);
773 sel_pos_end = cursor.pos();
774 if ((ocell == sel_cell_end) ||
775 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
776 sel_cell_end = tabular->GetCellBelow(sel_cell_end);
778 sel_cell_end = tabular->GetLastCellBelow(sel_cell_end);
779 UpdateLocal(bv, SELECTION, false);
783 result= moveDown(bv);
784 sel_pos_start = sel_pos_end = cursor.pos();
785 sel_cell_start = sel_cell_end = actcell;
787 UpdateLocal(bv, SELECTION, false);
793 sel_pos_end = cursor.pos();
794 if ((ocell == sel_cell_end) ||
795 (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
796 sel_cell_end = tabular->GetCellAbove(sel_cell_end);
798 sel_cell_end = tabular->GetLastCellAbove(sel_cell_end);
799 UpdateLocal(bv, SELECTION, false);
804 sel_pos_start = sel_pos_end = cursor.pos();
805 sel_cell_start = sel_cell_end = actcell;
807 UpdateLocal(bv, SELECTION, false);
819 if (the_locking_inset) {
820 UnlockInsetInInset(bv, the_locking_inset);
821 the_locking_inset = 0;
823 if (action == LFUN_TAB)
827 sel_pos_start = sel_pos_end = cursor.pos();
828 sel_cell_start = sel_cell_end = actcell;
830 UpdateLocal(bv, SELECTION, false);
832 case LFUN_LAYOUT_TABLE:
834 dialogs_ = bv->owner()->getDialogs();
835 dialogs_->showTabular(this);
838 case LFUN_TABULAR_FEATURE:
839 if (!TabularFeatures(bv, arg))
840 result = UNDISPATCHED;
844 result = UNDISPATCHED;
847 if (result!=FINISHED) {
848 if (!the_locking_inset) {
850 if (ocell != actcell)
851 bview->getOwner()->getPopups().updateFormTabular();
856 bv->unlockInset(this);
861 int InsetTabular::Latex(Buffer const * buf, ostream & os,
862 bool fragile, bool fp) const
864 return tabular->Latex(buf, os, fragile, fp);
868 int InsetTabular::Ascii(Buffer const *, ostream &) const
874 int InsetTabular::Linuxdoc(Buffer const *, ostream &) const
880 int InsetTabular::DocBook(Buffer const *, ostream &) const
886 void InsetTabular::Validate(LaTeXFeatures & features) const
888 tabular->Validate(features);
892 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv,
893 LyXFont const & font,
899 bool changed = false;
901 for(int i = 0; i < tabular->rows(); ++i) {
902 maxAsc = maxDesc = 0;
903 for(int j= 0; j < tabular->columns(); ++j) {
904 if (tabular->IsPartOfMultiColumn(i,j))
907 inset = tabular->GetCellInset(cell);
909 inset->update(bv, font, false);
910 maxAsc = max(maxAsc, inset->ascent(bv, font));
911 maxDesc = max(maxDesc, inset->descent(bv, font));
912 changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
914 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
915 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
921 void InsetTabular::GetCursorPos(BufferView *, int & x, int & y) const
923 x = cursor.x() - top_x;
928 void InsetTabular::ToggleInsetCursor(BufferView * bv)
930 if (the_locking_inset) {
931 the_locking_inset->ToggleInsetCursor(bv);
935 LyXFont font; // = the_locking_inset->GetFont(par, cursor.pos);
937 int asc = lyxfont::maxAscent(font);
938 int desc = lyxfont::maxDescent(font);
941 bv->hideLockedInsetCursor();
943 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
944 cursor_visible = !cursor_visible;
948 void InsetTabular::ShowInsetCursor(BufferView * bv)
950 if (!cursor_visible) {
951 LyXFont font; // = GetFont(par, cursor.pos);
953 int asc = lyxfont::maxAscent(font);
954 int desc = lyxfont::maxDescent(font);
955 bv->fitLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
956 bv->showLockedInsetCursor(cursor.x(), cursor.y(), asc, desc);
957 cursor_visible = true;
962 void InsetTabular::HideInsetCursor(BufferView * bv)
965 ToggleInsetCursor(bv);
969 void InsetTabular::setPos(BufferView * bv, int x, int y) const
974 actcell = actrow = actcol = 0;
975 int ly = tabular->GetDescentOfRow(actrow);
977 // first search the right row
978 while((ly < y) && (actrow < tabular->rows())) {
979 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
980 tabular->GetAscentOfRow(actrow+1) +
981 tabular->GetAdditionalHeight(tabular->GetCellNumber(actrow + 1,
984 ly = cursor.y() + tabular->GetDescentOfRow(actrow);
986 actcell = tabular->GetCellNumber(actrow, actcol);
988 // now search the right column
989 int lx = tabular->GetWidthOfColumn(actcell) -
990 tabular->GetAdditionalWidth(actcell);
991 for(; !tabular->IsLastCellInRow(actcell) && (lx < x);
992 ++actcell,lx += tabular->GetWidthOfColumn(actcell) +
993 tabular->GetAdditionalWidth(actcell - 1));
994 cursor.pos(((actcell+1) * 2) - 1);
996 if ((lx - (tabular->GetWidthOfColumn(actcell)/2)) < x) {
997 cursor.x(lx + top_x - 2);
999 cursor.pos(cursor.pos() - 1);
1000 cursor.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1006 int InsetTabular::getCellXPos(int cell) const
1010 for(c=cell;!tabular->IsFirstCellInRow(c);--c)
1012 int lx = tabular->GetWidthOfColumn(cell);
1013 for(; (c < cell); ++c) {
1014 lx += tabular->GetWidthOfColumn(c);
1016 return (lx - tabular->GetWidthOfColumn(cell) + top_x +
1017 ADD_TO_TABULAR_WIDTH);
1021 void InsetTabular::resetPos(BufferView * bv) const
1025 actcol = tabular->column_of_cell(actcell);
1030 for(; (cell<actcell) && !tabular->IsLastRow(cell); ++cell) {
1031 if (tabular->IsLastCellInRow(cell)) {
1032 cursor.y(cursor.y() + tabular->GetDescentOfRow(actrow) +
1033 tabular->GetAscentOfRow(actrow + 1) +
1034 tabular->GetAdditionalHeight(cell + 1));
1038 cursor.x(getCellXPos(actcell) + 2);
1039 if (cursor.pos() % 2) {
1040 LyXFont font(LyXFont::ALL_SANE);
1041 cursor.x(cursor.x() + tabular->GetCellInset(actcell)->width(bv,font) +
1042 tabular->GetBeginningOfTextInCell(actcell));
1044 if ((!the_locking_inset ||
1045 !the_locking_inset->GetFirstLockingInsetOfType(TABULAR_CODE)) &&
1046 (actcell != oldcell)) {
1047 dialogs_ = bv->owner()->getDialogs();
1048 dialogs_->updateTabular(const_cast<InsetTabular *>(this));
1054 UpdatableInset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1056 if (!cellstart(cursor.pos())) {
1057 if (tabular->IsLastCell(actcell))
1060 cursor.pos(cursor.pos() + 1);
1062 if (ActivateCellInset(bv))
1064 } else { // before the inset
1065 cursor.pos(cursor.pos() + 1);
1068 return DISPATCHED_NOUPDATE;
1072 UpdatableInset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1074 if (!cursor.pos()) {
1079 cursor.pos(cursor.pos() - 1);
1080 if (!cellstart(cursor.pos())) {
1082 } else if (lock) { // behind the inset
1083 if (ActivateCellInset(bv, 0, 0, 0, true))
1087 return DISPATCHED_NOUPDATE;
1091 UpdatableInset::RESULT InsetTabular::moveUp(BufferView * bv)
1093 int ocell = actcell;
1094 actcell = tabular->GetCellAbove(actcell);
1095 if (actcell == ocell) // we moved out of the inset
1098 return DISPATCHED_NOUPDATE;
1102 UpdatableInset::RESULT InsetTabular::moveDown(BufferView * bv)
1104 int ocell = actcell;
1105 actcell = tabular->GetCellBelow(actcell);
1106 if (actcell == ocell) // we moved out of the inset
1109 return DISPATCHED_NOUPDATE;
1113 bool InsetTabular::moveNextCell(BufferView * bv)
1115 if (tabular->IsLastCell(actcell))
1118 cursor.pos(cursor.pos() + 1);
1119 if (!cellstart(cursor.pos()))
1120 cursor.pos(cursor.pos() + 1);
1126 bool InsetTabular::movePrevCell(BufferView * bv)
1128 if (!actcell) // first cell
1131 cursor.pos(cursor.pos() - 1);
1132 if (cellstart(cursor.pos()))
1133 cursor.pos(cursor.pos() - 1);
1139 bool InsetTabular::Delete()
1145 void InsetTabular::SetFont(BufferView * bv, LyXFont const & font, bool tall)
1147 if (the_locking_inset)
1148 the_locking_inset->SetFont(bv, font, tall);
1152 bool InsetTabular::TabularFeatures(BufferView * bv, string what)
1154 int action = LyXTabular::LAST_ACTION;
1158 for(i=0; tabularFeatures[i].action != LyXTabular::LAST_ACTION; ++i) {
1159 if (!strncmp(tabularFeatures[i].feature.c_str(), what.c_str(),
1160 tabularFeatures[i].feature.length())) {
1161 action = tabularFeatures[i].action;
1165 if (action == LyXTabular::LAST_ACTION)
1168 val = frontStrip(what.substr(tabularFeatures[i].feature.length()));
1169 TabularFeatures(bv, action, val);
1174 void InsetTabular::TabularFeatures(BufferView * bv, int feature, string val)
1183 setAlign = LYX_ALIGN_LEFT,
1189 case LyXTabular::M_ALIGN_LEFT:
1190 case LyXTabular::ALIGN_LEFT:
1191 setAlign=LYX_ALIGN_LEFT;
1193 case LyXTabular::M_ALIGN_RIGHT:
1194 case LyXTabular::ALIGN_RIGHT:
1195 setAlign=LYX_ALIGN_RIGHT;
1197 case LyXTabular::M_ALIGN_CENTER:
1198 case LyXTabular::ALIGN_CENTER:
1199 setAlign=LYX_ALIGN_CENTER;
1201 case LyXTabular::M_VALIGN_TOP:
1202 case LyXTabular::VALIGN_TOP:
1203 setAlign=LyXTabular::LYX_VALIGN_TOP;
1205 case LyXTabular::M_VALIGN_BOTTOM:
1206 case LyXTabular::VALIGN_BOTTOM:
1207 setAlign=LyXTabular::LYX_VALIGN_BOTTOM;
1209 case LyXTabular::M_VALIGN_CENTER:
1210 case LyXTabular::VALIGN_CENTER:
1211 setAlign=LyXTabular::LYX_VALIGN_CENTER;
1216 if (hasSelection()) {
1218 sel_col_start = tabular->column_of_cell(sel_cell_start);
1219 sel_col_end = tabular->column_of_cell(sel_cell_end);
1220 if (sel_col_start > sel_col_end) {
1221 sel_col_end = sel_col_start;
1222 sel_col_start = tabular->column_of_cell(sel_cell_end);
1224 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
1227 sel_row_start = tabular->row_of_cell(sel_cell_start);
1228 sel_row_end = tabular->row_of_cell(sel_cell_end);
1229 if (sel_row_start > sel_row_end) {
1230 tmp = sel_row_start;
1231 sel_row_start = sel_row_end;
1235 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1236 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1238 bv->text->SetUndo(bv->buffer(), Undo::FINISH,
1240 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1241 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next
1243 bv->text->cursor.par()->previous,
1244 bv->text->cursor.par()->next
1248 int row = tabular->row_of_cell(actcell);
1249 int column = tabular->column_of_cell(actcell);
1253 case LyXTabular::SET_PWIDTH:
1255 bool update = (tabular->GetColumnPWidth(actcell) != val);
1256 tabular->SetColumnPWidth(actcell,val);
1258 for (int i=0; i < tabular->rows(); ++i) {
1259 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1262 UpdateLocal(bv, INIT, true);
1266 case LyXTabular::SET_MPWIDTH:
1268 bool update = (tabular->GetPWidth(actcell) != val);
1269 tabular->SetMColumnPWidth(actcell,val);
1271 for (int i=0; i < tabular->rows(); ++i) {
1272 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1275 UpdateLocal(bv, INIT, true);
1279 case LyXTabular::SET_SPECIAL_COLUMN:
1280 case LyXTabular::SET_SPECIAL_MULTI:
1281 tabular->SetAlignSpecial(actcell,val,feature);
1283 case LyXTabular::APPEND_ROW:
1284 // append the row into the tabular
1285 UnlockInsetInInset(bv, the_locking_inset);
1286 tabular->AppendRow(actcell);
1287 UpdateLocal(bv, INIT, true);
1289 case LyXTabular::APPEND_COLUMN:
1290 // append the column into the tabular
1291 tabular->AppendColumn(actcell);
1292 actcell = tabular->GetCellNumber(row, column);
1293 UpdateLocal(bv, INIT, true);
1295 case LyXTabular::DELETE_ROW:
1296 tabular->DeleteRow(tabular->row_of_cell(actcell));
1297 if ((row+1) > tabular->rows())
1299 actcell = tabular->GetCellNumber(row, column);
1300 UpdateLocal(bv, INIT, true);
1302 case LyXTabular::DELETE_COLUMN:
1303 tabular->DeleteColumn(tabular->column_of_cell(actcell));
1304 if ((column+1) > tabular->columns())
1306 actcell = tabular->GetCellNumber(row, column);
1307 UpdateLocal(bv, INIT, true);
1309 case LyXTabular::M_TOGGLE_LINE_TOP:
1311 case LyXTabular::TOGGLE_LINE_TOP:
1312 lineSet = !tabular->TopLine(actcell, flag);
1313 for(i=sel_row_start; i<=sel_row_end; ++i)
1314 for(j=sel_col_start; j<=sel_col_end; ++j)
1315 tabular->SetTopLine(tabular->GetCellNumber(i,j),lineSet, flag);
1316 UpdateLocal(bv, INIT, true);
1319 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1321 case LyXTabular::TOGGLE_LINE_BOTTOM:
1322 lineSet = !tabular->BottomLine(actcell, flag);
1323 for(i=sel_row_start; i<=sel_row_end; ++i)
1324 for(j=sel_col_start; j<=sel_col_end; ++j)
1325 tabular->SetBottomLine(tabular->GetCellNumber(i,j),lineSet,
1327 UpdateLocal(bv, INIT, true);
1330 case LyXTabular::M_TOGGLE_LINE_LEFT:
1332 case LyXTabular::TOGGLE_LINE_LEFT:
1333 lineSet = !tabular->LeftLine(actcell, flag);
1334 for(i=sel_row_start; i<=sel_row_end; ++i)
1335 for(j=sel_col_start; j<=sel_col_end; ++j)
1336 tabular->SetLeftLine(tabular->GetCellNumber(i,j),lineSet,
1338 UpdateLocal(bv, INIT, true);
1341 case LyXTabular::M_TOGGLE_LINE_RIGHT:
1343 case LyXTabular::TOGGLE_LINE_RIGHT:
1344 lineSet = !tabular->RightLine(actcell, flag);
1345 for(i=sel_row_start; i<=sel_row_end; ++i)
1346 for(j=sel_col_start; j<=sel_col_end; ++j)
1347 tabular->SetRightLine(tabular->GetCellNumber(i,j),lineSet,
1349 UpdateLocal(bv, INIT, true);
1351 case LyXTabular::M_ALIGN_LEFT:
1352 case LyXTabular::M_ALIGN_RIGHT:
1353 case LyXTabular::M_ALIGN_CENTER:
1355 case LyXTabular::ALIGN_LEFT:
1356 case LyXTabular::ALIGN_RIGHT:
1357 case LyXTabular::ALIGN_CENTER:
1358 for(i=sel_row_start; i<=sel_row_end; ++i)
1359 for(j=sel_col_start; j<=sel_col_end; ++j)
1360 tabular->SetAlignment(tabular->GetCellNumber(i,j),setAlign,
1363 UpdateLocal(bv, INIT, true);
1365 UpdateLocal(bv, CELL, true);
1367 case LyXTabular::M_VALIGN_TOP:
1368 case LyXTabular::M_VALIGN_BOTTOM:
1369 case LyXTabular::M_VALIGN_CENTER:
1371 case LyXTabular::VALIGN_TOP:
1372 case LyXTabular::VALIGN_BOTTOM:
1373 case LyXTabular::VALIGN_CENTER:
1374 for(i=sel_row_start; i<=sel_row_end; ++i)
1375 for(j=sel_col_start; j<=sel_col_end; ++j)
1376 tabular->SetVAlignment(tabular->GetCellNumber(i,j), setAlign,
1379 UpdateLocal(bv, INIT, true);
1381 UpdateLocal(bv, CELL, true);
1383 case LyXTabular::MULTICOLUMN:
1385 if (sel_row_start != sel_row_end) {
1386 WriteAlert(_("Impossible Operation!"),
1387 _("Multicolumns can only be horizontally."),
1391 // just multicol for one Single Cell
1392 if (!hasSelection()) {
1393 // check wether we are completly in a multicol
1394 if (tabular->IsMultiColumn(actcell)) {
1395 tabular->UnsetMultiColumn(actcell);
1396 UpdateLocal(bv, INIT, true);
1398 tabular->SetMultiColumn(actcell, 1);
1399 UpdateLocal(bv, CELL, true);
1403 // we have a selection so this means we just add all this
1404 // cells to form a multicolumn cell
1408 if (sel_cell_start > sel_cell_end) {
1409 s_start = sel_cell_end;
1410 s_end = sel_cell_start;
1412 s_start = sel_cell_start;
1413 s_end = sel_cell_end;
1415 tabular->SetMultiColumn(s_start, s_end - s_start + 1);
1418 sel_cell_end = sel_cell_start;
1419 sel_pos_end = sel_pos_start;
1420 UpdateLocal(bv, INIT, true);
1423 case LyXTabular::SET_ALL_LINES:
1425 case LyXTabular::UNSET_ALL_LINES:
1426 for(i=sel_row_start; i<=sel_row_end; ++i)
1427 for(j=sel_col_start; j<=sel_col_end; ++j)
1428 tabular->SetAllLines(tabular->GetCellNumber(i,j), setLines);
1429 UpdateLocal(bv, INIT, true);
1431 case LyXTabular::SET_LONGTABULAR:
1432 tabular->SetLongTabular(true);
1433 UpdateLocal(bv, INIT, true); // because this toggles displayed
1435 case LyXTabular::UNSET_LONGTABULAR:
1436 tabular->SetLongTabular(false);
1437 UpdateLocal(bv, INIT, true); // because this toggles displayed
1439 case LyXTabular::SET_ROTATE_TABULAR:
1440 tabular->SetRotateTabular(true);
1442 case LyXTabular::UNSET_ROTATE_TABULAR:
1443 tabular->SetRotateTabular(false);
1445 case LyXTabular::SET_ROTATE_CELL:
1446 for(i=sel_row_start; i<=sel_row_end; ++i)
1447 for(j=sel_col_start; j<=sel_col_end; ++j)
1448 tabular->SetRotateCell(tabular->GetCellNumber(i,j),true);
1450 case LyXTabular::UNSET_ROTATE_CELL:
1451 for(i=sel_row_start; i<=sel_row_end; ++i)
1452 for(j=sel_col_start; j<=sel_col_end; ++j)
1453 tabular->SetRotateCell(tabular->GetCellNumber(i,j),false);
1455 case LyXTabular::SET_LINEBREAKS:
1456 what = !tabular->GetLinebreaks(actcell);
1457 for(i=sel_row_start; i<=sel_row_end; ++i)
1458 for(j=sel_col_start; j<=sel_col_end; ++j)
1459 tabular->SetLinebreaks(tabular->GetCellNumber(i,j),what);
1461 case LyXTabular::SET_LTFIRSTHEAD:
1462 tabular->SetLTHead(actcell,true);
1464 case LyXTabular::SET_LTHEAD:
1465 tabular->SetLTHead(actcell,false);
1467 case LyXTabular::SET_LTFOOT:
1468 tabular->SetLTFoot(actcell,false);
1470 case LyXTabular::SET_LTLASTFOOT:
1471 tabular->SetLTFoot(actcell,true);
1473 case LyXTabular::SET_LTNEWPAGE:
1474 what = !tabular->GetLTNewPage(actcell);
1475 tabular->SetLTNewPage(actcell,what);
1481 bool InsetTabular::ActivateCellInset(BufferView * bv, int x, int y, int button,
1484 // the cursor.pos has to be before the inset so if it isn't now just
1485 // reset the curor pos first!
1486 if (cursor.pos() % 2) { // behind the inset
1487 cursor.pos(cursor.pos() - 1);
1490 UpdatableInset * inset =
1491 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
1492 LyXFont font(LyXFont::ALL_SANE);
1494 x = inset->x() + inset->width(bv, font);
1495 y = inset->descent(bv, font);
1497 inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1498 inset_y = cursor.y();
1499 inset->Edit(bv, x - inset_x, y - inset_y, button);
1500 if (!the_locking_inset)
1502 UpdateLocal(bv, CELL, false);
1507 bool InsetTabular::InsetHit(BufferView * bv, int x, int ) const
1509 InsetText * inset = tabular->GetCellInset(actcell);
1512 if (cursor.pos() % 2) { // behind the inset
1513 return (((x + top_x) < cursor.x()) &&
1514 ((x + top_x) > (cursor.x() - inset->width(bv,
1515 LyXFont(LyXFont::ALL_SANE)))));
1517 int x2 = cursor.x() + tabular->GetBeginningOfTextInCell(actcell);
1518 return ((x1 > x2) &&
1519 (x1 < (x2 + inset->width(bv, LyXFont(LyXFont::ALL_SANE)))));
1524 // This returns paperWidth() if the cell-width is unlimited or the width
1525 // in pixels if we have a pwidth for this cell.
1526 int InsetTabular::GetMaxWidthOfCell(Painter &, int cell) const
1528 string s = tabular->GetPWidth(cell);
1532 return VSpace(s).inPixels( 0, 0);
1536 int InsetTabular::getMaxWidth(Painter & pain,
1537 UpdatableInset const * inset) const
1540 int n = tabular->GetNumberOfCells();
1541 for(cell=0; cell < n; ++cell) {
1542 if (tabular->GetCellInset(cell) == inset)
1547 int w = GetMaxWidthOfCell(pain, cell);
1549 // because the inset then subtracts it's top_x and owner->x()
1550 w += (inset->x() - top_x);
1555 void InsetTabular::recomputeTextInsets(BufferView * bv,
1556 LyXFont const & font) const
1562 for(int j= 0; j < tabular->columns(); ++j) {
1563 for(int i = 0; i < tabular->rows(); ++i) {
1564 if (tabular->IsPartOfMultiColumn(i,j))
1566 cell = tabular->GetCellNumber(i,j);
1567 inset = tabular->GetCellInset(cell);
1568 inset->update(bv, font);
1569 tabular->SetWidthOfCell(cell, inset->width(bv, font));
1571 // cell = tabular->GetCellNumber(0, j);
1572 // cx += tabular->GetWidthOfColumn(cell);
1577 void InsetTabular::resizeLyXText(BufferView *) const
1582 LyXText * InsetTabular::getLyXText(BufferView * bv) const
1584 if (the_locking_inset)
1585 return the_locking_inset->getLyXText(bv);
1586 return Inset::getLyXText(bv);