]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
"Better" font handling. I've not found a cause for John's crash, but I'd
[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 "math_parser.h"
9 #include "funcrequest.h"
10 #include "debug.h"
11 #include "BufferView.h"
12 #include "frontends/Painter.h"
13
14
15 MathNestInset::MathNestInset(idx_type nargs)
16         : MathDimInset(), cells_(nargs), lock_(false)
17 {}
18
19
20 MathInset::idx_type MathNestInset::nargs() const
21 {
22         return cells_.size();
23 }
24
25
26 MathArray & MathNestInset::cell(idx_type i)
27 {
28         return cells_[i];
29 }
30
31
32 MathArray const & MathNestInset::cell(idx_type i) const
33 {
34         return cells_[i];
35 }
36
37
38 void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
39 {
40         MathArray const & ar = cell(idx);
41         x = ar.xo() + ar.pos2x(pos);
42         y = ar.yo();
43         // move cursor visually into empty cells ("blue rectangles");
44         if (cell(idx).empty())
45                 x += 2;
46 }
47
48 void MathNestInset::substitute(MathMacro const & m)
49 {
50         for (idx_type i = 0; i < nargs(); ++i)
51                 cell(i).substitute(m);
52 }
53
54
55 void MathNestInset::metrics(MathMetricsInfo const & mi) const
56 {
57         MathMetricsInfo m = mi;
58         for (idx_type i = 0; i < nargs(); ++i)
59                 cell(i).metrics(m);
60 }
61
62
63 void MathNestInset::metricsMarkers(int frame) const
64 {
65         dim_.d += frame;
66         dim_.w += 2 * frame;
67 }
68
69
70 void MathNestInset::metricsMarkers2(int frame) const
71 {
72         dim_.a += frame;
73         dim_.d += frame;
74         dim_.w += 2 * frame;
75 }
76
77
78 bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
79 {
80         if (idx + 1 >= nargs())
81                 return false;
82         ++idx;
83         pos = 0;
84         return true;
85 }
86
87
88 bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
89 {
90         return idxNext(idx, pos);
91 }
92
93
94 bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
95 {
96         if (idx == 0)
97                 return false;
98         --idx;
99         pos = cell(idx).size();
100         return true;
101 }
102
103
104 bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
105 {
106         return idxPrev(idx, pos);
107 }
108
109
110 bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
111 {
112         if (nargs() == 0)
113                 return false;
114         i = 0;
115         pos = 0;
116         return true;
117 }
118
119
120 bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
121 {
122         if (nargs() == 0)
123                 return false;
124         i = nargs() - 1;
125         pos = cell(i).size();
126         return true;
127 }
128
129
130 bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
131 {
132         if (pos == 0)
133                 return false;
134         pos = 0;
135         return true;
136 }
137
138
139 bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
140 {
141         pos_type n = cell(idx).size();
142         if (pos == n)
143                 return false;
144         pos = n;
145         return true;
146 }
147
148
149 void MathNestInset::dump() const
150 {
151         WriteStream os(lyxerr);
152         os << "---------------------------------------------\n";
153         write(os);
154         os << "\n";
155         for (idx_type i = 0; i < nargs(); ++i)
156                 os << cell(i) << "\n";
157         os << "---------------------------------------------\n";
158 }
159
160
161 //void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
162 void MathNestInset::draw(MathPainterInfo &, int, int) const
163 {
164 #if 0
165         if (lock_)
166                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
167                                         LColor::mathlockbg);
168 #endif
169 }
170
171
172 void MathNestInset::drawSelection(MathPainterInfo & pi,
173                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
174 {
175         if (idx1 == idx2) {
176                 MathArray const & c = cell(idx1);
177                 int x1 = c.xo() + c.pos2x(pos1);
178                 int y1 = c.yo() - c.ascent();
179                 int x2 = c.xo() + c.pos2x(pos2);
180                 int y2 = c.yo() + c.descent();
181                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
182         } else {
183                 for (idx_type i = 0; i < nargs(); ++i) {
184                         if (idxBetween(i, idx1, idx2)) {
185                                 MathArray const & c = cell(i);
186                                 int x1 = c.xo();
187                                 int y1 = c.yo() - c.ascent();
188                                 int x2 = c.xo() + c.width();
189                                 int y2 = c.yo() + c.descent();
190                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
191                         }
192                 }
193         }
194 }
195
196
197 void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
198 {
199         if (!editing())
200                 return;
201         int t = x + width() - 1;
202         int d = y + descent();
203         pi.pain.line(x, d - 3, x, d, LColor::mathframe);
204         pi.pain.line(t, d - 3, t, d, LColor::mathframe);
205         pi.pain.line(x, d, x + 3, d, LColor::mathframe);
206         pi.pain.line(t - 2, d, t, d, LColor::mathframe);
207 }
208
209
210 void MathNestInset::drawMarkers2(MathPainterInfo & pi, int x, int y) const
211 {
212         if (!editing())
213                 return;
214         drawMarkers(pi, x, y);
215         int t = x + width() - 1;
216         int a = y - ascent();
217         pi.pain.line(x, a + 3, x, a, LColor::mathframe);
218         pi.pain.line(t, a + 3, t, a, LColor::mathframe);
219         pi.pain.line(x, a, x + 3, a, LColor::mathframe);
220         pi.pain.line(t - 2, a, t, a, LColor::mathframe);
221 }
222
223
224 void MathNestInset::validate(LaTeXFeatures & features) const
225 {
226         for (idx_type i = 0; i < nargs(); ++i)
227                 cell(i).validate(features);
228 }
229
230
231 bool MathNestInset::match(MathAtom const & at) const
232 {
233         if (nargs() != at->nargs())
234                 return false;
235         for (idx_type i = 0; i < nargs(); ++i)
236                 if (!cell(i).match(at->cell(i)))
237                         return false;
238         return true;
239 }
240
241
242 void MathNestInset::replace(ReplaceData & rep)
243 {
244         for (idx_type i = 0; i < nargs(); ++i)
245                 cell(i).replace(rep);
246 }
247
248
249 bool MathNestInset::contains(MathArray const & ar) const
250 {
251         for (idx_type i = 0; i < nargs(); ++i)
252                 if (cell(i).contains(ar))
253                         return true;
254         return false;
255 }
256
257
258 bool MathNestInset::editing() const
259 {
260         return mathcursor && mathcursor->isInside(this);
261 }
262
263
264 bool MathNestInset::lock() const
265 {
266         return lock_;
267 }
268
269
270 void MathNestInset::lock(bool l)
271 {
272         lock_ = l;
273 }
274
275
276 bool MathNestInset::isActive() const
277 {
278         return nargs() > 0;
279 }
280
281
282 MathArray MathNestInset::glue() const
283 {
284         MathArray ar;
285         for (unsigned i = 0; i < nargs(); ++i)
286                 ar.append(cell(i));
287         return ar;
288 }
289
290
291 void MathNestInset::write(WriteStream & os) const
292 {
293         os << '\\' << name().c_str();
294         for (unsigned i = 0; i < nargs(); ++i)
295                 os << '{' << cell(i) << '}';
296         if (nargs() == 0)
297                 os.pendingSpace(true);
298         if (lock_ && !os.latex()) {
299                 os << "\\lyxlock";
300                 os.pendingSpace(true);
301         }
302 }
303
304
305 void MathNestInset::normalize(NormalStream & os) const
306 {
307         os << '[' << name().c_str();
308         for (unsigned i = 0; i < nargs(); ++i)
309                 os << ' ' << cell(i);
310         os << ']';
311 }
312
313
314 void MathNestInset::notifyCursorLeaves(idx_type idx)
315 {
316         cell(idx).notifyCursorLeaves();
317 }
318
319
320 MathInset::result_type MathNestInset::dispatch
321         (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
322 {
323         BufferView * bv = cmd.view();
324
325         switch (cmd.action) {
326
327                 case LFUN_PASTE: {
328                         MathArray ar;
329                         mathed_parse_cell(ar, cmd.argument);
330                         cell(idx).insert(pos, ar);
331                         pos += ar.size();
332                         return DISPATCHED;
333                 }
334
335                 case LFUN_PASTESELECTION:
336                         return
337                                 dispatch(
338                                         FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
339                         
340                 case LFUN_MOUSE_PRESS:
341                         if (cmd.button() == mouse_button::button2)
342                                 return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
343                         return UNDISPATCHED;
344
345                 default:        
346                         return MathInset::dispatch(cmd, idx, pos);
347         }
348         return UNDISPATCHED;
349 }