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