]> git.lyx.org Git - lyx.git/blob - src/mathed/math_scriptinset.C
mathed95.diff
[lyx.git] / src / mathed / math_scriptinset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include <functional>
8
9 #include "math_scriptinset.h"
10 #include "LColor.h"
11 #include "Painter.h"
12 #include "debug.h"
13 #include "mathed/support.h"
14 #include "support/LOstream.h"
15
16
17 MathScriptInset::MathScriptInset()
18         : MathInset("script", LM_OT_SCRIPT, 2), up_(false), down_(false)
19 {}
20
21 MathScriptInset::MathScriptInset(bool up, bool down)
22         : MathInset("script", LM_OT_SCRIPT, 2), up_(up), down_(down)
23 {}
24
25
26 MathInset * MathScriptInset::clone() const
27 {   
28         return new MathScriptInset(*this);
29 }
30
31
32 void MathScriptInset::Metrics(MathStyles st)
33 {
34         size_ = smallerStyleScript(st);
35         xcell(0).Metrics(size_);
36         xcell(1).Metrics(size_);
37
38         width_   = std::max(xcell(0).width(), xcell(1).width()) + 2; 
39         if (up())
40                 ascent_  = std::max(ascent_, xcell(0).height() + 9);
41         if (down())
42                 descent_ = std::max(descent_, xcell(1).height());
43 }
44
45
46 bool MathScriptInset::up() const
47 {
48         return up_;
49 }
50
51 bool MathScriptInset::down() const
52 {
53         return down_;
54 }
55
56 void MathScriptInset::up(bool b)
57 {
58         up_ = b;
59 }
60
61 void MathScriptInset::down(bool b)
62 {
63         down_ = b;
64 }
65
66
67 void MathScriptInset::draw(Painter & pain, int x, int y)
68
69         xo(x);
70         yo(y);
71         if (up())
72                 xcell(0).draw(pain, x, y - xcell(0).descent() - 9);
73         if (down())
74                 xcell(1).draw(pain, x, y + xcell(1).ascent());
75 }
76
77
78 void MathScriptInset::Write(std::ostream & os, bool fragile) const
79 {
80         if (up()) {
81                 os << "^{";
82                 cell(0).Write(os, fragile);
83                 os << "}";
84         }
85         if (down()) {
86                 os << "_{";
87                 cell(1).Write(os, fragile);
88                 os << "}";
89         }
90 }
91
92
93 void MathScriptInset::WriteNormal(std::ostream & os) const
94 {
95         if (up()) {
96                 os << "[superscript ";
97                 cell(0).WriteNormal(os);
98                 os << "] ";
99         }
100         if (down()) {
101                 os << "[subscript ";
102                 cell(1).WriteNormal(os);
103                 os << "] ";
104         }
105 }
106
107 bool MathScriptInset::idxRight(int &, int &) const
108 {
109         return false;
110 }
111
112 bool MathScriptInset::idxLeft(int &, int &) const
113 {
114         return false;
115 }
116
117
118 bool MathScriptInset::idxUp(int & idx, int & pos) const
119 {
120         if (idx == 0 || !up()) 
121                 return false;
122         idx = 0;
123         pos = 0;
124         return true;
125 }
126
127 bool MathScriptInset::idxDown(int & idx, int & pos) const
128 {
129         if (idx == 1 || !down()) 
130                 return false;
131         idx = 1;
132         pos = 0;
133         return true;
134 }
135
136 bool MathScriptInset::idxFirst(int & idx, int & pos) const
137 {
138         idx = up() ? 0 : 1;
139         pos = 0;
140         return true;
141 }
142
143 bool MathScriptInset::idxLast(int & idx, int & pos) const
144 {
145         idx = down() ? 1 : 0;
146         pos = cell(idx).size();
147         return true;
148 }
149
150
151 bool MathScriptInset::idxFirstUp(int & idx, int & pos) const
152 {
153         if (!up()) 
154                 return false;
155         idx = 0;
156         pos = 0;
157         return true;
158 }
159
160 bool MathScriptInset::idxFirstDown(int & idx, int & pos) const
161 {
162         if (!down()) 
163                 return false;
164         idx = 1;
165         pos = 0;
166         return true;
167 }
168
169 bool MathScriptInset::idxLastUp(int & idx, int & pos) const
170 {
171         if (!up()) 
172                 return false;
173         idx = 0;
174         pos = cell(idx).size();
175         return true;
176 }
177
178 bool MathScriptInset::idxLastDown(int & idx, int & pos) const
179 {
180         if (!down()) 
181                 return false;
182         idx = 1;
183         pos = cell(idx).size();
184         return true;
185 }
186
187
188 bool MathScriptInset::idxDelete(int idx)
189 {
190         if (idx == 0) {
191                 up(false);
192                 return !down();
193         } else {
194                 down(false);
195                 return !up();
196         }
197 }