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