]> git.lyx.org Git - lyx.git/blob - src/mathed/math_decorationinset.C
require amsmath for some under/over arrows
[lyx.git] / src / mathed / math_decorationinset.C
1 /**
2  * \file math_decorationinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "LaTeXFeatures.h"
15 #include "math_decorationinset.h"
16 #include "math_data.h"
17 #include "math_support.h"
18 #include "math_parser.h"
19 #include "math_mathmlstream.h"
20 #include "math_streamstr.h"
21
22 #include "debug.h"
23
24 #include "support/std_ostream.h"
25
26 using std::auto_ptr;
27
28
29 MathDecorationInset::MathDecorationInset(latexkeys const * key)
30         : MathNestInset(1), key_(key)
31 {
32 //      lyxerr << " creating deco " << key->name << std::endl;
33 }
34
35
36 auto_ptr<InsetBase> MathDecorationInset::doClone() const
37 {
38         return auto_ptr<InsetBase>(new MathDecorationInset(*this));
39 }
40
41
42 bool MathDecorationInset::upper() const
43 {
44         return key_->name.substr(0, 5) != "under";
45 }
46
47
48 bool MathDecorationInset::isScriptable() const
49 {
50         return
51                         key_->name == "overbrace" ||
52                         key_->name == "underbrace" ||
53                         key_->name == "overleftarrow" ||
54                         key_->name == "overrightarrow" ||
55                         key_->name == "overleftrightarrow" ||
56                         key_->name == "underleftarrow" ||
57                         key_->name == "underrightarrow" ||
58                         key_->name == "underleftrightarrow";
59 }
60
61
62 bool MathDecorationInset::protect() const
63 {
64         return
65                         key_->name == "overbrace" ||
66                         key_->name == "underbrace" ||
67                         key_->name == "overleftarrow" ||
68                         key_->name == "overrightarrow" ||
69                         key_->name == "overleftrightarrow" ||
70                         key_->name == "underleftarrow" ||
71                         key_->name == "underrightarrow" ||
72                         key_->name == "underleftrightarrow";
73 }
74
75
76 bool MathDecorationInset::wide() const
77 {
78         return
79                         key_->name == "overline" ||
80                         key_->name == "underline" ||
81                         key_->name == "overbrace" ||
82                         key_->name == "underbrace" ||
83                         key_->name == "overleftarrow" ||
84                         key_->name == "overrightarrow" ||
85                         key_->name == "overleftrightarrow" ||
86                         key_->name == "widehat" ||
87                         key_->name == "widetilde" ||
88                         key_->name == "underleftarrow" ||
89                         key_->name == "underrightarrow" ||
90                         key_->name == "underleftrightarrow";
91 }
92
93
94 bool MathDecorationInset::ams() const
95 {
96         return  
97                         key_->name == "overleftrightarrow" ||
98                         key_->name == "underleftarrow" ||
99                         key_->name == "underrightarrow" ||
100                         key_->name == "underleftrightarrow";
101 }
102
103
104 void MathDecorationInset::metrics(MetricsInfo & mi, Dimension & dim) const
105 {
106         cell(0).metrics(mi, dim);
107
108         dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
109         dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
110
111         if (upper()) {
112                 dy_ = -dim.asc - dh_;
113                 dim.asc += dh_ + 1;
114         } else {
115                 dy_ = dim.des + 1;
116                 dim.des += dh_ + 2;
117         }
118
119         metricsMarkers(dim);
120         dim_ = dim;
121 }
122
123
124 void MathDecorationInset::draw(PainterInfo & pi, int x, int y) const
125 {
126         cell(0).draw(pi, x + 1, y);
127         if (wide())
128                 mathed_draw_deco(pi, x + 1, y + dy_, cell(0).width(), dh_, key_->name);
129         else
130                 mathed_draw_deco(pi, x + 1 + (cell(0).width() - dw_) / 2,
131                         y + dy_, dw_, dh_, key_->name);
132         drawMarkers(pi, x, y);
133 }
134
135
136 void MathDecorationInset::validate(LaTeXFeatures & features) const
137 {
138         if (ams())
139                 features.require("amsmath");
140 }
141
142
143 void MathDecorationInset::write(WriteStream & os) const
144 {
145         if (os.fragile() && protect())
146                 os << "\\protect";
147         os << '\\' << key_->name << '{' << cell(0) << '}';
148 }
149
150
151 void MathDecorationInset::normalize(NormalStream & os) const
152 {
153         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
154 }
155
156
157 void MathDecorationInset::infoize(std::ostream & os) const
158 {
159         os << "Deco: " << key_->name;
160 }