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