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