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