]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
da4ea89da9fe469df1ef2a477850aa9739fa8486
[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(LCursor & cur) const
75 {
76         if (cur.idx() + 1 >= nargs())
77                 return false;
78         ++cur.idx();
79         cur.pos() = 0;
80         return true;
81 }
82
83
84 bool MathNestInset::idxRight(LCursor & cur) const
85 {
86         return idxNext(cur);
87 }
88
89
90 bool MathNestInset::idxPrev(LCursor & cur) const
91 {
92         if (cur.idx() == 0)
93                 return false;
94         --cur.idx();
95         cur.pos() = cur.lastpos();
96         return true;
97 }
98
99
100 bool MathNestInset::idxLeft(LCursor & cur) const
101 {
102         return idxPrev(cur);
103 }
104
105
106 bool MathNestInset::idxFirst(LCursor & cur) const
107 {
108         if (nargs() == 0)
109                 return false;
110         cur.idx() = 0;
111         cur.pos() = 0;
112         return true;
113 }
114
115
116 bool MathNestInset::idxLast(LCursor & cur) const
117 {
118         if (nargs() == 0)
119                 return false;
120         cur.idx() = nargs() - 1;
121         cur.pos() = cur.lastpos();
122         return true;
123 }
124
125
126 bool MathNestInset::idxHome(LCursor & cur) const
127 {
128         if (cur.pos() == 0)
129                 return false;
130         cur.pos() = 0;
131         return true;
132 }
133
134
135 bool MathNestInset::idxEnd(LCursor & cur) const
136 {
137         if (cur.lastpos() == cur.lastpos())
138                 return false;
139         cur.pos() = cur.lastpos();
140         return true;
141 }
142
143
144 void MathNestInset::dump() const
145 {
146         WriteStream os(lyxerr);
147         os << "---------------------------------------------\n";
148         write(os);
149         os << "\n";
150         for (idx_type i = 0; i < nargs(); ++i)
151                 os << cell(i) << "\n";
152         os << "---------------------------------------------\n";
153 }
154
155
156 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
157 void MathNestInset::draw(PainterInfo &, int, int) const
158 {
159 #if 0
160         if (lock_)
161                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
162                                         LColor::mathlockbg);
163 #endif
164 }
165
166
167 void MathNestInset::drawSelection(PainterInfo & pi,
168                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
169 {
170         if (idx1 == idx2) {
171                 MathArray const & c = cell(idx1);
172                 int x1 = c.xo() + c.pos2x(pos1);
173                 int y1 = c.yo() - c.ascent();
174                 int x2 = c.xo() + c.pos2x(pos2);
175                 int y2 = c.yo() + c.descent();
176                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
177         } else {
178                 for (idx_type i = 0; i < nargs(); ++i) {
179                         if (idxBetween(i, idx1, idx2)) {
180                                 MathArray const & c = cell(i);
181                                 int x1 = c.xo();
182                                 int y1 = c.yo() - c.ascent();
183                                 int x2 = c.xo() + c.width();
184                                 int y2 = c.yo() + c.descent();
185                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
186                         }
187                 }
188         }
189 }
190
191
192 void MathNestInset::validate(LaTeXFeatures & features) const
193 {
194         for (idx_type i = 0; i < nargs(); ++i)
195                 cell(i).validate(features);
196 }
197
198
199 void MathNestInset::replace(ReplaceData & rep)
200 {
201         for (idx_type i = 0; i < nargs(); ++i)
202                 cell(i).replace(rep);
203 }
204
205
206 bool MathNestInset::contains(MathArray const & ar) const
207 {
208         for (idx_type i = 0; i < nargs(); ++i)
209                 if (cell(i).contains(ar))
210                         return true;
211         return false;
212 }
213
214
215 bool MathNestInset::editing() const
216 {
217         return mathcursor && mathcursor->isInside(this);
218 }
219
220
221 bool MathNestInset::lock() const
222 {
223         return lock_;
224 }
225
226
227 void MathNestInset::lock(bool l)
228 {
229         lock_ = l;
230 }
231
232
233 bool MathNestInset::isActive() const
234 {
235         return nargs() > 0;
236 }
237
238
239 MathArray MathNestInset::glue() const
240 {
241         MathArray ar;
242         for (unsigned i = 0; i < nargs(); ++i)
243                 ar.append(cell(i));
244         return ar;
245 }
246
247
248 void MathNestInset::write(WriteStream & os) const
249 {
250         os << '\\' << name().c_str();
251         for (unsigned i = 0; i < nargs(); ++i)
252                 os << '{' << cell(i) << '}';
253         if (nargs() == 0)
254                 os.pendingSpace(true);
255         if (lock_ && !os.latex()) {
256                 os << "\\lyxlock";
257                 os.pendingSpace(true);
258         }
259 }
260
261
262 void MathNestInset::normalize(NormalStream & os) const
263 {
264         os << '[' << name().c_str();
265         for (unsigned i = 0; i < nargs(); ++i)
266                 os << ' ' << cell(i);
267         os << ']';
268 }
269
270
271 void MathNestInset::notifyCursorLeaves(idx_type idx)
272 {
273         cell(idx).notifyCursorLeaves();
274 }
275
276
277 DispatchResult
278 MathNestInset::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
279 {
280         CursorSlice & cur = cursorTip(bv);
281         switch (cmd.action) {
282
283                 case LFUN_PASTE: {
284                         MathArray ar;
285                         mathed_parse_cell(ar, cmd.argument);
286                         cur.cell().insert(cur.pos(), ar);
287                         cur.pos() += ar.size();
288                         return DispatchResult(true, true);
289                 }
290
291                 case LFUN_PASTESELECTION:
292                         return dispatch(bv, FuncRequest(LFUN_PASTE, bv.getClipboard())); 
293
294                 case LFUN_MOUSE_PRESS:
295                         if (cmd.button() == mouse_button::button2)
296                                 return priv_dispatch(bv, FuncRequest(LFUN_PASTESELECTION));
297                         return DispatchResult(false);
298
299                 default:
300                         return MathInset::priv_dispatch(bv, cmd);
301         }
302 }
303
304
305 void MathNestInset::metricsMarkers(int) const
306 {
307         dim_.wid += 2;
308         dim_.asc += 1;
309 }
310
311
312 void MathNestInset::metricsMarkers2(int) const
313 {
314         dim_.wid += 2;
315         dim_.asc += 1;
316         dim_.des += 1;
317 }
318
319 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
320 {
321         if (!editing())
322                 return;
323         int t = x + dim_.width() - 1;
324         int d = y + dim_.descent();
325         pi.pain.line(x, d - 3, x, d, LColor::mathframe);
326         pi.pain.line(t, d - 3, t, d, LColor::mathframe);
327         pi.pain.line(x, d, x + 3, d, LColor::mathframe);
328         pi.pain.line(t - 3, d, t, d, LColor::mathframe);
329 }
330
331
332 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
333 {
334         if (!editing())
335                 return;
336         drawMarkers(pi, x, y);
337         int t = x + dim_.width() - 1;
338         int a = y - dim_.ascent();
339         pi.pain.line(x, a + 3, x, a, LColor::mathframe);
340         pi.pain.line(t, a + 3, t, a, LColor::mathframe);
341         pi.pain.line(x, a, x + 3, a, LColor::mathframe);
342         pi.pain.line(t - 3, a, t, a, LColor::mathframe);
343 }