]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hullinset.C
Removed all redundant using directives from the source.
[lyx.git] / src / mathed / math_hullinset.C
1 /**
2  * \file math_hullinset.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_hullinset.h"
14 #include "math_mathmlstream.h"
15 #include "math_streamstr.h"
16 #include "math_cursor.h"
17 #include "math_support.h"
18 #include "math_extern.h"
19 #include "math_charinset.h"
20 #include "debug.h"
21 #include "textpainter.h"
22 #include "funcrequest.h"
23 #include "support/std_sstream.h"
24 #include "LaTeXFeatures.h"
25 #include "support/LAssert.h"
26
27 #include "frontends/Alert.h"
28 #include "lyxrc.h"
29 #include "gettext.h"
30
31 using namespace lyx::support;
32
33 using std::endl;
34 using std::max;
35
36 using std::auto_ptr;
37 using std::istringstream;
38 using std::ostringstream;
39 using std::pair;
40
41
42 namespace {
43
44         int getCols(string const & type)
45         {
46                 if (type == "eqnarray")
47                         return 3;
48                 if (type == "align")
49                         return 2;
50                 if (type == "flalign")
51                         return 2;
52                 if (type == "alignat")
53                         return 2;
54                 if (type == "xalignat")
55                         return 2;
56                 if (type == "xxalignat")
57                         return 2;
58                 return 1;
59         }
60
61
62         // returns position of first relation operator in the array
63         // used for "intelligent splitting"
64         MathArray::size_type firstRelOp(MathArray const & ar)
65         {
66                 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
67                         if ((*it)->isRelOp())
68                                 return it - ar.begin();
69                 return ar.size();
70         }
71
72
73         char const * star(bool numbered)
74         {
75                 return numbered ? "" : "*";
76         }
77
78
79         int typecode(string const & s)
80         {
81                 if (s == "none")      return 0;
82                 if (s == "simple")    return 1;
83                 if (s == "equation")  return 2;
84                 if (s == "eqnarray")  return 3;
85                 if (s == "align")     return 4;
86                 if (s == "alignat")   return 5;
87                 if (s == "xalignat")  return 6;
88                 if (s == "xxalignat") return 7;
89                 if (s == "multline")  return 8;
90                 if (s == "gather")    return 9;
91                 if (s == "flalign")   return 10;
92                 lyxerr << "unknown hull type '" << s << "'" << endl;
93                 return 0;
94         }
95
96         bool smaller(string const & s, string const & t)
97         {
98                 return typecode(s) < typecode(t);
99         }
100
101
102 } // end anon namespace
103
104
105 MathHullInset::MathHullInset()
106         : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
107 {
108         setDefaults();
109 }
110
111
112 MathHullInset::MathHullInset(string const & type)
113         : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
114 {
115         setDefaults();
116 }
117
118
119 auto_ptr<InsetBase> MathHullInset::clone() const
120 {
121         return auto_ptr<InsetBase>(new MathHullInset(*this));
122 }
123
124
125 MathInset::mode_type MathHullInset::currentMode() const
126 {
127         if (type_ == "none")
128                 return UNDECIDED_MODE;
129         // definitely math mode ...
130         return MATH_MODE;
131 }
132
133
134 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
135 {
136         idx = 0;
137         pos = 0;
138         return true;
139 }
140
141
142 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
143 {
144         idx = nargs() - 1;
145         pos = cell(idx).size();
146         return true;
147 }
148
149
150 char MathHullInset::defaultColAlign(col_type col)
151 {
152         if (type_ == "eqnarray")
153                 return "rcl"[col];
154         if (typecode(type_) >= typecode("align"))
155                 return "rl"[col & 1];
156         return 'c';
157 }
158
159
160 int MathHullInset::defaultColSpace(col_type col)
161 {
162         if (type_ == "align" || type_ == "alignat")
163                 return 0;
164         if (type_ == "xalignat")
165                 return (col & 1) ? 20 : 0;
166         if (type_ == "xxalignat" || type_ == "flalign")
167                 return (col & 1) ? 40 : 0;
168         return 0;
169 }
170
171
172 char const * MathHullInset::standardFont() const
173 {
174         if (type_ == "none")
175                 return "lyxnochange";
176         return "mathnormal";
177 }
178
179
180 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
181 {
182         FontSetChanger dummy1(mi.base, standardFont());
183         StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
184
185         // let the cells adjust themselves
186         MathGridInset::metrics(mi);
187
188         if (display()) {
189                 dim_.asc += 12;
190                 dim_.des += 12;
191         }
192
193         if (numberedType()) {
194                 FontSetChanger dummy(mi.base, "mathbf");
195                 int l = 0;
196                 for (row_type row = 0; row < nrows(); ++row)
197                         l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
198
199                 if (l)
200                         dim_.wid += 30 + l;
201         }
202
203         // make it at least as high as the current font
204         int asc = 0;
205         int des = 0;
206         math_font_max_dim(mi.base.font, asc, des);
207         dim_.asc = max(dim_.asc, asc);
208         dim_.des = max(dim_.des, des);
209
210         // for markers
211         metricsMarkers2();
212         dim = dim_;
213 }
214
215
216 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
217 {
218         FontSetChanger dummy1(pi.base, standardFont());
219         StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
220         MathGridInset::draw(pi, x + 1, y);
221
222         if (numberedType()) {
223                 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
224                 for (row_type row = 0; row < nrows(); ++row) {
225                         int const yy = y + rowinfo_[row].offset_;
226                         FontSetChanger dummy(pi.base, "mathrm");
227                         drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
228                 }
229         }
230
231         drawMarkers2(pi, x, y);
232 }
233
234
235 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
236 {
237         if (display()) {
238                 MathGridInset::metricsT(mi, dim);
239         } else {
240                 ostringstream os;
241                 WriteStream wi(os, false, true);
242                 write(wi);
243                 dim.wid = os.str().size();
244                 dim.asc = 1;
245                 dim.des = 0;
246         }
247 }
248
249
250 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
251 {
252         if (display()) {
253                 MathGridInset::drawT(pain, x, y);
254         } else {
255                 ostringstream os;
256                 WriteStream wi(os, false, true);
257                 write(wi);
258                 pain.draw(x, y, os.str().c_str());
259         }
260 }
261
262
263 string MathHullInset::label(row_type row) const
264 {
265         row_type n = nrows();
266         Assert(row < n);
267         return label_[row];
268 }
269
270
271 void MathHullInset::label(row_type row, string const & label)
272 {
273         //lyxerr << "setting label '" << label << "' for row " << row << endl;
274         label_[row] = label;
275 }
276
277
278 void MathHullInset::numbered(row_type row, bool num)
279 {
280         nonum_[row] = !num;
281 }
282
283
284 bool MathHullInset::numbered(row_type row) const
285 {
286         return !nonum_[row];
287 }
288
289
290 bool MathHullInset::ams() const
291 {
292         return
293                 type_ == "align" ||
294                 type_ == "flalign" ||
295                 type_ == "multline" ||
296                 type_ == "gather" ||
297                 type_ == "alignat" ||
298                 type_ == "xalignat" ||
299                 type_ == "xxalignat";
300 }
301
302
303 bool MathHullInset::display() const
304 {
305         return type_ != "simple" && type_ != "none";
306 }
307
308
309 void MathHullInset::getLabelList(std::vector<string> & labels) const
310 {
311         for (row_type row = 0; row < nrows(); ++row)
312                 if (!label_[row].empty() && nonum_[row] != 1)
313                         labels.push_back(label_[row]);
314 }
315
316
317 bool MathHullInset::numberedType() const
318 {
319         if (type_ == "none")
320                 return false;
321         if (type_ == "simple")
322                 return false;
323         if (type_ == "xxalignat")
324                 return false;
325         for (row_type row = 0; row < nrows(); ++row)
326                 if (!nonum_[row])
327                         return true;
328         return false;
329 }
330
331
332 void MathHullInset::validate(LaTeXFeatures & features) const
333 {
334         if (ams())
335                 features.require("amsmath");
336
337
338         // Validation is necessary only if not using AMS math.
339         // To be safe, we will always run mathedvalidate.
340         //if (features.amsstyle)
341         //  return;
342
343         features.require("boldsymbol");
344         //features.binom      = true;
345
346         MathGridInset::validate(features);
347 }
348
349
350 void MathHullInset::header_write(WriteStream & os) const
351 {
352         bool n = numberedType();
353
354         if (type_ == "none")
355                 ;
356
357         else if (type_ == "simple") {
358                 os << '$';
359                 if (cell(0).empty())
360                         os << ' ';
361         }
362
363         else if (type_ == "equation") {
364                 if (n)
365                         os << "\\begin{equation" << star(n) << "}\n";
366                 else
367                         os << "\\[\n";
368         }
369
370         else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
371                  || type_ == "gather" || type_ == "multline")
372                         os << "\\begin{" << type_ << star(n) << "}\n";
373
374         else if (type_ == "alignat" || type_ == "xalignat")
375                 os << "\\begin{" << type_ << star(n) << '}'
376                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
377
378         else if (type_ == "xxalignat")
379                 os << "\\begin{" << type_ << '}'
380                   << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
381
382         else
383                 os << "\\begin{unknown" << star(n) << '}';
384 }
385
386
387 void MathHullInset::footer_write(WriteStream & os) const
388 {
389         bool n = numberedType();
390
391         if (type_ == "none")
392                 os << "\n";
393
394         else if (type_ == "simple")
395                 os << '$';
396
397         else if (type_ == "equation")
398                 if (n)
399                         os << "\\end{equation" << star(n) << "}\n";
400                 else
401                         os << "\\]\n";
402
403         else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
404                  || type_ == "alignat" || type_ == "xalignat"
405                  || type_ == "gather" || type_ == "multline")
406                 os << "\\end{" << type_ << star(n) << "}\n";
407
408         else if (type_ == "xxalignat")
409                 os << "\\end{" << type_ << "}\n";
410
411         else
412                 os << "\\end{unknown" << star(n) << '}';
413 }
414
415
416 bool MathHullInset::colChangeOK() const
417 {
418         return
419                 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
420                 type_ == "xalignat" || type_ == "xxalignat";
421 }
422
423
424 void MathHullInset::addRow(row_type row)
425 {
426         nonum_.insert(nonum_.begin() + row + 1, !numberedType());
427         label_.insert(label_.begin() + row + 1, string());
428         MathGridInset::addRow(row);
429 }
430
431
432 void MathHullInset::delRow(row_type row)
433 {
434         if (nrows() <= 1)
435                 return;
436         MathGridInset::delRow(row);
437         nonum_.erase(nonum_.begin() + row);
438         label_.erase(label_.begin() + row);
439 }
440
441
442 void MathHullInset::addCol(col_type col)
443 {
444         if (colChangeOK())
445                 MathGridInset::addCol(col);
446         else
447                 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
448 }
449
450
451 void MathHullInset::delCol(col_type col)
452 {
453         if (colChangeOK())
454                 MathGridInset::delCol(col);
455         else
456                 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
457 }
458
459
460 string MathHullInset::nicelabel(row_type row) const
461 {
462         if (nonum_[row])
463                 return string();
464         if (label_[row].empty())
465                 return string("(#)");
466         return '(' + label_[row] + ')';
467 }
468
469
470 void MathHullInset::glueall()
471 {
472         MathArray ar;
473         for (idx_type i = 0; i < nargs(); ++i)
474                 ar.append(cell(i));
475         *this = MathHullInset("simple");
476         cell(0) = ar;
477         setDefaults();
478 }
479
480
481 string const & MathHullInset::getType() const
482 {
483         return type_;
484 }
485
486
487 void MathHullInset::setType(string const & type)
488 {
489         type_ = type;
490         setDefaults();
491 }
492
493
494
495 void MathHullInset::mutate(string const & newtype)
496 {
497         //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
498
499         // we try to move along the chain
500         // none <-> simple <-> equation <-> eqnarray
501
502         if (newtype == "dump") {
503                 dump();
504         }
505
506         else if (newtype == type_) {
507                 // done
508         }
509
510         else if (type_ == "none") {
511                 setType("simple");
512                 numbered(0, false);
513                 mutate(newtype);
514         }
515
516         else if (type_ == "simple") {
517                 if (newtype == "none") {
518                         setType("none");
519                 } else {
520                         setType("equation");
521                         numbered(0, false);
522                         mutate(newtype);
523                 }
524         }
525
526         else if (type_ == "equation") {
527                 if (smaller(newtype, type_)) {
528                         setType("simple");
529                         mutate(newtype);
530                 } else if (newtype == "eqnarray") {
531                         MathGridInset::addCol(1);
532                         MathGridInset::addCol(1);
533
534                         // split it "nicely" on the firest relop
535                         pos_type pos = firstRelOp(cell(0));
536                         cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
537                         cell(0).erase(pos, cell(0).size());
538
539                         if (cell(1).size()) {
540                                 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
541                                 cell(1).erase(1, cell(1).size());
542                         }
543                         setType("eqnarray");
544                         mutate(newtype);
545                 } else if (newtype == "multline" || newtype == "gather") {
546                         setType(newtype);
547                         numbered(0, false);
548                 } else {
549                         MathGridInset::addCol(1);
550                         // split it "nicely"
551                         pos_type pos = firstRelOp(cell(0));
552                         cell(1) = cell(0);
553                         cell(0).erase(pos, cell(0).size());
554                         cell(1).erase(0, pos);
555                         setType("align");
556                         mutate(newtype);
557                 }
558         }
559
560         else if (type_ == "eqnarray") {
561                 if (smaller(newtype, type_)) {
562                         // set correct (no)numbering
563                         bool allnonum = true;
564                         for (row_type row = 0; row < nrows(); ++row)
565                                 if (!nonum_[row])
566                                         allnonum = false;
567
568                         // set first non-empty label
569                         string label;
570                         for (row_type row = 0; row < nrows(); ++row) {
571                                 if (!label_[row].empty()) {
572                                         label = label_[row];
573                                         break;
574                                 }
575                         }
576
577                         glueall();
578                         nonum_[0] = allnonum;
579                         label_[0] = label;
580                         mutate(newtype);
581                 } else { // align & Co.
582                         for (row_type row = 0; row < nrows(); ++row) {
583                                 idx_type c = 3 * row + 1;
584                                 cell(c).append(cell(c + 1));
585                         }
586                         MathGridInset::delCol(2);
587                         setType("align");
588                         mutate(newtype);
589                 }
590         }
591
592         else if (type_ == "align") {
593                 if (smaller(newtype, type_)) {
594                         MathGridInset::addCol(1);
595                         setType("eqnarray");
596                         mutate(newtype);
597                 } else {
598                         setType(newtype);
599                 }
600         }
601
602         else if (type_ == "multline") {
603                 if (newtype == "gather" || newtype == "align" ||
604                     newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
605                         setType(newtype);
606                 else if (newtype == "eqnarray") {
607                         MathGridInset::addCol(1);
608                         MathGridInset::addCol(1);
609                         setType("eqnarray");
610                 } else {
611                         lyxerr << "mutation from '" << type_
612                                 << "' to '" << newtype << "' not implemented" << endl;
613                 }
614         }
615
616         else if (type_ == "gather") {
617                 if (newtype == "multline") {
618                         setType("multline");
619                 } else {
620                         lyxerr << "mutation from '" << type_
621                                 << "' to '" << newtype << "' not implemented" << endl;
622                 }
623         }
624
625         else {
626                 lyxerr << "mutation from '" << type_
627                                          << "' to '" << newtype << "' not implemented" << endl;
628         }
629 }
630
631
632 string MathHullInset::eolString(row_type row, bool fragile) const
633 {
634         string res;
635         if (numberedType()) {
636                 if (!label_[row].empty() && !nonum_[row])
637                         res += "\\label{" + label_[row] + '}';
638                 if (nonum_[row] && (type_ != "multline"))
639                         res += "\\nonumber ";
640         }
641         return res + MathGridInset::eolString(row, fragile);
642 }
643
644
645 void MathHullInset::write(WriteStream & os) const
646 {
647         header_write(os);
648         MathGridInset::write(os);
649         footer_write(os);
650 }
651
652
653 void MathHullInset::normalize(NormalStream & os) const
654 {
655         os << "[formula " << type_ << ' ';
656         MathGridInset::normalize(os);
657         os << "] ";
658 }
659
660
661 void MathHullInset::mathmlize(MathMLStream & os) const
662 {
663         MathGridInset::mathmlize(os);
664 }
665
666
667 void MathHullInset::infoize(std::ostream & os) const
668 {
669         os << "Type: " << type_;
670 }
671
672
673 void MathHullInset::check() const
674 {
675         Assert(nonum_.size() == nrows());
676         Assert(label_.size() == nrows());
677 }
678
679
680 void MathHullInset::doExtern
681         (FuncRequest const & func, idx_type & idx, pos_type & pos)
682 {
683         string lang;
684         string extra;
685         istringstream iss(func.argument.c_str());
686         iss >> lang >> extra;
687         if (extra.empty())
688                 extra = "noextra";
689
690 #ifdef WITH_WARNINGS
691 #warning temporarily disabled
692         //if (selection()) {
693         //      MathArray ar;
694         //      selGet(ar);
695         //      lyxerr << "use selection: " << ar << endl;
696         //      insert(pipeThroughExtern(lang, extra, ar));
697         //      return;
698         //}
699 #endif
700
701         MathArray eq;
702         eq.push_back(MathAtom(new MathCharInset('=')));
703
704         // go to first item in line
705         idx -= idx % ncols();
706         pos = 0;
707
708         if (getType() == "simple") {
709                 size_type pos = cell(idx).find_last(eq);
710                 MathArray ar;
711                 if (mathcursor && mathcursor->selection()) {
712                         asArray(mathcursor->grabAndEraseSelection(), ar);
713                 } else if (pos == cell(idx).size()) {
714                         ar = cell(idx);
715                         lyxerr << "use whole cell: " << ar << endl;
716                 } else {
717                         ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
718                         lyxerr << "use partial cell form pos: " << pos << endl;
719                 }
720                 cell(idx).append(eq);
721                 cell(idx).append(pipeThroughExtern(lang, extra, ar));
722                 pos = cell(idx).size();
723                 return;
724         }
725
726         if (getType() == "equation") {
727                 lyxerr << "use equation inset" << endl;
728                 mutate("eqnarray");
729                 MathArray & ar = cell(idx);
730                 lyxerr << "use cell: " << ar << endl;
731                 cell(idx + 1) = eq;
732                 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
733                 // move to end of line
734                 idx += 2;
735                 pos = cell(idx).size();
736                 return;
737         }
738
739         {
740                 lyxerr << "use eqnarray" << endl;
741                 idx -= idx % ncols();
742                 idx += 2;
743                 pos = 0;
744                 MathArray ar = cell(idx);
745                 lyxerr << "use cell: " << ar << endl;
746 #ifdef WITH_WARNINGS
747 #warning temporarily disabled
748 #endif
749                 addRow(row(idx));
750                 cell(idx + 2) = eq;
751                 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
752                 idx += 3;
753                 pos = cell(idx).size();
754         }
755 }
756
757
758 dispatch_result MathHullInset::dispatch
759         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
760 {
761         switch (cmd.action) {
762
763                 case LFUN_BREAKLINE:
764                         if (type_ == "simple" || type_ == "equation") {
765                                 mutate("eqnarray");
766                                 idx = 1;
767                                 pos = 0;
768                                 return DISPATCHED_POP;
769                         }
770                         return MathGridInset::dispatch(cmd, idx, pos);
771
772                 case LFUN_MATH_NUMBER:
773                         //lyxerr << "toggling all numbers" << endl;
774                         if (display()) {
775                                 //recordUndo(bv, Undo::INSERT);
776                                 bool old = numberedType();
777                                 if (type_ == "multline")
778                                         numbered(nrows() - 1, !old);
779                                 else
780                                         for (row_type row = 0; row < nrows(); ++row)
781                                                 numbered(row, !old);
782                                 //bv->owner()->message(old ? _("No number") : _("Number"));
783                         }
784                         return DISPATCHED;
785
786                 case LFUN_MATH_NONUMBER:
787                         if (display()) {
788                                 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
789                                 //recordUndo(bv, Undo::INSERT);
790                                 bool old = numbered(r);
791                                 //bv->owner()->message(old ? _("No number") : _("Number"));
792                                 numbered(r, !old);
793                         }
794                         return DISPATCHED;
795
796                 case LFUN_INSERT_LABEL: {
797                         row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
798                         string old_label = label(r);
799                         string new_label = cmd.argument;
800
801                         if (new_label.empty()) {
802                                 string const default_label =
803                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
804                                 pair<bool, string> const res = old_label.empty()
805                                         ? Alert::askForText(_("Enter new label to insert:"), default_label)
806                                         : Alert::askForText(_("Enter label:"), old_label);
807                                 if (!res.first)
808                                         return UNDISPATCHED;
809                                 new_label = trim(res.second);
810                         }
811
812                         //if (new_label == old_label)
813                         //      break;  // Nothing to do
814
815                         if (!new_label.empty())
816                                 numbered(r, true);
817                         label(r, new_label);
818                         return DISPATCHED;
819                 }
820
821                 case LFUN_MATH_EXTERN:
822                         doExtern(cmd, idx, pos);
823                         return DISPATCHED_POP;
824
825                 case LFUN_MATH_MUTATE: {
826                         row_type r = row(idx);
827                         col_type c = col(idx);
828                         mutate(cmd.argument);
829                         idx = r * ncols() + c;
830                         if (idx >= nargs())
831                                 idx = nargs() - 1;
832                         if (pos > cell(idx).size())
833                                 pos = cell(idx).size();
834                         return DISPATCHED_POP;
835                 }
836
837                 case LFUN_MATH_DISPLAY: {
838                         mutate(type_ == "simple" ? "equation" : "simple");
839                         idx = 0;
840                         pos = cell(idx).size();
841                         return DISPATCHED_POP;
842                 }
843
844                 default:
845                         return MathGridInset::dispatch(cmd, idx, pos);
846         }
847 }
848
849
850 string MathHullInset::fileInsetLabel() const
851 {
852         return "Formula";
853 }