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