]> git.lyx.org Git - lyx.git/blob - src/mathed/math_gridinset.C
Get rid of lyxstring, remove usage of STRCONV.
[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 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,
473                                      x + dim_.width() - 1, yy,
474                                      LColor::foreground);
475                 }
476
477         for (col_type col = 0; col <= ncols(); ++col)
478                 for (int i = 0; i < colinfo_[col].lines_; ++i) {
479                         int xx = x + colinfo_[col].offset_
480                                 - i * vlinesep() - vlinesep()/2 - colsep()/2;
481                         pi.pain.line(xx, y - dim_.ascent() + 1,
482                                      xx, y + dim_.descent() - 1,
483                                      LColor::foreground);
484                 }
485 }
486
487
488 void MathGridInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
489 {
490         // let the cells adjust themselves
491         //MathNestInset::metrics(mi);
492         for (idx_type i = 0; i < nargs(); ++i)
493                 cell(i).metricsT(mi, dim);
494
495         // compute absolute sizes of vertical structure
496         for (row_type row = 0; row < nrows(); ++row) {
497                 int asc  = 0;
498                 int desc = 0;
499                 for (col_type col = 0; col < ncols(); ++col) {
500                         MathArray const & c = cell(index(row, col));
501                         asc  = max(asc,  c.ascent());
502                         desc = max(desc, c.descent());
503                 }
504                 rowinfo_[row].ascent_  = asc;
505                 rowinfo_[row].descent_ = desc;
506         }
507         //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
508         rowinfo_[nrows()].ascent_  = 0;
509         rowinfo_[nrows()].descent_ = 0;
510
511         // compute vertical offsets
512         rowinfo_[0].offset_ = 0;
513         for (row_type row = 1; row <= nrows(); ++row) {
514                 rowinfo_[row].offset_  =
515                         rowinfo_[row - 1].offset_  +
516                         rowinfo_[row - 1].descent_ +
517                         //rowinfo_[row - 1].skipPixels() +
518                         1 + //rowsep() +
519                         //rowinfo_[row].lines_ * hlinesep() +
520                         rowinfo_[row].ascent_;
521         }
522
523         // adjust vertical offset
524         int h = 0;
525         switch (v_align_) {
526                 case 't':
527                         h = 0;
528                         break;
529                 case 'b':
530                         h = rowinfo_[nrows() - 1].offset_;
531                         break;
532                 default:
533                         h = rowinfo_[nrows() - 1].offset_ / 2;
534         }
535         for (row_type row = 0; row <= nrows(); ++row)
536                 rowinfo_[row].offset_ -= h;
537
538
539         // compute absolute sizes of horizontal structure
540         for (col_type col = 0; col < ncols(); ++col) {
541                 int wid = 0;
542                 for (row_type row = 0; row < nrows(); ++row)
543                         wid = max(wid, cell(index(row, col)).width());
544                 colinfo_[col].width_ = wid;
545         }
546         colinfo_[ncols()].width_  = 0;
547
548         // compute horizontal offsets
549         colinfo_[0].offset_ = border();
550         for (col_type col = 1; col <= ncols(); ++col) {
551                 colinfo_[col].offset_ =
552                         colinfo_[col - 1].offset_ +
553                         colinfo_[col - 1].width_ +
554                         colinfo_[col - 1].skip_ +
555                         1 ; //colsep() +
556                         //colinfo_[col].lines_ * vlinesep();
557         }
558
559
560         dim.wid  =  colinfo_[ncols() - 1].offset_
561                        + colinfo_[ncols() - 1].width_
562                  //+ vlinesep() * colinfo_[ncols()].lines_
563                        + 2;
564
565         dim.asc  = -rowinfo_[0].offset_
566                        + rowinfo_[0].ascent_
567                  //+ hlinesep() * rowinfo_[0].lines_
568                        + 1;
569
570         dim.des  =  rowinfo_[nrows() - 1].offset_
571                        + rowinfo_[nrows() - 1].descent_
572                  //+ hlinesep() * rowinfo_[nrows()].lines_
573                        + 1;
574 }
575
576
577 void MathGridInset::drawT(TextPainter & pain, int x, int y) const
578 {
579         for (idx_type idx = 0; idx < nargs(); ++idx)
580                 cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
581 }
582
583
584 string MathGridInset::eolString(row_type row, bool fragile) const
585 {
586         string eol;
587
588         if (!rowinfo_[row].crskip_.zero())
589                 eol += '[' + rowinfo_[row].crskip_.asLatexString() + ']';
590
591         // make sure an upcoming '[' does not break anything
592         if (row + 1 < nrows()) {
593                 MathArray const & c = cell(index(row + 1, 0));
594                 if (c.size() && c.front()->getChar() == '[')
595                         //eol += "[0pt]";
596                         eol += "{}";
597         }
598
599         // only add \\ if necessary
600         if (eol.empty() && row + 1 == nrows())
601                 return string();
602
603         return (fragile ? "\\protect\\\\" : "\\\\") + eol;
604 }
605
606
607 string MathGridInset::eocString(col_type col, col_type lastcol) const
608 {
609         if (col + 1 == lastcol)
610                 return string();
611         return " & ";
612 }
613
614
615 void MathGridInset::addRow(row_type row)
616 {
617         rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
618         cells_.insert
619                 (cells_.begin() + (row + 1) * ncols(), ncols(), MathArray());
620         cellinfo_.insert
621                 (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
622 }
623
624
625 void MathGridInset::appendRow()
626 {
627         rowinfo_.push_back(RowInfo());
628         //cells_.insert(cells_.end(), ncols(), MathArray());
629         for (col_type col = 0; col < ncols(); ++col) {
630                 cells_.push_back(cells_type::value_type());
631                 cellinfo_.push_back(CellInfo());
632         }
633 }
634
635
636 void MathGridInset::delRow(row_type row)
637 {
638         if (nrows() == 1)
639                 return;
640
641         cells_type::iterator it = cells_.begin() + row * ncols();
642         cells_.erase(it, it + ncols());
643
644         vector<CellInfo>::iterator jt = cellinfo_.begin() + row * ncols();
645         cellinfo_.erase(jt, jt + ncols());
646
647         rowinfo_.erase(rowinfo_.begin() + row);
648 }
649
650
651 void MathGridInset::copyRow(row_type row)
652 {
653         addRow(row);
654         for (col_type col = 0; col < ncols(); ++col)
655                 cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
656 }
657
658
659 void MathGridInset::swapRow(row_type row)
660 {
661         if (nrows() == 1)
662                 return;
663         if (row + 1 == nrows())
664                 --row;
665         for (col_type col = 0; col < ncols(); ++col)
666                 swap(cells_[row * ncols() + col], cells_[(row + 1) * ncols() + col]);
667 }
668
669
670 void MathGridInset::addCol(col_type newcol)
671 {
672         const col_type nc = ncols();
673         const row_type nr = nrows();
674         cells_type new_cells((nc + 1) * nr);
675         vector<CellInfo> new_cellinfo((nc + 1) * nr);
676
677         for (row_type row = 0; row < nr; ++row)
678                 for (col_type col = 0; col < nc; ++col) {
679                         new_cells[row * (nc + 1) + col + (col > newcol)]
680                                 = cells_[row * nc + col];
681                         new_cellinfo[row * (nc + 1) + col + (col > newcol)]
682                                 = cellinfo_[row * nc + col];
683                 }
684         swap(cells_, new_cells);
685         swap(cellinfo_, new_cellinfo);
686
687         ColInfo inf;
688         inf.skip_  = defaultColSpace(newcol);
689         inf.align_ = defaultColAlign(newcol);
690         colinfo_.insert(colinfo_.begin() + newcol, inf);
691 }
692
693
694 void MathGridInset::delCol(col_type col)
695 {
696         if (ncols() == 1)
697                 return;
698
699         cells_type tmpcells;
700         vector<CellInfo> tmpcellinfo;
701         for (col_type i = 0; i < nargs(); ++i)
702                 if (i % ncols() != col) {
703                         tmpcells.push_back(cells_[i]);
704                         tmpcellinfo.push_back(cellinfo_[i]);
705                 }
706         swap(cells_, tmpcells);
707         swap(cellinfo_, tmpcellinfo);
708
709         colinfo_.erase(colinfo_.begin() + col);
710 }
711
712
713 void MathGridInset::copyCol(col_type col)
714 {
715         addCol(col);
716         for (row_type row = 0; row < nrows(); ++row)
717                 cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
718 }
719
720
721 void MathGridInset::swapCol(col_type col)
722 {
723         if (ncols() == 1)
724                 return;
725         if (col + 1 == ncols())
726                 --col;
727         for (row_type row = 0; row < nrows(); ++row)
728                 swap(cells_[row * ncols() + col], cells_[row * ncols() + col + 1]);
729 }
730
731
732 int MathGridInset::cellXOffset(idx_type idx) const
733 {
734         col_type c = col(idx);
735         int x = colinfo_[c].offset_;
736         char align = colinfo_[c].align_;
737         if (align == 'r' || align == 'R')
738                 x += colinfo_[c].width_ - cell(idx).width();
739         if (align == 'c' || align == 'C')
740                 x += (colinfo_[c].width_ - cell(idx).width()) / 2;
741         return x;
742 }
743
744
745 int MathGridInset::cellYOffset(idx_type idx) const
746 {
747         return rowinfo_[row(idx)].offset_;
748 }
749
750
751 bool MathGridInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
752         int targetx) const
753 {
754         if (up) {
755                 if (idx < ncols())
756                         return false;
757                 idx -= ncols();
758                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
759                 return true;
760         } else {
761                 if (idx >= ncols() * (nrows() - 1))
762                         return false;
763                 idx += ncols();
764                 pos = cell(idx).x2pos(targetx - cell(idx).xo());
765                 return true;
766         }
767 }
768
769
770 bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
771 {
772         // leave matrix if on the left hand edge
773         if (col(idx) == 0)
774                 return false;
775         --idx;
776         pos = cell(idx).size();
777         return true;
778 }
779
780
781 bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
782 {
783         // leave matrix if on the right hand edge
784         if (col(idx) + 1 == ncols())
785                 return false;
786         ++idx;
787         pos = 0;
788         return true;
789 }
790
791
792 bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
793 {
794         switch (v_align_) {
795                 case 't':
796                         idx = 0;
797                         break;
798                 case 'b':
799                         idx = (nrows() - 1) * ncols();
800                         break;
801                 default:
802                         idx = ((nrows() - 1) / 2) * ncols();
803         }
804         pos = 0;
805         return true;
806 }
807
808
809 bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
810 {
811         switch (v_align_) {
812                 case 't':
813                         idx = ncols() - 1;
814                         break;
815                 case 'b':
816                         idx = nargs() - 1;
817                         break;
818                 default:
819                         idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
820         }
821         pos = cell(idx).size();
822         return true;
823 }
824
825
826 bool MathGridInset::idxHome(idx_type & idx, pos_type & pos) const
827 {
828         if (pos > 0) {
829                 pos = 0;
830                 return true;
831         }
832         if (col(idx) > 0) {
833                 idx -= idx % ncols();
834                 pos = 0;
835                 return true;
836         }
837         if (idx > 0) {
838                 idx = 0;
839                 pos = 0;
840                 return true;
841         }
842         return false;
843 }
844
845
846 bool MathGridInset::idxEnd(idx_type & idx, pos_type & pos) const
847 {
848         if (pos < cell(idx).size()) {
849                 pos = cell(idx).size();
850                 return true;
851         }
852         if (col(idx) < ncols() - 1) {
853                 idx = idx - idx % ncols() + ncols() - 1;
854                 pos = cell(idx).size();
855                 return true;
856         }
857         if (idx < nargs() - 1) {
858                 idx = nargs() - 1;
859                 pos = cell(idx).size();
860                 return true;
861         }
862         return false;
863 }
864
865
866 bool MathGridInset::idxDelete(idx_type & idx)
867 {
868         // nothing to do if we have just one row
869         if (nrows() == 1)
870                 return false;
871
872         // nothing to do if we are in the middle of the last row of the inset
873         if (idx + ncols() > nargs())
874                 return false;
875
876         // try to delete entire sequence of ncols() empty cells if possible
877         for (idx_type i = idx; i < idx + ncols(); ++i)
878                 if (cell(i).size())
879                         return false;
880
881         // move cells if necessary
882         for (idx_type i = index(row(idx), 0); i < idx; ++i)
883                 std::swap(cell(i), cell(i + ncols()));
884
885         delRow(row(idx));
886
887         if (idx >= nargs())
888                 idx = nargs() - 1;
889
890         // undo effect of Ctrl-Tab (i.e. pull next cell)
891         //if (idx + 1 != nargs())
892         //      cell(idx).swap(cell(idx + 1));
893
894         // we handled the event..
895         return true;
896 }
897
898
899 // reimplement old behaviour when pressing Delete in the last position
900 // of a cell
901 void MathGridInset::idxGlue(idx_type idx)
902 {
903         col_type c = col(idx);
904         if (c + 1 == ncols()) {
905                 if (row(idx) + 1 != nrows()) {
906                         for (col_type cc = 0; cc < ncols(); ++cc)
907                                 cell(idx).append(cell(idx + cc + 1));
908                         delRow(row(idx) + 1);
909                 }
910         } else {
911                 cell(idx).append(cell(idx + 1));
912                 for (col_type cc = c + 2; cc < ncols(); ++cc)
913                         cell(idx - c + cc - 1) = cell(idx - c + cc);
914                 cell(idx - c + ncols() - 1).clear();
915         }
916 }
917
918
919 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
920 {
921         return rowinfo_[row];
922 }
923
924
925 MathGridInset::RowInfo & MathGridInset::rowinfo(row_type row)
926 {
927         return rowinfo_[row];
928 }
929
930
931 bool MathGridInset::idxBetween(idx_type idx, idx_type from, idx_type to) const
932 {
933         row_type const ri = row(idx);
934         row_type const r1 = min(row(from), row(to));
935         row_type const r2 = max(row(from), row(to));
936         col_type const ci = col(idx);
937         col_type const c1 = min(col(from), col(to));
938         col_type const c2 = max(col(from), col(to));
939         return r1 <= ri && ri <= r2 && c1 <= ci && ci <= c2;
940 }
941
942
943
944 void MathGridInset::normalize(NormalStream & os) const
945 {
946         os << "[grid ";
947         for (row_type row = 0; row < nrows(); ++row) {
948                 os << "[row ";
949                 for (col_type col = 0; col < ncols(); ++col)
950                         os << "[cell " << cell(index(row, col)) << ']';
951                 os << ']';
952         }
953         os << ']';
954 }
955
956
957 void MathGridInset::mathmlize(MathMLStream & os) const
958 {
959         os << MTag("mtable");
960         for (row_type row = 0; row < nrows(); ++row) {
961                 os << MTag("mtr");
962                 for (col_type col = 0; col < ncols(); ++col)
963                         os << cell(index(row, col));
964                 os << ETag("mtr");
965         }
966         os << ETag("mtable");
967 }
968
969
970 void MathGridInset::write(WriteStream & os) const
971 {
972         for (row_type row = 0; row < nrows(); ++row) {
973                 os << verboseHLine(rowinfo_[row].lines_);
974                 // don't write & and empty cells at end of line
975                 col_type lastcol = 0;
976                 bool emptyline = true;
977                 for (col_type col = 0; col < ncols(); ++col)
978                         if (!cell(index(row, col)).empty()) {
979                                 lastcol = col + 1;
980                                 emptyline = false;
981                         }
982                 for (col_type col = 0; col < lastcol; ++col)
983                         os << cell(index(row, col)) << eocString(col, lastcol);
984                 os << eolString(row, os.fragile());
985                 // append newline only if line wasn't completely empty
986                 // and this was not the last line in the grid
987                 if (!emptyline && row + 1 < nrows())
988                         os << "\n";
989         }
990         string const s = verboseHLine(rowinfo_[nrows()].lines_);
991         if (!s.empty() && s != " ") {
992                 if (os.fragile())
993                         os << "\\protect";
994                 os << "\\\\" << s;
995         }
996 }
997
998
999 int MathGridInset::colsep() const
1000 {
1001         return 6;
1002 }
1003
1004
1005 int MathGridInset::rowsep() const
1006 {
1007         return 6;
1008 }
1009
1010
1011 int MathGridInset::hlinesep() const
1012 {
1013         return 3;
1014 }
1015
1016
1017 int MathGridInset::vlinesep() const
1018 {
1019         return 3;
1020 }
1021
1022
1023 int MathGridInset::border() const
1024 {
1025         return 1;
1026 }
1027
1028
1029 void MathGridInset::splitCell(idx_type & idx, pos_type & pos)
1030 {
1031         if (idx + 1 == nargs())
1032                 return;
1033         MathArray ar = cell(idx);
1034         ar.erase(0, pos);
1035         cell(idx).erase(pos, cell(idx).size());
1036         ++idx;
1037         pos = 0;
1038         cell(idx).insert(0, ar);
1039 }
1040
1041
1042 dispatch_result MathGridInset::dispatch
1043         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
1044 {
1045         switch (cmd.action) {
1046
1047                 case LFUN_MOUSE_RELEASE:
1048                         //if (cmd.button() == mouse_button::button3) {
1049                         //      GridInsetMailer(*this).showDialog();
1050                         //      return DISPATCHED;
1051                         //}
1052                         return UNDISPATCHED;
1053
1054                 case LFUN_INSET_DIALOG_UPDATE:
1055                         GridInsetMailer(*this).updateDialog(cmd.view());
1056                         return UNDISPATCHED;
1057
1058                 // insert file functions
1059                 case LFUN_DELETE_LINE_FORWARD:
1060                         //autocorrect_ = false;
1061                         //macroModeClose();
1062                         //if (selection_) {
1063                         //      selDel();
1064                         //      return;
1065                         //}
1066                         if (nrows() > 1)
1067                                 delRow(row(idx));
1068                         if (idx >= nargs())
1069                                 idx = nargs() - 1;
1070                         if (pos > cell(idx).size())
1071                                 pos = cell(idx).size();
1072                         return DISPATCHED_POP;
1073
1074                 case LFUN_CELL_SPLIT:
1075                         //recordUndo(bv, Undo::ATOMIC);
1076                         splitCell(idx, pos);
1077                         return DISPATCHED_POP;
1078
1079                 case LFUN_BREAKLINE: {
1080                         //recordUndo(bv, Undo::INSERT);
1081                         row_type const r = row(idx);
1082                         addRow(r);
1083
1084                         // split line
1085                         for (col_type c = col(idx) + 1; c < ncols(); ++c)
1086                                 std::swap(cell(index(r, c)), cell(index(r + 1, c)));
1087
1088                         // split cell
1089                         splitCell(idx, pos);
1090                         std::swap(cell(idx), cell(idx + ncols() - 1));
1091                         if (idx > 0)
1092                                 --idx;
1093                         pos = cell(idx).size();
1094
1095                         //mathcursor->normalize();
1096                         return DISPATCHED_POP;
1097                 }
1098
1099                 case LFUN_TABULAR_FEATURE: {
1100                         //lyxerr << "handling tabular-feature " << cmd.argument << endl;
1101                         istringstream is(cmd.argument);
1102                         string s;
1103                         is >> s;
1104                         if (s == "valign-top")
1105                                 valign('t');
1106                         else if (s == "valign-middle")
1107                                 valign('c');
1108                         else if (s == "valign-bottom")
1109                                 valign('b');
1110                         else if (s == "align-left")
1111                                 halign('l', col(idx));
1112                         else if (s == "align-right")
1113                                 halign('r', col(idx));
1114                         else if (s == "align-center")
1115                                 halign('c', col(idx));
1116                         else if (s == "append-row")
1117                                 for (int i = 0, n = extractInt(is); i < n; ++i)
1118                                         addRow(row(idx));
1119                         else if (s == "delete-row")
1120                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1121                                         delRow(row(idx));
1122                                         if (idx > nargs())
1123                                                 idx -= ncols();
1124                                 }
1125                         else if (s == "copy-row")
1126                                 for (int i = 0, n = extractInt(is); i < n; ++i)
1127                                         copyRow(row(idx));
1128                         else if (s == "swap-row")
1129                                 swapRow(row(idx));
1130                         else if (s == "append-column")
1131                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1132                                         row_type r = row(idx);
1133                                         col_type c = col(idx);
1134                                         addCol(c);
1135                                         idx = index(r, c);
1136                                 }
1137                         else if (s == "delete-column")
1138                                 for (int i = 0, n = extractInt(is); i < n; ++i) {
1139                                         row_type r = row(idx);
1140                                         col_type c = col(idx);
1141                                         delCol(col(idx));
1142                                         idx = index(r, c);
1143                                         if (idx > nargs())
1144                                                 idx -= ncols();
1145                                 }
1146                         else if (s == "copy-column")
1147                                 copyCol(col(idx));
1148                         else if (s == "swap-column")
1149                                 swapCol(col(idx));
1150                         else
1151                                 return UNDISPATCHED;
1152                         lyxerr << "returning DISPATCHED_POP" << endl;
1153                         return DISPATCHED_POP;
1154                 }
1155
1156                 case LFUN_PASTE: {
1157                         //lyxerr << "pasting '" << cmd.argument << "'" << endl;
1158                         MathGridInset grid(1, 1);
1159                         mathed_parse_normal(grid, cmd.argument);
1160                         if (grid.nargs() == 1) {
1161                                 // single cell/part of cell
1162                                 cell(idx).insert(pos, grid.cell(0));
1163                                 pos += grid.cell(0).size();
1164                         } else {
1165                                 // multiple cells
1166                                 col_type const numcols = min(grid.ncols(), ncols() - col(idx));
1167                                 row_type const numrows = min(grid.nrows(), nrows() - row(idx));
1168                                 for (row_type r = 0; r < numrows; ++r) {
1169                                         for (col_type c = 0; c < numcols; ++c) {
1170                                                 idx_type i = index(r + row(idx), c + col(idx));
1171                                                 cell(i).append(grid.cell(grid.index(r, c)));
1172                                         }
1173                                         // append the left over horizontal cells to the last column
1174                                         idx_type i = index(r + row(idx), ncols() - 1);
1175                                         for (MathInset::col_type c = numcols; c < grid.ncols(); ++c)
1176                                                 cell(i).append(grid.cell(grid.index(r, c)));
1177                                 }
1178                                 // append the left over vertical cells to the last _cell_
1179                                 idx_type i = nargs() - 1;
1180                                 for (row_type r = numrows; r < grid.nrows(); ++r)
1181                                         for (col_type c = 0; c < grid.ncols(); ++c)
1182                                                 cell(i).append(grid.cell(grid.index(r, c)));
1183                         }
1184                         return DISPATCHED_POP;
1185                 }
1186
1187                 default:
1188                         return MathNestInset::dispatch(cmd, idx, pos);
1189         }
1190 }