]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
Painter and scrollbar API patches
[lyx.git] / src / mathed / math_hullinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
11 #include "debug.h"
12 #include "frontends/Painter.h"
13 #include "textpainter.h"
14 #include "Lsstream.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
17
18 #include <vector>
19
20 using std::vector;
21 using std::max;
22 using std::endl;
23
24 namespace {
25
26         int getCols(MathInsetTypes type)
27         {
28                 switch (type) {
29                         case LM_OT_EQNARRAY:
30                                 return 3;
31                         case LM_OT_ALIGN:
32                         case LM_OT_ALIGNAT:
33                         case LM_OT_XALIGNAT:
34                         case LM_OT_XXALIGNAT:
35                                 return 2;
36                         default:;
37                 }
38                 return 1;
39         }
40
41
42         // returns position of first relation operator in the array
43         // used for "intelligent splitting"
44         MathArray::size_type firstRelOp(MathArray const & ar)
45         {
46                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
47                         if ((*it)->isRelOp())
48                                 return it - ar.begin();
49                 return ar.size();
50         }
51
52
53         char const * star(bool numbered)
54         {
55                 return numbered ? "" : "*";
56         }
57
58         MathInsetTypes typecode(string const & s)
59         {
60                 if (s == "none")      return LM_OT_NONE;
61                 if (s == "equation")  return LM_OT_EQUATION;
62                 if (s == "display")   return LM_OT_EQUATION;
63                 if (s == "eqnarray")  return LM_OT_EQNARRAY;
64                 if (s == "align")     return LM_OT_ALIGN;
65                 if (s == "alignat")   return LM_OT_ALIGNAT;
66                 if (s == "xalignat")  return LM_OT_XALIGNAT;
67                 if (s == "xxalignat") return LM_OT_XXALIGNAT;
68                 if (s == "multline")  return LM_OT_MULTLINE;
69                 if (s == "gather")    return LM_OT_GATHER;
70                 return LM_OT_SIMPLE;
71         }
72
73
74         string normalName(MathInsetTypes t)
75         {
76                 switch (t) {
77                         case LM_OT_EQUATION:  return "equation";
78                         case LM_OT_EQNARRAY:  return "eqnarray";
79                         case LM_OT_ALIGN:     return "align";
80                         case LM_OT_ALIGNAT:   return "alignat";
81                         case LM_OT_XALIGNAT:  return "xalignat";
82                         case LM_OT_XXALIGNAT: return "xxalignat";
83                         case LM_OT_MULTLINE:  return "multline";
84                         case LM_OT_GATHER:    return "gather";
85                         case LM_OT_SIMPLE:    return "simple";
86                         default: break;
87                 }
88                 return "unknown";
89         }
90
91 } // end anon namespace
92
93
94 MathHullInset::MathHullInset()
95         : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
96 {
97         setDefaults();
98 }
99
100
101 MathHullInset::MathHullInset(MathInsetTypes t)
102         : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
103 {
104         setDefaults();
105 }
106
107
108 MathHullInset::MathHullInset(MathInsetTypes t, MathGridInset const & grid)
109         : MathGridInset(grid), objtype_(t), nonum_(1), label_(1)
110 {
111         setDefaults();
112 }
113
114
115 MathHullInset::MathHullInset(MathInsetTypes t, col_type cols)
116         : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
117 {
118         setDefaults();
119 }
120
121
122 MathInset * MathHullInset::clone() const
123 {
124         return new MathHullInset(*this);
125 }
126
127
128 char MathHullInset::defaultColAlign(col_type col)
129 {
130         switch (getType()) {
131                 case LM_OT_ALIGN:
132                 case LM_OT_ALIGNAT:
133                 case LM_OT_XALIGNAT:
134                 case LM_OT_XXALIGNAT:
135                         return "rl"[col & 1];
136                 case LM_OT_EQNARRAY:
137                         return "rcl"[col];
138                 default:;
139         }
140         return 'c';
141 }
142
143
144 int MathHullInset::defaultColSpace(col_type col)
145 {
146         switch (getType()) {
147                 case LM_OT_ALIGN:
148                 case LM_OT_ALIGNAT:
149                         return 0;
150                 case LM_OT_XALIGNAT:
151                         return (col & 1) ? 20 : 0;
152                 case LM_OT_XXALIGNAT:
153                         return (col & 1) ? 40 : 0;
154                 default:;
155         }
156         return 0;
157 }
158
159
160 void MathHullInset::metrics(MathMetricsInfo & mi) const
161 {
162         MathFontSetChanger dummy(mi.base, "mathnormal");
163
164         // let the cells adjust themselves
165         MathGridInset::metrics(mi);
166
167         if (display()) {
168                 ascent_  += 12;
169                 descent_ += 12;
170         }
171
172         if (numberedType()) {
173                 MathFontSetChanger dummy(mi.base, "mathbf");
174                 int l = 0;
175                 for (row_type row = 0; row < nrows(); ++row)
176                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
177
178                 if (l)
179                         width_ += 30 + l;
180         }
181
182         // make it at least as high as the current font
183         int asc = 0;
184         int des = 0;
185         math_font_max_dim(mi.base.font, asc, des);
186         ascent_  = max(ascent_,  asc);
187         descent_ = max(descent_, des);
188 }
189
190
191 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
192 {
193         MathFontSetChanger dummy(pi.base, "mathnormal");
194         MathGridInset::draw(pi, x, y);
195
196         if (numberedType()) {
197                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
198                 for (row_type row = 0; row < nrows(); ++row) {
199                         int const yy = y + rowinfo_[row].offset_;
200                         drawStrBlack(pi, xx, yy, nicelabel(row));
201                 }
202         }
203 }
204
205
206 void MathHullInset::metricsT(TextMetricsInfo const &) const
207 {
208 #if 0
209         if (display()) {
210                 MathGridInset::metricsT(mi);
211         } else
212 #endif
213         {
214                 ostringstream os;
215                 WriteStream wi(os, false, true);
216                 write(wi);
217                 width_   = os.str().size();
218                 ascent_  = 1;
219                 descent_ = 0;
220         }
221 }
222
223
224 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
225 {
226 #if 0
227         if (display()) {
228                 MathGridInset::drawT(pain, x, y);
229         } else
230 #endif
231         {
232                 ostringstream os;
233                 WriteStream wi(os, false, true);
234                 write(wi);
235                 pain.draw(x, y, os.str().c_str());
236         }
237 }
238
239
240 string MathHullInset::label(row_type row) const
241 {
242         row_type n = nrows();
243         lyx::Assert(row < n);
244         return label_[row];
245 }
246
247
248 void MathHullInset::label(row_type row, string const & label)
249 {
250         label_[row] = label;
251 }
252
253
254 void MathHullInset::numbered(row_type row, bool num)
255 {
256         nonum_[row] = !num;
257 }
258
259
260 bool MathHullInset::numbered(row_type row) const
261 {
262         return !nonum_[row];
263 }
264
265
266 bool MathHullInset::ams() const
267 {
268         return
269                 objtype_ == LM_OT_ALIGN ||
270                 objtype_ == LM_OT_MULTLINE ||
271                 objtype_ == LM_OT_GATHER ||
272                 objtype_ == LM_OT_ALIGNAT ||
273                 objtype_ == LM_OT_XALIGNAT ||
274                 objtype_ == LM_OT_XXALIGNAT;
275 }
276
277
278 bool MathHullInset::display() const
279 {
280         return getType() != LM_OT_SIMPLE;
281 }
282
283
284 vector<string> const MathHullInset::getLabelList() const
285 {
286         vector<string> res;
287         for (row_type row = 0; row < nrows(); ++row)
288                 if (!label_[row].empty() && nonum_[row] != 1)
289                         res.push_back(label_[row]);
290         return res;
291 }
292
293
294 bool MathHullInset::numberedType() const
295 {
296         if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
297                 return false;
298         for (row_type row = 0; row < nrows(); ++row)
299                 if (!nonum_[row])
300                         return true;
301         return false;
302 }
303
304
305 void MathHullInset::validate(LaTeXFeatures & features) const
306 {
307         if (ams())
308                 features.require("amsmath");
309
310
311         // Validation is necessary only if not using AMS math.
312         // To be safe, we will always run mathedvalidate.
313         //if (features.amsstyle)
314         //  return;
315
316         features.require("boldsymbol");
317         //features.binom      = true;
318
319         MathNestInset::validate(features);
320 }
321
322
323 void MathHullInset::header_write(WriteStream & os) const
324 {
325         bool n = numberedType();
326
327         switch (getType()) {
328                 case LM_OT_SIMPLE:
329                         os << '$';
330                         if (cell(0).empty())
331                                 os << ' ';
332                         break;
333
334                 case LM_OT_EQUATION:
335                         if (n)
336                                 os << "\\begin{equation" << star(n) << "}\n";
337                         else
338                                 os << "\\[\n";
339                         break;
340
341                 case LM_OT_EQNARRAY:
342                         os << "\\begin{eqnarray" << star(n) << "}\n";
343                         break;
344
345                 case LM_OT_ALIGN:
346                         os << "\\begin{align" << star(n) << "}\n";
347                         break;
348
349                 case LM_OT_ALIGNAT:
350                         os << "\\begin{alignat" << star(n) << "}"
351                           << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
352                         break;
353
354                 case LM_OT_XALIGNAT:
355                         os << "\\begin{xalignat" << star(n) << "}"
356                            << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
357                         break;
358
359                 case LM_OT_XXALIGNAT:
360                         os << "\\begin{xxalignat}"
361                            << "{" << static_cast<unsigned int>(ncols()/2) << "}\n";
362                         break;
363
364                 case LM_OT_MULTLINE:
365                         os << "\\begin{multline}\n";
366                         break;
367
368                 case LM_OT_GATHER:
369                         os << "\\begin{gather}\n";
370                         break;
371
372                 case LM_OT_NONE:
373                         break;
374
375                 default:
376                         os << "\\begin{unknown" << star(n) << "}";
377         }
378 }
379
380
381 void MathHullInset::footer_write(WriteStream & os) const
382 {
383         bool n = numberedType();
384
385         switch (getType()) {
386                 case LM_OT_SIMPLE:
387                         os << '$';
388                         break;
389
390                 case LM_OT_EQUATION:
391                         if (n)
392                                 os << "\\end{equation" << star(n) << "}\n";
393                         else
394                                 os << "\\]\n";
395                         break;
396
397                 case LM_OT_EQNARRAY:
398                         os << "\n\\end{eqnarray" << star(n) << "}\n";
399                         break;
400
401                 case LM_OT_ALIGN:
402                         os << "\n\\end{align" << star(n) << "}\n";
403                         break;
404
405                 case LM_OT_ALIGNAT:
406                         os << "\n\\end{alignat" << star(n) << "}\n";
407                         break;
408
409                 case LM_OT_XALIGNAT:
410                         os << "\n\\end{xalignat" << star(n) << "}\n";
411                         break;
412
413                 case LM_OT_XXALIGNAT:
414                         os << "\n\\end{xxalignat}\n";
415                         break;
416
417                 case LM_OT_MULTLINE:
418                         os << "\n\\end{multline}\n";
419                         break;
420
421                 case LM_OT_GATHER:
422                         os << "\n\\end{gather}\n";
423                         break;
424
425                 case LM_OT_NONE:
426                         os << "\n";
427                         break;
428
429                 default:
430                         os << "\\end{unknown" << star(n) << "}";
431         }
432 }
433
434
435 void MathHullInset::addRow(row_type row)
436 {
437         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
438         label_.insert(label_.begin() + row + 1, string());
439         MathGridInset::addRow(row);
440 }
441
442
443 void MathHullInset::delRow(row_type row)
444 {
445         MathGridInset::delRow(row);
446         nonum_.erase(nonum_.begin() + row);
447         label_.erase(label_.begin() + row);
448 }
449
450
451 void MathHullInset::addFancyCol(col_type col)
452 {
453         switch (getType()) {
454                 case LM_OT_EQUATION:
455                         mutate(LM_OT_EQNARRAY);
456                         break;
457
458                 case LM_OT_EQNARRAY:
459                         mutate(LM_OT_ALIGN);
460                         addFancyCol(col);
461                         break;
462
463                 case LM_OT_ALIGN:
464                         mutate(LM_OT_ALIGNAT);
465                         addFancyCol(col);
466                         break;
467
468                 case LM_OT_ALIGNAT:
469                 case LM_OT_XALIGNAT:
470                 case LM_OT_XXALIGNAT:
471                         MathGridInset::addCol(col);
472                         MathGridInset::addCol(col + 1);
473                         break;
474
475                 default:
476                         break;
477         }
478 }
479
480
481 void MathHullInset::delFancyCol(col_type col)
482 {
483         switch (getType()) {
484                 case LM_OT_ALIGNAT:
485                 case LM_OT_XALIGNAT:
486                 case LM_OT_XXALIGNAT:
487                         MathGridInset::delCol(col + 1);
488                         MathGridInset::delCol(col);
489                         break;
490                 default:
491                         break;
492         }
493 }
494
495
496 string MathHullInset::nicelabel(row_type row) const
497 {
498         if (nonum_[row])
499                 return string();
500         if (label_[row].empty())
501                 return string("(#)");
502         return "(" + label_[row] + ")";
503 }
504
505
506 void MathHullInset::mutate(string const & newtype)
507 {
508         if (newtype == "dump") {
509                 dump();
510                 return;
511         }
512         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
513         mutate(typecode(newtype));
514 }
515
516
517 void MathHullInset::glueall()
518 {
519         MathArray ar;
520         for (idx_type i = 0; i < nargs(); ++i)
521                 ar.push_back(cell(i));
522         *this = MathHullInset(LM_OT_SIMPLE);
523         cell(0) = ar;
524 }
525
526
527 MathInsetTypes MathHullInset::getType() const
528 {
529         return objtype_;
530 }
531
532
533 void MathHullInset::setType(MathInsetTypes t)
534 {
535         objtype_ = t;
536         setDefaults();
537 }
538
539
540
541 void MathHullInset::mutate(MathInsetTypes newtype)
542 {
543         //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
544
545         if (newtype == getType())
546                 return;
547
548         switch (getType()) {
549                 case LM_OT_SIMPLE:
550                         setType(LM_OT_EQUATION);
551                         numbered(0, false);
552                         mutate(newtype);
553                         break;
554
555                 case LM_OT_EQUATION:
556                         switch (newtype) {
557                                 case LM_OT_SIMPLE:
558                                         setType(LM_OT_SIMPLE);
559                                         break;
560
561                                 case LM_OT_ALIGN:
562                                 case LM_OT_ALIGNAT:
563                                 case LM_OT_XALIGNAT:
564                                 case LM_OT_XXALIGNAT: {
565
566                                         MathGridInset::addCol(1);
567
568                                         // split it "nicely"
569                                         pos_type pos = firstRelOp(cell(0));
570                                         cell(1) = cell(0);
571                                         cell(0).erase(pos, cell(0).size());
572                                         cell(1).erase(0, pos);
573                                         setType(LM_OT_ALIGN);
574                                         mutate(newtype);
575                                         break;
576                                 }
577
578                                 case LM_OT_EQNARRAY:
579                                 default:
580                                         MathGridInset::addCol(1);
581                                         MathGridInset::addCol(1);
582
583                                         // split it "nicely" on the firest relop
584                                         pos_type pos = firstRelOp(cell(0));
585                                         cell(1) = MathArray(cell(0), pos, cell(0).size());
586                                         cell(0).erase(pos, cell(0).size());
587
588                                         if (cell(1).size()) {
589                                                 cell(2) = MathArray(cell(1), 1, cell(1).size());
590                                                 cell(1).erase(1, cell(1).size());
591                                         }
592
593                                         setType(LM_OT_EQNARRAY);
594                                         mutate(newtype);
595                                         break;
596                                 }
597                         break;
598
599                 case LM_OT_EQNARRAY:
600                         switch (newtype) {
601                                 case LM_OT_SIMPLE:
602                                 case LM_OT_EQUATION: {
603                                         // set correct (no)numbering
604                                         bool allnonum = true;
605                                         for (row_type row = 0; row < nrows(); ++row) {
606                                                 if (!nonum_[row])
607                                                         allnonum = false;
608                                         }
609
610                                         // set first non-empty label
611                                         string label;
612                                         for (row_type row = 0; row < nrows(); ++row) {
613                                                 if (!label_[row].empty()) {
614                                                         label = label_[row];
615                                                         break;
616                                                 }
617                                         }
618
619                                         glueall();
620
621                                         nonum_[0] = allnonum;
622                                         label_[0] = label;
623                                         mutate(newtype);
624                                         break;
625                                 }
626
627                                 case LM_OT_ALIGN:
628                                 case LM_OT_ALIGNAT:
629                                 case LM_OT_XALIGNAT:
630                                 case LM_OT_XXALIGNAT:
631                                 default: {
632                                         for (row_type row = 0; row < nrows(); ++row) {
633                                                 idx_type c = 3 * row + 1;
634                                                 cell(c).push_back(cell(c + 1));
635                                         }
636                                         MathGridInset::delCol(2);
637                                         setType(LM_OT_ALIGN);
638                                         mutate(newtype);
639                                         break;
640                                 }
641                         }
642                         break;
643
644                 case LM_OT_ALIGN:
645                         switch (newtype) {
646                                 case LM_OT_SIMPLE:
647                                 case LM_OT_EQUATION:
648                                 case LM_OT_EQNARRAY:
649                                         MathGridInset::addCol(1);
650                                         setType(LM_OT_EQNARRAY);
651                                         mutate(newtype);
652                                         break;
653
654                                 case LM_OT_ALIGNAT:
655                                 case LM_OT_XALIGNAT:
656                                 case LM_OT_XXALIGNAT:
657                                         setType(newtype);
658                                         break;
659
660                                 default:
661                                         lyxerr << "mutation from '" << getType()
662                                                 << "' to '" << newtype << "' not implemented"
663                                                << endl;
664                                         break;
665                         }
666                         break;
667
668                 case LM_OT_MULTLINE:
669                         switch (newtype) {
670                                 case LM_OT_GATHER:
671                                         setType(LM_OT_GATHER);
672                                         break;
673                                 default:
674                                         lyxerr << "mutation from '" << getType()
675                                                 << "' to '" << newtype << "' not implemented"
676                                                << endl;
677                                         break;
678                         }
679
680                 case LM_OT_GATHER:
681                         switch (newtype) {
682                                 case LM_OT_MULTLINE:
683                                         setType(LM_OT_MULTLINE);
684                                         break;
685                                 default:
686                                         lyxerr << "mutation from '" << getType()
687                                                 << "' to '" << newtype << "' not implemented"
688                                                << endl;
689                                         break;
690                         }
691
692                 default:
693                         lyxerr << "mutation from '" << getType()
694                                << "' to '" << newtype << "' not implemented"
695                                << endl;
696                         break;
697         }
698 }
699
700
701 void MathHullInset::write(WriteStream & os) const
702 {
703         header_write(os);
704
705         bool n = numberedType();
706
707         for (row_type row = 0; row < nrows(); ++row) {
708                 for (col_type col = 0; col < ncols(); ++col)
709                         os << cell(index(row, col)) << eocString(col);
710                 if (n) {
711                         if (!label_[row].empty())
712                                 os << "\\label{" << label_[row] << "}";
713                         if (nonum_[row])
714                                 os << "\\nonumber ";
715                 }
716                 os << eolString(row);
717         }
718
719         footer_write(os);
720 }
721
722
723 void MathHullInset::normalize(NormalStream & os) const
724 {
725         os << "[formula " << normalName(getType()) << " ";
726         MathGridInset::normalize(os);
727         os << "] ";
728 }
729
730
731 void MathHullInset::mathmlize(MathMLStream & os) const
732 {
733         MathGridInset::mathmlize(os);
734 }
735
736
737 void MathHullInset::infoize(std::ostream & os) const
738 {
739         os << normalName(getType());
740 }
741
742 void MathHullInset::check() const
743 {
744         lyx::Assert(nonum_.size() == nrows());
745         lyx::Assert(label_.size() == nrows());
746 }