]> git.lyx.org Git - lyx.git/blob - src/mathed/MathRow.cpp
Use Color_selectiontext as needed for partial selection in math
[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 "MathData.h"
16 #include "MathSupport.h"
17
18 #include "BufferView.h"
19 #include "ColorSet.h"
20 #include "CoordCache.h"
21 #include "LyXRC.h"
22 #include "MetricsInfo.h"
23
24 #include "mathed/InsetMath.h"
25
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/lassert.h"
32
33 #include <algorithm>
34 #include <ostream>
35
36 using namespace std;
37
38 namespace lyx {
39
40
41 MathRow::Element::Element(MetricsInfo const & mi, Type t, MathClass mc)
42         : type(t), mclass(mc), before(0), after(0), macro_nesting(mi.base.macro_nesting),
43           marker(marker_type::NO_MARKER), inset(nullptr), compl_unique_to(0), ar(nullptr),
44           color(Color_red)
45 {}
46
47
48 namespace {
49
50 // Helper functions for markers
51
52 int markerMargin(MathRow::Element const & e)
53 {
54         switch(e.marker) {
55         case marker_type::MARKER:
56         case marker_type::MARKER2:
57         case marker_type::BOX_MARKER:
58                 return 2;
59         case marker_type::NO_MARKER:
60                 return 0;
61         }
62         // should not happen
63         return 0;
64 }
65
66
67 void afterMetricsMarkers(MetricsInfo const & , MathRow::Element & e,
68                             Dimension & dim)
69 {
70         // handle vertical space for markers
71         switch(e.marker) {
72         case marker_type::NO_MARKER:
73                 break;
74         case marker_type::MARKER:
75                 ++dim.des;
76                 break;
77         case marker_type::MARKER2:
78                 ++dim.asc;
79                 ++dim.des;
80                 break;
81         case marker_type::BOX_MARKER:
82                 FontInfo font;
83                 font.setSize(TINY_SIZE);
84                 Dimension namedim;
85                 mathed_string_dim(font, e.inset->name(), namedim);
86                 int const namewid = 1 + namedim.wid + 1;
87
88                 if (namewid > dim.wid)
89                         e.after += namewid - dim.wid;
90                 ++dim.asc;
91                 dim.des += 3 + namedim.height();
92         }
93 }
94
95
96 void drawMarkers(PainterInfo const & pi, MathRow::Element const & e,
97                  int const x, int const y)
98 {
99         if (e.marker == marker_type::NO_MARKER)
100                 return;
101
102         CoordCache const & coords = pi.base.bv->coordCache();
103         Dimension const dim = coords.getInsets().dim(e.inset);
104
105         // the marker is before/after the inset. Necessary space has been reserved already.
106         int const l = x + e.before - (markerMargin(e) > 0 ? 1 : 0);
107         int const r = x + dim.width() - e.after;
108
109         // Grey lower box
110         if (e.marker == marker_type::BOX_MARKER) {
111                 // draw header and rectangle around
112                 FontInfo font;
113                 font.setSize(TINY_SIZE);
114                 font.setColor(Color_mathmacrolabel);
115                 Dimension namedim;
116                 mathed_string_dim(font, e.inset->name(), namedim);
117                 pi.pain.fillRectangle(l, y + dim.des - namedim.height() - 2,
118                                       dim.wid, namedim.height() + 2, Color_mathmacrobg);
119                 pi.pain.text(l, y + dim.des - namedim.des - 1, e.inset->name(), font);
120         }
121
122         // Color for corners
123         bool const highlight = e.inset->mouseHovered(pi.base.bv)
124                                || e.inset->editing(pi.base.bv);
125         ColorCode const pen_color = highlight ? Color_mathframe : Color_mathcorners;
126         // If the corners have the same color as the background, do not paint them.
127         if (lcolor.getX11HexName(Color_mathbg) == lcolor.getX11HexName(pen_color))
128                 return;
129
130         // Lower corners in all cases
131         int const d = y + dim.descent();
132         pi.pain.line(l, d - 3, l, d, pen_color);
133         pi.pain.line(r, d - 3, r, d, pen_color);
134         pi.pain.line(l, d, l + 3, d, pen_color);
135         pi.pain.line(r - 3, d, r, d, pen_color);
136
137         // Upper corners
138         if (e.marker == marker_type::BOX_MARKER
139             || e.marker == marker_type::MARKER2) {
140                 int const a = y - dim.ascent();
141                 pi.pain.line(l, a + 3, l, a, pen_color);
142                 pi.pain.line(r, a + 3, r, a, pen_color);
143                 pi.pain.line(l, a, l + 3, a, pen_color);
144                 pi.pain.line(r - 3, a, r, a, pen_color);
145         }
146 }
147
148 } // namespace
149
150
151 MathRow::MathRow(MetricsInfo & mi, MathData const * ar)
152 {
153         // First there is a dummy element of type "open"
154         push_back(Element(mi, DUMMY, MC_OPEN));
155
156         // Then insert the MathData argument
157         bool const has_contents = ar->addToMathRow(*this, mi);
158
159         // A MathRow should not be completely empty
160         if (!has_contents) {
161                 Element e(mi, BOX, MC_ORD);
162                 // empty arrays are visible when they are editable
163                 e.color = mi.base.macro_nesting == 0 ? Color_mathline : Color_none;
164                 push_back(e);
165         }
166
167         // Finally there is a dummy element of type "close"
168         push_back(Element(mi, DUMMY, MC_CLOSE));
169
170         /* Do spacing only in math mode. This test is a bit clumsy,
171          * but it is used in other places for guessing the current mode.
172          */
173         bool const dospacing = isMathFont(mi.base.fontname);
174
175         // update classes
176         if (dospacing) {
177                 for (int i = 1 ; i != static_cast<int>(elements_.size()) - 1 ; ++i) {
178                         if (elements_[i].mclass != MC_UNKNOWN)
179                                 update_class(elements_[i].mclass, elements_[before(i)].mclass,
180                                                          elements_[after(i)].mclass);
181                 }
182         }
183
184         // set spacing
185         // We go to the end to handle spacing at the end of equation
186         for (int i = 1 ; i != static_cast<int>(elements_.size()) ; ++i) {
187                 Element & e = elements_[i];
188
189                 Element & bef = elements_[before(i)];
190                 if (dospacing && e.mclass != MC_UNKNOWN) {
191                         int spc = class_spacing(bef.mclass, e.mclass, mi.base);
192                         bef.after += spc / 2;
193                         // this is better than spc / 2 to avoid rounding problems
194                         e.before += spc - spc / 2;
195                 }
196
197                 // finally reserve space for markers
198                 bef.after = max(bef.after, markerMargin(bef));
199                 if (e.mclass != MC_UNKNOWN)
200                         e.before = max(e.before, markerMargin(e));
201                 // for linearized insets (macros...) too
202                 if (e.type == BEGIN)
203                         bef.after = max(bef.after, markerMargin(e));
204                 if (e.type == END && e.marker != marker_type::NO_MARKER) {
205                         Element & aft = elements_[after(i)];
206                         aft.before = max(aft.before, markerMargin(e));
207                 }
208         }
209
210         // Do not lose spacing allocated to extremities
211         if (!elements_.empty()) {
212                 elements_[after(0)].before += elements_.front().after;
213                 elements_[before(elements_.size() - 1)].after += elements_.back().before;
214         }
215 }
216
217
218 int MathRow::before(int i) const
219 {
220         do
221                 --i;
222         while (elements_[i].mclass == MC_UNKNOWN);
223
224         return i;
225 }
226
227
228 int MathRow::after(int i) const
229 {
230         do
231                 ++i;
232         while (elements_[i].mclass == MC_UNKNOWN);
233
234         return i;
235 }
236
237
238 void MathRow::metrics(MetricsInfo & mi, Dimension & dim)
239 {
240         dim.wid = 0;
241         // In order to compute the dimension of macros and their
242         // arguments, it is necessary to keep track of them.
243         vector<pair<InsetMath const *, Dimension>> dim_insets;
244         vector<pair<MathData const *, Dimension>> dim_arrays;
245         CoordCache & coords = mi.base.bv->coordCache();
246         for (Element & e : elements_) {
247                 mi.base.macro_nesting = e.macro_nesting;
248                 Dimension d;
249                 switch (e.type) {
250                 case DUMMY:
251                 case BEGIN_SEL:
252                 case END_SEL:
253                         break;
254                 case INSET:
255                         e.inset->metrics(mi, d);
256                         d.wid += e.before + e.after;
257                         coords.insets().add(e.inset, d);
258                         break;
259                 case BEGIN:
260                         if (e.inset) {
261                                 dim_insets.push_back(make_pair(e.inset, Dimension()));
262                                 dim_insets.back().second.wid += e.before + e.after;
263                                 d.wid = e.before + e.after;
264                                 e.inset->beforeMetrics();
265                         }
266                         if (e.ar)
267                                 dim_arrays.push_back(make_pair(e.ar, Dimension()));
268                         break;
269                 case END:
270                         if (e.inset) {
271                                 e.inset->afterMetrics();
272                                 LATTEST(dim_insets.back().first == e.inset);
273                                 d = dim_insets.back().second;
274                                 afterMetricsMarkers(mi, e, d);
275                                 d.wid += e.before + e.after;
276                                 coords.insets().add(e.inset, d);
277                                 dim_insets.pop_back();
278                                 // We do not want to count the width again, but the
279                                 // padding and the vertical dimension are meaningful.
280                                 d.wid = e.before + e.after;
281                         }
282                         if (e.ar) {
283                                 LATTEST(dim_arrays.back().first == e.ar);
284                                 coords.arrays().add(e.ar, dim_arrays.back().second);
285                                 dim_arrays.pop_back();
286                         }
287                         break;
288                 case BOX:
289                         d = theFontMetrics(mi.base.font).dimension('I');
290                         if (e.color != Color_none) {
291                                 // allow for one pixel before/after the box.
292                                 d.wid += e.before + e.after + 2;
293                         } else {
294                                 // hide the box, but keep its height
295                                 d.wid = 0;
296                         }
297                         break;
298                 }
299
300                 if (!d.empty()) {
301                         dim += d;
302                         // Now add the dimension to current macros and arguments.
303                         for (auto & dim_macro : dim_insets)
304                                 dim_macro.second += d;
305                         for (auto & dim_array : dim_arrays)
306                                 dim_array.second += d;
307                 }
308
309                 if (e.compl_text.empty())
310                         continue;
311                 FontInfo font = mi.base.font;
312                 augmentFont(font, "mathnormal");
313                 dim.wid += mathed_string_width(font, e.compl_text);
314         }
315         LATTEST(dim_insets.empty() && dim_arrays.empty());
316 }
317
318
319 void MathRow::draw(PainterInfo & pi, int x, int const y) const
320 {
321         Changer change_color;
322         CoordCache & coords = pi.base.bv->coordCache();
323         for (Element const & e : elements_) {
324                 switch (e.type) {
325                 case INSET: {
326                         // This is hackish: the math inset does not know that space
327                         // has been added before and after it; we alter its dimension
328                         // while it is drawing, because it relies on this value.
329                         Geometry & g = coords.insets().geometry(e.inset);
330                         g.dim.wid -= e.before + e.after;
331                         if (pi.pain.develMode() && !e.inset->isBufferValid())
332                                 pi.pain.fillRectangle(x + e.before, y - g.dim.ascent(),
333                                                       g.dim.width(), g.dim.height(), Color_error);
334                         e.inset->draw(pi, x + e.before, y);
335                         g.pos = {x, y};
336                         g.dim.wid += e.before + e.after;
337                         drawMarkers(pi, e, x, y);
338                         x += g.dim.wid;
339                         break;
340                 }
341                 case BEGIN:
342                         if (e.ar) {
343                                 coords.arrays().add(e.ar, x, y);
344                                 e.ar->drawSelection(pi, x, y);
345                         }
346                         if (e.inset) {
347                                 coords.insets().add(e.inset, x, y);
348                                 drawMarkers(pi, e, x, y);
349                                 e.inset->beforeDraw(pi);
350                         }
351                         x += e.before + e.after;
352                         break;
353                 case END:
354                         if (e.inset)
355                                 e.inset->afterDraw(pi);
356                         x += e.before + e.after;
357                         break;
358                 case BEGIN_SEL:
359                         if (lyxrc.use_system_colors)
360                                 change_color = pi.base.font.changeColor(Color_selectiontext);
361                         break;
362                 case END_SEL:
363                         change_color = noChange();
364                         break;
365                 case BOX: {
366                         if (e.color == Color_none)
367                                 break;
368                         Dimension const d = theFontMetrics(pi.base.font).dimension('I');
369                         pi.pain.rectangle(x + e.before + 1, y - d.ascent(),
370                                           d.width() - 1, d.height() - 1, e.color);
371                         x += d.wid + 2 + e.before + e.after;
372                         break;
373                 }
374                 case DUMMY:
375                         break;
376                 }
377
378                 if (e.compl_text.empty())
379                         continue;
380                 FontInfo f = pi.base.font;
381                 augmentFont(f, "mathnormal");
382
383                 // draw the unique and the non-unique completion part
384                 // Note: this is not time-critical as it is
385                 // only done once per screen.
386                 docstring const s1 = e.compl_text.substr(0, e.compl_unique_to);
387                 docstring const s2 = e.compl_text.substr(e.compl_unique_to);
388
389                 if (!s1.empty()) {
390                         f.setColor(Color_inlinecompletion);
391                         // offset the text by e.after to make sure that the
392                         // spacing is after the completion, not before.
393                         pi.pain.text(x - e.after, y, s1, f);
394                         x += mathed_string_width(f, s1);
395                 }
396                 if (!s2.empty()) {
397                         f.setColor(Color_nonunique_inlinecompletion);
398                         pi.pain.text(x - e.after, y, s2, f);
399                         x += mathed_string_width(f, s2);
400                 }
401         }
402 }
403
404
405 int MathRow::kerning(BufferView const * bv) const
406 {
407         if (elements_.empty())
408                 return 0;
409         InsetMath const * inset = elements_[before(elements_.size() - 1)].inset;
410         return inset ? inset->kerning(bv) : 0;
411 }
412
413
414 ostream & operator<<(ostream & os, MathRow::Element const & e)
415 {
416         switch (e.type) {
417         case MathRow::DUMMY:
418                 os << (e.mclass == MC_OPEN ? "{" : "}");
419                 break;
420         case MathRow::INSET:
421                 os << "<" << e.before << "-"
422                    << to_utf8(class_to_string(e.mclass))
423                    << "-" << e.after << ">";
424                 break;
425         case MathRow::BEGIN:
426                 if (e.inset)
427                         os << "\\" << to_utf8(e.inset->name())
428                            << "^" << e.macro_nesting << "[";
429                 if (e.ar)
430                         os << "(";
431                 break;
432         case MathRow::END:
433                 if (e.ar)
434                         os << ")";
435                 if (e.inset)
436                         os << "]";
437                 break;
438         case MathRow::BEGIN_SEL:
439                 os << "<sel>";
440                 break;
441         case MathRow::END_SEL:
442                 os << "</sel>" ;
443                 break;
444         case MathRow::BOX:
445                 os << "<" << e.before << "-[]-" << e.after << ">";
446                 break;
447         }
448         return os;
449 }
450
451
452 ostream & operator<<(ostream & os, MathRow const & mrow)
453 {
454         for (MathRow::Element const & e : mrow)
455                 os << e << "  ";
456         return os;
457 }
458
459 } // namespace lyx