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