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