]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
Rewrite selection code in mathed
[lyx.git] / src / mathed / MathRow.cpp
1 /**
2  * \file MathRow.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathRow.h"
14
15 #include "InsetMath.h"
16 #include "MathClass.h"
17 #include "MathData.h"
18 #include "MathSupport.h"
19
20 #include "BufferView.h"
21 #include "CoordCache.h"
22 #include "MetricsInfo.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/lassert.h"
30
31 #include <algorithm>
32 #include <ostream>
33
34 using namespace std;
35
36 namespace lyx {
37
38
39 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
40         : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
41           marker(InsetMath::NO_MARKER), inset(0), compl_unique_to(0), ar(0),
42           color(Color_red)
43 {}
44
45
46 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
47 {
48         // First there is a dummy element of type "open"
49         push_back(Element(mi, DUMMY, MC_OPEN));
50
51         // Then insert the MathData argument
52         bool const has_contents = ar->addToMathRow(*this, mi);
53
54         // A MathRow should not be completely empty
55         if (!has_contents) {
56                 Element e(mi, BOX, MC_ORD);
57                 // empty arrays are visible when they are editable
58                 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
59                 push_back(e);
60         }
61
62         // Finally there is a dummy element of type "close"
63         push_back(Element(mi, DUMMY, MC_CLOSE));
64
65         /* Do spacing only in math mode. This test is a bit clumsy,
66          * but it is used in other places for guessing the current mode.
67          */
68         bool const dospacing = isMathFont(mi.base.fontname);
69
70         // update classes
71         if (dospacing) {
72                 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
73                         if (elements_[i].mclass != MC_UNKNOWN)
74                                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
75                                                          elements_[after(i)].mclass);
76                 }
77         }
78
79         // set spacing
80         // We go to the end to handle spacing at the end of equation
81         for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
82                 Element & e = elements_[i];
83
84                 Element & bef = elements_[before(i)];
85                 if (dospacing && e.mclass != MC_UNKNOWN) {
86                         int spc = class_spacing(bef.mclass, e.mclass, mi.base);
87                         bef.after += spc / 2;
88                         // this is better than spc / 2 to avoid rounding problems
89                         e.before += spc - spc / 2;
90                 }
91
92                 // finally reserve space for markers
93                 if (bef.marker != Inset::NO_MARKER)
94                         bef.after = max(bef.after, 1);
95                 if (e.mclass != MC_UNKNOWN && e.marker != Inset::NO_MARKER)
96                         e.before = max(e.before, 1);
97                 // for linearized insets (macros...) too
98                 if (e.type == BEGIN && e.marker != Inset::NO_MARKER)
99                         bef.after = max(bef.after, 1);
100                 if (e.type == END && e.marker != Inset::NO_MARKER) {
101                         Element & aft = elements_[after(i)];
102                         aft.before = max(aft.before, 1);
103                 }
104         }
105
106         // Do not lose spacing allocated to extremities
107         if (!elements_.empty()) {
108                 elements_[after(0)].before += elements_.front().after;
109                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
110         }
111 }
112
113
114 int MathRow::before(int i) const
115 {
116         do
117                 --i;
118         while (elements_[i].mclass == MC_UNKNOWN);
119
120         return i;
121 }
122
123
124 int MathRow::after(int i) const
125 {
126         do
127                 ++i;
128         while (elements_[i].mclass == MC_UNKNOWN);
129
130         return i;
131 }
132
133
134 namespace {
135
136 void metricsMarkersVertical(MetricsInfo const & , MathRow::Element const & e,
137                             Dimension & dim)
138 {
139         // handle vertical space for markers
140         switch(e.marker) {
141         case InsetMath::NO_MARKER:
142                 break;
143         case InsetMath::MARKER:
144                 ++dim.des;
145                 break;
146         case InsetMath::MARKER2:
147                 ++dim.asc;
148                 ++dim.des;
149         }
150 }
151
152 }
153
154
155 void MathRow::metrics(MetricsInfo & mi, Dimension & dim) const
156 {
157         dim.asc = 0;
158         dim.wid = 0;
159         // In order to compute the dimension of macros and their
160         // arguments, it is necessary to keep track of them.
161         vector<pair<InsetMath const *, Dimension>> dim_insets;
162         vector<pair<MathData const *, Dimension>> dim_arrays;
163         CoordCache & coords = mi.base.bv->coordCache();
164         for (Element const & e : elements_) {
165                 mi.base.macro_nesting = e.macro_nesting;
166                 Dimension d;
167                 switch (e.type) {
168                 case DUMMY:
169                         break;
170                 case INSET:
171                         e.inset->metrics(mi, d);
172                         d.wid += e.before + e.after;
173                         coords.insets().add(e.inset, d);
174                         break;
175                 case BEGIN:
176                         if (e.inset) {
177                                 dim_insets.push_back(make_pair(e.inset, Dimension()));
178                                 dim_insets.back().second.wid += e.before + e.after;
179                                 e.inset->beforeMetrics();
180                         }
181                         if (e.ar)
182                                 dim_arrays.push_back(make_pair(e.ar, Dimension()));
183                         break;
184                 case END:
185                         if (e.inset) {
186                                 e.inset->afterMetrics();
187                                 LATTEST(dim_insets.back().first == e.inset);
188                                 Dimension & idim = dim_insets.back().second;
189                                 metricsMarkersVertical(mi, e, idim);
190                                 idim.wid += e.before + e.after;
191                                 coords.insets().add(e.inset, idim);
192                                 dim_insets.pop_back();
193                         }
194                         if (e.ar) {
195                                 LATTEST(dim_arrays.back().first == e.ar);
196                                 coords.arrays().add(e.ar, dim_arrays.back().second);
197                                 dim_arrays.pop_back();
198                         }
199                         break;
200                 case BOX:
201                         d = theFontMetrics(mi.base.font).dimension('I');
202                         if (e.color != Color_none) {
203                                 // allow for one pixel before/after the box.
204                                 d.wid += e.before + e.after + 2;
205                         } else {
206                                 // hide the box, but give it some height
207                                 d.wid = 0;
208                         }
209                         break;
210                 }
211
212                 if (!d.empty()) {
213                         dim += d;
214                         // Now add the dimension to current macros and arguments.
215                         for (auto & dim_macro : dim_insets)
216                                 dim_macro.second += d;
217                         for (auto & dim_array : dim_arrays)
218                                 dim_array.second += d;
219                 }
220
221                 if (e.compl_text.empty())
222                         continue;
223                 FontInfo font = mi.base.font;
224                 augmentFont(font, "mathnormal");
225                 dim.wid += mathed_string_width(font, e.compl_text);
226         }
227         LATTEST(dim_insets.empty() && dim_arrays.empty());
228 }
229
230
231 namespace {
232
233 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e, int const x, int const y)
234 {
235         if (e.marker == InsetMath::NO_MARKER)
236                 return;
237
238         CoordCache const & coords = pi.base.bv->coordCache();
239         Dimension const dim = coords.getInsets().dim(e.inset);
240
241         // the marker is before/after the inset. Normally some space has been reserved already.
242         int const l = x + e.before - 1;
243         int const r = x + dim.width() - e.after;
244
245         // Duplicated from Inset.cpp and adapted. It is believed that the
246         // Inset version should die eventually
247         ColorCode pen_color = e.inset->mouseHovered(pi.base.bv) || e.inset->editing(pi.base.bv)?
248                 Color_mathframe : Color_mathcorners;
249
250         int const d = y + dim.descent();
251         pi.pain.line(l, d - 3, l, d, pen_color);
252         pi.pain.line(r, d - 3, r, d, pen_color);
253         pi.pain.line(l, d, l + 3, d, pen_color);
254         pi.pain.line(r - 3, d, r, d, pen_color);
255
256         if (e.marker == InsetMath::MARKER)
257                 return;
258
259         int const a = y - dim.ascent();
260         pi.pain.line(l, a + 3, l, a, pen_color);
261         pi.pain.line(r, a + 3, r, a, pen_color);
262         pi.pain.line(l, a, l + 3, a, pen_color);
263         pi.pain.line(r - 3, a, r, a, pen_color);
264 }
265
266 }
267
268 void MathRow::draw(PainterInfo & pi, int x, int const y) const
269 {
270         CoordCache & coords = pi.base.bv->coordCache();
271         for (Element const & e : elements_) {
272                 switch (e.type) {
273                 case INSET: {
274                         // This is hackish: the math inset does not know that space
275                         // has been added before and after it; we alter its dimension
276                         // while it is drawing, because it relies on this value.
277                         Dimension const d = coords.insets().dim(e.inset);
278                         Dimension d2 = d;
279                         d2.wid -= e.before + e.after;
280                         coords.insets().add(e.inset, d2);
281                         e.inset->draw(pi, x + e.before, y);
282                         coords.insets().add(e.inset, x, y);
283                         coords.insets().add(e.inset, d);
284                         drawMarkers(pi, e, x, y);
285                         x += d.wid;
286                         break;
287                 }
288                 case BEGIN:
289                         if (e.ar) {
290                                 coords.arrays().add(e.ar, x, y);
291                                 e.ar->drawSelection(pi, x, y);
292                         }
293                         if (e.inset) {
294                                 coords.insets().add(e.inset, x, y);
295                                 drawMarkers(pi, e, x, y);
296                                 e.inset->beforeDraw(pi);
297                         }
298                         x += e.before + e.after;
299                         break;
300                 case END:
301                         if (e.inset)
302                                 e.inset->afterDraw(pi);
303                         x += e.before + e.after;
304                         break;
305                 case BOX: {
306                         if (e.color == Color_none)
307                                 break;
308                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
309                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
310                                           d.width() - 1, d.height() - 1, e.color);
311                         x += d.wid + 2 + e.before + e.after;
312                         break;
313                 }
314                 case DUMMY:
315                         break;
316                 }
317
318                 if (e.compl_text.empty())
319                         continue;
320                 FontInfo f = pi.base.font;
321                 augmentFont(f, "mathnormal");
322
323                 // draw the unique and the non-unique completion part
324                 // Note: this is not time-critical as it is
325                 // only done once per screen.
326                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
327                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
328
329                 if (!s1.empty()) {
330                         f.setColor(Color_inlinecompletion);
331                         pi.pain.text(x, y, s1, f);
332                         x += mathed_string_width(f, s1);
333                 }
334                 if (!s2.empty()) {
335                         f.setColor(Color_nonunique_inlinecompletion);
336                         pi.pain.text(x, y, s2, f);
337                         x += mathed_string_width(f, s2);
338                 }
339         }
340 }
341
342
343 int MathRow::kerning(BufferView const * bv) const
344 {
345         if (elements_.empty())
346                 return 0;
347         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
348         return inset ? inset->kerning(bv) : 0;
349 }
350
351
352 ostream & operator<<(ostream & os, MathRow::Element const & e)
353 {
354         switch (e.type) {
355         case MathRow::DUMMY:
356                 os << (e.mclass == MC_OPEN ? "{" : "}");
357                 break;
358         case MathRow::INSET:
359                 os << "<" << e.before << "-"
360                    << to_utf8(class_to_string(e.mclass))
361                    << "-" << e.after << ">";
362                 break;
363         case MathRow::BEGIN:
364                 if (e.inset)
365                         os << "\\" << to_utf8(e.inset->name())
366                            << "^" << e.macro_nesting << "[";
367                 if (e.ar)
368                         os << "(";
369                 break;
370         case MathRow::END:
371                 if (e.ar)
372                         os << ")";
373                 if (e.inset)
374                         os << "]";
375                 break;
376         case MathRow::BOX:
377                 os << "<" << e.before << "-[]-" << e.after << ">";
378                 break;
379         }
380         return os;
381 }
382
383
384 ostream & operator<<(ostream & os, MathRow const & mrow)
385 {
386         for (MathRow::Element const & e : mrow)
387                 os << e << "  ";
388         return os;
389 }
390
391 } // namespace lyx