]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
using previews might be a good alternative to hard coding such diagrams...
[lyx.git] / src / mathed / math_nestinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_nestinset.h"
6 #include "math_cursor.h"
7 #include "math_mathmlstream.h"
8 #include "formulabase.h"
9 #include "BufferView.h"
10 #include "debug.h"
11 #include "frontends/Painter.h"
12 #include "graphics/PreviewLoader.h"
13 #include "graphics/Previews.h"
14
15
16 MathNestInset::MathNestInset(idx_type nargs)
17         : MathDimInset(), cells_(nargs), lock_(false)
18 {}
19
20
21 MathInset::idx_type MathNestInset::nargs() const
22 {
23         return cells_.size();
24 }
25
26
27 MathXArray & MathNestInset::xcell(idx_type i)
28 {
29         return cells_[i];
30 }
31
32
33 MathXArray const & MathNestInset::xcell(idx_type i) const
34 {
35         return cells_[i];
36 }
37
38
39 MathArray & MathNestInset::cell(idx_type i)
40 {
41         return cells_[i].data();
42 }
43
44
45 MathArray const & MathNestInset::cell(idx_type i) const
46 {
47         return cells_[i].data();
48 }
49
50
51 void MathNestInset::substitute(MathMacro const & m)
52 {
53         for (idx_type i = 0; i < nargs(); ++i)
54                 cell(i).substitute(m);
55 }
56
57
58 void MathNestInset::metrics(MathMetricsInfo const & mi) const
59 {
60         MathMetricsInfo m = mi;
61         for (idx_type i = 0; i < nargs(); ++i)
62                 xcell(i).metrics(m);
63 }
64
65
66 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
67 {
68         if (idx + 1 >= nargs())
69                 return false;
70         ++idx;
71         pos = 0;
72         return true;
73 }
74
75
76 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
77 {
78         return idxNext(idx, pos);
79 }
80
81
82 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
83 {
84         if (idx == 0)
85                 return false;
86         --idx;
87         pos = cell(idx).size();
88         return true;
89 }
90
91
92 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
93 {
94         return idxPrev(idx, pos);
95 }
96
97
98 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
99 {
100         if (nargs() == 0)
101                 return false;
102         i = 0;
103         pos = 0;
104         return true;
105 }
106
107
108 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
109 {
110         if (nargs() == 0)
111                 return false;
112         i = nargs() - 1;
113         pos = cell(i).size();
114         return true;
115 }
116
117
118 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
119 {
120         if (pos == 0)
121                 return false;
122         pos = 0;
123         return true;
124 }
125
126
127 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
128 {
129         pos_type n = cell(idx).size();
130         if (pos == n)
131                 return false;
132         pos = n;
133         return true;
134 }
135
136
137 void MathNestInset::dump() const
138 {
139         WriteStream os(lyxerr);
140         os << "---------------------------------------------\n";
141         write(os);
142         os << "\n";
143         for (idx_type i = 0; i < nargs(); ++i)
144                 os << cell(i) << "\n";
145         os << "---------------------------------------------\n";
146 }
147
148
149 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
150 void MathNestInset::draw(MathPainterInfo &, int, int) const
151 {
152 #if 0
153         if (lock_)
154                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
155                                         LColor::mathlockbg);
156 #endif
157 }
158
159
160 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
161 {
162         if (!editing())
163                 return;
164         int t = x + width() - 1;
165         int d = y + descent();
166         pi.pain.line(x, d - 3, x, d, LColor::mathframe); 
167         pi.pain.line(t, d - 3, t, d, LColor::mathframe); 
168         pi.pain.line(x, d, x + 3, d, LColor::mathframe); 
169         pi.pain.line(t - 2, d, t, d, LColor::mathframe); 
170 }
171
172
173 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
174 {
175         if (!editing())
176                 return;
177         drawMarkers(pi, x, y);  
178         int t = x + width() - 1;
179         int a = y - ascent();
180         pi.pain.line(x, a + 3, x, a, LColor::mathframe); 
181         pi.pain.line(t, a + 3, t, a, LColor::mathframe); 
182         pi.pain.line(x, a, x + 3, a, LColor::mathframe); 
183         pi.pain.line(t - 2, a, t, a, LColor::mathframe); 
184 }
185
186
187 void MathNestInset::validate(LaTeXFeatures & features) const
188 {
189         for (idx_type i = 0; i < nargs(); ++i)
190                 cell(i).validate(features);
191 }
192
193
194 bool MathNestInset::match(MathInset * p) const
195 {
196         if (nargs() != p->nargs())
197                 return false;
198         for (idx_type i = 0; i < nargs(); ++i)
199                 if (!cell(i).match(p->cell(i)))
200                         return false;
201         return true;
202 }
203
204
205 void MathNestInset::replace(ReplaceData & rep)
206 {
207         for (idx_type i = 0; i < nargs(); ++i)
208                 cell(i).replace(rep);
209 }
210
211
212 bool MathNestInset::contains(MathArray const & ar)
213 {
214         for (idx_type i = 0; i < nargs(); ++i)
215                 if (cell(i).contains(ar))
216                         return true;
217         return false;
218 }
219
220
221 bool MathNestInset::editing() const
222 {
223         return mathcursor && mathcursor->isInside(this);
224 }
225
226
227 bool MathNestInset::lock() const
228 {
229         return lock_;
230 }
231
232
233 void MathNestInset::lock(bool l)
234 {
235         lock_ = l;
236 }
237
238
239 bool MathNestInset::isActive() const
240 {
241         return nargs() > 0;
242 }
243
244
245 MathArray MathNestInset::glue() const
246 {
247         MathArray ar;
248         for (unsigned i = 0; i < nargs(); ++i)
249                 ar.push_back(cell(i));
250         return ar;
251 }
252
253
254 void MathNestInset::notifyCursorLeaves()
255 {
256         //lyxerr << "leaving " << *this << "\n";
257         if (!mathcursor || !grfx::Previews::activated())
258                 return;
259
260         InsetFormulaBase * inset = mathcursor->formula();
261         BufferView * bufferview = inset->view();
262         if (!bufferview || !bufferview->buffer())
263                 return;
264
265         grfx::Previews & previews = grfx::Previews::get();
266         grfx::PreviewLoader & loader = previews.loader(bufferview->buffer());
267
268         inset->generatePreview(loader);
269         loader.startLoading();
270 }