]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.C
do not create invalid .lyx files when importing \i{}, \j{}, \l{} or \L{}.
[lyx.git] / src / mathed / InsetMathDecoration.C
1 /**
2  * \file InsetMathDecoration.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 "InsetMathDecoration.h"
15 #include "MathData.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MathStream.h"
20
21 #include "LaTeXFeatures.h"
22 #include "debug.h"
23
24 #include "support/std_ostream.h"
25
26
27 namespace lyx {
28
29
30 InsetMathDecoration::InsetMathDecoration(latexkeys const * key)
31         : InsetMathNest(1), key_(key)
32 {
33 //      lyxerr << " creating deco " << key->name << std::endl;
34 }
35
36
37 std::auto_ptr<InsetBase> InsetMathDecoration::doClone() const
38 {
39         return std::auto_ptr<InsetBase>(new InsetMathDecoration(*this));
40 }
41
42
43 bool InsetMathDecoration::upper() const
44 {
45         return key_->name.substr(0, 5) != "under";
46 }
47
48
49 bool InsetMathDecoration::isScriptable() const
50 {
51         return
52                         key_->name == "overbrace" ||
53                         key_->name == "underbrace" ||
54                         key_->name == "overleftarrow" ||
55                         key_->name == "overrightarrow" ||
56                         key_->name == "overleftrightarrow" ||
57                         key_->name == "underleftarrow" ||
58                         key_->name == "underrightarrow" ||
59                         key_->name == "underleftrightarrow";
60 }
61
62
63 bool InsetMathDecoration::protect() const
64 {
65         return
66                         key_->name == "overbrace" ||
67                         key_->name == "underbrace" ||
68                         key_->name == "overleftarrow" ||
69                         key_->name == "overrightarrow" ||
70                         key_->name == "overleftrightarrow" ||
71                         key_->name == "underleftarrow" ||
72                         key_->name == "underrightarrow" ||
73                         key_->name == "underleftrightarrow";
74 }
75
76
77 bool InsetMathDecoration::wide() const
78 {
79         return
80                         key_->name == "overline" ||
81                         key_->name == "underline" ||
82                         key_->name == "overbrace" ||
83                         key_->name == "underbrace" ||
84                         key_->name == "overleftarrow" ||
85                         key_->name == "overrightarrow" ||
86                         key_->name == "overleftrightarrow" ||
87                         key_->name == "widehat" ||
88                         key_->name == "widetilde" ||
89                         key_->name == "underleftarrow" ||
90                         key_->name == "underrightarrow" ||
91                         key_->name == "underleftrightarrow";
92 }
93
94
95 bool InsetMathDecoration::ams() const
96 {
97         return
98                         key_->name == "overleftrightarrow" ||
99                         key_->name == "underleftarrow" ||
100                         key_->name == "underrightarrow" ||
101                         key_->name == "underleftrightarrow";
102 }
103
104
105 bool InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
106 {
107         cell(0).metrics(mi, dim);
108
109         dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
110         dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
111
112         if (upper()) {
113                 dy_ = -dim.asc - dh_;
114                 dim.asc += dh_ + 1;
115         } else {
116                 dy_ = dim.des + 1;
117                 dim.des += dh_ + 2;
118         }
119
120         metricsMarkers(dim);
121         if (dim_ == dim)
122                 return false;
123         dim_ = dim;
124         return true;
125 }
126
127
128 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
129 {
130         cell(0).draw(pi, x + 1, y);
131         if (wide())
132                 mathed_draw_deco(pi, x + 1, y + dy_, cell(0).width(), dh_, key_->name);
133         else
134                 mathed_draw_deco(pi, x + 1 + (cell(0).width() - dw_) / 2,
135                         y + dy_, dw_, dh_, key_->name);
136         drawMarkers(pi, x, y);
137         setPosCache(pi, x, y);
138 }
139
140
141 void InsetMathDecoration::write(WriteStream & os) const
142 {
143         if (os.fragile() && protect())
144                 os << "\\protect";
145         os << '\\' << key_->name << '{' << cell(0) << '}';
146 }
147
148
149 void InsetMathDecoration::normalize(NormalStream & os) const
150 {
151         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
152 }
153
154
155 void InsetMathDecoration::infoize(odocstream & os) const
156 {
157         os << "Deco: " << key_->name;
158 }
159
160
161 void InsetMathDecoration::validate(LaTeXFeatures & features) const
162 {
163         if (ams())
164                 features.require("amsmath");
165         InsetMathNest::validate(features);
166 }
167
168
169 } // namespace lyx