]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
Clean up InsetGraphics::Cache and rename as GraphicsInset.
[lyx.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "insettabular.h"
14 #include "insettext.h"
15
16 #include "buffer.h"
17 #include "BufferView.h"
18 #include "lfuns.h"
19 #include "debug.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "language.h"
24 #include "LaTeXFeatures.h"
25 #include "Lsstream.h"
26 #include "lyx_cb.h"
27 #include "lyxfunc.h"
28 #include "lyxlength.h"
29 #include "lyxlex.h"
30 #include "lyxtext.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "WordLangTuple.h"
34 #include "metricsinfo.h"
35
36 #include "frontends/Alert.h"
37 #include "frontends/Dialogs.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/Painter.h"
41
42 #include "support/LAssert.h"
43 #include "support/lstrings.h"
44
45 #include <fstream>
46 #include <algorithm>
47 #include <cstdlib>
48 #include <map>
49 //#include <signal.h>
50
51
52 using std::vector;
53 using std::ostream;
54 using std::ifstream;
55 using std::max;
56 using std::endl;
57 using std::swap;
58 using std::max;
59
60 namespace {
61
62 int const ADD_TO_HEIGHT = 2;
63 int const ADD_TO_TABULAR_WIDTH = 2;
64
65 ///
66 LyXTabular * paste_tabular = 0;
67
68
69 struct TabularFeature {
70         LyXTabular::Feature action;
71         string feature;
72 };
73
74
75 TabularFeature tabularFeature[] =
76 {
77         { LyXTabular::APPEND_ROW, "append-row" },
78         { LyXTabular::APPEND_COLUMN, "append-column" },
79         { LyXTabular::DELETE_ROW, "delete-row" },
80         { LyXTabular::DELETE_COLUMN, "delete-column" },
81         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
82         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
83         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
84         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
85         { LyXTabular::ALIGN_LEFT, "align-left" },
86         { LyXTabular::ALIGN_RIGHT, "align-right" },
87         { LyXTabular::ALIGN_CENTER, "align-center" },
88         { LyXTabular::ALIGN_BLOCK, "align-block" },
89         { LyXTabular::VALIGN_TOP, "valign-top" },
90         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
91         { LyXTabular::VALIGN_CENTER, "valign-center" },
92         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
93         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
94         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
95         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
96         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
97         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
98         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
99         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
100         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
101         { LyXTabular::M_VALIGN_CENTER, "m-valign-center" },
102         { LyXTabular::MULTICOLUMN, "multicolumn" },
103         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
104         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
105         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
106         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
107         { LyXTabular::SET_PWIDTH, "set-pwidth" },
108         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
109         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
110         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
111         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
112         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
113         { LyXTabular::SET_USEBOX, "set-usebox" },
114         { LyXTabular::SET_LTHEAD, "set-lthead" },
115         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
116         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
117         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
118         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
119         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
120         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
121         { LyXTabular::LAST_ACTION, "" }
122 };
123
124 struct FindFeature {
125         FindFeature(LyXTabular::Feature feature) : feature_(feature) {}
126         bool operator()(TabularFeature & tf)
127         {
128                 return tf.action == feature_;
129         }
130 private:
131         LyXTabular::Feature feature_;
132 };
133
134 } // namespace anon
135
136
137 string const featureAsString(LyXTabular::Feature feature)
138 {
139         TabularFeature * it  = tabularFeature;
140         TabularFeature * end = it +
141                 sizeof(tabularFeature) / sizeof(TabularFeature);
142         it = std::find_if(it, end, FindFeature(feature));
143         return (it == end) ? string() : it->feature;
144 }
145
146
147 bool InsetTabular::hasPasteBuffer() const
148 {
149         return (paste_tabular != 0);
150 }
151
152
153 InsetTabular::InsetTabular(Buffer const & buf, int rows, int columns)
154         : buffer_(&buf)
155 {
156         if (rows <= 0)
157                 rows = 1;
158         if (columns <= 0)
159                 columns = 1;
160         tabular.reset(new LyXTabular(buf.params, this, rows, columns));
161         // for now make it always display as display() inset
162         // just for test!!!
163         the_locking_inset = 0;
164         old_locking_inset = 0;
165         locked = false;
166         oldcell = -1;
167         actrow = actcell = 0;
168         clearSelection();
169         need_update = INIT;
170         in_update = false;
171         in_reset_pos = 0;
172         inset_x = 0;
173         inset_y = 0;
174 }
175
176
177 InsetTabular::InsetTabular(InsetTabular const & tab)
178         : UpdatableInset(tab), buffer_(tab.buffer_)
179 {
180         tabular.reset(new LyXTabular(buffer_->params,
181                                      this, *(tab.tabular)));
182         the_locking_inset = 0;
183         old_locking_inset = 0;
184         locked = false;
185         oldcell = -1;
186         actrow = actcell = 0;
187         clearSelection();
188         need_update = INIT;
189         in_update = false;
190         in_reset_pos = 0;
191         inset_x = 0;
192         inset_y = 0;
193 }
194
195
196 InsetTabular::~InsetTabular()
197 {
198         InsetTabularMailer mailer(*this);
199         mailer.hideDialog();
200 }
201
202
203 Inset * InsetTabular::clone() const
204 {
205         return new InsetTabular(*this);
206 }
207
208
209 BufferView * InsetTabular::view() const
210 {
211         return buffer_->getUser();
212 }
213
214
215 void InsetTabular::buffer(Buffer * b)
216 {
217         buffer_ = b;
218 }
219
220
221 void InsetTabular::write(Buffer const * buf, ostream & os) const
222 {
223         os << " Tabular" << endl;
224         tabular->Write(buf, os);
225 }
226
227
228 void InsetTabular::read(Buffer const * buf, LyXLex & lex)
229 {
230         bool const old_format = (lex.getString() == "\\LyXTable");
231
232         tabular.reset(new LyXTabular(buf, this, lex));
233
234         need_update = INIT;
235
236         if (old_format)
237                 return;
238
239         lex.nextToken();
240         string token = lex.getString();
241         while (lex.isOK() && (token != "\\end_inset")) {
242                 lex.nextToken();
243                 token = lex.getString();
244         }
245         if (token != "\\end_inset") {
246                 lex.printError("Missing \\end_inset at this point. "
247                                "Read: `$$Token'");
248         }
249 }
250
251
252 void InsetTabular::metrics(MetricsInfo &,
253         Dimension & dim) const
254 {
255         dim.asc = tabular->GetAscentOfRow(0);
256         dim.des = tabular->GetHeightOfTabular() - tabular->GetAscentOfRow(0) + 1;
257         dim.wid = tabular->GetWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
258 }
259
260
261 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
262 {
263         if (nodraw()) {
264                 need_update = FULL;
265                 return;
266         }
267
268         BufferView * bv = pi.base.bv;
269         int i;
270         int j;
271         int nx;
272
273 #if 0
274         UpdatableInset::draw(pi, x, y);
275 #else
276         if (!owner())
277                 x += scroll();
278 #endif
279
280         top_x = x;
281         top_baseline = y;
282         x += ADD_TO_TABULAR_WIDTH;
283
284         int cell = 0;
285         int cx;
286         first_visible_cell = -1;
287         for (i = 0; i < tabular->rows(); ++i) {
288                 nx = x;
289                 cell = tabular->GetCellNumber(i, 0);
290                 if (!((y + tabular->GetDescentOfRow(i)) > 0) &&
291                         (y - tabular->GetAscentOfRow(i)) < pi.pain.paperHeight())
292                 {
293                 y += tabular->GetDescentOfRow(i) +
294                                 tabular->GetAscentOfRow(i + 1) +
295                                 tabular->GetAdditionalHeight(i + 1);
296                         continue;
297                 }
298                 for (j = 0; j < tabular->columns(); ++j) {
299                         if (nx > bv->workWidth())
300                                 break;
301                         if (tabular->IsPartOfMultiColumn(i, j))
302                                 continue;
303                         cx = nx + tabular->GetBeginningOfTextInCell(cell);
304                         if (first_visible_cell < 0)
305                                 first_visible_cell = cell;
306                         if (hasSelection()) {
307                                 drawCellSelection(pi.pain, nx, y, i, j, cell);
308                         }
309
310                         tabular->GetCellInset(cell)->draw(pi, cx, y);
311                         drawCellLines(pi.pain, nx, y, i, cell);
312                         nx += tabular->GetWidthOfColumn(cell);
313                         ++cell;
314                 }
315
316                 // avoiding drawing the rest of a long table is
317                 // a pretty big speedup
318                 if (y > bv->workHeight())
319                         break;
320
321                 y += tabular->GetDescentOfRow(i) +
322                         tabular->GetAscentOfRow(i + 1) +
323                         tabular->GetAdditionalHeight(i + 1);
324         }
325
326         need_update = NONE;
327 }
328
329
330 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
331                                  int row, int cell) const
332 {
333         int x2 = x + tabular->GetWidthOfColumn(cell);
334         bool on_off;
335
336         if (!tabular->topAlreadyDrawn(cell)) {
337                 on_off = !tabular->TopLine(cell);
338                 pain.line(x, y - tabular->GetAscentOfRow(row),
339                           x2, y -  tabular->GetAscentOfRow(row),
340                           on_off ? LColor::tabularonoffline : LColor::tabularline,
341                           on_off ? Painter::line_onoffdash : Painter::line_solid);
342         }
343         on_off = !tabular->BottomLine(cell);
344         pain.line(x, y + tabular->GetDescentOfRow(row),
345                   x2, y + tabular->GetDescentOfRow(row),
346                   on_off ? LColor::tabularonoffline : LColor::tabularline,
347                   on_off ? Painter::line_onoffdash : Painter::line_solid);
348         if (!tabular->leftAlreadyDrawn(cell)) {
349                 on_off = !tabular->LeftLine(cell);
350                 pain.line(x, y -  tabular->GetAscentOfRow(row),
351                           x, y +  tabular->GetDescentOfRow(row),
352                           on_off ? LColor::tabularonoffline : LColor::tabularline,
353                           on_off ? Painter::line_onoffdash : Painter::line_solid);
354         }
355         on_off = !tabular->RightLine(cell);
356         pain.line(x2 - tabular->GetAdditionalWidth(cell),
357                   y -  tabular->GetAscentOfRow(row),
358                   x2 - tabular->GetAdditionalWidth(cell),
359                   y +  tabular->GetDescentOfRow(row),
360                   on_off ? LColor::tabularonoffline : LColor::tabularline,
361                   on_off ? Painter::line_onoffdash : Painter::line_solid);
362 }
363
364
365 void InsetTabular::drawCellSelection(Painter & pain, int x, int y,
366                                      int row, int column, int cell) const
367 {
368         lyx::Assert(hasSelection());
369         int cs = tabular->column_of_cell(sel_cell_start);
370         int ce = tabular->column_of_cell(sel_cell_end);
371         if (cs > ce) {
372                 ce = cs;
373                 cs = tabular->column_of_cell(sel_cell_end);
374         } else {
375                 ce = tabular->right_column_of_cell(sel_cell_end);
376         }
377
378         int rs = tabular->row_of_cell(sel_cell_start);
379         int re = tabular->row_of_cell(sel_cell_end);
380         if (rs > re)
381                 swap(rs, re);
382
383         if ((column >= cs) && (column <= ce) && (row >= rs) && (row <= re)) {
384                 int w = tabular->GetWidthOfColumn(cell);
385                 int h = tabular->GetAscentOfRow(row) + tabular->GetDescentOfRow(row)-1;
386                 pain.fillRectangle(x, y - tabular->GetAscentOfRow(row) + 1,
387                                    w, h, LColor::selection);
388         }
389 }
390
391
392 void InsetTabular::update(BufferView * bv, bool reinit)
393 {
394         if (in_update) {
395                 if (reinit) {
396                         resetPos(bv);
397                         if (owner())
398                                 owner()->update(bv, true);
399                 }
400                 return;
401         }
402         in_update = true;
403         if (reinit) {
404                 need_update = INIT;
405                 if (calculate_dimensions_of_cells(bv, true))
406                         resetPos(bv);
407                 if (owner())
408                         owner()->update(bv, true);
409                 in_update = false;
410                 return;
411         }
412         if (the_locking_inset)
413                 the_locking_inset->update(bv, reinit);
414         if (need_update < FULL &&
415                 bv->text->refreshStatus() == LyXText::REFRESH_AREA)
416         {
417                 need_update = FULL;
418         }
419
420         switch (need_update) {
421         case INIT:
422         case FULL:
423         case CELL:
424                 if (calculate_dimensions_of_cells(bv, false)) {
425                         need_update = INIT;
426                         resetPos(bv);
427                 }
428                 break;
429         case SELECTION:
430                 need_update = FULL;
431                 break;
432         default:
433                 break;
434         }
435         in_update = false;
436 }
437
438
439 string const InsetTabular::editMessage() const
440 {
441         return _("Opened table");
442 }
443
444
445 void InsetTabular::insetUnlock(BufferView * bv)
446 {
447         if (the_locking_inset) {
448                 the_locking_inset->insetUnlock(bv);
449                 updateLocal(bv, CELL);
450                 the_locking_inset = 0;
451         }
452         actcell = 0;
453         oldcell = -1;
454         locked = false;
455         if (scroll(false) || hasSelection()) {
456                 clearSelection();
457                 if (scroll(false)) {
458                         scroll(bv, 0.0F);
459                 }
460                 updateLocal(bv, FULL);
461         }
462 }
463
464
465 void InsetTabular::updateLocal(BufferView * bv, UpdateCodes what) const
466 {
467         if (what == INIT) {
468                 calculate_dimensions_of_cells(bv, true);
469         }
470         if (!locked && what == CELL)
471                 what = FULL;
472         if (need_update < what) // only set this if it has greater update
473                 need_update = what;
474         // Dirty Cast! (Lgb)
475         if (need_update != NONE) {
476                 bv->updateInset(const_cast<InsetTabular *>(this));
477                 if (locked)
478                         resetPos(bv);
479         }
480 }
481
482
483 bool InsetTabular::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
484 {
485         lyxerr[Debug::INSETTEXT] << "InsetTabular::LockInsetInInset("
486                               << inset << "): ";
487         if (!inset)
488                 return false;
489         oldcell = -1;
490         if (inset == tabular->GetCellInset(actcell)) {
491                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
492                 the_locking_inset = tabular->GetCellInset(actcell);
493                 resetPos(bv);
494                 return true;
495         } else if (!the_locking_inset) {
496                 int const n = tabular->GetNumberOfCells();
497                 int const id = inset->id();
498                 for (int i = 0; i < n; ++i) {
499                         InsetText * in = tabular->GetCellInset(i);
500                         if (inset == in) {
501                                 actcell = i;
502                                 the_locking_inset = in;
503                                 locked = true;
504                                 resetPos(bv);
505                                 return true;
506                         }
507                         if (in->getInsetFromID(id)) {
508                                 actcell = i;
509                                 in->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
510                                 return the_locking_inset->lockInsetInInset(bv, inset);
511                         }
512                 }
513         } else if (the_locking_inset && (the_locking_inset == inset)) {
514                 lyxerr[Debug::INSETTEXT] << "OK" << endl;
515                 resetPos(bv);
516         } else if (the_locking_inset) {
517                 lyxerr[Debug::INSETTEXT] << "MAYBE" << endl;
518                 return the_locking_inset->lockInsetInInset(bv, inset);
519         }
520         lyxerr[Debug::INSETTEXT] << "NOT OK" << endl;
521         return false;
522 }
523
524
525 bool InsetTabular::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
526                                       bool lr)
527 {
528         if (!the_locking_inset)
529                 return false;
530         if (the_locking_inset == inset) {
531                 the_locking_inset->insetUnlock(bv);
532 #ifdef WITH_WARNINGS
533 #warning fix scrolling when cellinset has requested a scroll (Jug)!!!
534 #endif
535 #if 0
536                 if (scroll(false))
537                         scroll(bv, 0.0F);
538 #endif
539                 updateLocal(bv, CELL);
540                 // this has to be here otherwise we don't redraw the cell!
541                 the_locking_inset = 0;
542                 return true;
543         }
544         if (the_locking_inset->unlockInsetInInset(bv, inset, lr)) {
545                 if (inset->lyxCode() == TABULAR_CODE &&
546                     !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) {
547                         InsetTabularMailer mailer(*this);
548                         mailer.updateDialog(bv);
549                         oldcell = actcell;
550                 }
551                 return true;
552         }
553         return false;
554 }
555
556
557 bool InsetTabular::updateInsetInInset(BufferView * bv, Inset * inset)
558 {
559         Inset * tl_inset = inset;
560         // look if this inset is really inside myself!
561         while(tl_inset->owner() && tl_inset->owner() != this)
562                 tl_inset = tl_inset->owner();
563         // if we enter here it's not ower inset
564         if (!tl_inset->owner())
565                 return false;
566         // we only have to do this if this is a subinset of our cells
567         if (tl_inset != inset) {
568                 if (!static_cast<InsetText *>(tl_inset)->updateInsetInInset(bv, inset))
569                         return false;
570         }
571         updateLocal(bv, CELL);
572         return true;
573 }
574
575
576 int InsetTabular::insetInInsetY() const
577 {
578         if (!the_locking_inset)
579                 return 0;
580         return inset_y + the_locking_inset->insetInInsetY();
581 }
582
583
584 UpdatableInset * InsetTabular::getLockingInset() const
585 {
586         return the_locking_inset ? the_locking_inset->getLockingInset() :
587                 const_cast<InsetTabular *>(this);
588 }
589
590
591 UpdatableInset * InsetTabular::getFirstLockingInsetOfType(Inset::Code c)
592 {
593         if (c == lyxCode())
594                 return this;
595         if (the_locking_inset)
596                 return the_locking_inset->getFirstLockingInsetOfType(c);
597         return 0;
598 }
599
600
601 bool InsetTabular::insertInset(BufferView * bv, Inset * inset)
602 {
603         if (the_locking_inset)
604                 return the_locking_inset->insertInset(bv, inset);
605         return false;
606 }
607
608
609 void InsetTabular::lfunMousePress(FuncRequest const & cmd)
610 {
611         if (hasSelection() && cmd.button() == mouse_button::button3)
612                 return;
613
614         if (hasSelection()) {
615                 clearSelection();
616                 updateLocal(cmd.view(), SELECTION);
617         }
618
619         int const ocell = actcell;
620         int const orow = actrow;
621         BufferView * bv = cmd.view();
622
623         if (!locked) {
624                 locked = true;
625                 the_locking_inset = 0;
626                 inset_x = 0;
627                 inset_y = 0;
628         }
629         setPos(bv, cmd.x, cmd.y);
630         if (actrow != orow)
631                 updateLocal(bv, NONE);
632         clearSelection();
633 #if 0
634         if (cmd.button() == mouse_button::button3) {
635                 if ((ocell != actcell) && the_locking_inset) {
636                         the_locking_inset->insetUnlock(bv);
637                         updateLocal(bv, CELL);
638                         the_locking_inset = 0;
639                 }
640                 return;
641         }
642 #endif
643
644         bool const inset_hit = insetHit(bv, cmd.x, cmd.y);
645
646         if ((ocell == actcell) && the_locking_inset && inset_hit) {
647                 resetPos(bv);
648                 FuncRequest cmd1 = cmd;
649                 cmd1.x -= inset_x;
650                 cmd1.y -= inset_y;
651                 the_locking_inset->localDispatch(cmd1);
652                 return;
653         }
654
655         if (the_locking_inset) {
656                 the_locking_inset->insetUnlock(bv);
657                 updateLocal(bv, CELL);
658                 the_locking_inset = 0;
659         }
660
661         if (cmd.button() == mouse_button::button2) {
662                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
663                 return;
664         }
665
666         if (inset_hit && bv->theLockingInset()) {
667                 if (!bv->lockInset(static_cast<UpdatableInset*>
668                                 (tabular->GetCellInset(actcell))))
669                 {
670                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
671                         return;
672                 }
673                 FuncRequest cmd1 = cmd;
674                 cmd1.x -= inset_x;
675                 cmd1.y -= inset_y;
676                 the_locking_inset->localDispatch(cmd1);
677                 return;
678         }
679 }
680
681
682 bool InsetTabular::lfunMouseRelease(FuncRequest const & cmd)
683 {
684         bool ret = false;
685         if (the_locking_inset) {
686                 FuncRequest cmd1 = cmd;
687                 cmd1.x -= inset_x;
688                 cmd1.y -= inset_y;
689                 ret = the_locking_inset->localDispatch(cmd1);
690         }
691         if (cmd.button() == mouse_button::button3 && !ret) {
692                 InsetTabularMailer(*this).showDialog(cmd.view());
693                 return true;
694         }
695         return ret;
696 }
697
698
699 void InsetTabular::lfunMouseMotion(FuncRequest const & cmd)
700 {
701         if (the_locking_inset) {
702                 FuncRequest cmd1 = cmd;
703                 cmd1.x -= inset_x;
704                 cmd1.y -= inset_y;
705                 the_locking_inset->localDispatch(cmd1);
706                 return;
707         }
708
709         BufferView * bv = cmd.view();
710         int const old_cell = actcell;
711
712         setPos(bv, cmd.x, cmd.y);
713         if (!hasSelection()) {
714                 setSelection(actcell, actcell);
715                 updateLocal(bv, SELECTION);
716         } else if (old_cell != actcell) {
717                 setSelection(sel_cell_start, actcell);
718                 updateLocal(bv, SELECTION);
719         }
720 }
721
722
723 Inset::RESULT InsetTabular::localDispatch(FuncRequest const & cmd)
724 {
725         // We need to save the value of the_locking_inset as the call to
726         // the_locking_inset->localDispatch might unlock it.
727         old_locking_inset = the_locking_inset;
728         RESULT result = UpdatableInset::localDispatch(cmd);
729         BufferView * bv = cmd.view();
730
731         if (cmd.action == LFUN_INSET_EDIT) {
732
733                 if (!bv->lockInset(this)) {
734                         lyxerr[Debug::INSETTEXT] << "InsetTabular::Cannot lock inset" << endl;
735                         return DISPATCHED;
736                 }
737
738                 finishUndo();
739                 locked = true;
740                 the_locking_inset = 0;
741                 inset_x = 0;
742                 inset_y = 0;
743
744                 if (cmd.argument.size()) {
745                         if (cmd.argument == "left") {
746                                 if (isRightToLeft(bv))
747                                         actcell = tabular->GetLastCellInRow(0);
748                                 else
749                                         actcell = 0;
750                         } else {
751                                 if (isRightToLeft(bv))
752                                         actcell = tabular->GetFirstCellInRow(tabular->rows()-1);
753                                 else
754                                         actcell = tabular->GetNumberOfCells() - 1;
755                         }
756                         clearSelection();
757                         resetPos(bv);
758                         bv->fitCursor();
759                 }
760
761                 else {
762                         setPos(bv, cmd.x, cmd.y);
763                         clearSelection();
764                         finishUndo();
765                         if (insetHit(bv, cmd.x, cmd.y) && cmd.button() != mouse_button::button3) {
766                                 activateCellInsetAbs(bv, cmd.x, cmd.y, cmd.button());
767                         }
768                 }
769                 return DISPATCHED;
770         }
771
772         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE) {
773                 resetPos(bv);
774                 return result;
775         }
776
777         if (cmd.action < 0 && cmd.argument.empty())
778                 return FINISHED;
779
780         bool hs = hasSelection();
781
782         result = DISPATCHED;
783         // this one have priority over the locked InsetText, if we're not already
784         // inside another tabular then that one get's priority!
785         if (getFirstLockingInsetOfType(Inset::TABULAR_CODE) == this) {
786                 switch (cmd.action) {
787                 case LFUN_MOUSE_PRESS:
788                         lfunMousePress(cmd);
789                         return DISPATCHED;
790
791                 case LFUN_MOUSE_MOTION:
792                         lfunMouseMotion(cmd);
793                         return DISPATCHED;
794
795                 case LFUN_MOUSE_RELEASE:
796                         return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
797
798                 case LFUN_CELL_BACKWARD:
799                 case LFUN_CELL_FORWARD:
800                         unlockInsetInInset(bv, the_locking_inset);
801                         if (cmd.action == LFUN_CELL_FORWARD)
802                                 moveNextCell(bv, old_locking_inset != 0);
803                         else
804                                 movePrevCell(bv, old_locking_inset != 0);
805                         clearSelection();
806                         if (hs)
807                                 updateLocal(bv, SELECTION);
808                         if (!the_locking_inset) {
809                                 return DISPATCHED_NOUPDATE;
810                         }
811                         return result;
812                 // this to avoid compiler warnings.
813                 default:
814                         break;
815                 }
816         }
817
818         kb_action action = cmd.action;
819         string    arg    = cmd.argument;
820         if (the_locking_inset) {
821                 result = the_locking_inset->localDispatch(cmd);
822                 if (result == DISPATCHED_NOUPDATE) {
823                         int sc = scroll();
824                         resetPos(bv);
825                         if (sc != scroll()) { // inset has been scrolled
826                                 updateLocal(bv, FULL);
827                         }
828                         return result;
829                 } else if (result == DISPATCHED) {
830                         updateLocal(bv, CELL);
831                         return result;
832                 } else if (result == FINISHED_UP) {
833                         action = LFUN_UP;
834                         // Make sure to reset status message after
835                         // exiting, e.g. math inset
836                         bv->owner()->clearMessage();
837                 } else if (result == FINISHED_DOWN) {
838                         action = LFUN_DOWN;
839                         bv->owner()->clearMessage();
840                 } else if (result == FINISHED_RIGHT) {
841                         action = LFUN_RIGHT;
842                         bv->owner()->clearMessage();
843                 } else if (result == FINISHED) {
844                         bv->owner()->clearMessage();
845                 }
846         }
847
848         result = DISPATCHED;
849         switch (action) {
850                 // --- Cursor Movements ----------------------------------
851         case LFUN_RIGHTSEL: {
852                 int const start = hasSelection() ? sel_cell_start : actcell;
853                 if (tabular->IsLastCellInRow(actcell)) {
854                         setSelection(start, actcell);
855                         break;
856                 }
857
858                 int end = actcell;
859                 // if we are starting a selection, only select
860                 // the current cell at the beginning
861                 if (hasSelection()) {
862                         moveRight(bv, false);
863                         end = actcell;
864                 }
865                 setSelection(start, end);
866                 updateLocal(bv, SELECTION);
867                 break;
868         }
869         case LFUN_RIGHT:
870                 result = moveRight(bv);
871                 clearSelection();
872                 if (hs)
873                         updateLocal(bv, SELECTION);
874                 break;
875         case LFUN_LEFTSEL: {
876                 int const start = hasSelection() ? sel_cell_start : actcell;
877                 if (tabular->IsFirstCellInRow(actcell)) {
878                         setSelection(start, actcell);
879                         break;
880                 }
881
882                 int end = actcell;
883                 // if we are starting a selection, only select
884                 // the current cell at the beginning
885                 if (hasSelection()) {
886                         moveLeft(bv, false);
887                         end = actcell;
888                 }
889                 setSelection(start, end);
890                 updateLocal(bv, SELECTION);
891                 break;
892         }
893         case LFUN_LEFT:
894                 result = moveLeft(bv);
895                 clearSelection();
896                 if (hs)
897                         updateLocal(bv, SELECTION);
898                 break;
899         case LFUN_DOWNSEL: {
900                 int const start = hasSelection() ? sel_cell_start : actcell;
901                 int const ocell = actcell;
902                 // if we are starting a selection, only select
903                 // the current cell at the beginning
904                 if (hasSelection()) {
905                         moveDown(bv, false);
906                         if ((ocell == sel_cell_end) ||
907                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
908                                 setSelection(start, tabular->GetCellBelow(sel_cell_end));
909                         else
910                                 setSelection(start, tabular->GetLastCellBelow(sel_cell_end));
911                 } else {
912                         setSelection(start, start);
913                 }
914                 updateLocal(bv, SELECTION);
915         }
916         break;
917         case LFUN_DOWN:
918                 result = moveDown(bv, old_locking_inset != 0);
919                 clearSelection();
920                 if (hs) {
921                         updateLocal(bv, SELECTION);
922                 }
923                 break;
924         case LFUN_UPSEL: {
925                 int const start = hasSelection() ? sel_cell_start : actcell;
926                 int const ocell = actcell;
927                 // if we are starting a selection, only select
928                 // the current cell at the beginning
929                 if (hasSelection()) {
930                         moveUp(bv, false);
931                         if ((ocell == sel_cell_end) ||
932                             (tabular->column_of_cell(ocell)>tabular->column_of_cell(actcell)))
933                                 setSelection(start, tabular->GetCellAbove(sel_cell_end));
934                         else
935                                 setSelection(start, tabular->GetLastCellAbove(sel_cell_end));
936                 } else {
937                         setSelection(start, start);
938                 }
939                 updateLocal(bv, SELECTION);
940         }
941         break;
942         case LFUN_UP:
943                 result = moveUp(bv, old_locking_inset != 0);
944                 clearSelection();
945                 if (hs)
946                         updateLocal(bv, SELECTION);
947                 break;
948         case LFUN_NEXT: {
949                 UpdateCodes code = CURSOR;
950                 if (hs) {
951                         clearSelection();
952                         code = SELECTION;
953                 }
954                 int column = actcol;
955                 unlockInsetInInset(bv, the_locking_inset);
956                 if (bv->text->top_y() + bv->painter().paperHeight() <
957                     (top_baseline + tabular->GetHeightOfTabular()))
958                         {
959                                 bv->scrollDocView(bv->text->top_y() + bv->painter().paperHeight());
960                                 code = FULL;
961                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
962                         } else {
963                                 actcell = tabular->GetFirstCellInRow(tabular->rows() - 1) + column;
964                         }
965                 resetPos(bv);
966                 updateLocal(bv, code);
967                 break;
968         }
969         case LFUN_PRIOR: {
970                 UpdateCodes code = CURSOR;
971                 if (hs) {
972                         clearSelection();
973                         code = SELECTION;
974                 }
975                 int column = actcol;
976                 unlockInsetInInset(bv, the_locking_inset);
977                 if (top_baseline < 0) {
978                         bv->scrollDocView(bv->text->top_y() - bv->painter().paperHeight());
979                         code = FULL;
980                         if (top_baseline > 0)
981                                 actcell = column;
982                         else
983                                 actcell = tabular->GetCellBelow(first_visible_cell) + column;
984                 } else {
985                         actcell = column;
986                 }
987                 resetPos(bv);
988                 updateLocal(bv, code);
989                 break;
990         }
991         // none of these make sense for insettabular,
992         // but we must catch them to prevent any
993         // selection from being confused
994         case LFUN_PRIORSEL:
995         case LFUN_NEXTSEL:
996         case LFUN_WORDLEFT:
997         case LFUN_WORDLEFTSEL:
998         case LFUN_WORDRIGHT:
999         case LFUN_WORDRIGHTSEL:
1000         case LFUN_WORDSEL:
1001         case LFUN_DOWN_PARAGRAPH:
1002         case LFUN_DOWN_PARAGRAPHSEL:
1003         case LFUN_UP_PARAGRAPH:
1004         case LFUN_UP_PARAGRAPHSEL:
1005         case LFUN_BACKSPACE:
1006         case LFUN_HOME:
1007         case LFUN_HOMESEL:
1008         case LFUN_END:
1009         case LFUN_ENDSEL:
1010         case LFUN_BEGINNINGBUF:
1011         case LFUN_BEGINNINGBUFSEL:
1012         case LFUN_ENDBUF:
1013         case LFUN_ENDBUFSEL:
1014                 break;
1015         case LFUN_LAYOUT_TABULAR: {
1016                 InsetTabularMailer mailer(*this);
1017                 mailer.showDialog(bv);
1018                 break;
1019         }
1020         case LFUN_INSET_DIALOG_UPDATE: {
1021                 InsetTabularMailer mailer(*this);
1022                 mailer.updateDialog(bv);
1023                 break;
1024         }
1025         case LFUN_TABULAR_FEATURE:
1026                 if (!tabularFeatures(bv, arg))
1027                         result = UNDISPATCHED;
1028                 break;
1029                 // insert file functions
1030         case LFUN_FILE_INSERT_ASCII_PARA:
1031         case LFUN_FILE_INSERT_ASCII:
1032         {
1033                 string tmpstr = getContentsOfAsciiFile(bv, arg, false);
1034                 if (tmpstr.empty())
1035                         break;
1036                 if (insertAsciiString(bv, tmpstr, false))
1037                         updateLocal(bv, INIT);
1038                 else
1039                         result = UNDISPATCHED;
1040                 break;
1041         }
1042         // cut and paste functions
1043         case LFUN_CUT:
1044                 if (!copySelection(bv))
1045                         break;
1046                 // no break here!
1047         case LFUN_DELETE:
1048                 setUndo(bv, Undo::DELETE, bv->text->cursor.par());
1049                 cutSelection(bv->buffer()->params);
1050                 updateLocal(bv, INIT);
1051                 break;
1052         case LFUN_COPY:
1053                 if (!hasSelection())
1054                         break;
1055                 finishUndo();
1056                 copySelection(bv);
1057                 break;
1058         case LFUN_PASTESELECTION:
1059         {
1060                 string const clip(bv->getClipboard());
1061                         if (clip.empty())
1062                         break;
1063 #if 0
1064                 if (clip.find('\t') != string::npos) {
1065                         int cols = 1;
1066                         int rows = 1;
1067                         int maxCols = 1;
1068                         string::size_type len = clip.length();
1069                         string::size_type p = 0;
1070
1071                         while (p < len &&
1072                               ((p = clip.find_first_of("\t\n", p)) != string::npos)) {
1073                                 switch (clip[p]) {
1074                                 case '\t':
1075                                         ++cols;
1076                                         break;
1077                                 case '\n':
1078                                         if ((p+1) < len)
1079                                                 ++rows;
1080                                         maxCols = max(cols, maxCols);
1081                                         cols = 1;
1082                                         break;
1083                                 }
1084                                 ++p;
1085                         }
1086                         maxCols = max(cols, maxCols);
1087                         delete paste_tabular;
1088                         paste_tabular = new LyXTabular(bv->buffer()->params,
1089                                                        this, rows, maxCols);
1090                         string::size_type op = 0;
1091                         int cell = 0;
1092                         int cells = paste_tabular->GetNumberOfCells();
1093                         p = cols = 0;
1094                         while ((cell < cells) && (p < len) &&
1095                               (p = clip.find_first_of("\t\n", p)) != string::npos) {
1096                                 if (p >= len)
1097                                         break;
1098                                 switch (clip[p]) {
1099                                 case '\t':
1100                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1101                                         ++cols;
1102                                         ++cell;
1103                                         break;
1104                                 case '\n':
1105                                         paste_tabular->GetCellInset(cell)->setText(clip.substr(op, p-op));
1106                                         while (cols++ < maxCols)
1107                                                 ++cell;
1108                                         cols = 0;
1109                                         break;
1110                                 }
1111                                 ++p;
1112                                 op = p;
1113                         }
1114                         // check for the last cell if there is no trailing '\n'
1115                         if ((cell < cells) && (op < len))
1116                                 paste_tabular->GetCellInset(cell)->setText(clip.substr(op, len-op));
1117                 } else
1118 #else
1119                 if (!insertAsciiString(bv, clip, true))
1120 #endif
1121                 {
1122                         // so that the clipboard is used and it goes on
1123                         // to default
1124                         // and executes LFUN_PASTESELECTION in insettext!
1125                         delete paste_tabular;
1126                         paste_tabular = 0;
1127                 }
1128         }
1129         case LFUN_PASTE:
1130                 if (hasPasteBuffer()) {
1131                         setUndo(bv, Undo::INSERT, bv->text->cursor.par());
1132                         pasteSelection(bv);
1133                         updateLocal(bv, INIT);
1134                         break;
1135                 }
1136                 // ATTENTION: the function above has to be PASTE and PASTESELECTION!!!
1137         default:
1138                 // handle font changing stuff on selection before we lock the inset
1139                 // in the default part!
1140                 result = UNDISPATCHED;
1141                 if (hs) {
1142                         switch(action) {
1143                         case LFUN_LANGUAGE:
1144                         case LFUN_EMPH:
1145                         case LFUN_BOLD:
1146                         case LFUN_NOUN:
1147                         case LFUN_CODE:
1148                         case LFUN_SANS:
1149                         case LFUN_ROMAN:
1150                         case LFUN_DEFAULT:
1151                         case LFUN_UNDERLINE:
1152                         case LFUN_FONT_SIZE:
1153                                 if (bv->dispatch(FuncRequest(bv, action, arg)))
1154                                         result = DISPATCHED;
1155                                 break;
1156                         default:
1157                                 break;
1158                         }
1159                 }
1160                 // we try to activate the actual inset and put this event down to
1161                 // the insets dispatch function.
1162                 if ((result == DISPATCHED) || the_locking_inset)
1163                         break;
1164                 nodraw(true);
1165                 if (activateCellInset(bv)) {
1166                         // reset need_update setted in above function!
1167                         need_update = NONE;
1168                         result = the_locking_inset->localDispatch(FuncRequest(bv, action, arg));
1169                         if ((result == UNDISPATCHED) || (result >= FINISHED)) {
1170                                 unlockInsetInInset(bv, the_locking_inset);
1171                                 nodraw(false);
1172                                 // we need to update if this was requested before
1173                                 updateLocal(bv, NONE);
1174                                 return UNDISPATCHED;
1175                         } else if (hs) {
1176                                 clearSelection();
1177                                 // so the below CELL is not set because this is higher
1178                                 // priority and we get a full redraw
1179                                 need_update = SELECTION;
1180                         }
1181                         nodraw(false);
1182                         updateLocal(bv, CELL);
1183                         return result;
1184                 }
1185                 break;
1186         }
1187         if (result < FINISHED) {
1188                 if (!the_locking_inset) {
1189                         if (bv->fitCursor())
1190                                 updateLocal(bv, FULL);
1191                 }
1192         } else
1193                 bv->unlockInset(this);
1194         return result;
1195 }
1196
1197
1198 int InsetTabular::latex(Buffer const * buf, ostream & os,
1199                         LatexRunParams const & runparams) const
1200 {
1201         return tabular->latex(buf, os, runparams);
1202 }
1203
1204
1205 int InsetTabular::ascii(Buffer const * buf, ostream & os, int ll) const
1206 {
1207         if (ll > 0)
1208                 return tabular->ascii(buf, os, (int)parOwner()->params().depth(),
1209                                       false,0);
1210         return tabular->ascii(buf, os, 0, false,0);
1211 }
1212
1213
1214 int InsetTabular::linuxdoc(Buffer const * buf, ostream & os) const
1215 {
1216         os << "<![CDATA[";
1217         int const ret = tabular->ascii(buf,os,
1218                                        (int)parOwner()->params().depth(),
1219                                        false, 0);
1220         os << "]]>";
1221         return ret;
1222 }
1223
1224
1225 int InsetTabular::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1226 {
1227         int ret = 0;
1228         Inset * master;
1229
1230         // if the table is inside a float it doesn't need the informaltable
1231         // wrapper. Search for it.
1232         for(master = owner();
1233             master && master->lyxCode() != Inset::FLOAT_CODE;
1234             master = master->owner());
1235
1236         if (!master) {
1237                 os << "<informaltable>";
1238                 if (mixcont)
1239                         os << endl;
1240                 ret++;
1241         }
1242         ret+= tabular->docbook(buf, os, mixcont);
1243         if (!master) {
1244                 os << "</informaltable>";
1245                 if (mixcont)
1246                         os << endl;
1247                 ret++;
1248         }
1249         return ret;
1250 }
1251
1252
1253 void InsetTabular::validate(LaTeXFeatures & features) const
1254 {
1255         tabular->Validate(features);
1256 }
1257
1258
1259 bool InsetTabular::calculate_dimensions_of_cells(BufferView * bv, bool reinit) const
1260 {
1261         int cell = -1;
1262         int maxAsc = 0;
1263         int maxDesc = 0;
1264         InsetText * inset;
1265         bool changed = false;
1266
1267         // FIXME: since InsetText ignores this anyway, it doesn't
1268         // matter what we pass it. Ugly
1269         LyXFont font;
1270
1271         // if we have a locking_inset we should have to check only this cell for
1272         // change so I'll try this to have a boost, but who knows ;)
1273         if ((need_update != INIT) &&
1274             (the_locking_inset == tabular->GetCellInset(actcell))) {
1275                 for(int i = 0; i < tabular->columns(); ++i) {
1276                         maxAsc = max(tabular->GetCellInset(actrow, i)->ascent(bv, font),
1277                                      maxAsc);
1278                         maxDesc = max(tabular->GetCellInset(actrow, i)->descent(bv, font),
1279                                       maxDesc);
1280                 }
1281                 changed = tabular->SetWidthOfCell(actcell, the_locking_inset->width(bv, font));
1282                 changed = tabular->SetAscentOfRow(actrow, maxAsc + ADD_TO_HEIGHT) || changed;
1283                 changed = tabular->SetDescentOfRow(actrow, maxDesc + ADD_TO_HEIGHT) || changed;
1284                 return changed;
1285         }
1286         for (int i = 0; i < tabular->rows(); ++i) {
1287                 maxAsc = 0;
1288                 maxDesc = 0;
1289                 for (int j = 0; j < tabular->columns(); ++j) {
1290                         if (tabular->IsPartOfMultiColumn(i,j))
1291                                 continue;
1292                         ++cell;
1293                         inset = tabular->GetCellInset(cell);
1294                         if (!reinit && !tabular->GetPWidth(cell).zero())
1295                                 inset->update(bv, false);
1296                         maxAsc = max(maxAsc, inset->ascent(bv, font));
1297                         maxDesc = max(maxDesc, inset->descent(bv, font));
1298                         changed = tabular->SetWidthOfCell(cell, inset->width(bv, font)) || changed;
1299                 }
1300                 changed = tabular->SetAscentOfRow(i, maxAsc + ADD_TO_HEIGHT) || changed;
1301                 changed = tabular->SetDescentOfRow(i, maxDesc + ADD_TO_HEIGHT) || changed;
1302         }
1303         if (changed)
1304                 tabular->reinit();
1305         return changed;
1306 }
1307
1308
1309 void InsetTabular::getCursor(BufferView & bv, int & x, int & y) const
1310 {
1311         if (the_locking_inset) {
1312                 the_locking_inset->getCursor(bv, x, y);
1313                 return;
1314         }
1315
1316         x = cursor_.x();
1317         y = cursor_.y() + InsetTabular::y();
1318
1319         // Fun stuff
1320         int desc = tabular->GetDescentOfRow(actrow);
1321         y += desc;
1322         int ascdesc = tabular->GetAscentOfRow(actrow) + desc;
1323         y -= ascdesc / 2;
1324         y += ADD_TO_HEIGHT * 2;
1325         y += TEXT_TO_INSET_OFFSET;
1326 }
1327
1328
1329 void InsetTabular::getCursorPos(BufferView * bv, int & x, int & y) const
1330 {
1331         if (the_locking_inset) {
1332                 the_locking_inset->getCursorPos(bv, x, y);
1333                 return;
1334         }
1335         x = cursor_.x() - top_x;
1336         y = cursor_.y();
1337 }
1338
1339
1340 void InsetTabular::fitInsetCursor(BufferView * bv) const
1341 {
1342         if (the_locking_inset) {
1343                 int old_top_y = bv->text->top_y();
1344                 the_locking_inset->fitInsetCursor(bv);
1345                 if (old_top_y != bv->text->top_y())
1346                         need_update = FULL;
1347                 return;
1348         }
1349         LyXFont font;
1350
1351         int const asc = font_metrics::maxAscent(font);
1352         int const desc = font_metrics::maxDescent(font);
1353         resetPos(bv);
1354
1355         if (bv->fitLockedInsetCursor(cursor_.x(), cursor_.y(), asc, desc))
1356                 need_update = FULL;
1357 }
1358
1359
1360 void InsetTabular::setPos(BufferView * bv, int x, int y) const
1361 {
1362         cursor_.y(0);
1363
1364         actcell = actrow = actcol = 0;
1365         int ly = tabular->GetDescentOfRow(actrow);
1366
1367         // first search the right row
1368         while ((ly < y) && ((actrow+1) < tabular->rows())) {
1369                 cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1370                                  tabular->GetAscentOfRow(actrow + 1) +
1371                                  tabular->GetAdditionalHeight(actrow + 1));
1372                 ++actrow;
1373                 ly = cursor_.y() + tabular->GetDescentOfRow(actrow);
1374         }
1375         actcell = tabular->GetCellNumber(actrow, actcol);
1376
1377         // now search the right column
1378         int lx = tabular->GetWidthOfColumn(actcell) -
1379                 tabular->GetAdditionalWidth(actcell);
1380         for (; !tabular->IsLastCellInRow(actcell) && lx < x; ++actcell) {
1381                 lx += tabular->GetWidthOfColumn(actcell + 1)
1382                         + tabular->GetAdditionalWidth(actcell);
1383         }
1384         cursor_.x(lx - tabular->GetWidthOfColumn(actcell) + top_x + 2);
1385         resetPos(bv);
1386 }
1387
1388
1389 int InsetTabular::getCellXPos(int cell) const
1390 {
1391         int c = cell;
1392
1393         for (; !tabular->IsFirstCellInRow(c); --c)
1394                 ;
1395         int lx = tabular->GetWidthOfColumn(cell);
1396         for (; c < cell; ++c) {
1397                 lx += tabular->GetWidthOfColumn(c);
1398         }
1399         return (lx - tabular->GetWidthOfColumn(cell) + top_x);
1400 }
1401
1402
1403 void InsetTabular::resetPos(BufferView * bv) const
1404 {
1405 #ifdef WITH_WARNINGS
1406 #warning This should be fixed in the right manner (20011128 Jug)
1407 #endif
1408         // fast hack to fix infinite repaintings!
1409         if (in_reset_pos > 0)
1410                 return;
1411
1412         int cell = 0;
1413         actcol = tabular->column_of_cell(actcell);
1414         actrow = 0;
1415         cursor_.y(0);
1416         for (; (cell < actcell) && !tabular->IsLastRow(cell); ++cell) {
1417                 if (tabular->IsLastCellInRow(cell)) {
1418                         cursor_.y(cursor_.y() + tabular->GetDescentOfRow(actrow) +
1419                                          tabular->GetAscentOfRow(actrow + 1) +
1420                                          tabular->GetAdditionalHeight(actrow + 1));
1421                         ++actrow;
1422                 }
1423         }
1424         if (!locked || nodraw()) {
1425                 if (the_locking_inset)
1426                         inset_y = cursor_.y();
1427                 return;
1428         }
1429         // we need this only from here on!!!
1430         ++in_reset_pos;
1431         static int const offset = ADD_TO_TABULAR_WIDTH + 2;
1432         int new_x = getCellXPos(actcell);
1433         int old_x = cursor_.x();
1434         new_x += offset;
1435         cursor_.x(new_x);
1436 //    cursor.x(getCellXPos(actcell) + offset);
1437         if ((actcol < tabular->columns() - 1) && scroll(false) &&
1438                 (tabular->GetWidthOfTabular() < bv->workWidth()-20))
1439         {
1440                 scroll(bv, 0.0F);
1441                 updateLocal(bv, FULL);
1442         } else if (the_locking_inset &&
1443                  (tabular->GetWidthOfColumn(actcell) > bv->workWidth()-20))
1444         {
1445                 int xx = cursor_.x() - offset + bv->text->getRealCursorX();
1446                 if (xx > (bv->workWidth()-20)) {
1447                         scroll(bv, -(xx - bv->workWidth() + 60));
1448                         updateLocal(bv, FULL);
1449                 } else if (xx < 20) {
1450                         if (xx < 0)
1451                                 xx = -xx + 60;
1452                         else
1453                                 xx = 60;
1454                         scroll(bv, xx);
1455                         updateLocal(bv, FULL);
1456                 }
1457         } else if ((cursor_.x() - offset) > 20 &&
1458                    (cursor_.x() - offset + tabular->GetWidthOfColumn(actcell))
1459                    > (bv->workWidth() - 20)) {
1460                 scroll(bv, -tabular->GetWidthOfColumn(actcell) - 20);
1461                 updateLocal(bv, FULL);
1462         } else if ((cursor_.x() - offset) < 20) {
1463                 scroll(bv, 20 - cursor_.x() + offset);
1464                 updateLocal(bv, FULL);
1465         } else if (scroll() && top_x > 20 &&
1466                    (top_x + tabular->GetWidthOfTabular()) > (bv->workWidth() - 20)) {
1467                 scroll(bv, old_x - cursor_.x());
1468                 updateLocal(bv, FULL);
1469         }
1470         if (the_locking_inset) {
1471                 inset_x = cursor_.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
1472                 inset_y = cursor_.y();
1473         }
1474         if ((!the_locking_inset ||
1475              !the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE)) &&
1476             actcell != oldcell) {
1477                 InsetTabular * inset = const_cast<InsetTabular *>(this);
1478                 InsetTabularMailer mailer(*inset);
1479                 mailer.updateDialog(bv);
1480                 oldcell = actcell;
1481         }
1482         in_reset_pos = 0;
1483 }
1484
1485
1486 Inset::RESULT InsetTabular::moveRight(BufferView * bv, bool lock)
1487 {
1488         if (lock && !old_locking_inset) {
1489                 if (activateCellInset(bv))
1490                         return DISPATCHED;
1491         } else {
1492                 bool moved = isRightToLeft(bv)
1493                         ? movePrevCell(bv) : moveNextCell(bv);
1494                 if (!moved)
1495                         return FINISHED_RIGHT;
1496                 if (lock && activateCellInset(bv))
1497                         return DISPATCHED;
1498         }
1499         resetPos(bv);
1500         return DISPATCHED_NOUPDATE;
1501 }
1502
1503
1504 Inset::RESULT InsetTabular::moveLeft(BufferView * bv, bool lock)
1505 {
1506         bool moved = isRightToLeft(bv) ? moveNextCell(bv) : movePrevCell(bv);
1507         if (!moved)
1508                 return FINISHED;
1509         if (lock) {       // behind the inset
1510                 if (activateCellInset(bv, 0, 0, mouse_button::none, true))
1511                         return DISPATCHED;
1512         }
1513         resetPos(bv);
1514         return DISPATCHED_NOUPDATE;
1515 }
1516
1517
1518 Inset::RESULT InsetTabular::moveUp(BufferView * bv, bool lock)
1519 {
1520         int const ocell = actcell;
1521         actcell = tabular->GetCellAbove(actcell);
1522         if (actcell == ocell) // we moved out of the inset
1523                 return FINISHED_UP;
1524         resetPos(bv);
1525         if (lock) {
1526                 int x = 0;
1527                 int y = 0;
1528                 if (old_locking_inset) {
1529                         old_locking_inset->getCursorPos(bv, x, y);
1530                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1531                 }
1532                 if (activateCellInset(bv, x, 0))
1533                         return DISPATCHED;
1534         }
1535         return DISPATCHED_NOUPDATE;
1536 }
1537
1538
1539 Inset::RESULT InsetTabular::moveDown(BufferView * bv, bool lock)
1540 {
1541         int const ocell = actcell;
1542         actcell = tabular->GetCellBelow(actcell);
1543         if (actcell == ocell) // we moved out of the inset
1544                 return FINISHED_DOWN;
1545         resetPos(bv);
1546         if (lock) {
1547                 int x = 0;
1548                 int y = 0;
1549                 if (old_locking_inset) {
1550                         old_locking_inset->getCursorPos(bv, x, y);
1551                         x -= cursor_.x() + tabular->GetBeginningOfTextInCell(actcell);
1552                 }
1553                 if (activateCellInset(bv, x, 0))
1554                         return DISPATCHED;
1555         }
1556         return DISPATCHED_NOUPDATE;
1557 }
1558
1559
1560 bool InsetTabular::moveNextCell(BufferView * bv, bool lock)
1561 {
1562         if (isRightToLeft(bv)) {
1563                 if (tabular->IsFirstCellInRow(actcell)) {
1564                         int row = tabular->row_of_cell(actcell);
1565                         if (row == tabular->rows() - 1)
1566                                 return false;
1567                         actcell = tabular->GetLastCellInRow(row);
1568                         actcell = tabular->GetCellBelow(actcell);
1569                 } else {
1570                         if (!actcell)
1571                                 return false;
1572                         --actcell;
1573                 }
1574         } else {
1575                 if (tabular->IsLastCell(actcell))
1576                         return false;
1577                 ++actcell;
1578         }
1579         if (lock) {
1580                 bool rtl = tabular->GetCellInset(actcell)->paragraphs.begin()->
1581                         isRightToLeftPar(bv->buffer()->params);
1582                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1583         }
1584         resetPos(bv);
1585         return true;
1586 }
1587
1588
1589 bool InsetTabular::movePrevCell(BufferView * bv, bool lock)
1590 {
1591         if (isRightToLeft(bv)) {
1592                 if (tabular->IsLastCellInRow(actcell)) {
1593                         int row = tabular->row_of_cell(actcell);
1594                         if (row == 0)
1595                                 return false;
1596                         actcell = tabular->GetFirstCellInRow(row);
1597                         actcell = tabular->GetCellAbove(actcell);
1598                 } else {
1599                         if (tabular->IsLastCell(actcell))
1600                                 return false;
1601                         ++actcell;
1602                 }
1603         } else {
1604                 if (!actcell) // first cell
1605                         return false;
1606                 --actcell;
1607         }
1608         if (lock) {
1609                 bool rtl = tabular->GetCellInset(actcell)->paragraphs.begin()->
1610                         isRightToLeftPar(bv->buffer()->params);
1611                 activateCellInset(bv, 0, 0, mouse_button::none, !rtl);
1612         }
1613         resetPos(bv);
1614         return true;
1615 }
1616
1617
1618 void InsetTabular::setFont(BufferView * bv, LyXFont const & font, bool tall,
1619                            bool selectall)
1620 {
1621         if (selectall) {
1622                 setSelection(0, tabular->GetNumberOfCells() - 1);
1623         }
1624         if (hasSelection()) {
1625                 setUndo(bv, Undo::EDIT, bv->text->cursor.par());
1626                 bool const frozen = undo_frozen;
1627                 if (!frozen)
1628                         freezeUndo();
1629                 // apply the fontchange on the whole selection
1630                 int sel_row_start;
1631                 int sel_row_end;
1632                 int sel_col_start;
1633                 int sel_col_end;
1634                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1635                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1636                         for(int j = sel_col_start; j <= sel_col_end; ++j) {
1637                                 tabular->GetCellInset(i, j)->setFont(bv, font, tall, true);
1638                         }
1639                 }
1640                 if (!frozen)
1641                         unFreezeUndo();
1642                 if (selectall)
1643                         clearSelection();
1644                 updateLocal(bv, INIT);
1645         }
1646         if (the_locking_inset)
1647                 the_locking_inset->setFont(bv, font, tall);
1648 }
1649
1650
1651 bool InsetTabular::tabularFeatures(BufferView * bv, string const & what)
1652 {
1653         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1654
1655         int i = 0;
1656         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1657                 string const tmp = tabularFeature[i].feature;
1658
1659                 if (tmp == what.substr(0, tmp.length())) {
1660                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1661                         //tabularFeatures[i].feature.length())) {
1662                         action = tabularFeature[i].action;
1663                         break;
1664                 }
1665         }
1666         if (action == LyXTabular::LAST_ACTION)
1667                 return false;
1668
1669         string const val =
1670                 ltrim(what.substr(tabularFeature[i].feature.length()));
1671         tabularFeatures(bv, action, val);
1672         return true;
1673 }
1674
1675 namespace {
1676
1677 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1678                           string const & special, bool & flag)
1679 {
1680         if (special == "dl_above") {
1681                 ltt.topDL = flag;
1682                 ltt.set = false;
1683         } else if (special == "dl_below") {
1684                 ltt.bottomDL = flag;
1685                 ltt.set = false;
1686         } else if (special == "empty") {
1687                 ltt.empty = flag;
1688                 ltt.set = false;
1689         } else if (flag) {
1690                 ltt.empty = false;
1691                 ltt.set = true;
1692         }
1693 }
1694
1695 }
1696
1697
1698 void InsetTabular::tabularFeatures(BufferView * bv,
1699                                    LyXTabular::Feature feature,
1700                                    string const & value)
1701 {
1702         int sel_col_start;
1703         int sel_col_end;
1704         int sel_row_start;
1705         int sel_row_end;
1706         bool setLines = false;
1707         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1708         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1709
1710         switch (feature) {
1711         case LyXTabular::M_ALIGN_LEFT:
1712         case LyXTabular::ALIGN_LEFT:
1713                 setAlign = LYX_ALIGN_LEFT;
1714                 break;
1715         case LyXTabular::M_ALIGN_RIGHT:
1716         case LyXTabular::ALIGN_RIGHT:
1717                 setAlign = LYX_ALIGN_RIGHT;
1718                 break;
1719         case LyXTabular::M_ALIGN_CENTER:
1720         case LyXTabular::ALIGN_CENTER:
1721                 setAlign = LYX_ALIGN_CENTER;
1722                 break;
1723         case LyXTabular::ALIGN_BLOCK:
1724                 setAlign = LYX_ALIGN_BLOCK;
1725                 break;
1726         case LyXTabular::M_VALIGN_TOP:
1727         case LyXTabular::VALIGN_TOP:
1728                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1729                 break;
1730         case LyXTabular::M_VALIGN_BOTTOM:
1731         case LyXTabular::VALIGN_BOTTOM:
1732                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1733                 break;
1734         case LyXTabular::M_VALIGN_CENTER:
1735         case LyXTabular::VALIGN_CENTER:
1736                 setVAlign = LyXTabular::LYX_VALIGN_CENTER;
1737                 break;
1738         default:
1739                 break;
1740         }
1741         if (hasSelection()) {
1742                 getSelection(sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1743         } else {
1744                 sel_col_start = sel_col_end = tabular->column_of_cell(actcell);
1745                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
1746         }
1747         setUndo(bv, Undo::FINISH, bv->text->cursor.par());
1748
1749         int row =  tabular->row_of_cell(actcell);
1750         int column = tabular->column_of_cell(actcell);
1751         bool flag = true;
1752         LyXTabular::ltType ltt;
1753
1754         switch (feature) {
1755         case LyXTabular::SET_PWIDTH:
1756         {
1757                 LyXLength const vallen(value);
1758                 LyXLength const & tmplen = tabular->GetColumnPWidth(actcell);
1759
1760                 bool const update = (tmplen != vallen);
1761                 tabular->SetColumnPWidth(actcell, vallen);
1762                 if (update) {
1763                         // We need this otherwise we won't resize
1764                         // the insettext of the active cell (if any)
1765                         // until later (see InsetText::do_resize)
1766                         unlockInsetInInset(bv, the_locking_inset);
1767
1768                         int cell;
1769                         for (int i = 0; i < tabular->rows(); ++i) {
1770                                 cell = tabular->GetCellNumber(i,column);
1771                                 tabular->GetCellInset(cell)->resizeLyXText(bv);
1772                         }
1773                         updateLocal(bv, INIT);
1774                 }
1775
1776                 if (vallen.zero()
1777                     && tabular->GetAlignment(actcell, true) == LYX_ALIGN_BLOCK)
1778                         tabularFeatures(bv, LyXTabular::ALIGN_CENTER, string());
1779                 else if (!vallen.zero()
1780                          && tabular->GetAlignment(actcell, true) != LYX_ALIGN_BLOCK)
1781                         tabularFeatures(bv, LyXTabular::ALIGN_BLOCK, string());
1782         }
1783         break;
1784         case LyXTabular::SET_MPWIDTH:
1785         {
1786                 LyXLength const vallen(value);
1787                 LyXLength const & tmplen = tabular->GetPWidth(actcell);
1788
1789                 bool const update = (tmplen != vallen);
1790                 tabular->SetMColumnPWidth(actcell, vallen);
1791                 if (update) {
1792                         // We need this otherwise we won't resize
1793                         // the insettext of the active cell (if any)
1794                         // until later (see InsetText::do_resize)
1795                         unlockInsetInInset(bv, the_locking_inset);
1796
1797                         for (int i = 0; i < tabular->rows(); ++i) {
1798                                 tabular->GetCellInset(tabular->GetCellNumber(i, column))->
1799                                         resizeLyXText(bv);
1800                         }
1801                         updateLocal(bv, INIT);
1802                 }
1803         }
1804         break;
1805         case LyXTabular::SET_SPECIAL_COLUMN:
1806         case LyXTabular::SET_SPECIAL_MULTI:
1807                 tabular->SetAlignSpecial(actcell,value,feature);
1808                 updateLocal(bv, FULL);
1809                 break;
1810         case LyXTabular::APPEND_ROW:
1811                 // append the row into the tabular
1812                 unlockInsetInInset(bv, the_locking_inset);
1813                 tabular->AppendRow(bv->buffer()->params, actcell);
1814                 updateLocal(bv, INIT);
1815                 break;
1816         case LyXTabular::APPEND_COLUMN:
1817                 // append the column into the tabular
1818                 unlockInsetInInset(bv, the_locking_inset);
1819                 tabular->AppendColumn(bv->buffer()->params, actcell);
1820                 actcell = tabular->GetCellNumber(row, column);
1821                 updateLocal(bv, INIT);
1822                 break;
1823         case LyXTabular::DELETE_ROW:
1824                 unlockInsetInInset(bv, the_locking_inset);
1825                 for(int i = sel_row_start; i <= sel_row_end; ++i) {
1826                         tabular->DeleteRow(sel_row_start);
1827                 }
1828                 if (sel_row_start >= tabular->rows())
1829                         --sel_row_start;
1830                 actcell = tabular->GetCellNumber(sel_row_start, column);
1831                 clearSelection();
1832                 updateLocal(bv, INIT);
1833                 break;
1834         case LyXTabular::DELETE_COLUMN:
1835                 unlockInsetInInset(bv, the_locking_inset);
1836                 for(int i = sel_col_start; i <= sel_col_end; ++i) {
1837                         tabular->DeleteColumn(sel_col_start);
1838                 }
1839                 if (sel_col_start >= tabular->columns())
1840                         --sel_col_start;
1841                 actcell = tabular->GetCellNumber(row, sel_col_start);
1842                 clearSelection();
1843                 updateLocal(bv, INIT);
1844                 break;
1845         case LyXTabular::M_TOGGLE_LINE_TOP:
1846                 flag = false;
1847         case LyXTabular::TOGGLE_LINE_TOP:
1848         {
1849                 bool lineSet = !tabular->TopLine(actcell, flag);
1850                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1851                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1852                                 tabular->SetTopLine(
1853                                         tabular->GetCellNumber(i, j),
1854                                         lineSet, flag);
1855                 updateLocal(bv, INIT);
1856                 break;
1857         }
1858
1859         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1860                 flag = false;
1861         case LyXTabular::TOGGLE_LINE_BOTTOM:
1862         {
1863                 bool lineSet = !tabular->BottomLine(actcell, flag);
1864                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1865                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1866                                 tabular->SetBottomLine(
1867                                         tabular->GetCellNumber(i, j),
1868                                         lineSet,
1869                                         flag);
1870                 updateLocal(bv, INIT);
1871                 break;
1872         }
1873
1874         case LyXTabular::M_TOGGLE_LINE_LEFT:
1875                 flag = false;
1876         case LyXTabular::TOGGLE_LINE_LEFT:
1877         {
1878                 bool lineSet = !tabular->LeftLine(actcell, flag);
1879                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1880                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1881                                 tabular->SetLeftLine(
1882                                         tabular->GetCellNumber(i,j),
1883                                         lineSet,
1884                                         flag);
1885                 updateLocal(bv, INIT);
1886                 break;
1887         }
1888
1889         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1890                 flag = false;
1891         case LyXTabular::TOGGLE_LINE_RIGHT:
1892         {
1893                 bool lineSet = !tabular->RightLine(actcell, flag);
1894                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1895                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1896                                 tabular->SetRightLine(
1897                                         tabular->GetCellNumber(i,j),
1898                                         lineSet,
1899                                         flag);
1900                 updateLocal(bv, INIT);
1901                 break;
1902         }
1903
1904         case LyXTabular::M_ALIGN_LEFT:
1905         case LyXTabular::M_ALIGN_RIGHT:
1906         case LyXTabular::M_ALIGN_CENTER:
1907                 flag = false;
1908         case LyXTabular::ALIGN_LEFT:
1909         case LyXTabular::ALIGN_RIGHT:
1910         case LyXTabular::ALIGN_CENTER:
1911         case LyXTabular::ALIGN_BLOCK:
1912                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1913                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1914                                 tabular->SetAlignment(
1915                                         tabular->GetCellNumber(i, j),
1916                                         setAlign,
1917                                         flag);
1918                 updateLocal(bv, INIT);
1919                 break;
1920         case LyXTabular::M_VALIGN_TOP:
1921         case LyXTabular::M_VALIGN_BOTTOM:
1922         case LyXTabular::M_VALIGN_CENTER:
1923                 flag = false;
1924         case LyXTabular::VALIGN_TOP:
1925         case LyXTabular::VALIGN_BOTTOM:
1926         case LyXTabular::VALIGN_CENTER:
1927                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1928                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1929                                 tabular->SetVAlignment(
1930                                         tabular->GetCellNumber(i, j),
1931                                         setVAlign, flag);
1932                 updateLocal(bv, INIT);
1933                 break;
1934         case LyXTabular::MULTICOLUMN:
1935         {
1936                 if (sel_row_start != sel_row_end) {
1937 #ifdef WITH_WARNINGS
1938 #warning Need I say it ? This is horrible.
1939 #endif
1940                         Alert::error(_("Error setting multicolumn"),
1941                                    _("You cannot set multicolumn vertically."));
1942                         return;
1943                 }
1944                 // just multicol for one Single Cell
1945                 if (!hasSelection()) {
1946                         // check wether we are completly in a multicol
1947                         if (tabular->IsMultiColumn(actcell)) {
1948                                 tabular->UnsetMultiColumn(actcell);
1949                                 updateLocal(bv, INIT);
1950                         } else {
1951                                 tabular->SetMultiColumn(bv->buffer(), actcell, 1);
1952                                 updateLocal(bv, CELL);
1953                         }
1954                         break;
1955                 }
1956                 // we have a selection so this means we just add all this
1957                 // cells to form a multicolumn cell
1958                 int s_start;
1959                 int s_end;
1960
1961                 if (sel_cell_start > sel_cell_end) {
1962                         s_start = sel_cell_end;
1963                         s_end = sel_cell_start;
1964                 } else {
1965                         s_start = sel_cell_start;
1966                         s_end = sel_cell_end;
1967                 }
1968                 tabular->SetMultiColumn(bv->buffer(), s_start, s_end - s_start + 1);
1969                 actcell = s_start;
1970                 clearSelection();
1971                 updateLocal(bv, INIT);
1972                 break;
1973         }
1974         case LyXTabular::SET_ALL_LINES:
1975                 setLines = true;
1976         case LyXTabular::UNSET_ALL_LINES:
1977                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1978                         for (int j = sel_col_start; j <= sel_col_end; ++j)
1979                                 tabular->SetAllLines(
1980                                         tabular->GetCellNumber(i,j), setLines);
1981                 updateLocal(bv, INIT);
1982                 break;
1983         case LyXTabular::SET_LONGTABULAR:
1984                 tabular->SetLongTabular(true);
1985                 updateLocal(bv, INIT); // because this toggles displayed
1986                 break;
1987         case LyXTabular::UNSET_LONGTABULAR:
1988                 tabular->SetLongTabular(false);
1989                 updateLocal(bv, INIT); // because this toggles displayed
1990                 break;
1991         case LyXTabular::SET_ROTATE_TABULAR:
1992                 tabular->SetRotateTabular(true);
1993                 break;
1994         case LyXTabular::UNSET_ROTATE_TABULAR:
1995                 tabular->SetRotateTabular(false);
1996                 break;
1997         case LyXTabular::SET_ROTATE_CELL:
1998                 for (int i = sel_row_start; i <= sel_row_end; ++i)
1999                         for (int j = sel_col_start; j<=sel_col_end; ++j)
2000                                 tabular->SetRotateCell(
2001                                         tabular->GetCellNumber(i, j),
2002                                         true);
2003                 break;
2004         case LyXTabular::UNSET_ROTATE_CELL:
2005                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2006                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2007                                 tabular->SetRotateCell(
2008                                         tabular->GetCellNumber(i, j), false);
2009                 break;
2010         case LyXTabular::SET_USEBOX:
2011         {
2012                 LyXTabular::BoxType val = LyXTabular::BoxType(strToInt(value));
2013                 if (val == tabular->GetUsebox(actcell))
2014                         val = LyXTabular::BOX_NONE;
2015                 for (int i = sel_row_start; i <= sel_row_end; ++i)
2016                         for (int j = sel_col_start; j <= sel_col_end; ++j)
2017                                 tabular->SetUsebox(
2018                                         tabular->GetCellNumber(i, j), val);
2019                 break;
2020         }
2021         case LyXTabular::UNSET_LTFIRSTHEAD:
2022                 flag = false;
2023         case LyXTabular::SET_LTFIRSTHEAD:
2024                 (void)tabular->GetRowOfLTFirstHead(row, ltt);
2025                 checkLongtableSpecial(ltt, value, flag);
2026                 tabular->SetLTHead(row, flag, ltt, true);
2027                 break;
2028         case LyXTabular::UNSET_LTHEAD:
2029                 flag = false;
2030         case LyXTabular::SET_LTHEAD:
2031                 (void)tabular->GetRowOfLTHead(row, ltt);
2032                 checkLongtableSpecial(ltt, value, flag);
2033                 tabular->SetLTHead(row, flag, ltt, false);
2034                 break;
2035         case LyXTabular::UNSET_LTFOOT:
2036                 flag = false;
2037         case LyXTabular::SET_LTFOOT:
2038                 (void)tabular->GetRowOfLTFoot(row, ltt);
2039                 checkLongtableSpecial(ltt, value, flag);
2040                 tabular->SetLTFoot(row, flag, ltt, false);
2041                 break;
2042         case LyXTabular::UNSET_LTLASTFOOT:
2043                 flag = false;
2044         case LyXTabular::SET_LTLASTFOOT:
2045                 (void)tabular->GetRowOfLTLastFoot(row, ltt);
2046                 checkLongtableSpecial(ltt, value, flag);
2047                 tabular->SetLTFoot(row, flag, ltt, true);
2048                 break;
2049         case LyXTabular::SET_LTNEWPAGE:
2050         {
2051                 bool what = !tabular->GetLTNewPage(row);
2052                 tabular->SetLTNewPage(row, what);
2053                 break;
2054         }
2055         // dummy stuff just to avoid warnings
2056         case LyXTabular::LAST_ACTION:
2057                 break;
2058         }
2059
2060         InsetTabularMailer mailer(*this);
2061         mailer.updateDialog(bv);
2062 }
2063
2064
2065 bool InsetTabular::activateCellInset(BufferView * bv, int x, int y, mouse_button::state button,
2066                                      bool behind)
2067 {
2068         UpdatableInset * inset =
2069                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2070         LyXFont font(LyXFont::ALL_SANE);
2071         if (behind) {
2072                 x = inset->x() + inset->width(bv, font);
2073                 y = inset->descent(bv, font);
2074         }
2075         //inset_x = cursor.x() - top_x + tabular->GetBeginningOfTextInCell(actcell);
2076         //inset_y = cursor.y();
2077         inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, x,  y, button));
2078         if (!the_locking_inset)
2079                 return false;
2080         updateLocal(bv, CELL);
2081         return (the_locking_inset != 0);
2082 }
2083
2084
2085 bool InsetTabular::activateCellInsetAbs(BufferView * bv, int x, int y,
2086                                         mouse_button::state button)
2087 {
2088         inset_x = cursor_.x()
2089                 - top_x + tabular->GetBeginningOfTextInCell(actcell);
2090         inset_y = cursor_.y();
2091         return activateCellInset(bv, x - inset_x, y - inset_y, button);
2092 }
2093
2094
2095 bool InsetTabular::insetHit(BufferView *, int x, int) const
2096 {
2097         return (x + top_x)
2098                 > (cursor_.x() + tabular->GetBeginningOfTextInCell(actcell));
2099 }
2100
2101
2102 // This returns paperWidth() if the cell-width is unlimited or the width
2103 // in pixels if we have a pwidth for this cell.
2104 int InsetTabular::getMaxWidthOfCell(BufferView * bv, int cell) const
2105 {
2106         LyXLength const len = tabular->GetPWidth(cell);
2107
2108         if (len.zero())
2109                 return -1;
2110         return len.inPixels(latexTextWidth(bv));
2111 }
2112
2113
2114 int InsetTabular::getMaxWidth(BufferView * bv,
2115                               UpdatableInset const * inset) const
2116 {
2117         int cell = tabular->GetCellFromInset(inset, actcell);
2118
2119         if (cell == -1) {
2120                 lyxerr << "Own inset not found, shouldn't really happen!"
2121                        << endl;
2122                 return -1;
2123         }
2124
2125         int w = getMaxWidthOfCell(bv, cell);
2126         if (w > 0) {
2127                 // because the inset then subtracts it's top_x and owner->x()
2128                 w += (inset->x() - top_x);
2129         }
2130
2131         return w;
2132 }
2133
2134
2135 void InsetTabular::deleteLyXText(BufferView * bv, bool recursive) const
2136 {
2137         resizeLyXText(bv, recursive);
2138 }
2139
2140
2141 void InsetTabular::resizeLyXText(BufferView * bv, bool force) const
2142 {
2143         if (force) {
2144                 for(int i = 0; i < tabular->rows(); ++i) {
2145                         for(int j = 0; j < tabular->columns(); ++j) {
2146                                 tabular->GetCellInset(i, j)->resizeLyXText(bv, true);
2147                         }
2148                 }
2149         }
2150         need_update = FULL;
2151 }
2152
2153
2154 LyXText * InsetTabular::getLyXText(BufferView const * bv,
2155                                    bool const recursive) const
2156 {
2157         if (the_locking_inset)
2158                 return the_locking_inset->getLyXText(bv, recursive);
2159 #if 0
2160         // if we're locked lock the actual insettext and return it's LyXText!!!
2161         if (locked) {
2162                 UpdatableInset * inset =
2163                         static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2164                 inset->edit(const_cast<BufferView *>(bv), 0,  0, 0);
2165                 return the_locking_inset->getLyXText(bv, recursive);
2166         }
2167 #endif
2168         return Inset::getLyXText(bv, recursive);
2169 }
2170
2171
2172 bool InsetTabular::showInsetDialog(BufferView * bv) const
2173 {
2174         if (!the_locking_inset || !the_locking_inset->showInsetDialog(bv)) {
2175                 InsetTabular * tmp = const_cast<InsetTabular *>(this);
2176                 InsetTabularMailer mailer(*tmp);
2177                 mailer.showDialog(bv);
2178         }
2179         return true;
2180 }
2181
2182
2183 void InsetTabular::openLayoutDialog(BufferView * bv) const
2184 {
2185         if (the_locking_inset) {
2186                 InsetTabular * i = static_cast<InsetTabular *>
2187                         (the_locking_inset->getFirstLockingInsetOfType(TABULAR_CODE));
2188                 if (i) {
2189                         i->openLayoutDialog(bv);
2190                         return;
2191                 }
2192         }
2193         InsetTabular * tmp = const_cast<InsetTabular *>(this);
2194         InsetTabularMailer mailer(*tmp);
2195         mailer.showDialog(bv);
2196 }
2197
2198
2199 //
2200 // function returns an object as defined in func_status.h:
2201 // states OK, Unknown, Disabled, On, Off.
2202 //
2203 FuncStatus InsetTabular::getStatus(string const & what) const
2204 {
2205         int action = LyXTabular::LAST_ACTION;
2206         FuncStatus status;
2207
2208         int i = 0;
2209         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
2210                 string const tmp = tabularFeature[i].feature;
2211                 if (tmp == what.substr(0, tmp.length())) {
2212                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
2213                         //   tabularFeatures[i].feature.length())) {
2214                         action = tabularFeature[i].action;
2215                         break;
2216                 }
2217         }
2218         if (action == LyXTabular::LAST_ACTION) {
2219                 status.clear();
2220                 return status.unknown(true);
2221         }
2222
2223         string const argument = ltrim(what.substr(tabularFeature[i].feature.length()));
2224
2225         int sel_row_start;
2226         int sel_row_end;
2227         int dummy;
2228         LyXTabular::ltType dummyltt;
2229         bool flag = true;
2230
2231         if (hasSelection()) {
2232                 getSelection(sel_row_start, sel_row_end, dummy, dummy);
2233         } else {
2234                 sel_row_start = sel_row_end = tabular->row_of_cell(actcell);
2235         }
2236
2237         switch (action) {
2238         case LyXTabular::SET_PWIDTH:
2239         case LyXTabular::SET_MPWIDTH:
2240         case LyXTabular::SET_SPECIAL_COLUMN:
2241         case LyXTabular::SET_SPECIAL_MULTI:
2242         case LyXTabular::APPEND_ROW:
2243         case LyXTabular::APPEND_COLUMN:
2244         case LyXTabular::DELETE_ROW:
2245         case LyXTabular::DELETE_COLUMN:
2246         case LyXTabular::SET_ALL_LINES:
2247         case LyXTabular::UNSET_ALL_LINES:
2248                 return status.clear();
2249
2250         case LyXTabular::MULTICOLUMN:
2251                 status.setOnOff(tabular->IsMultiColumn(actcell));
2252                 break;
2253         case LyXTabular::M_TOGGLE_LINE_TOP:
2254                 flag = false;
2255         case LyXTabular::TOGGLE_LINE_TOP:
2256                 status.setOnOff(tabular->TopLine(actcell, flag));
2257                 break;
2258         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
2259                 flag = false;
2260         case LyXTabular::TOGGLE_LINE_BOTTOM:
2261                 status.setOnOff(tabular->BottomLine(actcell, flag));
2262                 break;
2263         case LyXTabular::M_TOGGLE_LINE_LEFT:
2264                 flag = false;
2265         case LyXTabular::TOGGLE_LINE_LEFT:
2266                 status.setOnOff(tabular->LeftLine(actcell, flag));
2267                 break;
2268         case LyXTabular::M_TOGGLE_LINE_RIGHT:
2269                 flag = false;
2270         case LyXTabular::TOGGLE_LINE_RIGHT:
2271                 status.setOnOff(tabular->RightLine(actcell, flag));
2272                 break;
2273         case LyXTabular::M_ALIGN_LEFT:
2274                 flag = false;
2275         case LyXTabular::ALIGN_LEFT:
2276                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_LEFT);
2277                 break;
2278         case LyXTabular::M_ALIGN_RIGHT:
2279                 flag = false;
2280         case LyXTabular::ALIGN_RIGHT:
2281                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_RIGHT);
2282                 break;
2283         case LyXTabular::M_ALIGN_CENTER:
2284                 flag = false;
2285         case LyXTabular::ALIGN_CENTER:
2286                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_CENTER);
2287                 break;
2288         case LyXTabular::ALIGN_BLOCK:
2289                 status.disabled(tabular->GetPWidth(actcell).zero());
2290                 status.setOnOff(tabular->GetAlignment(actcell, flag) == LYX_ALIGN_BLOCK);
2291                 break;
2292         case LyXTabular::M_VALIGN_TOP:
2293                 flag = false;
2294         case LyXTabular::VALIGN_TOP:
2295                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_TOP);
2296                 break;
2297         case LyXTabular::M_VALIGN_BOTTOM:
2298                 flag = false;
2299         case LyXTabular::VALIGN_BOTTOM:
2300                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_BOTTOM);
2301                 break;
2302         case LyXTabular::M_VALIGN_CENTER:
2303                 flag = false;
2304         case LyXTabular::VALIGN_CENTER:
2305                 status.setOnOff(tabular->GetVAlignment(actcell, flag) == LyXTabular::LYX_VALIGN_CENTER);
2306                 break;
2307         case LyXTabular::SET_LONGTABULAR:
2308                 status.setOnOff(tabular->IsLongTabular());
2309                 break;
2310         case LyXTabular::UNSET_LONGTABULAR:
2311                 status.setOnOff(!tabular->IsLongTabular());
2312                 break;
2313         case LyXTabular::SET_ROTATE_TABULAR:
2314                 status.setOnOff(tabular->GetRotateTabular());
2315                 break;
2316         case LyXTabular::UNSET_ROTATE_TABULAR:
2317                 status.setOnOff(!tabular->GetRotateTabular());
2318                 break;
2319         case LyXTabular::SET_ROTATE_CELL:
2320                 status.setOnOff(tabular->GetRotateCell(actcell));
2321                 break;
2322         case LyXTabular::UNSET_ROTATE_CELL:
2323                 status.setOnOff(!tabular->GetRotateCell(actcell));
2324                 break;
2325         case LyXTabular::SET_USEBOX:
2326                 status.setOnOff(strToInt(argument) == tabular->GetUsebox(actcell));
2327                 break;
2328         case LyXTabular::SET_LTFIRSTHEAD:
2329                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2330                 break;
2331         case LyXTabular::SET_LTHEAD:
2332                 status.setOnOff(tabular->GetRowOfLTHead(sel_row_start, dummyltt));
2333                 break;
2334         case LyXTabular::SET_LTFOOT:
2335                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2336                 break;
2337         case LyXTabular::SET_LTLASTFOOT:
2338                 status.setOnOff(tabular->GetRowOfLTFoot(sel_row_start, dummyltt));
2339                 break;
2340         case LyXTabular::SET_LTNEWPAGE:
2341                 status.setOnOff(tabular->GetLTNewPage(sel_row_start));
2342                 break;
2343         default:
2344                 status.clear();
2345                 status.disabled(true);
2346                 break;
2347         }
2348         return status;
2349 }
2350
2351
2352 vector<string> const InsetTabular::getLabelList() const
2353 {
2354         return tabular->getLabelList();
2355 }
2356
2357
2358 bool InsetTabular::copySelection(BufferView * bv)
2359 {
2360         if (!hasSelection())
2361                 return false;
2362
2363         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2364         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2365         if (sel_col_start > sel_col_end) {
2366                 sel_col_start = sel_col_end;
2367                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2368         } else {
2369                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2370         }
2371         int const columns = sel_col_end - sel_col_start + 1;
2372
2373         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2374         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2375         if (sel_row_start > sel_row_end) {
2376                 swap(sel_row_start, sel_row_end);
2377         }
2378         int const rows = sel_row_end - sel_row_start + 1;
2379
2380         delete paste_tabular;
2381         paste_tabular = new LyXTabular(bv->buffer()->params,
2382                                        this, *tabular); // rows, columns);
2383         for (int i = 0; i < sel_row_start; ++i)
2384                 paste_tabular->DeleteRow(0);
2385         while (paste_tabular->rows() > rows)
2386                 paste_tabular->DeleteRow(rows);
2387         paste_tabular->SetTopLine(0, true, true);
2388         paste_tabular->SetBottomLine(paste_tabular->GetFirstCellInRow(rows - 1),
2389                                      true, true);
2390         for (int i = 0; i < sel_col_start; ++i)
2391                 paste_tabular->DeleteColumn(0);
2392         while (paste_tabular->columns() > columns)
2393                 paste_tabular->DeleteColumn(columns);
2394         paste_tabular->SetLeftLine(0, true, true);
2395         paste_tabular->SetRightLine(paste_tabular->GetLastCellInRow(0),
2396                                     true, true);
2397
2398         ostringstream sstr;
2399         paste_tabular->ascii(bv->buffer(), sstr,
2400                              (int)parOwner()->params().depth(), true, '\t');
2401         bv->stuffClipboard(STRCONV(sstr.str()));
2402         return true;
2403 }
2404
2405
2406 bool InsetTabular::pasteSelection(BufferView * bv)
2407 {
2408         if (!paste_tabular)
2409                 return false;
2410
2411         for (int r1 = 0, r2 = actrow;
2412              (r1 < paste_tabular->rows()) && (r2 < tabular->rows());
2413              ++r1, ++r2) {
2414                 for(int c1 = 0, c2 = actcol;
2415                     (c1 < paste_tabular->columns()) && (c2 < tabular->columns());
2416                     ++c1, ++c2) {
2417                         if (paste_tabular->IsPartOfMultiColumn(r1,c1) &&
2418                             tabular->IsPartOfMultiColumn(r2,c2))
2419                                 continue;
2420                         if (paste_tabular->IsPartOfMultiColumn(r1,c1)) {
2421                                 --c2;
2422                                 continue;
2423                         }
2424                         if (tabular->IsPartOfMultiColumn(r2,c2)) {
2425                                 --c1;
2426                                 continue;
2427                         }
2428                         int const n1 = paste_tabular->GetCellNumber(r1, c1);
2429                         int const n2 = tabular->GetCellNumber(r2, c2);
2430                         *(tabular->GetCellInset(n2)) = *(paste_tabular->GetCellInset(n1));
2431                         tabular->GetCellInset(n2)->setOwner(this);
2432                         tabular->GetCellInset(n2)->deleteLyXText(bv);
2433                         tabular->GetCellInset(n2)->markNew();
2434                 }
2435         }
2436         return true;
2437 }
2438
2439
2440 bool InsetTabular::cutSelection(BufferParams const & bp)
2441 {
2442         if (!hasSelection())
2443                 return false;
2444
2445         int sel_col_start = tabular->column_of_cell(sel_cell_start);
2446         int sel_col_end = tabular->column_of_cell(sel_cell_end);
2447         if (sel_col_start > sel_col_end) {
2448                 sel_col_start = sel_col_end;
2449                 sel_col_end = tabular->right_column_of_cell(sel_cell_start);
2450         } else {
2451                 sel_col_end = tabular->right_column_of_cell(sel_cell_end);
2452         }
2453         int sel_row_start = tabular->row_of_cell(sel_cell_start);
2454         int sel_row_end = tabular->row_of_cell(sel_cell_end);
2455         if (sel_row_start > sel_row_end) {
2456                 swap(sel_row_start, sel_row_end);
2457         }
2458         if (sel_cell_start > sel_cell_end) {
2459                 swap(sel_cell_start, sel_cell_end);
2460         }
2461         for (int i = sel_row_start; i <= sel_row_end; ++i) {
2462                 for (int j = sel_col_start; j <= sel_col_end; ++j) {
2463                         tabular->GetCellInset(tabular->GetCellNumber(i, j))->clear(bp.tracking_changes);
2464                 }
2465         }
2466         return true;
2467 }
2468
2469
2470 bool InsetTabular::isRightToLeft(BufferView * bv)
2471 {
2472         return bv->getParentLanguage(this)->RightToLeft();
2473 }
2474
2475
2476 bool InsetTabular::nodraw() const
2477 {
2478         if (!UpdatableInset::nodraw() && the_locking_inset)
2479                 return the_locking_inset->nodraw();
2480         return UpdatableInset::nodraw();
2481 }
2482
2483
2484 int InsetTabular::scroll(bool recursive) const
2485 {
2486         int sx = UpdatableInset::scroll(false);
2487
2488         if (recursive && the_locking_inset)
2489                 sx += the_locking_inset->scroll(recursive);
2490
2491         return sx;
2492 }
2493
2494
2495 void InsetTabular::getSelection(int & srow, int & erow,
2496                                 int & scol, int & ecol) const
2497 {
2498         int const start = hasSelection() ? sel_cell_start : actcell;
2499         int const end = hasSelection() ? sel_cell_end : actcell;
2500
2501         srow = tabular->row_of_cell(start);
2502         erow = tabular->row_of_cell(end);
2503         if (srow > erow) {
2504                 swap(srow, erow);
2505         }
2506
2507         scol = tabular->column_of_cell(start);
2508         ecol = tabular->column_of_cell(end);
2509         if (scol > ecol) {
2510                 swap(scol, ecol);
2511         } else {
2512                 ecol = tabular->right_column_of_cell(end);
2513         }
2514 }
2515
2516
2517 ParagraphList * InsetTabular::getParagraphs(int i) const
2518 {
2519         return (i < tabular->GetNumberOfCells())
2520                 ? tabular->GetCellInset(i)->getParagraphs(0)
2521                 : 0;
2522 }
2523
2524
2525 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
2526 {
2527         if (the_locking_inset)
2528                 return the_locking_inset->cursor(bv);
2529         return Inset::cursor(bv);
2530 }
2531
2532
2533 Inset * InsetTabular::getInsetFromID(int id_arg) const
2534 {
2535         if (id_arg == id())
2536                 return const_cast<InsetTabular *>(this);
2537
2538         Inset * result;
2539         for(int i = 0; i < tabular->rows(); ++i) {
2540                 for(int j = 0; j < tabular->columns(); ++j) {
2541                         if ((result = tabular->GetCellInset(i, j)->getInsetFromID(id_arg)))
2542                                 return result;
2543                 }
2544         }
2545         return 0;
2546 }
2547
2548
2549 WordLangTuple const
2550 InsetTabular::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2551 {
2552         nodraw(true);
2553         if (the_locking_inset) {
2554                 WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2555                 if (!word.word().empty()) {
2556                         nodraw(false);
2557                         return word;
2558                 }
2559                 if (tabular->IsLastCell(actcell)) {
2560                         bv->unlockInset(const_cast<InsetTabular *>(this));
2561                         nodraw(false);
2562                         return WordLangTuple();
2563                 }
2564                 ++actcell;
2565         }
2566         // otherwise we have to lock the next inset and ask for it's selecttion
2567         UpdatableInset * inset =
2568                 static_cast<UpdatableInset*>(tabular->GetCellInset(actcell));
2569         inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2570         WordLangTuple word(selectNextWordInt(bv, value));
2571         nodraw(false);
2572         if (!word.word().empty())
2573                 resetPos(bv);
2574         return word;
2575 }
2576
2577
2578 WordLangTuple InsetTabular::selectNextWordInt(BufferView * bv, float & value) const
2579 {
2580         // when entering this function the inset should be ALWAYS locked!
2581         lyx::Assert(the_locking_inset);
2582
2583         WordLangTuple word(the_locking_inset->selectNextWordToSpellcheck(bv, value));
2584         if (!word.word().empty())
2585                 return word;
2586
2587         if (tabular->IsLastCell(actcell)) {
2588                 bv->unlockInset(const_cast<InsetTabular *>(this));
2589                 return WordLangTuple();
2590         }
2591
2592         // otherwise we have to lock the next inset and ask for it's selecttion
2593         UpdatableInset * inset =
2594                 static_cast<UpdatableInset*>(tabular->GetCellInset(++actcell));
2595         inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
2596         return selectNextWordInt(bv, value);
2597 }
2598
2599
2600 void InsetTabular::selectSelectedWord(BufferView * bv)
2601 {
2602         if (the_locking_inset) {
2603                 the_locking_inset->selectSelectedWord(bv);
2604                 return;
2605         }
2606         return;
2607 }
2608
2609
2610 void InsetTabular::toggleSelection(BufferView * bv, bool kill_selection)
2611 {
2612         if (the_locking_inset) {
2613                 the_locking_inset->toggleSelection(bv, kill_selection);
2614         }
2615 }
2616
2617
2618 void InsetTabular::markErased()
2619 {
2620         int cell = 0;
2621
2622         while (!tabular->IsLastCell(cell)) {
2623                 ++cell;
2624                 InsetText * inset = tabular->GetCellInset(cell);
2625                 inset->markErased();
2626         }
2627 }
2628
2629
2630 bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
2631 {
2632         if (the_locking_inset) {
2633                 if (the_locking_inset->nextChange(bv, length)) {
2634                         updateLocal(bv, CELL);
2635                         return true;
2636                 }
2637                 if (tabular->IsLastCell(actcell))
2638                         return false;
2639                 ++actcell;
2640         }
2641         InsetText * inset = tabular->GetCellInset(actcell);
2642         if (inset->nextChange(bv, length)) {
2643                 updateLocal(bv, FULL);
2644                 return true;
2645         }
2646         while (!tabular->IsLastCell(actcell)) {
2647                 ++actcell;
2648                 inset = tabular->GetCellInset(actcell);
2649                 if (inset->nextChange(bv, length)) {
2650                         updateLocal(bv, FULL);
2651                         return true;
2652                 }
2653         }
2654         return false;
2655 }
2656
2657
2658 bool InsetTabular::searchForward(BufferView * bv, string const & str,
2659                                  bool cs, bool mw)
2660 {
2661         int cell = 0;
2662         if (the_locking_inset) {
2663                 if (the_locking_inset->searchForward(bv, str, cs, mw)) {
2664                         updateLocal(bv, CELL);
2665                         return true;
2666                 }
2667                 if (tabular->IsLastCell(actcell))
2668                         return false;
2669                 cell = actcell + 1;
2670         }
2671         InsetText * inset = tabular->GetCellInset(cell);
2672         if (inset->searchForward(bv, str, cs, mw)) {
2673                 updateLocal(bv, FULL);
2674                 return true;
2675         }
2676         while (!tabular->IsLastCell(cell)) {
2677                 ++cell;
2678                 inset = tabular->GetCellInset(cell);
2679                 if (inset->searchForward(bv, str, cs, mw)) {
2680                         updateLocal(bv, FULL);
2681                         return true;
2682                 }
2683         }
2684         return false;
2685 }
2686
2687
2688 bool InsetTabular::searchBackward(BufferView * bv, string const & str,
2689                                bool cs, bool mw)
2690 {
2691         int cell = tabular->GetNumberOfCells();
2692         if (the_locking_inset) {
2693                 if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
2694                         updateLocal(bv, CELL);
2695                         return true;
2696                 }
2697                 cell = actcell;
2698         }
2699
2700         while (cell) {
2701                 --cell;
2702                 InsetText * inset = tabular->GetCellInset(cell);
2703                 if (inset->searchBackward(bv, str, cs, mw)) {
2704                         updateLocal(bv, CELL);
2705                         return true;
2706                 }
2707         }
2708         return false;
2709 }
2710
2711
2712 bool InsetTabular::insetAllowed(Inset::Code code) const
2713 {
2714         if (the_locking_inset)
2715                 return the_locking_inset->insetAllowed(code);
2716         // we return true here because if the inset is not locked someone
2717         // wants to insert something in one of our insettexts and we generally
2718         // allow to do so.
2719         return true;
2720 }
2721
2722
2723 bool InsetTabular::forceDefaultParagraphs(Inset const * in) const
2724 {
2725         const int cell = tabular->GetCellFromInset(in, actcell);
2726
2727         if (cell != -1)
2728                 return tabular->GetPWidth(cell).zero();
2729
2730         // well we didn't obviously find it so maybe our owner knows more
2731         if (owner())
2732                 return owner()->forceDefaultParagraphs(in);
2733         // if we're here there is really something strange going on!!!
2734         return false;
2735 }
2736
2737 bool InsetTabular::insertAsciiString(BufferView * bv, string const & buf,
2738                                      bool usePaste)
2739 {
2740         if (buf.length() <= 0)
2741                 return true;
2742
2743         int cols = 1;
2744         int rows = 1;
2745         int maxCols = 1;
2746         string::size_type len = buf.length();
2747         string::size_type p = 0;
2748
2749         while (p < len &&
2750                ((p = buf.find_first_of("\t\n", p)) != string::npos))
2751         {
2752                 switch (buf[p]) {
2753                 case '\t':
2754                         ++cols;
2755                         break;
2756                 case '\n':
2757                         if ((p+1) < len)
2758                                 ++rows;
2759                         maxCols = max(cols, maxCols);
2760                         cols = 1;
2761                         break;
2762                 }
2763                 ++p;
2764         }
2765         maxCols = max(cols, maxCols);
2766         LyXTabular * loctab;
2767         int cell = 0;
2768         int ocol = 0;
2769         int row = 0;
2770         if (usePaste) {
2771                 delete paste_tabular;
2772                 paste_tabular = new LyXTabular(bv->buffer()->params,
2773                                                this, rows, maxCols);
2774                 loctab = paste_tabular;
2775                 cols = 0;
2776         } else {
2777                 loctab = tabular.get();
2778                 cell = actcell;
2779                 ocol = actcol;
2780                 row = actrow;
2781         }
2782
2783         string::size_type op = 0;
2784         int cells = loctab->GetNumberOfCells();
2785         p = 0;
2786         cols = ocol;
2787         rows = loctab->rows();
2788         int const columns = loctab->columns();
2789
2790         while ((cell < cells) && (p < len) && (row < rows) &&
2791                (p = buf.find_first_of("\t\n", p)) != string::npos)
2792         {
2793                 if (p >= len)
2794                         break;
2795                 switch (buf[p]) {
2796                 case '\t':
2797                         // we can only set this if we are not too far right
2798                         if (cols < columns) {
2799                                 InsetText * ti = loctab->GetCellInset(cell);
2800                                 LyXFont const font = ti->getLyXText(bv)->
2801                                         getFont(bv->buffer(), ti->paragraphs.begin(), 0);
2802                                 ti->setText(buf.substr(op, p - op), font);
2803                                 ++cols;
2804                                 ++cell;
2805                         }
2806                         break;
2807                 case '\n':
2808                         // we can only set this if we are not too far right
2809                         if (cols < columns) {
2810                                 InsetText * ti = loctab->GetCellInset(cell);
2811                                 LyXFont const font = ti->getLyXText(bv)->
2812                                         getFont(bv->buffer(), ti->paragraphs.begin(), 0);
2813                                 ti->setText(buf.substr(op, p - op), font);
2814                         }
2815                         cols = ocol;
2816                         ++row;
2817                         if (row < rows)
2818                                 cell = loctab->GetCellNumber(row, cols);
2819                         break;
2820                 }
2821                 ++p;
2822                 op = p;
2823         }
2824         // check for the last cell if there is no trailing '\n'
2825         if ((cell < cells) && (op < len)) {
2826                 InsetText * ti = loctab->GetCellInset(cell);
2827                 LyXFont const font = ti->getLyXText(bv)->
2828                         getFont(bv->buffer(), ti->paragraphs.begin(), 0);
2829                 ti->setText(buf.substr(op, len - op), font);
2830         }
2831
2832         return true;
2833 }
2834
2835
2836 void InsetTabular::addPreview(grfx::PreviewLoader & loader) const
2837 {
2838         int const rows = tabular->rows();
2839         int const columns = tabular->columns();
2840         for (int i = 0; i < rows; ++i) {
2841                 for (int j = 0; j < columns; ++j) {
2842                         tabular->GetCellInset(i,j)->addPreview(loader);
2843                 }
2844         }
2845 }
2846
2847
2848 string const InsetTabularMailer:: name_("tabular");
2849
2850 InsetTabularMailer::InsetTabularMailer(InsetTabular & inset)
2851         : inset_(inset)
2852 {}
2853
2854
2855 string const InsetTabularMailer::inset2string() const
2856 {
2857         return params2string(inset_);
2858 }
2859
2860
2861 int InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
2862 {
2863         istringstream data(STRCONV(in));
2864         LyXLex lex(0,0);
2865         lex.setStream(data);
2866
2867 #warning CHECK verify that this is a sane value to return.
2868         if (in.empty())
2869                 return -1;
2870
2871         if (lex.isOK()) {
2872                 lex.next();
2873                 string const token = lex.getString();
2874                 if (token != name_)
2875                         return -1;
2876         }
2877
2878         int cell = -1;
2879         if (lex.isOK()) {
2880                 lex.next();
2881                 string const token = lex.getString();
2882                 if (token != "\\active_cell")
2883                         return -1;
2884                 lex.next();
2885                 cell = lex.getInteger();
2886         }
2887
2888         // This is part of the inset proper that is usually swallowed
2889         // by Buffer::readInset
2890         if (lex.isOK()) {
2891                 lex.next();
2892                 string const token = lex.getString();
2893                 if (token != "Tabular")
2894                         return -1;
2895         }
2896
2897         if (!lex.isOK())
2898                 return -1;
2899
2900         // FIXME: even current_view would be better than this.
2901         BufferView * const bv = inset.view();
2902         Buffer const * const buffer = bv ? bv->buffer() : 0;
2903         if (buffer)
2904                 inset.read(buffer, lex);
2905
2906         // We can't set the active cell, but we can tell the frontend
2907         // what it is.
2908         return cell;
2909 }
2910
2911
2912 string const InsetTabularMailer::params2string(InsetTabular const & inset)
2913 {
2914         // FIXME: even current_view would be better than this.
2915         BufferView * const bv = inset.view();
2916         Buffer const * const buffer = bv ? bv->buffer() : 0;
2917         if (!buffer)
2918                 return string();
2919
2920         ostringstream data;
2921         data << name_ << " \\active_cell " << inset.getActCell() << '\n';
2922         inset.write(buffer, data);
2923         data << "\\end_inset\n";
2924         return STRCONV(data.str());
2925 }