]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
IU of drawing phase one without 'semantic changes' as requested by John
[lyx.git] / src / mathed / math_gridinset.C
1 #include "math_gridinset.h"
2 #include "math_mathmlstream.h"
3 #include "math_streamstr.h"
4 #include "lyxfont.h"
5 #include "funcrequest.h"
6 #include "frontends/Painter.h"
7 #include "debug.h"
8 #include "Lsstream.h"
9
10
11 #include "insets/mailinset.h"
12
13 using std::swap;
14 using std::max;
15 using std::min;
16 using std::vector;
17 using std::istream;
18
19
20 class GridInsetMailer : public MailInset {
21 public:
22         GridInsetMailer(MathGridInset & inset) : inset_(inset) {}
23         ///
24         virtual string const & name() const
25         {
26                 static const string theName = "tabular";
27                 return theName;
28         }
29         ///
30         virtual string const inset2string() const
31         {
32                 ostringstream data;
33                 //data << name() << " active_cell " << inset.getActCell() << '\n';
34                 data << name() << " active_cell " << 0 << '\n';
35                 WriteStream ws(data);
36                 inset_.write(ws);
37                 return STRCONV(data.str());
38         }
39
40 protected:
41         InsetBase & inset() const { return inset_; }
42         MathGridInset & inset_; 
43 };
44
45
46 void mathed_parse_normal(MathGridInset &, string const & argument);
47
48 namespace {
49
50 string verboseHLine(int n)
51 {
52         string res;
53         for (int i = 0; i < n; ++i)
54                 res += "\\hline";
55         if (n)
56                 res += ' ';
57         return res;
58 }
59
60
61 int extractInt(istream & is)
62 {
63         int num = 1;
64         is >> num;
65         return (num == 0) ? 1 : num;
66 }
67
68 }
69
70
71 //////////////////////////////////////////////////////////////
72
73
74 MathGridInset::CellInfo::CellInfo()
75         : dummy_(false)
76 {}
77
78
79
80
81 //////////////////////////////////////////////////////////////
82
83
84 MathGridInset::RowInfo::RowInfo()
85         : lines_(0), skip_(0)
86 {}
87
88
89
90 int MathGridInset::RowInfo::skipPixels() const
91 {
92         return crskip_.inBP();
93 }
94
95
96
97 //////////////////////////////////////////////////////////////
98
99
100 MathGridInset::ColInfo::ColInfo()
101         : align_('c'), leftline_(false), rightline_(false), lines_(0)
102 {}
103
104
105 //////////////////////////////////////////////////////////////
106
107
108 MathGridInset::MathGridInset(char v, string const & h)
109         : MathNestInset(guessColumns(h)),
110           rowinfo_(2),
111           colinfo_(guessColumns(h) + 1),
112           cellinfo_(1 * guessColumns(h))
113 {
114         setDefaults();
115         valign(v);
116         halign(h);
117         //lyxerr << "created grid with " << ncols() << " columns\n";
118 }
119
120
121 MathGridInset::MathGridInset()
122         : MathNestInset(1),
123           rowinfo_(1 + 1),
124                 colinfo_(1 + 1),
125                 cellinfo_(1),
126                 v_align_('c')
127 {
128         setDefaults();
129 }
130
131
132 MathGridInset::MathGridInset(col_type m, row_type n)
133         : MathNestInset(m * n),
134           rowinfo_(n + 1),
135                 colinfo_(m + 1),
136                 cellinfo_(m * n),
137                 v_align_('c')
138 {
139         setDefaults();
140 }
141
142
143 MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
144         : MathNestInset(m * n),
145           rowinfo_(n + 1),
146           colinfo_(m + 1),
147                 cellinfo_(m * n),
148                 v_align_(v)
149 {
150         setDefaults();
151         valign(v);
152         halign(h);
153 }
154
155
156 MathGridInset::~MathGridInset()
157 {
158         GridInsetMailer mailer(*this);
159         mailer.hideDialog();
160 }
161
162
163 MathInset * MathGridInset::clone() const
164 {
165         return new MathGridInset(*this);
166 }
167
168
169 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
170 {
171         return col + ncols() * row;
172 }
173
174
175 void MathGridInset::setDefaults()
176 {
177         if (ncols() <= 0)
178                 lyxerr << "positive number of columns expected\n";
179         //if (nrows() <= 0)
180         //      lyxerr << "positive number of rows expected\n";
181         for (col_type col = 0; col < ncols(); ++col) {
182                 colinfo_[col].align_ = defaultColAlign(col);
183                 colinfo_[col].skip_  = defaultColSpace(col);
184         }
185 }
186
187
188 void MathGridInset::halign(string const & hh)
189 {
190         col_type col = 0;
191         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
192                 if (col >= ncols())
193                         break;
194                 char c = *it;
195                 if (c == '|') {
196                         colinfo_[col].lines_++;
197                 } else if (c == 'c' || c == 'l' || c == 'r') {
198                         colinfo_[col].align_ = c;
199                         ++col;
200                         colinfo_[col].lines_ = 0;
201                 } else {
202                         lyxerr << "unknown column separator: '" << c << "'\n";
203                 }
204         }
205
206 /*
207         col_type n = hh.size();
208         if (n > ncols())
209                 n = ncols();
210         for (col_type col = 0; col < n; ++col)
211                 colinfo_[col].align_ = hh[col];
212 */
213 }
214
215
216 MathGridInset::col_type MathGridInset::guessColumns(string const & hh) const
217 {
218         col_type col = 0;
219         for (string::const_iterator it = hh.begin(); it != hh.end(); ++it)
220                 if (*it == 'c' || *it == 'l' || *it == 'r')
221                         ++col;
222         // let's have at least one column, even if we did not recognize its
223         // alignment
224         if (col == 0)
225                 col = 1;
226         return col;
227 }
228
229
230 void MathGridInset::halign(char h, col_type col)
231 {
232         colinfo_[col].align_ = h;
233 }
234
235
236 char MathGridInset::halign(col_type col) const
237 {
238         return colinfo_[col].align_;
239 }
240
241
242 string MathGridInset::halign() const
243 {
244         string res;
245         for (col_type col = 0; col < ncols(); ++col) {
246                 res += string(colinfo_[col].lines_, '|');
247                 res += colinfo_[col].align_;
248         }
249         return res + string(colinfo_[ncols()].lines_, '|');
250 }
251
252
253 void MathGridInset::valign(char c)
254 {
255         v_align_ = c;
256 }
257
258
259 char MathGridInset::valign() const
260 {
261         return v_align_;
262 }
263
264
265 MathGridInset::col_type MathGridInset::ncols() const
266 {
267         return colinfo_.size() - 1;
268 }
269
270
271 MathGridInset::row_type MathGridInset::nrows() const
272 {
273         return rowinfo_.size() - 1;
274 }
275
276
277 MathGridInset::col_type MathGridInset::col(idx_type idx) const
278 {
279         return idx % ncols();
280 }
281
282
283 MathGridInset::row_type MathGridInset::row(idx_type idx) const
284 {
285         return idx / ncols();
286 }
287
288
289 void MathGridInset::vcrskip(LyXLength const & crskip, row_type row)
290 {
291         rowinfo_[row].crskip_ = crskip;
292 }
293
294
295 LyXLength MathGridInset::vcrskip(row_type row) const
296 {
297         return rowinfo_[row].crskip_;
298 }
299
300
301 void MathGridInset::metrics(MetricsInfo & mi) const
302 {
303         // let the cells adjust themselves
304         MathNestInset::metrics(mi);
305
306         // compute absolute sizes of vertical structure
307         for (row_type row = 0; row < nrows(); ++row) {
308                 int asc  = 0;
309                 int desc = 0;
310                 for (col_type col = 0; col < ncols(); ++col) {
311                         MathArray const & c = cell(index(row, col));
312                         asc  = max(asc,  c.ascent());
313                         desc = max(desc, c.descent());
314                 }
315                 rowinfo_[row].ascent_  = asc;
316                 rowinfo_[row].descent_ = desc;
317         }
318         rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
319         rowinfo_[nrows()].ascent_  = 0;
320         rowinfo_[nrows()].descent_ = 0;
321
322         // compute vertical offsets
323         rowinfo_[0].offset_ = 0;
324         for (row_type row = 1; row <= nrows(); ++row) {
325                 rowinfo_[row].offset_  =
326                         rowinfo_[row - 1].offset_  +
327                         rowinfo_[row - 1].descent_ +
328                         rowinfo_[row - 1].skipPixels() +
329                         rowsep() +
330                         rowinfo_[row].lines_ * hlinesep() +
331                         rowinfo_[row].ascent_;
332         }
333
334         // adjust vertical offset
335         int h = 0;
336         switch (v_align_) {
337                 case 't':
338                         h = 0;
339                         break;
340                 case 'b':
341                         h = rowinfo_[nrows() - 1].offset_;
342                         break;
343                 default:
344                         h = rowinfo_[nrows() - 1].offset_ / 2;
345         }
346         for (row_type row = 0; row <= nrows(); ++row)
347                 rowinfo_[row].offset_ -= h;
348
349
350         // compute absolute sizes of horizontal structure
351         for (col_type col = 0; col < ncols(); ++col) {
352                 int wid = 0;
353                 for (row_type row = 0; row < nrows(); ++row)
354                         wid = max(wid, cell(index(row, col)).width());
355                 colinfo_[col].width_ = wid;
356         }
357         colinfo_[ncols()].width_  = 0;
358
359         // compute horizontal offsets
360         colinfo_[0].offset_ = border();
361         for (col_type col = 1; col <= ncols(); ++col) {
362                 colinfo_[col].offset_ =
363                         colinfo_[col - 1].offset_ +
364                         colinfo_[col - 1].width_ +
365                         colinfo_[col - 1].skip_ +
366                         colsep() +
367                         colinfo_[col].lines_ * vlinesep();
368         }
369
370
371         dim_.wid   =   colinfo_[ncols() - 1].offset_
372                        + colinfo_[ncols() - 1].width_
373                  + vlinesep() * colinfo_[ncols()].lines_
374                        + border();
375
376         dim_.asc  = - rowinfo_[0].offset_
377                        + rowinfo_[0].ascent_
378                  + hlinesep() * rowinfo_[0].lines_
379                        + border();
380
381         dim_.des =   rowinfo_[nrows() - 1].offset_
382                        + rowinfo_[nrows() - 1].descent_
383                  + hlinesep() * rowinfo_[nrows()].lines_
384                        + border();
385
386
387 /*
388         // Increase ws_[i] for 'R' columns (except the first one)
389         for (int i = 1; i < nc_; ++i)
390                 if (align_[i] == 'R')
391                         ws_[i] += 10 * df_width;
392         // Increase ws_[i] for 'C' column
393         if (align_[0] == 'C')
394                 if (ws_[0] < 7 * workwidth / 8)
395                         ws_[0] = 7 * workwidth / 8;
396
397         // Adjust local tabs
398         width = colsep();
399         for (cxrow = row_.begin(); cxrow; ++cxrow) {
400                 int rg = COLSEP;
401                 int lf = 0;
402                 for (int i = 0; i < nc_; ++i) {
403                         bool isvoid = false;
404                         if (cxrow->getTab(i) <= 0) {
405                                 cxrow->setTab(i, df_width);
406                                 isvoid = true;
407                         }
408                         switch (align_[i]) {
409                         case 'l':
410                                 lf = 0;
411                                 break;
412                         case 'c':
413                                 lf = (ws_[i] - cxrow->getTab(i))/2;
414                                 break;
415                         case 'r':
416                         case 'R':
417                                 lf = ws_[i] - cxrow->getTab(i);
418                                 break;
419                         case 'C':
420                                 if (cxrow == row_.begin())
421                                         lf = 0;
422                                 else if (cxrow.is_last())
423                                         lf = ws_[i] - cxrow->getTab(i);
424                                 else
425                                         lf = (ws_[i] - cxrow->getTab(i))/2;
426                                 break;
427                         }
428                         int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
429                         cxrow->setTab(i, lf + rg);
430                         rg = ws_[i] - ww + colsep();
431                         if (cxrow == row_.begin())
432                                 width += ws_[i] + colsep();
433                 }
434                 cxrow->setBaseline(cxrow->getBaseline() - ascent);
435         }
436 */
437 }
438
439
440 void MathGridInset::metrics(MetricsInfo & mi, Dimension & dim) const
441 {
442         metrics(mi);
443         dim = dim_;
444 }
445
446
447 void MathGridInset::draw(PainterInfo & pi, int x, int y) const
448 {
449         for (idx_type idx = 0; idx < nargs(); ++idx)
450                 cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
451
452         for (row_type row = 0; row <= nrows(); ++row)
453                 for (int i = 0; i < rowinfo_[row].lines_; ++i) {
454                         int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
455                                 - i * hlinesep() - hlinesep()/2 - rowsep()/2;
456                         pi.pain.line(x + 1, yy, x + dim_.width() - 1, yy);
457                 }
458
459         for (col_type col = 0; col <= ncols(); ++col)
460                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
461                         int xx = x + colinfo_[col].offset_
462                                 - i * vlinesep() - vlinesep()/2 - colsep()/2;
463                         pi.pain.line(xx, y - dim_.ascent() + 1, xx, y + dim_.descent() - 1);
464                 }
465 }
466
467
468 void MathGridInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
469 {
470         // let the cells adjust themselves
471         //MathNestInset::metrics(mi);
472         for (idx_type i = 0; i < nargs(); ++i)
473                 cell(i).metricsT(mi, dim);
474
475         // compute absolute sizes of vertical structure
476         for (row_type row = 0; row < nrows(); ++row) {
477                 int asc  = 0;
478                 int desc = 0;
479                 for (col_type col = 0; col < ncols(); ++col) {
480                         MathArray const & c = cell(index(row, col));
481                         asc  = max(asc,  c.ascent());
482                         desc = max(desc, c.descent());
483                 }
484                 rowinfo_[row].ascent_  = asc;
485                 rowinfo_[row].descent_ = desc;
486         }
487         //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
488         rowinfo_[nrows()].ascent_  = 0;
489         rowinfo_[nrows()].descent_ = 0;
490
491         // compute vertical offsets
492         rowinfo_[0].offset_ = 0;
493         for (row_type row = 1; row <= nrows(); ++row) {
494                 rowinfo_[row].offset_  =
495                         rowinfo_[row - 1].offset_  +
496                         rowinfo_[row - 1].descent_ +
497                         //rowinfo_[row - 1].skipPixels() +
498                         1 + //rowsep() +
499                         //rowinfo_[row].lines_ * hlinesep() +
500                         rowinfo_[row].ascent_;
501         }
502
503         // adjust vertical offset
504         int h = 0;
505         switch (v_align_) {
506                 case 't':
507                         h = 0;
508                         break;
509                 case 'b':
510                         h = rowinfo_[nrows() - 1].offset_;
511                         break;
512                 default:
513                         h = rowinfo_[nrows() - 1].offset_ / 2;
514         }
515         for (row_type row = 0; row <= nrows(); ++row)
516                 rowinfo_[row].offset_ -= h;
517
518
519         // compute absolute sizes of horizontal structure
520         for (col_type col = 0; col < ncols(); ++col) {
521                 int wid = 0;
522                 for (row_type row = 0; row < nrows(); ++row)
523                         wid = max(wid, cell(index(row, col)).width());
524                 colinfo_[col].width_ = wid;
525         }
526         colinfo_[ncols()].width_  = 0;
527
528         // compute horizontal offsets
529         colinfo_[0].offset_ = border();
530         for (col_type col = 1; col <= ncols(); ++col) {
531                 colinfo_[col].offset_ =
532                         colinfo_[col - 1].offset_ +
533                         colinfo_[col - 1].width_ +
534                         colinfo_[col - 1].skip_ +
535                         1 ; //colsep() +
536                         //colinfo_[col].lines_ * vlinesep();
537         }
538
539
540         dim.wid  =  colinfo_[ncols() - 1].offset_
541                        + colinfo_[ncols() - 1].width_
542                  //+ vlinesep() * colinfo_[ncols()].lines_
543                        + 2;
544
545         dim.asc  = -rowinfo_[0].offset_
546                        + rowinfo_[0].ascent_
547                  //+ hlinesep() * rowinfo_[0].lines_
548                        + 1;
549
550         dim.des  =  rowinfo_[nrows() - 1].offset_
551                        + rowinfo_[nrows() - 1].descent_
552                  //+ hlinesep() * rowinfo_[nrows()].lines_
553                        + 1;
554 }
555
556
557 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
558 {
559         for (idx_type idx = 0; idx < nargs(); ++idx)
560                 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
561 }
562
563
564 string MathGridInset::eolString(row_type row, bool fragile) const
565 {
566         string eol;
567
568         if (!rowinfo_[row].crskip_.zero())
569                 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
570
571         // make sure an upcoming '[' does not break anything
572         if (row + 1 < nrows()) {
573                 MathArray const & c = cell(index(row + 1, 0));
574                 if (c.size() && c.front()->getChar() == '[')
575                         //eol += "[0pt]";
576                         eol += "{}";
577         }
578
579         // only add \\ if necessary
580         if (eol.empty() && row + 1 == nrows())
581                 return string();
582
583         return (fragile ? "\\protect\\\\" : "\\\\") + eol;
584 }
585
586
587 string MathGridInset::eocString(col_type col, col_type lastcol) const
588 {
589         if (col + 1 == lastcol)
590                 return string();
591         return " & ";
592 }
593
594
595 void MathGridInset::addRow(row_type row)
596 {
597         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
598         cells_.insert
599                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
600         cellinfo_.insert
601                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
602 }
603
604
605 void MathGridInset::appendRow()
606 {
607         rowinfo_.push_back(RowInfo());
608         //cells_.insert(cells_.end(), ncols(), MathArray());
609         for (col_type col = 0; col < ncols(); ++col) {
610                 cells_.push_back(cells_type::value_type());
611                 cellinfo_.push_back(CellInfo());
612         }
613 }
614
615
616 void MathGridInset::delRow(row_type row)
617 {
618         if (nrows() == 1)
619                 return;
620
621         cells_type::iterator it = cells_.begin() + row * ncols();
622         cells_.erase(it, it + ncols());
623
624         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
625         cellinfo_.erase(jt, jt + ncols());
626
627         rowinfo_.erase(rowinfo_.begin() + row);
628 }
629
630
631 void MathGridInset::copyRow(row_type row)
632 {
633         addRow(row);
634         for (col_type col = 0; col < ncols(); ++col)
635                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
636 }
637
638
639 void MathGridInset::swapRow(row_type row)
640 {
641         if (nrows() == 1)
642                 return;
643         if (row + 1 == nrows())
644                 --row;
645         for (col_type col = 0; col < ncols(); ++col)
646                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
647 }
648
649
650 void MathGridInset::addCol(col_type newcol)
651 {
652         const col_type nc = ncols();
653         const row_type nr = nrows();
654         cells_type new_cells((nc + 1) * nr);
655         vector<CellInfo> new_cellinfo((nc + 1) * nr);
656
657         for (row_type row = 0; row < nr; ++row)
658                 for (col_type col = 0; col < nc; ++col) {
659                         new_cells[row * (nc + 1) + col + (col > newcol)]
660                                 = cells_[row * nc + col];
661                         new_cellinfo[row * (nc + 1) + col + (col > newcol)]
662                                 = cellinfo_[row * nc + col];
663                 }
664         swap(cells_, new_cells);
665         swap(cellinfo_, new_cellinfo);
666
667         ColInfo inf;
668         inf.skip_  = defaultColSpace(newcol);
669         inf.align_ = defaultColAlign(newcol);
670         colinfo_.insert(colinfo_.begin() + newcol, inf);
671 }
672
673
674 void MathGridInset::delCol(col_type col)
675 {
676         if (ncols() == 1)
677                 return;
678
679         cells_type tmpcells;
680         vector<CellInfo> tmpcellinfo;
681         for (col_type i = 0; i < nargs(); ++i)
682                 if (i % ncols() != col) {
683                         tmpcells.push_back(cells_[i]);
684                         tmpcellinfo.push_back(cellinfo_[i]);
685                 }
686         swap(cells_, tmpcells);
687         swap(cellinfo_, tmpcellinfo);
688
689         colinfo_.erase(colinfo_.begin() + col);
690 }
691
692
693 void MathGridInset::copyCol(col_type col)
694 {
695         addCol(col);
696         for (row_type row = 0; row < nrows(); ++row)
697                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
698 }
699
700
701 void MathGridInset::swapCol(col_type col)
702 {
703         if (ncols() == 1)
704                 return;
705         if (col + 1 == ncols())
706                 --col;
707         for (row_type row = 0; row < nrows(); ++row)
708                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
709 }
710
711
712 int MathGridInset::cellXOffset(idx_type idx) const
713 {
714         col_type c = col(idx);
715         int x = colinfo_[c].offset_;
716         char align = colinfo_[c].align_;
717         if (align == 'r' || align == 'R')
718                 x += colinfo_[c].width_ - cell(idx).width();
719         if (align == 'c' || align == 'C')
720                 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
721         return x;
722 }
723
724
725 int MathGridInset::cellYOffset(idx_type idx) const
726 {
727         return rowinfo_[row(idx)].offset_;
728 }
729
730
731 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
732         int targetx) const
733 {
734         if (up) {
735                 if (idx < ncols())
736                         return false;
737                 idx -= ncols();
738                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
739                 return true;
740         } else {
741                 if (idx >= ncols() * (nrows() - 1))
742                         return false;
743                 idx += ncols();
744                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
745                 return true;
746         }
747 }
748
749
750 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
751 {
752         // leave matrix if on the left hand edge
753         if (col(idx) == 0)
754                 return false;
755         --idx;
756         pos = cell(idx).size();
757         return true;
758 }
759
760
761 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
762 {
763         // leave matrix if on the right hand edge
764         if (col(idx) + 1 == ncols())
765                 return false;
766         ++idx;
767         pos = 0;
768         return true;
769 }
770
771
772 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
773 {
774         switch (v_align_) {
775                 case 't':
776                         idx = 0;
777                         break;
778                 case 'b':
779                         idx = (nrows() - 1) * ncols();
780                         break;
781                 default:
782                         idx = ((nrows() - 1) / 2) * ncols();
783         }
784         pos = 0;
785         return true;
786 }
787
788
789 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
790 {
791         switch (v_align_) {
792                 case 't':
793                         idx = ncols() - 1;
794                         break;
795                 case 'b':
796                         idx = nargs() - 1;
797                         break;
798                 default:
799                         idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
800         }
801         pos = cell(idx).size();
802         return true;
803 }
804
805
806 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
807 {
808         if (pos > 0) {
809                 pos = 0;
810                 return true;
811         }
812         if (col(idx) > 0) {
813                 idx -= idx % ncols();
814                 pos = 0;
815                 return true;
816         }
817         if (idx > 0) {
818                 idx = 0;
819                 pos = 0;
820                 return true;
821         }
822         return false;
823 }
824
825
826 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
827 {
828         if (pos < cell(idx).size()) {
829                 pos = cell(idx).size();
830                 return true;
831         }
832         if (col(idx) < ncols() - 1) {
833                 idx = idx - idx % ncols() + ncols() - 1;
834                 pos = cell(idx).size();
835                 return true;
836         }
837         if (idx < nargs() - 1) {
838                 idx = nargs() - 1;
839                 pos = cell(idx).size();
840                 return true;
841         }
842         return false;
843 }
844
845
846 bool MathGridInset::idxDelete(idx_type & idx)
847 {
848         // nothing to do if we have just one row
849         if (nrows() == 1)
850                 return false;
851
852         // nothing to do if we are in the middle of the last row of the inset
853         if (idx + ncols() > nargs())
854                 return false;
855
856         // try to delete entire sequence of ncols() empty cells if possible
857         for (idx_type i = idx; i < idx + ncols(); ++i)
858                 if (cell(i).size())
859                         return false;
860
861         // move cells if necessary
862         for (idx_type i = index(row(idx), 0); i < idx; ++i)
863                 std::swap(cell(i), cell(i + ncols()));
864
865         delRow(row(idx));
866
867         if (idx >= nargs())
868                 idx = nargs() - 1;
869
870         // undo effect of Ctrl-Tab (i.e. pull next cell)
871         //if (idx + 1 != nargs())
872         //      cell(idx).swap(cell(idx + 1));
873
874         // we handled the event..
875         return true;
876 }
877
878
879 // reimplement old behaviour when pressing Delete in the last position
880 // of a cell
881 void MathGridInset::idxGlue(idx_type idx)
882 {
883         col_type c = col(idx);
884         if (c + 1 == ncols()) {
885                 if (row(idx) + 1 != nrows()) {
886                         for (col_type cc = 0; cc < ncols(); ++cc)
887                                 cell(idx).append(cell(idx + cc + 1));
888                         delRow(row(idx) + 1);
889                 }
890         } else {
891                 cell(idx).append(cell(idx + 1));
892                 for (col_type cc = c + 2; cc < ncols(); ++cc)
893                         cell(idx - c + cc - 1) = cell(idx - c + cc);
894                 cell(idx - c + ncols() - 1).clear();
895         }
896 }
897
898
899 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
900 {
901         return rowinfo_[row];
902 }
903
904
905 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
906 {
907         return rowinfo_[row];
908 }
909
910
911 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
912 {
913         row_type const ri = row(idx);
914         row_type const r1 = min(row(from), row(to));
915         row_type const r2 = max(row(from), row(to));
916         col_type const ci = col(idx);
917         col_type const c1 = min(col(from), col(to));
918         col_type const c2 = max(col(from), col(to));
919         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
920 }
921
922
923
924 void MathGridInset::normalize(NormalStream & os) const
925 {
926         os << "[grid ";
927         for (row_type row = 0; row < nrows(); ++row) {
928                 os << "[row ";
929                 for (col_type col = 0; col < ncols(); ++col)
930                         os << "[cell " << cell(index(row, col)) << ']';
931                 os << ']';
932         }
933         os << ']';
934 }
935
936
937 void MathGridInset::mathmlize(MathMLStream & os) const
938 {
939         os << MTag("mtable");
940         for (row_type row = 0; row < nrows(); ++row) {
941                 os << MTag("mtr");
942                 for (col_type col = 0; col < ncols(); ++col)
943                         os << cell(index(row, col));
944                 os << ETag("mtr");
945         }
946         os << ETag("mtable");
947 }
948
949
950 void MathGridInset::write(WriteStream & os) const
951 {
952         for (row_type row = 0; row < nrows(); ++row) {
953                 os << verboseHLine(rowinfo_[row].lines_);
954                 // don't write & and empty cells at end of line
955                 col_type lastcol = 0;
956                 bool emptyline = true;
957                 for (col_type col = 0; col < ncols(); ++col)
958                         if (!cell(index(row, col)).empty()) {
959                                 lastcol = col + 1;
960                                 emptyline = false;
961                         }
962                 for (col_type col = 0; col < lastcol; ++col)
963                         os << cell(index(row, col)) << eocString(col, lastcol);
964                 os << eolString(row, os.fragile());
965                 // append newline only if line wasn't completely empty
966                 // and this was not the last line in the grid
967                 if (!emptyline && row + 1 < nrows())
968                         os << "\n";
969         }
970         string const s = verboseHLine(rowinfo_[nrows()].lines_);
971         if (!s.empty() && s != " ") {
972                 if (os.fragile())
973                         os << "\\protect";
974                 os << "\\\\" << s;
975         }
976 }
977
978
979 int MathGridInset::colsep() const
980 {
981         return 6;
982 }
983
984
985 int MathGridInset::rowsep() const
986 {
987         return 6;
988 }
989
990
991 int MathGridInset::hlinesep() const
992 {
993         return 3;
994 }
995
996
997 int MathGridInset::vlinesep() const
998 {
999         return 3;
1000 }
1001
1002
1003 int MathGridInset::border() const
1004 {
1005         return 1;
1006 }
1007
1008
1009 void MathGridInset::splitCell(idx_type & idx, pos_type & pos)
1010 {
1011         if (idx + 1 == nargs())
1012                 return;
1013         MathArray ar = cell(idx);
1014         ar.erase(0, pos);
1015         cell(idx).erase(pos, cell(idx).size());
1016         ++idx;
1017         pos = 0;
1018         cell(idx).insert(0, ar);
1019 }
1020
1021
1022 dispatch_result MathGridInset::dispatch
1023         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
1024 {
1025         switch (cmd.action) {
1026
1027                 case LFUN_MOUSE_RELEASE:
1028                         //if (cmd.button() == mouse_button::button3) {
1029                         //      GridInsetMailer(*this).showDialog();
1030                         //      return DISPATCHED;
1031                         //}
1032                         return UNDISPATCHED;
1033
1034                 case LFUN_INSET_DIALOG_UPDATE: 
1035                         GridInsetMailer(*this).updateDialog(cmd.view());
1036                         return UNDISPATCHED;
1037
1038                 // insert file functions
1039                 case LFUN_DELETE_LINE_FORWARD:
1040                         //autocorrect_ = false;
1041                         //macroModeClose();
1042                         //if (selection_) {
1043                         //      selDel();
1044                         //      return;
1045                         //}
1046                         if (nrows() > 1)
1047                                 delRow(row(idx));
1048                         if (idx >= nargs())
1049                                 idx = nargs() - 1;
1050                         if (pos > cell(idx).size())
1051                                 pos = cell(idx).size();
1052                         return DISPATCHED_POP;
1053
1054                 case LFUN_CELL_SPLIT:
1055                         //bv->lockedInsetStoreUndo(Undo::EDIT);
1056                         splitCell(idx, pos);
1057                         return DISPATCHED_POP;
1058
1059                 case LFUN_BREAKLINE: {
1060                         //bv->lockedInsetStoreUndo(Undo::INSERT);
1061                         row_type const r = row(idx);
1062                         addRow(r);
1063
1064                         // split line
1065                         for (col_type c = col(idx) + 1; c < ncols(); ++c)
1066                                 std::swap(cell(index(r, c)), cell(index(r + 1, c)));
1067
1068                         // split cell
1069                         splitCell(idx, pos);
1070                         std::swap(cell(idx), cell(idx + ncols() - 1));
1071                         if (idx > 0)
1072                                 --idx;
1073                         pos = cell(idx).size();
1074
1075                         //mathcursor->normalize();
1076                         return DISPATCHED_POP;
1077                 }
1078
1079                 case LFUN_TABULAR_FEATURE: {
1080                         //lyxerr << "handling tabular-feature " << cmd.argument << "\n";
1081                         istringstream is(STRCONV(cmd.argument));
1082                         string s;
1083                         is >> s;
1084                         if (s == "valign-top")
1085                                 valign('t');
1086                         else if (s == "valign-center")
1087                                 valign('c');
1088                         else if (s == "valign-bottom")
1089                                 valign('b');
1090                         else if (s == "align-left")
1091                                 halign('l', col(idx));
1092                         else if (s == "align-right")
1093                                 halign('r', col(idx));
1094                         else if (s == "align-center")
1095                                 halign('c', col(idx));
1096                         else if (s == "append-row")
1097                                 for (int i = 0, n = extractInt(is); i < n; ++i)
1098                                         addRow(row(idx));
1099                         else if (s == "delete-row") 
1100                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1101                                         delRow(row(idx));
1102                                         if (idx > nargs())
1103                                                 idx -= ncols();
1104                                 }
1105                         else if (s == "copy-row") 
1106                                 for (int i = 0, n = extractInt(is); i < n; ++i)
1107                                         copyRow(row(idx));
1108                         else if (s == "swap-row")
1109                                 swapRow(row(idx));
1110                         else if (s == "append-column") 
1111                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1112                                         row_type r = row(idx);
1113                                         col_type c = col(idx);
1114                                         addCol(c);
1115                                         idx = index(r, c);
1116                                 }
1117                         else if (s == "delete-column") 
1118                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1119                                         row_type r = row(idx);
1120                                         col_type c = col(idx);
1121                                         delCol(col(idx));
1122                                         idx = index(r, c);
1123                                         if (idx > nargs())
1124                                                 idx -= ncols();
1125                                 }
1126                         else if (s == "copy-column")
1127                                 copyCol(col(idx));
1128                         else if (s == "swap-column")
1129                                 swapCol(col(idx));
1130                         else
1131                                 return UNDISPATCHED;
1132                         lyxerr << "returning DISPATCHED_POP\n";
1133                         return DISPATCHED_POP;
1134                 }
1135
1136                 case LFUN_PASTE: {
1137                         //lyxerr << "pasting '" << cmd.argument << "'\n";
1138                         MathGridInset grid(1, 1);
1139                         mathed_parse_normal(grid, cmd.argument);
1140                         if (grid.nargs() == 1) {
1141                                 // single cell/part of cell
1142                                 cell(idx).insert(pos, grid.cell(0));
1143                                 pos += grid.cell(0).size();
1144                         } else {
1145                                 // multiple cells
1146                                 col_type const numcols = min(grid.ncols(), ncols() - col(idx));
1147                                 row_type const numrows = min(grid.nrows(), nrows() - row(idx));
1148                                 for (row_type r = 0; r < numrows; ++r) {
1149                                         for (col_type c = 0; c < numcols; ++c) {
1150                                                 idx_type i = index(r + row(idx), c + col(idx));
1151                                                 cell(i).append(grid.cell(grid.index(r, c)));
1152                                         }
1153                                         // append the left over horizontal cells to the last column
1154                                         idx_type i = index(r + row(idx), ncols() - 1);
1155                                         for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1156                                                 cell(i).append(grid.cell(grid.index(r, c)));
1157                                 }
1158                                 // append the left over vertical cells to the last _cell_
1159                                 idx_type i = nargs() - 1;
1160                                 for (row_type r = numrows; r < grid.nrows(); ++r)
1161                                         for (col_type c = 0; c < grid.ncols(); ++c)
1162                                                 cell(i).append(grid.cell(grid.index(r, c)));
1163                         }
1164                         return DISPATCHED_POP;
1165                 }
1166
1167                 default:
1168                         return MathNestInset::dispatch(cmd, idx, pos);
1169         }
1170 }